-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (180 loc) · 5.25 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const fs = require("fs");
module.exports = transform;
/**
* This is the entry point for this jscodeshift transform.
* It scans JavaScript files that use the Ember global and updates
* them to use the module syntax from the proposed new RFC.
*/
function transform(file, api, options) {
let source = file.source;
let j = api.jscodeshift;
let root = j(source);
replaceDirectEmberKObjectProperty(root);
replaceMemberExpressions(root);
let aliasedName = removeDestructuringAlias(root);
if (aliasedName) {
replaceAliasedEmberKObjectProperty(root, aliasedName);
replaceAliasedFunctionArgument(root, aliasedName);
replaceAliasedAssignment(root, aliasedName);
replaceAliasedInLogicalExpression(root, aliasedName);
replaceAliasedInAssignmentPattern(root, aliasedName);
}
removeEmptyDestructure(root);
return root.toSource();
/**
* Replaces things like
* {
* foo: Ember.K
* }
*/
function replaceDirectEmberKObjectProperty(root) {
root.find(j.Property, {
method: false,
shorthand: false,
computed: false
})
.filter(({ value: node }) => isEmberDotK(node.value))
.forEach(({ value: node }) => convertToEmptyMethod(node));
}
/**
* Replaces things like:
* ```js
* const { computed, K } = Ember;
* export default {
* foo: K
* }
* ```
*
* It also handles aliases like:
* ```js
* const { computed, K: noop } = Ember;
* export default {
* foo: noop
* }
* ```
*/
function replaceAliasedEmberKObjectProperty(root, aliasedName) {
root.find(j.Property, {
method: false,
shorthand: false,
computed: false
})
.filter(({ value: node }) => node.value.name === aliasedName)
.forEach(({ value: node }) => convertToEmptyMethod(node));
}
/**
* Replaces things like:
* ```js
* const { K: noop } = Ember;
* Ember.set(this, 'propName', noop)
* ```
*/
function replaceAliasedFunctionArgument(root, aliasedName) {
root.find(j.CallExpression)
.forEach(({ value: node }) => {
let index = node.arguments.findIndex((arg) => j.Identifier.check(arg) && arg.name === aliasedName);
if (index > -1) {
node.arguments[index] = createEmberKReplacement();
}
});
}
/**
* Replaces things like:
* ```js
* Ember.K;
* ```
*/
function replaceMemberExpressions(root) {
root.find(j.MemberExpression)
.filter(({ value: node }) => isEmberDotK(node))
.replaceWith(() => createEmberKReplacement());
}
/**
* Replaces things like:
* ```js
* const { K } = Ember;
* obj['foo'] = K;
* obj.foo = K;
* ```
*/
function replaceAliasedAssignment(root, aliasedName) {
root.find(j.AssignmentExpression)
.filter(({ value: node }) => node.right.name === aliasedName)
.forEach(({ value: node }) => node.right = createEmberKReplacement());
}
/**
* Replaces things like:
* ```js
* foo || noop
* ```
*/
function replaceAliasedInLogicalExpression(root, aliasedName) {
root.find(j.LogicalExpression)
.filter(({ value: node }) => node.right.name === aliasedName)
.forEach(({ value: node }) => node.right = createEmberKReplacement());
root.find(j.LogicalExpression)
.filter(({ value: node }) => node.left.name === aliasedName)
.forEach(({ value: node }) => node.left = createEmberKReplacement());
}
/**
* Replaces things like:
* ```js
* function(doNothing = noop) {}
* ```
*/
function replaceAliasedInAssignmentPattern(root, aliasedName) {
root.find(j.AssignmentPattern)
.filter(({ value: node }) => node.right.name === aliasedName)
.forEach(({ value: node }) => node.right = createEmberKReplacement());
}
/**
* Deletes things like:
* ```js
* const {} = Ember;
* ```
*/
function removeEmptyDestructure(root) {
root.find(j.VariableDeclarator)
.filter(({ value: node }) => {
return node.id.type === 'ObjectPattern' && node.id.properties.length === 0;
}).remove();
}
function removeDestructuringAlias(root) {
let aliasedName;
root.find(j.VariableDeclarator)
.filter(({ value: node }) => {
return node.init &&
(node.init.name === 'Ember' || node.init.name === 'Em') &&
node.id.type === 'ObjectPattern';
})
.forEach(({ value: node }) => {
if (!aliasedName) {
let index = node.id.properties.findIndex((prop) => prop.key.name === 'K');
if (index > -1) {
aliasedName = node.id.properties[index].value.name;
node.id.properties = arrayWithoutItemAt(node.id.properties, index);
}
}
});
return aliasedName;
}
function isEmberDotK(node) {
return j.MemberExpression.check(node) &&
(node.object.name === 'Ember' || node.object.name === 'Em') &&
node.property.name === 'K';
}
function convertToEmptyMethod(node) {
node.method = true;
node.value = createEmberKReplacement();
}
function createEmberKReplacement() {
let statements = [];
if (process.env.RETURN_THIS === 'true') {
statements.push(j.returnStatement(j.thisExpression()));
}
return j.functionExpression(null, [], j.blockStatement(statements));
}
}
function arrayWithoutItemAt(ary, index) {
return ary.slice(0, index).concat(ary.slice(index+1));
}