From 7fcb5e5c7a978cc2dd2f25f735da60f3ce3d735f Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sun, 11 Sep 2022 07:59:17 +0700 Subject: [PATCH 1/3] add `inverse` option to `core-js-compat` --- packages/core-js-compat/README.md | 19 ++++++++++--------- packages/core-js-compat/compat.js | 6 ++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/core-js-compat/README.md b/packages/core-js-compat/README.md index c8890689d422..bdfd299f4a43 100644 --- a/packages/core-js-compat/README.md +++ b/packages/core-js-compat/README.md @@ -12,19 +12,20 @@ import compat from 'core-js-compat'; const { - list, // array of required modules - targets, // object with targets for each module + list, // array of required modules + targets, // object with targets for each module } = compat({ - targets: '> 1%', // browserslist query or object of minimum environment versions to support, see below - modules: [ // optional list / filter of modules - regex, sting or an array of them: - 'core-js/actual', // - an entry point - 'esnext.array.unique-by', // - a module name (or just a start of a module name) - /^web\./, // - regex that a module name must satisfy + targets: '> 1%', // browserslist query or object of minimum environment versions to support, see below + modules: [ // optional list / filter of modules - regex, sting or an array of them: + 'core-js/actual', // - an entry point + 'esnext.array.unique-by', // - a module name (or just a start of a module name) + /^web\./, // - regex that a module name must satisfy ], - exclude: [ // optional list / filter of modules to exclude, the signature is similar to `modules` option + exclude: [ // optional list / filter of modules to exclude, the signature is similar to `modules` option 'web.atob', ], - version: '3.25', // used `core-js` version, by default - the latest + version: '3.25', // used `core-js` version, by default - the latest + inverse: false, // inverse of the result - shows modules that are NOT required for the target environment }); console.log(targets); diff --git a/packages/core-js-compat/compat.js b/packages/core-js-compat/compat.js index 0a53d8ed0968..b932315004fc 100644 --- a/packages/core-js-compat/compat.js +++ b/packages/core-js-compat/compat.js @@ -54,8 +54,10 @@ module.exports = function ({ exclude = [], targets = null, version = null, + inverse = false, } = {}) { if (modules == null) modules = filter; + inverse = !!inverse; const parsedTargets = targets ? targetsParser(targets) : null; @@ -72,12 +74,12 @@ module.exports = function ({ modules = intersection(modules, version ? getModulesListForTargetVersion(version) : allModules); - modules = filterOutStabilizedProposals(modules); + if (!inverse) modules = filterOutStabilizedProposals(modules); for (const key of modules) { const check = checkModule(key, parsedTargets); - if (check.required) { + if (check.required ^ inverse) { result.list.push(key); result.targets[key] = check.targets; } From 65c08d4153c10d98cd792abd91994301e7cbfb01 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sun, 11 Sep 2022 09:31:07 +0700 Subject: [PATCH 2/3] add some tests --- tests/compat-tools/compat.mjs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/compat-tools/compat.mjs b/tests/compat-tools/compat.mjs index 795460563c80..e22f4a2d08a0 100644 --- a/tests/compat-tools/compat.mjs +++ b/tests/compat-tools/compat.mjs @@ -1,4 +1,4 @@ -import { deepEqual } from 'assert/strict'; +import { deepEqual, ok } from 'assert/strict'; import compat from 'core-js-compat/compat.js'; deepEqual(compat({ @@ -119,4 +119,15 @@ deepEqual(compat({ }, }, 'some targets'); +const { list: inverted1 } = compat({ targets: { esmodules: true }, inverse: true }); + +ok(inverted1.includes('es.symbol.iterator'), 'inverse #1'); +ok(!inverted1.includes('esnext.iterator.from'), 'inverse #2'); +ok(!inverted1.includes('esnext.array.at'), 'inverse #3'); + +const { list: inverted2 } = compat({ modules: 'core-js/es/math', targets: { esmodules: true }, inverse: true }); + +ok(inverted2.includes('es.math.acosh'), 'inverse #4'); +ok(!inverted2.includes('es.map'), 'inverse #5'); + echo(chalk.green('compat tool tested')); From cb0c4f67a01dd93347e5175b70b93dcadba4f299 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Thu, 6 Oct 2022 01:46:03 +0700 Subject: [PATCH 3/3] update the changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22c94e8f46d7..1b7ed6c8e832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Changelog ##### Unreleased -- Nothing +- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119) ##### [3.25.5 - 2022.10.04](https://github.com/zloirock/core-js/releases/tag/v3.25.5) - Fixed regression with an error on reuse of some built-in methods from another realm, [#1133](https://github.com/zloirock/core-js/issues/1133)