Skip to content

Commit

Permalink
[ESLint Plugin] Add next/script support for `no-unwanted-polyfillio…
Browse files Browse the repository at this point in the history
…` rule
  • Loading branch information
ka2n committed Sep 9, 2021
1 parent 1389339 commit 664adc4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,22 @@ module.exports = {
},

create: function (context) {
let scriptImport = null

return {
'JSXOpeningElement[name.name=script][attributes.length>0]'(node) {
ImportDeclaration(node) {
if (node.source.value === 'next/script') {
scriptImport = node.specifiers[0].local.name
}
},
JSXOpeningElement(node) {
if (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'
)
Expand Down
29 changes: 29 additions & 0 deletions test/unit/eslint-plugin-next/no-unwanted-polyfillio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ ruleTester.run('unwanted-polyfillsio', rule, {
);
}
}`,
`import Script from 'next/script';
export function MyApp({ Component, pageProps }) {
return (
<div>
<Component {...pageProps} />
<Script src='https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver' />
</div>
);
}`,
],

invalid: [
Expand Down Expand Up @@ -82,5 +92,24 @@ ruleTester.run('unwanted-polyfillsio', rule, {
},
],
},
{
code: `import NextScript from 'next/script';
export function MyApp({ Component, pageProps }) {
return (
<div>
<Component {...pageProps} />
<NextScript src='https://polyfill.io/v3/polyfill.min.js?features=Array.prototype.copyWithin' />
</div>
);
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. Array.prototype.copyWithin is already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio.',
type: 'JSXOpeningElement',
},
],
},
],
})

0 comments on commit 664adc4

Please sign in to comment.