-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (55 loc) · 1.67 KB
/
index.ts
File metadata and controls
65 lines (55 loc) · 1.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
/**
* Wraps calls to ES2015 instance methods of Array and String
* with a call to a polyfill that then uses the ponyfills of
* core.js.
*/
export default function({types: t}) {
return {
visitor: {
/**
* We need to check every method call and use
* the polyfill when its name is one of the
* names of the new instance methods.
*/
CallExpression(path, state) {
var node = path.node,
callee = node.callee,
property;
if (!t.isMemberExpression(callee)) {
return;
}
property = callee.property;
if (callee.computed || METHODS.indexOf(property.name) < 0) {
return;
}
path.replaceWith(
t.callExpression(
state.addImport('babel-runtime-es2015-instance-methods/call-instance-method', 'default', 'callInstanceMethod'),
[
callee.object,
t.stringLiteral(property.name),
t.arrayExpression(node.arguments)
]
)
);
}
}
};
}
//////////
var METHODS = [
// Array
'copyWithin',
'entries',
'fill',
'find',
'findIndex',
'keys',
'values',
// String
'codePointAt',
'endsWith',
'includes',
'repeat',
'startsWith'
];