-
Notifications
You must be signed in to change notification settings - Fork 43
/
destroy-app-transform.js
77 lines (63 loc) · 1.98 KB
/
destroy-app-transform.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
var isImportFor = require('./helpers/is-import-for');
var parseAst = require('../helpers/parse-ast');
var recast = require('recast');
var types = recast.types.namedTypes;
var builders = recast.types.builders;
var addDefaultImport = require('./helpers/add-default-import');
function isEmberCall(node) {
return types.MemberExpression.check(node.callee) &&
node.callee.object.name === 'Ember';
}
function isLegacyDestroy(node) {
return types.MemberExpression.check(node.callee) &&
node.callee.object.name === 'Ember' &&
node.callee.property.name === 'run' &&
node.arguments[1].value === 'destroy';
}
function isStartAppAssignment(node) {
return types.CallExpression.check(node.right) &&
node.right.callee.name === 'startApp';
}
module.exports = function transform(source) {
var ast = parseAst(source);
var appName = null;
var addDestroyAppImport = false;
var removeEmberImport = true;
var emberImport = null;
recast.visit(ast, {
visitCallExpression: function(path) {
var node = path.node;
if(isLegacyDestroy(node)) {
if (addDestroyAppImport !== true) {
addDestroyAppImport = true;
}
path.replace(builders.callExpression(
builders.identifier('destroyApp'),
[appName]
));
} else if (isEmberCall(node)) {
removeEmberImport = false;
}
this.traverse(path);
},
visitImportDeclaration: function(path) {
if (isImportFor('ember', path.node)) {
emberImport = path;
}
this.traverse(path);
},
visitAssignmentExpression: function(path) {
if (isStartAppAssignment(path.node)) {
appName = path.node.left;
}
this.traverse(path);
}
});
if (addDestroyAppImport) {
addDefaultImport(ast, '../helpers/destroy-app', 'destroyApp');
}
if (removeEmberImport && emberImport) {
emberImport.prune();
}
return recast.print(ast, { tabWidth: 2, quote: 'single' }).code;
};