Skip to content

Commit d8d7c80

Browse files
committed
prototype magic options as core-js version for tooling
1 parent 09d785d commit d8d7c80

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

packages/core-js-compat/get-modules-list-for-target-version.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
'use strict';
2-
const { compare, intersection, semver } = require('./helpers');
2+
const { compare, intersection, normalizeCoreJSVersion } = require('./helpers');
33
const modulesByVersions = require('./modules-by-versions');
44
const modules = require('./modules');
55

66
module.exports = function (raw) {
7-
if (!['string', 'object'].includes(typeof raw)) {
8-
throw new TypeError('`core-js` version should be specified as a SemVer string with minor component');
9-
}
10-
const corejs = semver(raw, true);
11-
if (corejs.major !== 4) {
12-
throw new RangeError('This version of `@core-js/compat` works only with `core-js@4`');
13-
}
7+
const corejs = normalizeCoreJSVersion(raw);
148
const result = [];
159
for (const version of Object.keys(modulesByVersions)) {
1610
if (compare(version, '<=', corejs)) {

packages/core-js-compat/helpers.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
'use strict';
22
const SEMVER = /(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?/;
33
// eslint-disable-next-line redos/no-vulnerable -- ok
4-
const SEMVER_WITH_REQUIRED_MINOR = /(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/;
4+
const SEMVER_WITH_REQUIRED_MINOR_COMPONENT = /(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/;
55

6-
function semver(input, requiredMinor) {
6+
function semver(input, requiredMinorComponent) {
77
if (input instanceof semver) return input;
88
// eslint-disable-next-line new-cap -- ok
9-
if (!(this instanceof semver)) return new semver(input, requiredMinor);
10-
const match = (requiredMinor ? SEMVER_WITH_REQUIRED_MINOR : SEMVER).exec(input);
9+
if (!(this instanceof semver)) return new semver(input, requiredMinorComponent);
10+
11+
const match = (requiredMinorComponent ? SEMVER_WITH_REQUIRED_MINOR_COMPONENT : SEMVER).exec(input);
12+
1113
if (!match) {
1214
let message = `Invalid version: ${ input }`;
13-
if (requiredMinor && SEMVER.test(input)) message += ', minor component required';
15+
if (requiredMinorComponent && SEMVER.test(input)) message += ', minor component required';
1416
throw new TypeError(message);
1517
}
18+
1619
const { major, minor, patch } = match.groups;
1720
this.major = parseInt(major, 10);
1821
this.minor = minor ? parseInt(minor, 10) : 0;
@@ -32,6 +35,35 @@ function compare($a, operator, $b) {
3235
} return operator === '==' || operator === '<=' || operator === '>=';
3336
}
3437

38+
function normalizeCoreJSVersion(raw) {
39+
if (!['string', 'object'].includes(typeof raw)) {
40+
throw new TypeError('`core-js` version should be specified as a SemVer string with minor component');
41+
}
42+
43+
let requiredMinorComponent = true;
44+
45+
if (raw === 'node_modules') {
46+
// eslint-disable-next-line node/global-require -- ok
47+
raw = require('core-js/package.json').version;
48+
} else if (raw === 'package.json') {
49+
requiredMinorComponent = false;
50+
// eslint-disable-next-line node/global-require, import/no-dynamic-require -- ok
51+
const { dependencies, devDependencies, peerDependencies } = require(`${ process.cwd() }/package.json`);
52+
raw = dependencies?.['core-js'] ?? devDependencies?.['core-js'] ?? peerDependencies?.['core-js'];
53+
if (raw === undefined) {
54+
throw new TypeError('`core-js` is not specified in your `package.json`');
55+
}
56+
}
57+
58+
const version = semver(raw, requiredMinorComponent);
59+
60+
if (version.major !== 4) {
61+
throw new RangeError('This version of `@core-js/compat` works only with `core-js@4`');
62+
}
63+
64+
return version;
65+
}
66+
3567
function filterOutStabilizedProposals(modules) {
3668
const modulesSet = new Set(modules);
3769

@@ -60,6 +92,7 @@ module.exports = {
6092
compare,
6193
filterOutStabilizedProposals,
6294
intersection,
95+
normalizeCoreJSVersion,
6396
semver,
6497
sortObjectByKey,
6598
};

0 commit comments

Comments
 (0)