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

Add eslint rule for not allowing styled-jsx in _document.js #32678

Merged
merged 19 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions packages/eslint-plugin-next/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'no-document-import-in-page': require('./rules/no-document-import-in-page'),
'no-head-import-in-document': require('./rules/no-head-import-in-document'),
'no-script-in-document': require('./rules/no-script-in-document'),
'no-style-in-document': require('./rules/no-style-in-document'),
'no-script-in-head': require('./rules/no-script-in-head'),
'no-server-import-in-page': require('./rules/no-server-import-in-page'),
'no-typos': require('./rules/no-typos'),
Expand All @@ -38,6 +39,7 @@ module.exports = {
'@next/next/no-document-import-in-page': 2,
'@next/next/no-head-import-in-document': 2,
'@next/next/no-script-in-document': 2,
'@next/next/no-style-in-document': 2,
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
'@next/next/no-script-in-head': 2,
'@next/next/no-server-import-in-page': 2,
'@next/next/no-typos': 1,
Expand Down
45 changes: 45 additions & 0 deletions packages/eslint-plugin-next/lib/rules/no-style-in-document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path')

module.exports = {
meta: {
docs: {
description: 'Disallow using custom styles inside pages/_document.js',
recommended: true,
},
fixable: 'code',
},
create: function (context) {
return {
ImportDeclaration(node) {
const document = context.getFilename().split('pages')[1]
if (!document || !path.parse(document).name.startsWith('_document')) {
return
}
},
JSXElement(node) {
const styleTag = node.children.find(
(child) =>
child.openingElement &&
child.openingElement.name &&
child.openingElement.name.type === 'JSXIdentifier' &&
child.openingElement.name.name === 'style'
)

const jsxAttr =
styleTag &&
styleTag.openingElement.attributes.find(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'jsx'
)

if (styleTag && jsxAttr) {
context.report({
node,
message: `Do not use <style> inside pages/_document.js.`,
})
}
},
}
},
}

module.exports.schema = []
74 changes: 74 additions & 0 deletions test/unit/eslint-plugin-next/no-style-in-document.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-style-in-document'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()

ruleTester.run('no-style-in-document', rule, {
valid: [
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
`import Document, { Html, Head, Main, NextScript } from 'next/document'

export class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}

render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}`,
],

invalid: [
{
code: `
import Document, { Html, Head, Main, NextScript } from 'next/document'

export class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}

render() {
return (
<Html>
<Head />
<style jsx>{"\
body{\
color:red;\
}\
"}</style>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}`,
errors: [
{
message: 'Do not use <style> inside pages/_document.js.',
},
],
},
],
})