rsnext/packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
Joost De Cock 34dcede690
fix(eslint-plugin-next): Broken links in eslint output (#32837)
This fixes broken links in the eslint output by removing the trailing full stop.
It also makes the formatting of (the output of) the various rules consistent.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

> I don't think this is a bug, nor a feature, nor is it really documentation. 
> It's just a small nuisance that I bumped into and felt compelled to fix.
> I went with documentation as that seems the closest match

## What does this pull request do?

The elslint output of `eslint-plugin-next` contains useful links to the documentation about the various rules.
Unfortunately, on most (but not all) rules, those links are immediately followed by a full stop (`.`).

The terminal (or any parser) has no way of knowing that the full stop is not part of the URL. 
So it includes it and clicking the link leads to a 404 on the nextjs.org website.
 
![eslint](https://user-images.githubusercontent.com/1708494/147452577-43ad4ce7-df75-4d48-ab78-70b9b8212b7e.png)

This PR fixes that by removing the full stop. 

## But a final full stop is better grammar

I considered alternatives (such as [a zero-width space character](https://en.wikipedia.org/wiki/Zero-width_space#Prohibited_in_URLs)) in case the final full stop was part of the style guide or something.

However, as I went through the eslint rules, I notices that the messages for various rules were formatted inconsistently. 
Some with final full stop, some without.

As such, I made the all consistent with this structure:

> [message]. See: [url]

I feel this is a better solution than using the zero-width space as these sort of invisible characters 
in code can be a red flag that something fishy is going on.

I submit this pull request in the hope it will be useful, and a positive contribution to a project I have a great deal of appreciation for.
That being said, I fully understand if people would consider this a non-issue.
2021-12-28 02:18:39 +00:00

128 lines
3.8 KiB
JavaScript

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,
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,
message: `No duplicate polyfills from Polyfill.io are allowed. ${unwantedFeatures.join(
', '
)} ${
unwantedFeatures.length > 1 ? 'are' : 'is'
} already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio`,
})
}
}
},
}
},
}