-
Notifications
You must be signed in to change notification settings - Fork 109
/
updated-computed-props.js
79 lines (77 loc) · 1.5 KB
/
updated-computed-props.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const keywords = {
'this': true,
'function': true,
'if': true,
'return': true,
'var': true,
'else': true,
'for': true,
'new': true,
'in': true,
'typeof': true,
'while': true,
'case': true,
'break': true,
'try': true,
'catch': true,
'delete': true,
'throw': true,
'switch': true,
'continue': true,
'default': true,
'instanceof': true,
'do': true,
'void': true,
'finally': true,
'with': true,
'debugger': true,
'implements': true,
'interface': true,
'package': true,
'private': true,
'protected': true,
'public': true,
'static': true,
'class': true,
'enum': true,
'export': true,
'extends': true,
'import': true,
'super': true,
'true': true,
'false': true,
'null': true,
'abstract': true,
'boolean': true,
'byte': true,
'char': true,
'const': true,
'double': true,
'final': true,
'float': true,
'goto': true,
'int': true,
'long': true,
'native': true,
'short': true,
'synchronized': true,
'throws': true,
'transient': true,
'volatile': true,
};
module.exports = function(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const didTransform = root
.find(j.MemberExpression, {computed: true, property: {type: 'Literal'}})
.filter(p => !!keywords[p.value.property.value])
.replaceWith(
p => j.memberExpression(
p.value.object,
j.identifier(p.value.property.value),
false
)
)
.size();
return didTransform ? root.toSource() : null;
};