-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnative.js
More file actions
285 lines (235 loc) · 7.67 KB
/
native.js
File metadata and controls
285 lines (235 loc) · 7.67 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const j = require('jscodeshift').withParser('ts');
const SilentError = require('../silent-error');
function addClassDecorator(classDeclaration, name, args) {
let existing = findDecorator(classDeclaration, name);
if (existing) {
existing.value.expression.arguments = args;
} else {
if (!classDeclaration.value.decorators) {
classDeclaration.value.decorators = [];
}
classDeclaration.value.decorators.unshift(
args === undefined
? j.decorator(j.identifier(name))
: j.decorator(j.callExpression(j.identifier(name), args))
);
}
}
function isDecorator(path, name, withArgs) {
let node = path.value;
let isCall =
node.expression.type === 'CallExpression' && node.expression.callee.type === 'Identifier';
return (
node.type === 'Decorator' &&
((isCall && node.expression.callee.name === name) ||
(!isCall && node.expression.name === name)) &&
(withArgs === undefined || (withArgs === true && isCall) || (withArgs === false && !isCall))
);
}
function isProperty(path, name) {
let node = path.value;
return node.type === 'ClassProperty' && node.key.type === 'Identifier' && node.key.name === name;
}
function isMethod(path, name) {
let node = path.value;
return node.type === 'ClassMethod' && node.key.type === 'Identifier' && node.key.name === name;
}
function findStringProperty(properties, name, defaultValue = null) {
let propertyPath = properties.filter(path => isProperty(path, name))[0];
if (!propertyPath) {
return defaultValue;
}
let valuePath = propertyPath.get('value');
if (valuePath.value.type !== 'StringLiteral') {
throw new SilentError(`Unexpected \`${name}\` value: ${j(valuePath).toSource()}`);
}
return valuePath.value.value;
}
function findDecorator(path, name, withArgs) {
let decorators = path.get('decorators');
if (!decorators.value) {
return;
}
let existing = decorators.filter(path => isDecorator(path, name, withArgs));
if (existing.length > 0) {
return existing[0];
}
}
function findStringDecorator(path, name) {
let decorator = findDecorator(path, name, true);
if (!decorator) {
return;
}
return decorator.value.expression.arguments[0].value;
}
function findTagName(path) {
let value = findStringDecorator(path, 'tagName');
return value !== undefined ? value : 'div';
}
function findElementId(path) {
return findStringProperty(path, 'elementId');
}
function findStringArrayDecorator(path, name) {
let decorator = findDecorator(path, name, true);
if (!decorator) {
return [];
}
let args = decorator.get('expression').value.arguments;
return args.map(element => {
if (element.type !== 'StringLiteral') {
throw new SilentError(`Unexpected \`${name}\` value: ${j(args).toSource()}`);
}
return element.value;
});
}
function findAttributeBindings(classDeclaration) {
let attrBindings = new Map();
for (let binding of findStringArrayDecorator(classDeclaration, 'attributeBindings')) {
let [value, attr] = binding.split(':');
attrBindings.set(attr || value, value);
}
j(classDeclaration)
.find(j.ClassProperty)
.forEach(path => {
let value = path.node.key.name;
let key = findStringDecorator(path, 'attribute');
if (key === undefined) {
let decorator = findDecorator(path, 'attribute', false);
if (decorator) {
key = value;
}
}
if (key !== undefined) {
attrBindings.set(key, value);
}
});
return attrBindings;
}
function findClassNames(classDeclaration) {
return findStringArrayDecorator(classDeclaration, 'classNames');
}
function findClassNameBindings(classDeclaration) {
let classNameBindings = new Map();
for (let binding of findStringArrayDecorator(classDeclaration, 'classNameBindings')) {
let parts = binding.split(':');
if (parts.length === 1) {
throw new SilentError(`Unsupported non-boolean \`@classNameBindings\` value: ${binding}`);
} else if (parts.length === 2) {
classNameBindings.set(parts[0], [parts[1], null]);
} else if (parts.length === 3) {
classNameBindings.set(parts[0], [parts[1] || null, parts[2]]);
} else {
throw new SilentError(`Unexpected \`@classNameBindings\` value: ${binding}`);
}
}
j(classDeclaration)
.find(j.ClassProperty)
.forEach(path => {
let key = path.node.key.name;
if (findDecorator(path, 'className', false)) {
throw new SilentError(`Unsupported non-boolean \`@className\` for property: ${key}`);
}
let decorator = findDecorator(path, 'className', true);
if (!decorator) {
return;
}
let args = decorator.get('expression').value.arguments;
let [truthy, falsy] = args.map(element => element.value);
classNameBindings.set(key, [truthy || null, falsy || null]);
});
return classNameBindings;
}
function removeDecorator(root, classDeclaration, name, source) {
let decorator = findDecorator(classDeclaration, name);
if (decorator) {
j(decorator).remove();
removeImport(root, name, source);
}
}
function ensureImport(root, name, source) {
let body = root.get().value.program.body;
let declaration = root.find(j.ImportDeclaration, {
source: { value: source },
});
if (declaration.length) {
if (
declaration.find(j.ImportSpecifier, {
imported: {
name,
},
}).length === 0
) {
declaration.get('specifiers').push(j.importSpecifier(j.identifier(name)));
}
} else {
let importStatement = createImportStatement(source, null, [name]);
body.unshift(importStatement);
body[0].comments = body[1].comments;
delete body[1].comments;
}
}
function removeImport(root, name, source) {
let declaration = root.find(j.ImportDeclaration, {
source: { value: source },
});
if (declaration.length) {
declaration
.find(j.ImportSpecifier, {
imported: {
name,
},
})
.remove();
if (declaration.get().value.specifiers.length === 0) {
declaration.remove();
}
}
}
// shamelessly taken from https://github.com/ember-codemods/ember-modules-codemod/blob/3afaab6b77fcb494873e2667f6e1bb14362f3845/transform.js#L607
function createImportStatement(source, imported, local) {
let declaration, variable, idIdentifier, nameIdentifier;
// if no variable name, return `import 'jquery'`
if (!local) {
declaration = j.importDeclaration([], j.literal(source));
return declaration;
}
// multiple variable names indicates a destructured import
if (Array.isArray(local)) {
let variableIds = local.map(function(v) {
return j.importSpecifier(j.identifier(v), j.identifier(v));
});
declaration = j.importDeclaration(variableIds, j.literal(source));
} else {
// else returns `import $ from 'jquery'`
nameIdentifier = j.identifier(local); //import var name
variable = j.importDefaultSpecifier(nameIdentifier);
// if propName, use destructuring `import {pluck} from 'underscore'`
if (imported && imported !== 'default') {
idIdentifier = j.identifier(imported);
variable = j.importSpecifier(idIdentifier, nameIdentifier); // if both are same, one is dropped...
}
declaration = j.importDeclaration([variable], j.literal(source));
}
return declaration;
}
function renameEventHandler(path) {
let oldName = path.value.key.name;
let newName = `handle${oldName.charAt(0).toUpperCase()}${oldName.slice(1)}`;
path.value.key.name = newName;
return newName;
}
module.exports = {
addClassDecorator,
isProperty,
isMethod,
findAttributeBindings,
findClassNames,
findClassNameBindings,
findElementId,
findTagName,
findDecorator,
removeDecorator,
ensureImport,
removeImport,
renameEventHandler,
};