From 40bc839354f9db53aa73a9f5fae4730be518c783 Mon Sep 17 00:00:00 2001 From: Takahiro Kato Date: Wed, 24 Jul 2024 17:39:31 +0900 Subject: [PATCH] apply no-html-link-for-pages rule to app directory --- .../src/rules/no-html-link-for-pages.ts | 4 +++- .../no-html-link-for-pages.test.ts | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts b/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts index dbb8b43c47ccd..dbc3597e8a3b4 100644 --- a/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts +++ b/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts @@ -93,6 +93,8 @@ export = defineRule({ } const pageUrls = getUrlFromPagesDirectories('/', foundPagesDirs) + const appUrls = getUrlFromPagesDirectories('/', foundAppDirs) + return { JSXOpeningElement(node) { if (node.name.name !== 'a') { @@ -134,7 +136,7 @@ export = defineRule({ return } - pageUrls.forEach((pageUrl) => { + ;[...pageUrls, ...appUrls].forEach((pageUrl) => { if (pageUrl.test(normalizeURL(hrefPath))) { context.report({ node, diff --git a/test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts b/test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts index fe3ac7b429203..37a8d7804fbc9 100644 --- a/test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts +++ b/test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts @@ -251,6 +251,13 @@ describe('no-html-link-for-pages', function () { assert.deepEqual(report, []) }) + it('valid link element with app linter', function () { + const report = withAppLinter.verify(validCode, linterConfig, { + filename: 'foo.js', + }) + assert.deepEqual(report, []) + }) + it('valid link element with multiple directories', function () { const report = withCustomPagesLinter.verify( validCode, @@ -332,6 +339,21 @@ describe('no-html-link-for-pages', function () { ) }) + it('invalid static route with app directory linter', function () { + const [report] = withAppLinter.verify( + invalidStaticCode, + linterConfigWithCustomDirectory, + { + filename: 'foo.js', + } + ) + assert.notEqual(report, undefined, 'No lint errors found.') + assert.equal( + report.message, + 'Do not use an `` element to navigate to `/`. Use `` from `next/link` instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages' + ) + }) + it('invalid dynamic route', function () { const [report] = withCustomPagesLinter.verify( invalidDynamicCode,