Skip to content

Commit

Permalink
Update polyfill for eslint no-unwanted-polyfillio rule (#33170)
Browse files Browse the repository at this point in the history
Fixes #33072

I documented all `esXXX` features to be sure that they were already polyfilled. Only `es2019` feature `Object.fromEntries` is not already polyfilled by nextjs.

I added some unwanted polyfill (that are polyfilled by nextjs).

I kept the `es5`, `es6` and `es2015` "as-is" as they contain functions that does not seem to be explicitly polyfilled (all `Math` functions or `Date.now` for example) in the [polyfill file](https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js)

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
jdeniau and ijjk committed Aug 8, 2022
1 parent f60f8ae commit 8ebd1a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
@@ -1,3 +1,4 @@
// Keep in sync with next.js polyfills file : https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js
const NEXT_POLYFILLED_FEATURES = [
'Array.prototype.@@iterator',
'Array.prototype.copyWithin',
Expand All @@ -20,7 +21,11 @@ const NEXT_POLYFILLED_FEATURES = [
'Number.isSafeInteger',
'Number.MAX_SAFE_INTEGER',
'Number.MIN_SAFE_INTEGER',
'Number.parseFloat',
'Number.parseInt',
'Object.assign',
'Object.entries',
'Object.fromEntries',
'Object.getOwnPropertyDescriptor',
'Object.getOwnPropertyDescriptors',
'Object.is',
Expand All @@ -42,21 +47,21 @@ const NEXT_POLYFILLED_FEATURES = [
'String.prototype.startsWith',
'String.prototype.trimEnd',
'String.prototype.trimStart',
'String.prototype.trim',
'URL',
'URL.prototype.toJSON',
'URLSearchParams',
'WeakMap',
'WeakSet',
'Promise',
'Promise.prototype.finally',
'es2015', // Should be covered by babel-preset-env instead.
'es2016', // Should be covered by babel-preset-env instead.
'es2017', // Should be covered by babel-preset-env instead.
'es2018', // Should be covered by babel-preset-env instead.
'es2019', // Should be covered by babel-preset-env instead.
'es2016', // contains polyfilled 'Array.prototype.includes', 'String.prototype.padEnd' and 'String.prototype.padStart'
'es2017', // contains polyfilled 'Object.entries', 'Object.getOwnPropertyDescriptors', 'Object.values', 'String.prototype.padEnd' and 'String.prototype.padStart'
'es2018', // contains polyfilled 'Promise.prototype.finally' and ''Symbol.asyncIterator'
'es2019', // Contains polyfilled 'Object.fromEntries' and polyfilled 'Array.prototype.flat', 'Array.prototype.flatMap', 'String.prototype.trimEnd' and 'String.prototype.trimStart'
'es5', // Should be covered by babel-preset-env instead.
'es6', // Should be covered by babel-preset-env instead.
'es7', // Should be covered by babel-preset-env instead.
'es7', // contains polyfilled 'Array.prototype.includes', 'String.prototype.padEnd' and 'String.prototype.padStart'
]

const url = 'https://nextjs.org/docs/messages/no-unwanted-polyfillio'
Expand Down
1 change: 1 addition & 0 deletions packages/next-polyfill-nomodule/src/index.js
@@ -1,4 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
// Keep in sync with eslint no-unwanted-polyfillio rule: https://github.com/vercel/next.js/blob/master/packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
import 'core-js/features/array/copy-within'
import 'core-js/features/array/fill'
import 'core-js/features/array/find'
Expand Down
20 changes: 20 additions & 0 deletions test/unit/eslint-plugin-next/no-unwanted-polyfillio.test.ts
Expand Up @@ -111,5 +111,25 @@ ruleTester.run('unwanted-polyfillsio', rule, {
},
],
},
{
code: `import {Head} from 'next/document';
export class ES2019Features extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=Object.fromEntries'></script>
</div>
);
}
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. Object.fromEntries is already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio',
},
],
},
],
})

0 comments on commit 8ebd1a2

Please sign in to comment.