rsnext/packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js

129 lines
3.8 KiB
JavaScript
Raw Normal View History

const NEXT_POLYFILLED_FEATURES = [
'Array.prototype.@@iterator',
'Array.prototype.copyWithin',
'Array.prototype.fill',
'Array.prototype.find',
'Array.prototype.findIndex',
'Array.prototype.flatMap',
'Array.prototype.flat',
'Array.from',
'Array.prototype.includes',
'Array.of',
'Function.prototype.name',
'fetch',
'Map',
'Number.EPSILON',
'Number.Epsilon',
'Number.isFinite',
'Number.isNaN',
'Number.isInteger',
'Number.isSafeInteger',
'Number.MAX_SAFE_INTEGER',
'Number.MIN_SAFE_INTEGER',
'Object.entries',
'Object.getOwnPropertyDescriptor',
'Object.getOwnPropertyDescriptors',
'Object.is',
'Object.keys',
'Object.values',
'Reflect',
'Set',
'Symbol',
'Symbol.asyncIterator',
'String.prototype.codePointAt',
'String.prototype.endsWith',
'String.fromCodePoint',
'String.prototype.includes',
'String.prototype.@@iterator',
'String.prototype.padEnd',
'String.prototype.padStart',
'String.prototype.repeat',
'String.raw',
'String.prototype.startsWith',
'String.prototype.trimEnd',
'String.prototype.trimStart',
'String.prototype.trim',
'URL',
'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.
'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.
]
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description:
'Prohibit unwanted features to be listed in Polyfill.io tag.',
category: 'HTML',
recommended: true,
2021-10-16 08:16:41 +02:00
url: 'https://nextjs.org/docs/messages/no-unwanted-polyfillio',
},
fixable: null, // or "code" or "whitespace"
},
create: function (context) {
let scriptImport = null
return {
ImportDeclaration(node) {
if (node.source && node.source.value === 'next/script') {
scriptImport = node.specifiers[0].local.name
}
},
JSXOpeningElement(node) {
if (
node.name &&
node.name.name !== 'script' &&
node.name.name !== scriptImport
) {
return
}
if (node.attributes.length === 0) {
return
}
const srcNode = node.attributes.find(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'src'
)
if (!srcNode || srcNode.value.type !== 'Literal') {
return
}
const src = srcNode.value.value
if (
src.startsWith('https://cdn.polyfill.io/v2/') ||
src.startsWith('https://polyfill.io/v3/')
) {
const featureQueryString = new URL(src).searchParams.get('features')
const featuresRequested = (featureQueryString || '').split(',')
const unwantedFeatures = featuresRequested.filter((feature) =>
NEXT_POLYFILLED_FEATURES.includes(feature)
)
if (unwantedFeatures.length > 0) {
context.report({
node,
Adds ESLint with default rule-set (#23702) This PR re-includes ESLint with some notable changes, namely a guided setup similar to how TypeScript is instantiated in a Next.js application. To add ESLint to a project, developers will have to create an `.eslintrc` file in the root of their project or add an empty `eslintConfig` object to their `package.json` file. ```js touch .eslintrc ``` Then running `next build` will show instructions to install the required packages needed: <img width="862" alt="Screen Shot 2021-04-19 at 7 38 27 PM" src="https://user-images.githubusercontent.com/12476932/115316182-dfd51b00-a146-11eb-830c-90bad20ed151.png"> Once installed and `next build` is run again, `.eslintrc` will be automatically configured to include the default config: ```json { "extends": "next" } ``` In addition to this change: - The feature is now under the experimental flag and requires opt-in. After testing and feedback, it will be switched to the top-level namespace and turned on by default. - A new ESLint shareable configuration package is included that can be extended in any application with `{ extends: 'next' }` - This default config extends recommended rule sets from [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react), [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks), and [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next) - All rules in [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next) have been modified to include actionable links that show more information to help resolve each issue
2021-04-30 13:09:07 +02:00
message: `No duplicate polyfills from Polyfill.io are allowed. ${unwantedFeatures.join(
', '
Adds ESLint with default rule-set (#23702) This PR re-includes ESLint with some notable changes, namely a guided setup similar to how TypeScript is instantiated in a Next.js application. To add ESLint to a project, developers will have to create an `.eslintrc` file in the root of their project or add an empty `eslintConfig` object to their `package.json` file. ```js touch .eslintrc ``` Then running `next build` will show instructions to install the required packages needed: <img width="862" alt="Screen Shot 2021-04-19 at 7 38 27 PM" src="https://user-images.githubusercontent.com/12476932/115316182-dfd51b00-a146-11eb-830c-90bad20ed151.png"> Once installed and `next build` is run again, `.eslintrc` will be automatically configured to include the default config: ```json { "extends": "next" } ``` In addition to this change: - The feature is now under the experimental flag and requires opt-in. After testing and feedback, it will be switched to the top-level namespace and turned on by default. - A new ESLint shareable configuration package is included that can be extended in any application with `{ extends: 'next' }` - This default config extends recommended rule sets from [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react), [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks), and [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next) - All rules in [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next) have been modified to include actionable links that show more information to help resolve each issue
2021-04-30 13:09:07 +02:00
)} ${
unwantedFeatures.length > 1 ? 'are' : 'is'
} already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio.`,
})
}
}
},
}
},
}