Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Oct 1, 2022
1 parent 5455c63 commit bbbe7e1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"build": "swc -d dist src",
"dev": "tsc -w",
"prepublishOnly": "cd ../../ && turbo run build"
}
}
26 changes: 13 additions & 13 deletions packages/eslint-plugin-next/src/rules/date-hydration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { JSXElement, JSXAttribute } from 'estree-jsx'
import type { JSXElement, JSXExpressionContainer } from 'estree-jsx'
import { defineRule } from '../utils/define-rule'
import NodeAttributes from '../utils/node-attributes'

const url = 'https://nextjs.org/docs/messages/date-hydration'

Expand All @@ -15,26 +16,25 @@ export = defineRule({
},
create(context) {
return {
/** @param {import("estree-jsx").JSXExpressionContainer} node */
JSXExpressionContainer(node) {
if (node.expression.type !== 'CallExpression') return
if (node.expression.callee.type !== 'MemberExpression') return
if (node.expression.callee.object.type !== 'NewExpression') return
if (node.expression.callee.object.callee.type !== 'Identifier') return
if (node.expression.callee.object.callee.name !== 'Date') return
const n = node as JSXExpressionContainer
if (n.expression.type !== 'CallExpression') return
else if (n.expression.callee.type !== 'MemberExpression') return
else if (n.expression.callee.object.type !== 'NewExpression') return
else if (n.expression.callee.object.callee.type !== 'Identifier') return
else if (n.expression.callee.object.callee.name !== 'Date') return

const parent = context
.getAncestors()
.reverse()[0] as unknown as JSXElement
const suppressed = parent.openingElement.attributes.some(
(a) => (a as JSXAttribute).name.name === 'suppressHydrationWarning'
)

const attributes = new NodeAttributes(parent.openingElement)
const suppressed = attributes.has('suppressHydrationWarning')

if (suppressed) return

context.report({
node,
suggest: [
{ desc: `<Date> is not a valid React element. See: ${url}` },
],
message: `Rendering \`Date\` directly can cause a hydration mismatch. See ${url}`,
})
},
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-next/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2019"
"target": "es2019",
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
Expand Down
12 changes: 3 additions & 9 deletions test/unit/eslint-plugin-next/date-hydration.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import rule from '@next/eslint-plugin-next/lib/rules/date-hydration'
import { RuleTester } from 'eslint'
import rule from '@next/eslint-plugin-next/dist/rules/date-hydration'
import { ruleTester } from './utils'

new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: { modules: true, jsx: true },
},
}).run('date-hydration', rule, {
ruleTester.run('date-hydration', rule, {
valid: [
`export default function Page() {
return <p suppressHydrationWarning={true}>{new Date().toLocaleString()}</p>
Expand Down
9 changes: 9 additions & 0 deletions test/unit/eslint-plugin-next/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RuleTester } from 'eslint'

export const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: { modules: true, jsx: true },
},
})

0 comments on commit bbbe7e1

Please sign in to comment.