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

fix eslint link-passhref rule #33857

Merged
merged 2 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/lib/rules/link-passhref.js
Expand Up @@ -23,7 +23,7 @@ module.exports = {
},

JSXOpeningElement(node) {
if (node.name.name !== 'Link' || node.name.name !== linkImport) {
if (node.name.name !== linkImport) {
return
}

Expand Down
37 changes: 34 additions & 3 deletions test/unit/eslint-plugin-next/link-passhref.test.ts
Expand Up @@ -14,17 +14,23 @@ const ruleTester = new RuleTester()

ruleTester.run('link-passhref', rule, {
valid: [
`export const Home = () => (
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test"></Link>
)`,

`export const Home = () => (
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test">
<a>Test</a>
</Link>
)`,

`export const Home = () => (
`
import Link from 'next/link'
export const Home = () => (
<Link href="/test" passHref>
<StyledLink>Test</StyledLink>
</Link>
Expand All @@ -37,6 +43,14 @@ ruleTester.run('link-passhref', rule, {
<MyButton />
</Link>
)`,

`
import NextLink from 'next/link'
export const Home = () => (
<NextLink href="/test" passHref>
<StyledLink>Test</StyledLink>
</NextLink>
)`,
],

invalid: [
Expand All @@ -57,6 +71,23 @@ ruleTester.run('link-passhref', rule, {
},
],
},
{
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'
Expand Down