999
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
function transform(file, { j }, options) {
const root = j(file.source);
const dirty = transformArrayMethod(
'array.prototype.lastindexof',
'lastIndexOf',
root,
j
);
return dirty ? root.toSource(options) : file.source;
}
function transformArrayMethod(method, identifierName, root, j) {
const { identifier, dirtyFlag: importDirtyFlag } = removeImport(
method,
root,
j
);
let dirtyFlag = importDirtyFlag;
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier
}
})
.forEach((path) => {
const [arrayArg, ...otherArgs] = path.node.arguments;
if (j.Identifier.check(arrayArg) || j.ArrayExpression.check(arrayArg)) {
path.replace(
j.callExpression(
j.memberExpression(arrayArg, j.identifier(identifierName)),
otherArgs
)
);
dirtyFlag = true;
}
});
return dirtyFlag;
}
function removeImport(name, root, j) {
// Find the import or require statement for 'is-boolean-object'
const importDeclaration = root.find(j.ImportDeclaration, {
source: {
value: name
}
});
const requireDeclaration = root
.find(j.CallExpression, {
callee: {
name: 'require'
},
arguments: [
{
value: name
}
]
})
.closest(j.VariableDeclarator);
// Require statements without declarations like `Object.is = require("object-is");`
const requireAssignment = root.find(j.AssignmentExpression, {
operator: '=',
right: {
callee: {
name: 'require'
},
arguments: [
{
value: name
}
]
}
});
// Side effect requires statements like `require("error-cause/auto");`
const sideEffectRequireExpression = root.find(j.ExpressionStatement, {
expression: {
callee: {
name: 'require'
},
arguments: [
{
value: name
}
]
}
});
// Return the identifier name, e.g. 'fn' in `import { fn } from 'is-boolean-object'`
// or `var fn = require('is-boolean-object')`
const identifier =
importDeclaration.paths().length > 0
? importDeclaration.get().node.specifiers[0].local.name
: requireDeclaration.paths().length > 0
? requireDeclaration.find(j.Identifier).get().node.name
: requireAssignment.paths().length > 0
? requireAssignment.find(j.Identifier).get().node.name
: null;
importDeclaration.remove();
requireDeclaration.remove();
requireAssignment.remove();
sideEffectRequireExpression.remove();
const dirtyFlag =
importDeclaration.length > 0 ||
requireDeclaration.length > 0 ||
requireAssignment.length > 0 ||
sideEffectRequireExpression.length > 0;
return { identifier, dirtyFlag };
}
export default transform;
Input
9
1
2
3
4
5
6
7
var lastIndexOf = require('array.prototype.lastindexof');
var assert = require('assert');
assert.equal(lastIndexOf([1, 2, 3], 2), 1);
assert.equal(lastIndexOf([1, 0, 1], 1), 2);
assert.equal(lastIndexOf([1, 2, 3], 4), -1);
assert.equal(lastIndexOf([NaN], NaN), -1);
Output
loading
Read-only