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 authored and robatwilliams committed Jan 17, 2024
1 parent 0787451 commit 33392cd
Show file tree
Hide file tree
Showing 2 changed files with 51 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
43 changes: 41 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) {

Check failure on line 20 in packages/check-es-compat/bin/cli.mjs

View workflow job for this annotation

GitHub Actions / ci (20.x)

'args' is already declared in the upper scope on line 5 column 7
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) {
es2023: true,
},
rules: {
'ecmascript-compat/compat': 'error',
'ecmascript-compat/compat': ['error', { polyfills }],
},
},
});
Expand All @@ -46,3 +47,41 @@ async function execute(files) {

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

function parseArguments(args) {

Check failure on line 51 in packages/check-es-compat/bin/cli.mjs

View workflow job for this annotation

GitHub Actions / ci (20.x)

Function 'parseArguments' has too many statements (15). Maximum allowed is 10

Check failure on line 51 in packages/check-es-compat/bin/cli.mjs

View workflow job for this annotation

GitHub Actions / ci (20.x)

'args' is already declared in the upper scope on line 5 column 7
const files = [];
const polyfills = [];
let nextArgIsPolyfills = false;

for (const arg of args) {
if (nextArgIsPolyfills) {
nextArgIsPolyfills = false;
polyfills.push(...splitPolyfillsArgument(arg));
continue;

Check failure on line 60 in packages/check-es-compat/bin/cli.mjs

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected use of continue statement
}

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

continue;

Check failure on line 70 in packages/check-es-compat/bin/cli.mjs

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected use of continue statement
}

files.push(arg);
}

return { files, polyfills };
}

function splitPolyfillsArgument(polyfills) {
const prototypeAtPolyfill = '{Array,String,TypedArray}.prototype.at';
const prototypeAtPlaceholder = '{{PROTOTYPEAT}}';

return polyfills
.replace(prototypeAtPolyfill, prototypeAtPlaceholder)
.split(',')
.map(polyfill => polyfill === prototypeAtPlaceholder ? prototypeAtPolyfill : polyfill);
}

0 comments on commit 33392cd

Please sign in to comment.