From abc09807370aff3e7b2c3b7ec0740eb72d185deb Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Wed, 27 Apr 2022 16:15:02 +0200 Subject: [PATCH 1/2] Remove passhref Eslint rule as it's no longer needed with new link behavior --- packages/eslint-plugin-next/lib/index.js | 2 - .../lib/rules/link-passhref.js | 64 ------------------- 2 files changed, 66 deletions(-) delete mode 100644 packages/eslint-plugin-next/lib/rules/link-passhref.js diff --git a/packages/eslint-plugin-next/lib/index.js b/packages/eslint-plugin-next/lib/index.js index 98554159b660..84b77fddeb32 100644 --- a/packages/eslint-plugin-next/lib/index.js +++ b/packages/eslint-plugin-next/lib/index.js @@ -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, diff --git a/packages/eslint-plugin-next/lib/rules/link-passhref.js b/packages/eslint-plugin-next/lib/rules/link-passhref.js deleted file mode 100644 index 97c13029aff9..000000000000 --- a/packages/eslint-plugin-next/lib/rules/link-passhref.js +++ /dev/null @@ -1,64 +0,0 @@ -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`, - }) - } - }, - } - }, -} From 5e734fc21b180b5c95e6e9780218eae472706656 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Wed, 27 Apr 2022 17:12:34 +0200 Subject: [PATCH 2/2] Remove test --- .../eslint-plugin-next/link-passhref.test.ts | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 test/unit/eslint-plugin-next/link-passhref.test.ts diff --git a/test/unit/eslint-plugin-next/link-passhref.test.ts b/test/unit/eslint-plugin-next/link-passhref.test.ts deleted file mode 100644 index 6b8f3668edf0..000000000000 --- a/test/unit/eslint-plugin-next/link-passhref.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -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 = () => ( - - )`, - - ` - import Link from 'next/link' - export const Home = () => ( - - Test - - )`, - - ` - import Link from 'next/link' - export const Home = () => ( - - Test - - )`, - - `const Link = () =>
Fake Link
- - const Home = () => ( - - - - )`, - - ` - import NextLink from 'next/link' - export const Home = () => ( - - Test - - )`, - ], - - invalid: [ - { - code: ` - import Link from 'next/link' - - export const Home = () => ( - - Test - - )`, - errors: [ - { - message: - 'passHref is missing. See: https://nextjs.org/docs/messages/link-passhref', - type: 'JSXOpeningElement', - }, - ], - }, - { - code: ` - import NextLink from 'next/link' - - export const Home = () => ( - - Test - - )`, - errors: [ - { - message: - 'passHref is missing. See: https://nextjs.org/docs/messages/link-passhref', - type: 'JSXOpeningElement', - }, - ], - }, - { - code: ` - import Link from 'next/link' - - export const Home = () => ( - - Test - - )`, - errors: [ - { - message: - 'passHref must be set to true. See: https://nextjs.org/docs/messages/link-passhref', - type: 'JSXOpeningElement', - }, - ], - }, - ], -})