Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions packages/eslint-plugin/src/rules/href_or_on_click.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,31 @@ ruleTester.run('href-or-on-click', HrefOnClick, {
{
code: dedent`
module.export = () => (
<EuiButton onClick={executeAction} />
<EuiButton href="/" onClick={executeAction} />
)
`,
languageOptions,
},
{
code: dedent`
module.export = () => (
<EuiButton onClick={() => executeAction()} />
<EuiButtonEmpty href="/" onClick={handleClick} />
)
`,
languageOptions,
},
{
code: dedent`
module.export = () => (
<EuiLink href="/home" onClick={handleClick} />
)
`,
languageOptions,
},
{
code: dedent`
module.export = () => (
<SomeOtherComponent onClick={handleClick} />
)
`,
languageOptions,
Expand All @@ -73,7 +89,20 @@ ruleTester.run('href-or-on-click', HrefOnClick, {
{
code: dedent`
module.export = () => (
<EuiButton href="/" onClick={fooBar} />
<EuiButton onClick={handleClick} />
)
`,
languageOptions,
errors: [
{
messageId: 'hrefOrOnClick',
},
],
},
{
code: dedent`
module.export = () => (
<EuiButtonEmpty onClick={handleClick} />
)
`,
languageOptions,
Expand All @@ -86,7 +115,7 @@ ruleTester.run('href-or-on-click', HrefOnClick, {
{
code: dedent`
module.export = () => (
<EuiButtonEmpty href="/" onClick={fooBar} />
<EuiLink onClick={handleClick} />
)
`,
languageOptions,
Expand All @@ -99,7 +128,7 @@ ruleTester.run('href-or-on-click', HrefOnClick, {
{
code: dedent`
module.export = () => (
<EuiLink href="/" onClick={fooBar} />
<EuiBadge onClick={handleClick} />
)
`,
languageOptions,
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin/src/rules/href_or_on_click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export const HrefOnClick = ESLintUtils.RuleCreator.withoutDocs({
return;
}

// Check if the node has both `href` and `onClick` attributes
// 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 both attributes are present
if (hasHref && hasOnClick) {
// Report an issue if `onClick` is present without `href`
if (hasOnClick && !hasHref) {
context.report({
Comment thread
TamerlanG marked this conversation as resolved.
Outdated
node,
messageId: 'hrefOrOnClick',
Expand All @@ -44,15 +44,15 @@ export const HrefOnClick = ESLintUtils.RuleCreator.withoutDocs({
};
},
meta: {
type: 'problem',
type: 'suggestion',
docs: {
description:
'Discourage supplying both `href` and `onClick` to certain EUI components.',
'Recommend including `href` alongside `onClick` on link-like EUI components so that Cmd+Click (open in new tab) works.',
},
schema: [],
messages: {
hrefOrOnClick:
'<{{name}}> supplied with both `href` and `onClick`; is this intentional? (Valid use cases include programmatic navigation via `onClick` while preserving "Open in new tab" style functionality via `href`.)',
'<{{name}}> has `onClick` but no `href`. Consider adding an `href` so that the component renders as a link and supports Cmd+Click / "Open in new tab".',
Comment thread
TamerlanG marked this conversation as resolved.
Outdated
},
},
defaultOptions: [],
Expand Down
Loading