Change href-or-on-click ESLint rule to require href with onClick#9615
Change href-or-on-click ESLint rule to require href with onClick#9615TamerlanG wants to merge 14 commits into
Conversation
…esent Instead of warning when both `href` and `onClick` are supplied, the rule now warns when `onClick` is used without `href` on link-like EUI components, encouraging proper link semantics so that Cmd+Click / "Open in new tab" works.
There was a problem hiding this comment.
Pull request overview
Updates the href-or-on-click custom ESLint rule in packages/eslint-plugin to encourage link-like EUI components to include an href when an onClick handler is provided, improving expected “open in new tab” behavior.
Changes:
- Invert rule behavior to report when
onClickis present withouthref(instead of reporting when both are present). - Reclassify the rule as a
suggestionand update the rule’s documentation/message text accordingly. - Update and expand RuleTester cases to match the new expected valid/invalid patterns.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/eslint-plugin/src/rules/href_or_on_click.ts | Changes the rule logic to flag onClick without href and updates metadata/message text. |
| packages/eslint-plugin/src/rules/href_or_on_click.test.ts | Updates valid/invalid test cases to reflect the new rule behavior. |
Comments suppressed due to low confidence (1)
packages/eslint-plugin/src/rules/href_or_on_click.test.ts:85
- Given the rule change, consider adding a test case that includes a JSX spread attribute (e.g.
<EuiButton {...props} onClick={handleClick} />). If the rule is updated to bail out on spreads (to avoid false positives), this should be a valid case; if not, the test should assert the intended behavior explicitly.
{
code: dedent`
module.export = () => (
<EuiButtonEmpty href="/" onClick={handleClick} />
)
`,
languageOptions,
},
{
code: dedent`
module.export = () => (
<EuiLink href="/home" onClick={handleClick} />
)
`,
languageOptions,
},
{
code: dedent`
module.export = () => (
<SomeOtherComponent onClick={handleClick} />
)
`,
languageOptions,
},
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@TamerlanG for releasable packages like |
weronikaolejniczak
left a comment
There was a problem hiding this comment.
We should also update README.md accordingly 😄
When props are spread (e.g. `<EuiButton {...linkProps} onClick={fn} />`),
we can't statically know if `href` is provided, so skip reporting to
avoid false positives. Uses the existing `hasSpread` utility.
Replace "Cmd+Click" with "Ctrl/Cmd+Click" and "Open in new tab" with "Open link in new tab" so the message is accurate on all platforms.
|
Wait, I just noticed that this affects the buttons and badge too. Let's have this be just for links? I can create a separate rule for that? wdyt @weronikaolejniczak and @tkajtoch |
|
@TamerlanG ah, so for |
…lick` Revert `href-or-on-click` to its original behavior (warn when both `href` and `onClick` are present). Extract the new logic into a separate `require-href-for-link` rule that only targets EuiLink and warns when `onClick` is used without `href`.
|
I still have to add the changelog and update the readme, so lemme do that :) |
|
added updates to readme and change log here: 20768c6 |
weronikaolejniczak
left a comment
There was a problem hiding this comment.
I added a couple of nits. The only blocking thing is the changelog entry 😄
Co-authored-by: Weronika Olejniczak <32842468+weronikaolejniczak@users.noreply.github.com>
Co-authored-by: Weronika Olejniczak <32842468+weronikaolejniczak@users.noreply.github.com>
Co-authored-by: Weronika Olejniczak <32842468+weronikaolejniczak@users.noreply.github.com>
💚 Build SucceededHistory
|
💚 Build Succeeded
History
|
weronikaolejniczak
left a comment
There was a problem hiding this comment.
I'll approve but as discussed on Slack, we'll merge it only after the next release
Summary
We have cases where we would want both href and onClick, for example cmd-click behavior, collecting telemetry within the onClick, etc...
Changes:
href-or-on-clickESLint rule to warn whenonClickis present withouthref.hrefandonClickwere supplied; now it encourages always includinghrefalongsideonClickso that Cmd+Click / "Open in new tab" works