Skip to content

Commit

Permalink
apply no-html-link-for-pages rule to app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
doz13189 committed Jul 24, 2024
1 parent 91dc0a0 commit 40bc839
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export = defineRule({
}

const pageUrls = getUrlFromPagesDirectories('/', foundPagesDirs)
const appUrls = getUrlFromPagesDirectories('/', foundAppDirs)

return {
JSXOpeningElement(node) {
if (node.name.name !== 'a') {
Expand Down Expand Up @@ -134,7 +136,7 @@ export = defineRule({
return
}

pageUrls.forEach((pageUrl) => {
;[...pageUrls, ...appUrls].forEach((pageUrl) => {
if (pageUrl.test(normalizeURL(hrefPath))) {
context.report({
node,
Expand Down
22 changes: 22 additions & 0 deletions test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 `<a>` element to navigate to `/`. Use `<Link />` 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,
Expand Down

0 comments on commit 40bc839

Please sign in to comment.