Goldinteractive/js-base

View on GitHub
src/features.js

Summary

Maintainability
F
3 days
Test Coverage

File features.js has 464 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/**
 * Features module.
 * @module base/features
 */

Severity: Minor
Found in src/features.js - About 7 hrs to fix

Function init has 58 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export async function init(container = document.body, name = null, options = {}) {
  options = Object.assign({}, defaultInitOptions, options)

  const instances = []
  const names = name ? name.split(ATTR_FEATURES_SEPARATOR) : null
Severity: Major
Found in src/features.js - About 2 hrs to fix

Function loadExternals has 46 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export async function loadExternals(bundles, assetPath) {
  let elements = document.querySelectorAll(`[${ATTR_EXTERNAL_SCRIPT}]`)
  let features = getFeatures()
  let scripts = []
  let scriptsLoaded = []
Severity: Minor
Found in src/features.js - About 1 hr to fix

Function lazyload has 37 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export function lazyload(bundles, assetPath) {
  if (!bundles || !assetPath) {
    throw Error('Cannot lazyload features without a bundles file or a path!')
  }

Severity: Minor
Found in src/features.js - About 1 hr to fix

Function destroy has 34 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export function destroy(container = document.body, name = null, options = {}) {
  options = Object.assign({}, defaultDestroyOptions, options)

  const names = name ? name.split(ATTR_FEATURES_SEPARATOR) : null
  const featureNodes = [...container.querySelectorAll(`[${ATTR_FEATURES}]`)]
Severity: Minor
Found in src/features.js - About 1 hr to fix

Function getRelevantBundles has 31 lines of code (exceeds 25 allowed). Consider refactoring.
Open

export async function getRelevantBundles(bundles) {
  const features = getFeatures()
  let extElements = document.querySelectorAll(`[${ATTR_EXTERNAL_SCRIPT}]`)
  let extFeatures = []
  let optimizedBundles = {}
Severity: Minor
Found in src/features.js - About 1 hr to fix

Function lazyload has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

export function lazyload(bundles, assetPath) {
  if (!bundles || !assetPath) {
    throw Error('Cannot lazyload features without a bundles file or a path!')
  }

Severity: Minor
Found in src/features.js - About 1 hr to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Function removeEventListener has 26 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  removeEventListener(node, type = null, fn = null) {
    if (!isElement(node) && node !== window) {
      let currentNode = node.length
      while (currentNode--) {
        this.removeEventListener(node[currentNode], type, fn)
Severity: Minor
Found in src/features.js - About 1 hr to fix

Consider simplifying this complex logical expression.
Open

      if (
        !feature || // feature has not been added yet
        (ignoreFeatures && ignoreFeatures.indexOf(featureName) > -1) || // feature is ignored on this node
        (name && names.indexOf(featureName) < 0) || // name is not whitelisted
        (featureNode._baseFeatureInstances && // feature has already been initalized on this node
Severity: Major
Found in src/features.js - About 1 hr to fix

Consider simplifying this complex logical expression.
Open

      if (
        nodeInstances.hasOwnProperty(featureName) &&
        (!name || names.indexOf(featureName) > -1) && // name is whitelisted
        (!ignoreFeatures || ignoreFeatures.indexOf(featureName) < 0) && // feature is ignore on this node
        (featureNode._baseFeatureInstances && // feature instance exists
Severity: Major
Found in src/features.js - About 1 hr to fix

Function loadExternals has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Open

export async function loadExternals(bundles, assetPath) {
  let elements = document.querySelectorAll(`[${ATTR_EXTERNAL_SCRIPT}]`)
  let features = getFeatures()
  let scripts = []
  let scriptsLoaded = []
Severity: Minor
Found in src/features.js - About 55 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Function addEventListener has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

  addEventListener(node, type, fn, options = {}) {
    if (!isElement(node) && node !== window) {
      let currentNode = node.length
      while (currentNode--) {
        this.addEventListener(node[currentNode], type, fn, options)
Severity: Minor
Found in src/features.js - About 45 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Function removeEventListener has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

  removeEventListener(node, type = null, fn = null) {
    if (!isElement(node) && node !== window) {
      let currentNode = node.length
      while (currentNode--) {
        this.removeEventListener(node[currentNode], type, fn)
Severity: Minor
Found in src/features.js - About 45 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Function removeAllEventListener has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

  removeAllEventListener(node = null, fn = null) {
    if (node && !isElement(node) && node !== window) {
      let currentNode = node.length
      while (currentNode--) {
        this.removeAllEventListener(node[currentNode], fn)
Severity: Minor
Found in src/features.js - About 35 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

    extFeatures.forEach(feature => {
      if (bundle[1].includes(feature) && !externalBundles[bundle[0]]) {
        externalBundles[bundle[0]] = bundle[1]
      }
    })
Severity: Major
Found in src/features.js and 1 other location - About 1 hr to fix
src/features.js on lines 174..178

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 69.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

    features.forEach(feature => {
      if (bundle[1].includes(feature) && !optimizedBundles[bundle[0]]) {
        optimizedBundles[bundle[0]] = bundle[1]
      }
    })
Severity: Major
Found in src/features.js and 1 other location - About 1 hr to fix
src/features.js on lines 183..187

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 69.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

There are no issues that match your filters.

Category
Status