compileAbe.js ➔ compileAbe   F
last analyzed

Complexity

Conditions 22
Paths 91

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
c 0
b 0
f 0
nc 91
nop 0
dl 0
loc 79
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like compileAbe.js ➔ compileAbe often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import Handlebars from 'handlebars'
2
import abeEngine from './abeEngine'
3
import xss from 'xss'
4
5
import {config} from '../../../'
6
7
/**
8
 * Abe handlebar helper, that retrieve text to add to handlebars templating engine
9
 * @return {String} the string to replace {{ handlebars_key }}
10
 */
11
export default function compileAbe() {
12
  var content = abeEngine.instance.content
13
  if (arguments[0].hash['key'] == null) return ''
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
  var key
15
  var hash
16
  var value
17
  var testXSS
18
19
  if (arguments[0].hash['key'].indexOf('}-') > 0) {
20
    key = arguments[0].hash['key'].split('-')
21
    key = key[key.length - 1]
22
    hash = arguments[0].hash
23
    // hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
24
    hash.key = hash.key.replace(
25
      /\{\{@index\}\}/,
26
      `[${arguments[0].data.index}]`
27
    )
28
    try {
29
      value = content
30
        ? eval(
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
31
            `content["${hash.dictionnary}"][${arguments[0].data.index}].${key}`
32
          )
33
        : hash.key
34
      // value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
35
    } catch (e) {
36
      console.log(e.stack)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
      value = ''
38
    }
39
    if (typeof value === 'function' || value == null) {
40
      value = hash.value != null ? hash.value : ''
41
    }
42
    if (hash.type != null && (hash.type === 'rich' || hash.type === 'code')) {
43
      if (config.xss) {
44
        testXSS = xss(value.replace(/"/g, '"'), {
45
          whiteList: config.htmlWhiteList,
46
          stripIgnoreTag: true
47
        })
48
        return new Handlebars.SafeString(testXSS)
49
      } else {
50
        return new Handlebars.SafeString(value.replace(/"/g, '"'))
51
      }
52
    }
53
54
    return value.replace(/%27/, "'")
55
  }
56
57
  key = arguments[0].hash['key'].replace('.', '-')
58
59
  hash = arguments[0].hash
60
  if (content) {
61
    try {
62
      if (hash.key.indexOf('.') > -1)
63
        value = eval(`content["${hash.key.split('.').join('"]["')}"]`)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
64
      else value = eval(`content["${hash.key}"]`)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
65
    } catch (e) {
66
      value = ''
67
    }
68
  } else {
69
    value = hash.key
70
  }
71
72
  if (typeof value === 'function' || value == null) {
73
    value = hash.value != null ? hash.value : ''
74
  }
75
76
  if (hash.type != null && (hash.type === 'rich' || hash.type === 'code')) {
77
    if (config.xss) {
78
      testXSS = xss(value.replace(/"/g, '"'), {
79
        whiteList: config.htmlWhiteList,
80
        stripIgnoreTag: true
81
      })
82
      return new Handlebars.SafeString(testXSS)
83
    } else {
84
      return new Handlebars.SafeString(value.replace(/"/g, '"'))
85
    }
86
  }
87
88
  return value.replace(/%27/, "'")
89
}
90