Skip to content

Commit

Permalink
Implement polyfills option in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Nov 30, 2023
1 parent 5e5e5cd commit 6e1de62
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/check-es-compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ Firefox >= 58

<!--- Absolute link, in order to work from NPM website --->

The optional `--polyfills` argument can be used to specify polyfills that your application loads. These features are
therefore considered supported in all browsers. Features that are polyfillable and can be specified here can be found
in the [rule schema](https://github.com/robatwilliams/es-compat/blob/master/packages/eslint-plugin-ecmascript-compat/lib/rule.js).

```bash
$ npx check-es-compat . --polyfills="Array.prototype.includes,Promise.prototype.finally"
```

<!--- Absolute link, in order to work from NPM website --->

It [doesn't currently support](https://github.com/robatwilliams/es-compat/issues/69) ES modules.

<!--- Absolute link, in order to work from NPM website --->
Expand Down
33 changes: 31 additions & 2 deletions packages/check-es-compat/bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ if (args.length === 0) {
}
}

async function execute(files) {
async function execute(args) {
const { files, polyfills } = parseArguments(args);
const eslint = new ESLint({
// Ignore any config files
useEslintrc: false,
Expand All @@ -34,7 +35,7 @@ async function execute(files) {
es2021: true,
},
rules: {
'ecmascript-compat/compat': 'error',
'ecmascript-compat/compat': ['error', { polyfills }],
},
},
});
Expand All @@ -46,3 +47,31 @@ async function execute(files) {

return { hasErrors: results.some((result) => result.errorCount > 0) };
}

function parseArguments(args) {
const files = [];
const polyfills = [];
let nextArgIsPolyfills = false;

for (const arg of args) {
if (nextArgIsPolyfills) {
nextArgIsPolyfills = false;
polyfills.push(...arg.split(','));
continue;
}

if (arg.startsWith('--polyfills')) {
if (arg.startsWith('--polyfills=')) {
polyfills.push(...arg.slice(12).split(','));
} else {
nextArgIsPolyfills = true;
}

continue;
}

files.push(arg);
}

return { files, polyfills };
}

0 comments on commit 6e1de62

Please sign in to comment.