Passed
Push — master ( 61dd3f...482d1b )
by greg
01:51
created

compileAbe.js ➔ compileAbe   D

Complexity

Conditions 19
Paths 21

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 19
c 1
b 0
f 0
nc 21
dl 0
loc 48
rs 4.8943
nop 0

How to fix   Complexity   

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 {
6
  config
7
} from '../../../'
8
9
/**
10
 * Abe handlebar helper, that retrieve text to add to handlebars templating engine
11
 * @return {String} the string to replace {{ handlebars_key }}
12
 */
13
export default function compileAbe(){
14
  var content = abeEngine.instance.content
15
  if(typeof arguments[0].hash['key'] === 'undefined' || 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...
16
  var key
17
  var hash
18
  var value
19
  var testXSS
20
  if(arguments[0].hash['key'].indexOf('}-') > 0){
21
    key = arguments[0].hash['key'].split('-')
22
    key = key[key.length - 1]
23
    hash = arguments[0].hash
24
    hash.key = hash.key.replace(/\{\{@index\}\}/, '[{{@index}}]')
25
    try{
26
      value = content ? content[hash['dictionnary']][arguments[0].data.index][key] : hash.key
27
    }
28
    catch(e){
29
      value = ''
30
    }
31
    if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
32
      value = ''
33
    }
34
    if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
35
      testXSS = xss(value.replace(/"/g, '"'), {
36
        'whiteList': config.htmlWhiteList,
37
        stripIgnoreTag: true
38
      })
39
      return new Handlebars.SafeString(testXSS)
40
    }
41
    return value.replace(/%27/, '\'')
42
  }
43
44
  key = arguments[0].hash['key'].replace('.', '-')
45
46
  var hash = arguments[0].hash
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable hash already seems to be declared on line 17. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
47
  var value = ((content) ? content[hash.key.replace('.', '-')] : hash.key)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable value already seems to be declared on line 18. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
48
  if(typeof value === 'undefined' || typeof value === 'function' || value === null) {
49
    value = ''
50
  }
51
  
52
  if(typeof hash.type !== 'undefined' && hash.type !== null && hash.type === 'rich'){
53
    testXSS = xss(value.replace(/"/g, '"'), {
54
      'whiteList': config.htmlWhiteList,
55
      stripIgnoreTag: true
56
    })
57
    return new Handlebars.SafeString(testXSS)
58
  }
59
  return value.replace(/%27/, '\'')
60
}
61