Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only required polyfill.io lint rule #15277

Merged
merged 6 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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',
'es5',
'es6',
]

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Prohibit full page refresh for nextjs pages',
Copy link
Contributor

@janicklas-ralph janicklas-ralph Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description needs to be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

category: 'HTML',
recommended: true,
},
fixable: null, // or "code" or "whitespace"
},

create: function (context) {
return {
'JSXOpeningElement[name.name=script][attributes.length>0]'(node) {
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: `You're requesting polyfills from polyfill.io which are already shipped with NextJS. Please remove ${unwantedFeatures.join(
', '
)} from the features list.`,
})
}
}
},
}
},
}
88 changes: 88 additions & 0 deletions test/eslint-plugin-next/no-unwanted-polyfillio.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const rule = require('@next/eslint-plugin-next/lib/rules/no-unwanted-polyfillio')

const RuleTester = require('eslint').RuleTester

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})

var ruleTester = new RuleTester()
ruleTester.run('unwanted-polyfillsio', rule, {
valid: [
`import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=AbortController'></script>
</div>
);
}
}`,
`import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver'></script>
</div>
);
}
}`,
],

invalid: [
{
code: `import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=WeakSet%2CPromise%2CPromise.prototype.finally%2Ces2015%2Ces5%2Ces6'></script>
</div>
);
}
}`,
errors: [
{
message:
"You're requesting polyfills from polyfill.io which are already shipped with NextJS. Please remove WeakSet, Promise, Promise.prototype.finally, es2015, es5, es6 from the features list.",
type: 'JSXOpeningElement',
},
],
},
{
code: `
export class Blah {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=Array.prototype.copyWithin'></script>
</div>
);
}
}`,
errors: [
{
message:
"You're requesting polyfills from polyfill.io which are already shipped with NextJS. Please remove Array.prototype.copyWithin from the features list.",
type: 'JSXOpeningElement',
},
],
},
],
})