Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inverse option to core-js-compat #1119

Merged
merged 3 commits into from Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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)
Expand Down
19 changes: 10 additions & 9 deletions packages/core-js-compat/README.md
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js-compat/compat.js
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion 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({
Expand Down Expand Up @@ -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'));