Skip to content

Commit df4990a

Browse files
committed
add min-args plugin filter
1 parent fdc5c27 commit df4990a

File tree

48 files changed

+300
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+300
-34
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ function resolveHint(desc, meta) {
3333
if (hint === $hint) return desc[hint];
3434
required = false;
3535
}
36-
if (required) return desc.common;
36+
return required ? desc.common : null;
3737
}
3838

3939
module.exports = defineProvider(({
4040
createMetaResolver,
4141
debug,
4242
method,
4343
targets,
44+
babel: { types: t },
4445
}, {
4546
pkg,
4647
pkgs,
@@ -88,6 +89,17 @@ module.exports = defineProvider(({
8889
}
8990
}
9091

92+
function filter(name, args, path) {
93+
const { node, parent } = path;
94+
// eslint-disable-next-line sonarjs/no-small-switch -- tba
95+
switch (name) {
96+
case 'min-args':
97+
if (!t.isCallExpression(parent, { callee: node }) && !t.isNewExpression(parent, { callee: node })) return false;
98+
if (parent.arguments.length >= args[0]) return false;
99+
return parent.arguments.every(arg => !t.isSpreadElement(arg));
100+
}
101+
}
102+
91103
return {
92104
name: 'core-js@4',
93105
polyfills: modulesListForTargetVersion,
@@ -97,15 +109,17 @@ module.exports = defineProvider(({
97109
injectCoreJSModulesForEntry(entry, utils);
98110
path.remove();
99111
},
100-
usageGlobal(meta, utils) {
112+
usageGlobal(meta, utils, path) {
101113
const resolved = resolve(meta);
102114
if (!resolved) return;
103115
let { kind, desc: { global: desc } } = resolved;
104116
if (kind === 'instance') {
105117
desc = resolveHint(desc, meta);
106-
if (!desc) return true;
118+
if (desc === null) return true;
107119
}
108-
for (const entry of desc.dependencies) {
120+
const { dependencies, filters } = desc;
121+
if (filters?.some(([name, ...args]) => filter(name, args, path))) return true;
122+
for (const entry of dependencies) {
109123
injectCoreJSModulesForEntry(`${ mode }/${ entry }`, utils);
110124
}
111125
return true;

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
const ErrorConstructorDependencies = { global: 'error/constructor' };
1+
const ErrorConstructor = { global: {
2+
dependencies: 'error/constructor',
3+
filters: [['min-args', 2]],
4+
} };
25

3-
const ErrorStaticDependencies = { isError: 'error/is-error' };
6+
const ErrorStatic = { isError: 'error/is-error' };
47

5-
const TypedArrayStaticMethods = {
8+
const TypedArrayStatic = {
69
from: { global: 'typed-array/from' },
710
of: { global: 'typed-array/of' },
811
};
@@ -15,8 +18,8 @@ export const Globals = {
1518
DataView: { global: 'data-view/constructor' },
1619
DOMException: { global: 'dom-exception/constructor' },
1720
DisposableStack: 'disposable-stack/constructor',
18-
Error: ErrorConstructorDependencies,
19-
EvalError: ErrorConstructorDependencies,
21+
Error: ErrorConstructor,
22+
EvalError: ErrorConstructor,
2023
Float32Array: 'typed-array/float32-array',
2124
Float64Array: 'typed-array/float64-array',
2225
Int8Array: 'typed-array/int8-array',
@@ -30,16 +33,16 @@ export const Globals = {
3033
Map: 'map/constructor',
3134
Number: { global: 'number/constructor' },
3235
Promise: 'promise/constructor',
33-
RangeError: ErrorConstructorDependencies,
34-
ReferenceError: ErrorConstructorDependencies,
36+
RangeError: ErrorConstructor,
37+
ReferenceError: ErrorConstructor,
3538
Reflect: undefined,
3639
RegExp: { global: 'regexp/constructor' },
3740
Set: 'set/constructor',
3841
SuppressedError: 'suppressed-error/constructor',
3942
Symbol: 'symbol/constructor',
40-
SyntaxError: ErrorConstructorDependencies,
41-
TypeError: ErrorConstructorDependencies,
42-
URIError: ErrorConstructorDependencies,
43+
SyntaxError: ErrorConstructor,
44+
TypeError: ErrorConstructor,
45+
URIError: ErrorConstructor,
4346
URL: 'url/constructor',
4447
URLSearchParams: 'url-search-params/constructor',
4548
WeakMap: 'weak-map/constructor',
@@ -70,8 +73,8 @@ export const StaticProperties = {
7073
ArrayBuffer: {
7174
isView: { global: 'array-buffer/is-view' },
7275
},
73-
Error: ErrorStaticDependencies,
74-
EvalError: ErrorStaticDependencies,
76+
Error: ErrorStatic,
77+
EvalError: ErrorStatic,
7578
Iterator: {
7679
concat: 'iterator/concat',
7780
from: 'iterator/from',
@@ -155,8 +158,8 @@ export const StaticProperties = {
155158
try: 'promise/try',
156159
withResolvers: 'promise/with-resolvers',
157160
},
158-
RangeError: ErrorStaticDependencies,
159-
ReferenceError: ErrorStaticDependencies,
161+
RangeError: ErrorStatic,
162+
ReferenceError: ErrorStatic,
160163
Reflect: {
161164
apply: 'reflect/apply',
162165
construct: 'reflect/construct',
@@ -208,9 +211,9 @@ export const StaticProperties = {
208211
toStringTag: 'symbol/to-string-tag',
209212
unscopables: 'symbol/unscopables',
210213
},
211-
SyntaxError: ErrorStaticDependencies,
212-
TypeError: ErrorStaticDependencies,
213-
URIError: ErrorStaticDependencies,
214+
SyntaxError: ErrorStatic,
215+
TypeError: ErrorStatic,
216+
URIError: ErrorStatic,
214217
URL: {
215218
canParse: 'url/can-parse',
216219
parse: 'url/parse',
@@ -223,23 +226,23 @@ export const StaticProperties = {
223226
from: 'weak-set/from',
224227
of: 'weak-set/of',
225228
},
226-
Float32Array: TypedArrayStaticMethods,
227-
Float64Array: TypedArrayStaticMethods,
228-
Int8Array: TypedArrayStaticMethods,
229-
Int16Array: TypedArrayStaticMethods,
230-
Int32Array: TypedArrayStaticMethods,
229+
Float32Array: TypedArrayStatic,
230+
Float64Array: TypedArrayStatic,
231+
Int8Array: TypedArrayStatic,
232+
Int16Array: TypedArrayStatic,
233+
Int32Array: TypedArrayStatic,
231234
Uint8Array: {
232-
...TypedArrayStaticMethods,
235+
...TypedArrayStatic,
233236
fromBase64: 'typed-array/from-base64',
234237
fromHex: 'typed-array/from-hex',
235238
},
236-
Uint8ClampedArray: TypedArrayStaticMethods,
237-
Uint16Array: TypedArrayStaticMethods,
238-
Uint32Array: TypedArrayStaticMethods,
239+
Uint8ClampedArray: TypedArrayStatic,
240+
Uint16Array: TypedArrayStatic,
241+
Uint32Array: TypedArrayStatic,
239242
WebAssembly: {
240-
CompileError: ErrorConstructorDependencies,
241-
LinkError: ErrorConstructorDependencies,
242-
RuntimeError: ErrorConstructorDependencies,
243+
CompileError: ErrorConstructor,
244+
LinkError: ErrorConstructor,
245+
RuntimeError: ErrorConstructor,
243246
},
244247
};
245248

scripts/build-entries/entries-definitions.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ export const features = {
889889
template: $path,
890890
},
891891
'error/constructor': {
892-
modules: [/^(?:es|esnext)\.error\./],
892+
modules: ['es.error.cause'],
893893
template: $path, // !!!!!!!
894894
},
895895
'error/is-error': {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error(msg);
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+
Error(msg);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error(msg, opts);
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "core-js/modules/es.error.cause";
2+
Error(msg, opts);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new Error(msg);

0 commit comments

Comments
 (0)