Skip to content

Commit 827062f

Browse files
committed
add arg-is-string plugin filter
1 parent df4990a commit 827062f

File tree

29 files changed

+167
-3
lines changed

29 files changed

+167
-3
lines changed

packages/core-js-babel-plugin/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,23 @@ module.exports = defineProvider(({
8989
}
9090
}
9191

92+
function isCallOrNew(node, callee) {
93+
return t.isCallExpression(node, { callee }) || t.isNewExpression(node, { callee });
94+
}
95+
9296
function filter(name, args, path) {
9397
const { node, parent } = path;
94-
// eslint-disable-next-line sonarjs/no-small-switch -- tba
9598
switch (name) {
9699
case 'min-args':
97-
if (!t.isCallExpression(parent, { callee: node }) && !t.isNewExpression(parent, { callee: node })) return false;
100+
if (!isCallOrNew(parent, node)) return false;
98101
if (parent.arguments.length >= args[0]) return false;
99102
return parent.arguments.every(arg => !t.isSpreadElement(arg));
103+
case 'arg-is-string': {
104+
if (!isCallOrNew(parent, node)) return false;
105+
if (parent.arguments.length < args[0]) return true;
106+
const arg = parent.arguments[args[0]];
107+
return t.isStringLiteral(arg) || t.isTemplateLiteral(arg);
108+
}
100109
}
101110
}
102111

packages/core-js-compat/src/built-in-definitions.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ export const InstanceProperties = {
312312
some: { global: 'iterator/some' },
313313
sort: 'instance/sort',
314314
splice: 'instance/splice',
315-
split: undefined,
315+
split: { global: {
316+
common: { dependencies: 'string/split', filters: [['arg-is-string', 0]] },
317+
string: { dependencies: 'string/split', filters: [['arg-is-string', 0]] },
318+
} },
316319
startsWith: 'instance/starts-with',
317320
sticky: { global: 'regexp/sticky' },
318321
strike: { global: 'string/strike' },
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({}).split();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"plugins": [
3+
[
4+
"@core-js",
5+
{
6+
"method": "usage-global",
7+
"version": "4.0",
8+
"targets": {
9+
"ie": 11
10+
}
11+
}
12+
]
13+
]
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({}).split();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({}).split(/./);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"plugins": [
3+
[
4+
"@core-js",
5+
{
6+
"method": "usage-global",
7+
"version": "4.0",
8+
"targets": {
9+
"ie": 11
10+
}
11+
}
12+
]
13+
]
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({}).split(/./);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({}).split('');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"plugins": [
3+
[
4+
"@core-js",
5+
{
6+
"method": "usage-global",
7+
"version": "4.0",
8+
"targets": {
9+
"ie": 11
10+
}
11+
}
12+
]
13+
]
14+
}

0 commit comments

Comments
 (0)