Unverified Commit f550da70 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove passhref Eslint rule as it's no longer needed with new link behavior (#36511)

parent 51bdd970
......@@ -10,7 +10,6 @@ module.exports = {
'no-title-in-document-head': require('./rules/no-title-in-document-head'),
'google-font-display': require('./rules/google-font-display'),
'google-font-preconnect': require('./rules/google-font-preconnect'),
'link-passhref': require('./rules/link-passhref'),
'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-component-in-head': require('./rules/no-script-component-in-head'),
......@@ -36,7 +35,6 @@ module.exports = {
'@next/next/no-title-in-document-head': 1,
'@next/next/google-font-display': 1,
'@next/next/google-font-preconnect': 1,
'@next/next/link-passhref': 1,
'@next/next/next-script-for-ga': 1,
'@next/next/no-document-import-in-page': 2,
'@next/next/no-head-import-in-document': 2,
......
const NodeAttributes = require('../utils/node-attributes.js')
module.exports = {
meta: {
docs: {
description:
'Ensure passHref is assigned if child of Link component is a custom component',
category: 'HTML',
recommended: true,
url: 'https://nextjs.org/docs/messages/link-passhref',
},
fixable: null,
},
create: function (context) {
let linkImport = null
return {
ImportDeclaration(node) {
if (node.source.value === 'next/link') {
linkImport = node.specifiers[0].local.name
}
},
JSXOpeningElement(node) {
if (node.name.name !== linkImport) {
return
}
const attributes = new NodeAttributes(node)
const children = node.parent.children
if (
!attributes.hasAny() ||
!attributes.has('href') ||
!children.some((attr) => attr.type === 'JSXElement')
) {
return
}
const hasPassHref =
attributes.has('passHref') &&
(typeof attributes.value('passHref') === 'undefined' ||
attributes.value('passHref') === true)
const hasAnchorChild = children.some(
(attr) =>
attr.type === 'JSXElement' && attr.openingElement.name.name === 'a'
)
if (!hasAnchorChild && !hasPassHref) {
context.report({
node,
message: `passHref ${
attributes.value('passHref') !== true
? 'must be set to true'
: 'is missing'
}. See: https://nextjs.org/docs/messages/link-passhref`,
})
}
},
}
},
}
import rule from '@next/eslint-plugin-next/lib/rules/link-passhref'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()
ruleTester.run('link-passhref', rule, {
valid: [
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test"></Link>
)`,
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test">
<a>Test</a>
</Link>
)`,
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test" passHref>
<StyledLink>Test</StyledLink>
</Link>
)`,
`const Link = () => <div>Fake Link</div>
const Home = () => (
<Link href="/test">
<MyButton />
</Link>
)`,
`
import NextLink from 'next/link'
export const Home = () => (
<NextLink href="/test" passHref>
<StyledLink>Test</StyledLink>
</NextLink>
)`,
],
invalid: [
{
code: `
import Link from 'next/link'
export const Home = () => (
<Link href="/test">
<StyledLink>Test</StyledLink>
</Link>
)`,
errors: [
{
message:
'passHref is missing. See: https://nextjs.org/docs/messages/link-passhref',
type: 'JSXOpeningElement',
},
],
},
{
code: `
import NextLink from 'next/link'
export const Home = () => (
<NextLink href="/test">
<StyledLink>Test</StyledLink>
</NextLink>
)`,
errors: [
{
message:
'passHref is missing. See: https://nextjs.org/docs/messages/link-passhref',
type: 'JSXOpeningElement',
},
],
},
{
code: `
import Link from 'next/link'
export const Home = () => (
<Link href="/test" passHref={false}>
<StyledLink>Test</StyledLink>
</Link>
)`,
errors: [
{
message:
'passHref must be set to true. See: https://nextjs.org/docs/messages/link-passhref',
type: 'JSXOpeningElement',
},
],
},
],
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment