-
Notifications
You must be signed in to change notification settings - Fork 877
Change href-or-on-click ESLint rule to require href with onClick #9615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
weronikaolejniczak
merged 14 commits into
elastic:main
from
TamerlanG:refactor/eui-eslint-rule
May 12, 2026
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2e175c2
Change `href-or-on-click` rule to require `href` when `onClick` is pr…
TamerlanG c8ee8ac
Bail out of href-or-on-click rule when spread attributes are present
TamerlanG f013d19
Use OS-agnostic wording in href-or-on-click rule message
TamerlanG 17de416
Add `require-href-for-link` rule for EuiLink and revert `href-or-on-c…
TamerlanG edf36c3
refactor: remove eui link from href_or_on_click rule
TamerlanG 40fd5f3
remove eui link test
TamerlanG 5dc6e08
Simplify require-href-for-link to compare against EuiLink directly
TamerlanG 20768c6
Add README docs and CHANGELOG entry for require-href-for-link rule
TamerlanG 824f8bd
fix changelog
TamerlanG b3c2f24
fix typo
TamerlanG 2221ace
Update packages/eslint-plugin/README.md
TamerlanG 003f149
Update packages/eslint-plugin/changelogs/upcoming/9615.md
TamerlanG 62518bb
Update packages/eslint-plugin/src/rules/require_href_for_link.ts
TamerlanG ff0b3d2
Merge branch 'main' into refactor/eui-eslint-rule
TamerlanG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## [`v2.12.0`](https://github.com/elastic/eui/releases/v2.12.0) | ||
|
|
||
| - Added new `require-href-for-link` rule. ([#9615](https://github.com/elastic/eui/pull/9615)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/eslint-plugin/src/rules/require_href_for_link.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import dedent from 'dedent'; | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
|
||
| import { RequireHrefForLink } from './require_href_for_link'; | ||
|
|
||
| const languageOptions = { | ||
| parserOptions: { | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('require-href-for-link', RequireHrefForLink, { | ||
| valid: [ | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiLink /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiLink href="/" /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiLink href="/" onClick={handleClick} /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiLink {...linkProps} onClick={handleClick} /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiButton onClick={handleClick} /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: dedent` | ||
| module.export = () => ( | ||
| <EuiLink onClick={handleClick} /> | ||
| ) | ||
| `, | ||
| languageOptions, | ||
| errors: [ | ||
| { | ||
| messageId: 'requireHrefForLink', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { TSESTree, ESLintUtils } from '@typescript-eslint/utils'; | ||
|
|
||
| import { hasSpread } from '../utils/has_spread'; | ||
|
|
||
| export const RequireHrefForLink = ESLintUtils.RuleCreator.withoutDocs({ | ||
| create(context) { | ||
| return { | ||
| JSXOpeningElement(node: TSESTree.JSXOpeningElement): void { | ||
| if ( | ||
| node.name.type !== 'JSXIdentifier' || | ||
| node.name.name !== 'EuiLink' | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| // Bail out if props are spread — we can't statically determine | ||
| // whether `href` is provided via the spread | ||
| if (hasSpread(node.attributes)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the node has `href` and `onClick` attributes | ||
| const hasHref = node.attributes.some( | ||
| (attr) => attr.type === 'JSXAttribute' && attr.name.name === 'href' | ||
| ); | ||
| const hasOnClick = node.attributes.some( | ||
| (attr) => attr.type === 'JSXAttribute' && attr.name.name === 'onClick' | ||
| ); | ||
|
|
||
| // Report an issue if `onClick` is present without `href` | ||
| if (hasOnClick && !hasHref) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'requireHrefForLink', | ||
| data: { | ||
| name: node.name.name, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Recommend including `href` alongside `onClick` on EuiLink so that Ctrl/Cmd+Click (open link in new tab) works.', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| requireHrefForLink: | ||
| '<{{name}}> has `onClick` but no `href`. Consider adding an `href` so that the component renders as a link and supports Ctrl/Cmd+Click / "Open link in new tab".', | ||
|
TamerlanG marked this conversation as resolved.
Outdated
|
||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.