-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (55 loc) · 1.55 KB
/
index.js
File metadata and controls
62 lines (55 loc) · 1.55 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
'use strict';
const { getParser } = require('codemod-cli').jscodeshift;
const { createQuerySelectorExpression, createQuerySelectorAllExpression } = require('../utils');
/**
* Transform find/findAll to this.element.querySelector/-All
*
* @param file
* @param api
* @returns {*|string}
*/
function transform(file, api) {
let source = file.source;
let j = getParser(api);
let root = j(source);
let importStatement = root.find(j.ImportDeclaration, {
source: { value: '@ember/test-helpers' },
});
if (importStatement.length === 0) {
return root.toSource({ quote: 'single' });
}
let oldSpecifiers = importStatement.get('specifiers');
let newSpecifiers = [];
oldSpecifiers.each(({ node: specifier }) => {
let importedName = specifier.imported.name;
switch (importedName) {
case 'find':
root
.find(j.CallExpression, {
callee: {
name: 'find',
},
})
.replaceWith(({ node }) => createQuerySelectorExpression(j, node.arguments));
break;
case 'findAll':
root
.find(j.CallExpression, {
callee: {
name: 'findAll',
},
})
.replaceWith(({ node }) => createQuerySelectorAllExpression(j, node.arguments));
break;
default:
newSpecifiers.push(specifier);
}
});
if (newSpecifiers.length > 0) {
oldSpecifiers.replace(newSpecifiers);
} else {
importStatement.remove();
}
return root.toSource({ quote: 'single' });
}
module.exports = transform;