forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhref_or_on_click.ts
More file actions
59 lines (54 loc) 路 1.93 KB
/
Copy pathhref_or_on_click.ts
File metadata and controls
59 lines (54 loc) 路 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* 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';
const componentNames = ['EuiButton', 'EuiButtonEmpty', 'EuiBadge'];
export const HrefOnClick = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
return {
JSXOpeningElement(node: TSESTree.JSXOpeningElement): void {
// Ensure node name is one of the valid component names
if (
node.name.type !== 'JSXIdentifier' ||
!componentNames.includes(node.name.name)
) {
return;
}
// Check if the node has both `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) {
context.report({
node,
messageId: 'hrefOrOnClick',
data: {
name: node.name.name,
},
});
}
},
};
},
meta: {
type: 'problem',
docs: {
description:
'Discourage supplying both `href` and `onClick` to certain EUI components.',
},
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`.)',
},
},
defaultOptions: [],
});