Skip to content

Commit

Permalink
prototype magic options as core-js version for tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 15, 2023
1 parent b571791 commit 738c92d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
10 changes: 2 additions & 8 deletions packages/core-js-compat/get-modules-list-for-target-version.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
'use strict';
const { compare, intersection, semver } = require('./helpers');
const { compare, intersection, normalizeCoreJSVersion } = require('./helpers');
const modulesByVersions = require('./modules-by-versions');
const modules = require('./modules');

module.exports = function (raw) {
if (!['string', 'object'].includes(typeof raw)) {
throw new TypeError('`core-js` version should be specified as a SemVer string with minor component');
}
const corejs = semver(raw, true);
if (corejs.major !== 4) {
throw new RangeError('This version of `@core-js/compat` works only with `core-js@4`');
}
const corejs = normalizeCoreJSVersion(raw);
const result = [];
for (const version of Object.keys(modulesByVersions)) {
if (compare(version, '<=', corejs)) {
Expand Down
43 changes: 38 additions & 5 deletions packages/core-js-compat/helpers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use strict';
const SEMVER = /(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?/;
// eslint-disable-next-line redos/no-vulnerable -- ok
const SEMVER_WITH_REQUIRED_MINOR = /(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/;
const SEMVER_WITH_REQUIRED_MINOR_COMPONENT = /(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/;

function semver(input, requiredMinor) {
function semver(input, requiredMinorComponent) {
if (input instanceof semver) return input;
// eslint-disable-next-line new-cap -- ok
if (!(this instanceof semver)) return new semver(input, requiredMinor);
const match = (requiredMinor ? SEMVER_WITH_REQUIRED_MINOR : SEMVER).exec(input);
if (!(this instanceof semver)) return new semver(input, requiredMinorComponent);

const match = (requiredMinorComponent ? SEMVER_WITH_REQUIRED_MINOR_COMPONENT : SEMVER).exec(input);

if (!match) {
let message = `Invalid version: ${ input }`;
if (requiredMinor && SEMVER.test(input)) message += ', minor component required';
if (requiredMinorComponent && SEMVER.test(input)) message += ', minor component required';
throw new TypeError(message);
}

const { major, minor, patch } = match.groups;
this.major = parseInt(major, 10);
this.minor = minor ? parseInt(minor, 10) : 0;
Expand All @@ -32,6 +35,35 @@ function compare($a, operator, $b) {
} return operator === '==' || operator === '<=' || operator === '>=';
}

function normalizeCoreJSVersion(raw) {
if (!['string', 'object'].includes(typeof raw)) {
throw new TypeError('`core-js` version should be specified as a SemVer string with minor component');
}

let requiredMinorComponent = true;

if (raw === 'node_modules') {
// eslint-disable-next-line node/global-require -- ok
raw = require('core-js/package.json').version;
} else if (raw === 'package.json') {
requiredMinorComponent = false;
// eslint-disable-next-line node/global-require, import/no-dynamic-require -- ok
const { dependencies, devDependencies, peerDependencies } = require(`${ process.cwd() }/package.json`);
raw = dependencies?.['core-js'] ?? devDependencies?.['core-js'] ?? peerDependencies?.['core-js'];
if (raw === undefined) {
throw new TypeError('`core-js` is not specified in your `package.json`');
}
}

const version = semver(raw, requiredMinorComponent);

if (version.major !== 4) {
throw new RangeError('This version of `@core-js/compat` works only with `core-js@4`');
}

return version;
}

function filterOutStabilizedProposals(modules) {
const modulesSet = new Set(modules);

Expand Down Expand Up @@ -60,6 +92,7 @@ module.exports = {
compare,
filterOutStabilizedProposals,
intersection,
normalizeCoreJSVersion,
semver,
sortObjectByKey,
};

0 comments on commit 738c92d

Please sign in to comment.