Skip to content

Commit 4e345f6

Browse files
feat(eslint): allow a for internal url when target="blank" present (vercel#32780)
When an `a` tag is used to link to an internal page, but the `target="_blank"` attribute is present, ESLint should not report it as an error and should not enforce using `next/link`. Fixes vercel#28547 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
1 parent b769d1b commit 4e345f6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/eslint-plugin-next/lib/rules/no-html-link-for-pages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ module.exports = {
9090
return
9191
}
9292

93+
const target = node.attributes.find(
94+
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'target'
95+
)
96+
97+
if (target && target.value.value === '_blank') {
98+
return
99+
}
100+
93101
const href = node.attributes.find(
94102
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'href'
95103
)

test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ export class Blah extends Head {
9696
}
9797
}
9898
`
99+
const validTargetBlankLinkCode = `
100+
import Link from 'next/link';
101+
102+
export class Blah extends Head {
103+
render() {
104+
return (
105+
<div>
106+
<a target="_blank" href='/new-tab'>New Tab</a>
107+
<h1>Hello title</h1>
108+
</div>
109+
);
110+
}
111+
}
112+
`
99113

100114
const validPublicFile = `
101115
import Link from 'next/link';
@@ -211,6 +225,13 @@ describe('no-html-link-for-pages', function () {
211225
assert.deepEqual(report, [])
212226
})
213227

228+
it('valid target="_blank" link element', function () {
229+
const report = linter.verify(validTargetBlankLinkCode, linterConfig, {
230+
filename: 'foo.js',
231+
})
232+
assert.deepEqual(report, [])
233+
})
234+
214235
it('valid public file link element', function () {
215236
const report = linter.verify(validPublicFile, linterConfig, {
216237
filename: 'foo.js',

0 commit comments

Comments
 (0)