Skip to content

Commit 81556f3

Browse files
committed
Also detect javascript:void
1 parent d493ba2 commit 81556f3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

javascript/packages/linter/src/rules/html-anchor-require-href.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ class AnchorRequireHrefVisitor extends BaseRuleVisitor {
4040
return
4141
}
4242

43+
if (hrefValue !== null && hrefValue.startsWith("javascript:void")) {
44+
this.addOffense(
45+
'Avoid `javascript:void(0)` in `href` on `<a>`. Links should navigate somewhere. If you need a clickable element without navigation, use a `<button>` instead.',
46+
hrefAttribute.location,
47+
)
48+
49+
return
50+
}
51+
4352
if (this.hasNilHrefValue(hrefAttribute)) {
4453
this.addOffense(
4554
"Avoid passing `nil` as the URL for `link_to`. Links should navigate somewhere. If you need a clickable element without navigation, use a `<button>` instead.",

javascript/packages/linter/test/rules/html-anchor-require-href-rule.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { expectNoOffenses, expectError, assertOffenses } = createLinterTest(HTMLA
66

77
const MISSING_HREF_MESSAGE = "Add an `href` attribute to `<a>` to ensure it is focusable and accessible. Links should navigate somewhere. If you need a clickable element without navigation, use a `<button>` instead."
88
const HASH_HREF_MESSAGE = 'Avoid `href="#"` on `<a>`. `href="#"` does not navigate anywhere, scrolls the page to the top, and adds `#` to the URL. If you need a clickable element without navigation, use a `<button>` instead.'
9+
const JAVASCRIPT_VOID_HREF_MESSAGE = 'Avoid `javascript:void(0)` in `href` on `<a>`. Links should navigate somewhere. If you need a clickable element without navigation, use a `<button>` instead.'
910
const NIL_HREF_MESSAGE = "Avoid passing `nil` as the URL for `link_to`. Links should navigate somewhere. If you need a clickable element without navigation, use a `<button>` instead."
1011

1112
describe("html-anchor-require-href", () => {
@@ -82,6 +83,24 @@ describe("html-anchor-require-href", () => {
8283
assertOffenses("<A>My link</A>")
8384
})
8485

86+
test("fails for a with href='javascript:void(0)'", () => {
87+
expectError(JAVASCRIPT_VOID_HREF_MESSAGE)
88+
89+
assertOffenses('<a href="javascript:void(0)">My link</a>')
90+
})
91+
92+
test("fails for a with href='javascript:void(0)' and other attributes", () => {
93+
expectError(JAVASCRIPT_VOID_HREF_MESSAGE)
94+
95+
assertOffenses('<a href="javascript:void(0)" data-action="click->doSomething">My link</a>')
96+
})
97+
98+
test("fails for a with href='javascript:void()'", () => {
99+
expectError(JAVASCRIPT_VOID_HREF_MESSAGE)
100+
101+
assertOffenses('<a href="javascript:void()">My link</a>')
102+
})
103+
85104
test("fails for link_to with href='#'", () => {
86105
expectError(HASH_HREF_MESSAGE)
87106

0 commit comments

Comments
 (0)