-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7fcdc0
adding a lint rule for no unwanted polyfill.io features
prateekbh 432a8a7
adding URL and URLSearch Params
prateekbh 9a2dc9a
Merge branch 'canary' into polyfillio-lint
prateekbh 35ad5db
fixing the description
prateekbh 79a22e5
Merge branch 'canary' into polyfillio-lint
prateekbh eba1f86
Merge branch 'canary' into polyfillio-lint
prateekbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
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.`, | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done