-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
157 lines (145 loc) · 4.68 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
'use strict';
const { getParser } = require('codemod-cli').jscodeshift;
const { addImportStatement, writeImportStatements } = require('../utils');
const importMigrations = [
'click',
'find',
'findAll',
'fillIn',
'focus',
'blur',
'triggerEvent',
['keyEvent', 'triggerKeyEvent'],
'waitFor',
'waitUntil',
'currentURL',
'currentRouteName',
'visit'
];
const importMigrationsLookup = importMigrations.reduce((result, specifier) => {
let key = specifier;
let value = specifier;
if (Array.isArray(specifier)) {
key = specifier[0];
value = specifier[1];
}
return Object.assign(result, { [key]: value });
}, {});
function renameCallee(j, root, name, newName) {
root
.find(j.CallExpression, {
callee: {
name
}
})
.forEach(({ node }) => node.callee.name = newName);
}
/**
* find(selector, context) => context.querySelector(selector)
* findAll(selector, context) => context.querySelectorAll(selector)
* click(selector, context) => click(context.querySelector(selector))
* click(selector, context, { ...options }) => click(context.querySelector(selector), { ...options })
*/
function migrateHelpersWithContext(j, root, importedName) {
switch (importedName) {
case 'find':
root
.find(j.CallExpression, {
callee: {
name: 'find'
},
arguments: [ {}, {} ]
})
.replaceWith(({node}) => {
let [ selector, context ] = node.arguments;
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
return j.callExpression(j.identifier('find'), [ j.literal(`${context.value} ${selector.value}`) ]);
}
return j.callExpression(j.memberExpression(context, j.identifier('querySelector')), [ selector ])
});
break;
case 'findAll':
root
.find(j.CallExpression, {
callee: {
name: 'findAll'
},
arguments: [ {}, {} ]
})
.replaceWith(({node}) => {
let [ selector, context ] = node.arguments;
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
return j.callExpression(j.identifier('findAll'), [ j.literal(`${context.value} ${selector.value}`) ]);
}
return j.callExpression(j.memberExpression(context, j.identifier('querySelectorAll')), [ selector ])
});
break;
case 'click':
root
.find(j.CallExpression, {
callee: {
name: 'click'
},
arguments: [ {}, {} ] // matches 2 or 3 arguments
})
.replaceWith(({node}) => {
let [ selector, context, options ] = node.arguments;
if (context.type === 'ObjectExpression') {
// click(selector, { ...options })
return node;
}
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
return j.callExpression(j.identifier('click'), [ j.literal(`${context.value} ${selector.value}`) ]);
}
return j.callExpression(j.identifier('click'), [ j.callExpression(j.memberExpression(context, j.identifier('querySelector')), [ selector ]), options ].filter(Boolean));
});
break;
}
}
/**
* Transform imports from ember-native-dom-helpers to @ember/test-helpers
*
* @param file
* @param api
* @returns {*|string}
*/
function transform(file, api) {
let source = file.source;
let j = getParser(api);
let root = j(source);
let nativeDomImportStatements = root.find(j.ImportDeclaration, {
source: { value: 'ember-native-dom-helpers' }
});
if (nativeDomImportStatements.length === 0) {
return root.toSource({ quote: 'single' });
}
let newImports = [];
nativeDomImportStatements.forEach((importStatement) => {
let oldSpecifiers = importStatement.get('specifiers');
let newSpecifiers = [];
oldSpecifiers.each(({ node: specifier }) => {
let importedName = specifier.imported.name;
if (importedName in importMigrationsLookup) {
let mappedName = importMigrationsLookup[importedName];
// @todo local != imported
// let localName = specifier.local.name;
newImports.push(mappedName);
if (importedName !== mappedName) {
renameCallee(j, root, importedName, mappedName);
}
migrateHelpersWithContext(j, root, importedName);
} else {
newSpecifiers.push(specifier);
}
});
if (newSpecifiers.length > 0) {
oldSpecifiers.replace(newSpecifiers);
} else {
importStatement.prune();
}
});
addImportStatement(newImports);
writeImportStatements(j, root);
return root.toSource({ quote: 'single' });
}
module.exports = transform;