Conditions | 22 |
Paths | 91 |
Total Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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' |
||
11 | export default function compileAbe() { |
||
12 | var content = abeEngine.instance.content |
||
13 | if (arguments[0].hash['key'] == null) return '' |
||
|
|||
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( |
||
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) |
||
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('"]["')}"]`) |
||
64 | else value = eval(`content["${hash.key}"]`) |
||
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 |
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 you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.