Skip to content

Commit 04c4020

Browse files
committed
fix(lint): no-excessive-primary-actions rule to handle popover elements
- Added tests to ensure emphasis buttons inside popover elements are ignored. - Implemented logic to count emphasis buttons outside popover elements, ensuring proper validation. - Introduced a helper function to check for popover ancestors in the DOM structure. This update improves the linting accuracy for emphasis button usage in complex UI structures. Signed-off-by: Cory Rylan <crylan@nvidia.com>
1 parent 6126e1c commit 04c4020

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎projects/lint/src/eslint/rules/no-excessive-primary-actions.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,51 @@ describe('noExcessivePrimaryActions', () => {
8080
});
8181
});
8282

83+
it('should ignore emphasis buttons inside popover elements', () => {
84+
tester.run('popover emphasis buttons', rule, {
85+
valid: [
86+
`<nve-button interaction="emphasis">Page action one</nve-button>
87+
<nve-dialog>
88+
<nve-dialog-footer>
89+
<nve-button interaction="emphasis">Dialog action one</nve-button>
90+
</nve-dialog-footer>
91+
</nve-dialog>
92+
<nve-dialog>
93+
<nve-dialog-footer>
94+
<nve-button interaction="emphasis">Dialog action two</nve-button>
95+
</nve-dialog-footer>
96+
</nve-dialog>
97+
<nve-button interaction="emphasis">Page action two</nve-button>`,
98+
`<nve-drawer><nve-button interaction="emphasis">Drawer action</nve-button></nve-drawer>
99+
<nve-dropdown><nve-button interaction="emphasis">Dropdown action</nve-button></nve-dropdown>
100+
<nve-notification><nve-button interaction="emphasis">Notification action</nve-button></nve-notification>
101+
<nve-notification-group><nve-button interaction="emphasis">Group action</nve-button></nve-notification-group>
102+
<nve-page-loader><nve-button interaction="emphasis">Loader action</nve-button></nve-page-loader>
103+
<nve-toast><nve-button interaction="emphasis">Toast action</nve-button></nve-toast>
104+
<nve-toggletip><nve-button interaction="emphasis">Toggletip action</nve-button></nve-toggletip>
105+
<nve-tooltip><nve-button interaction="emphasis">Tooltip action</nve-button></nve-tooltip>`
106+
],
107+
invalid: []
108+
});
109+
});
110+
111+
it('should continue counting emphasis buttons outside popover elements', () => {
112+
tester.run('page emphasis buttons around popovers', rule, {
113+
valid: [],
114+
invalid: [
115+
{
116+
code: `<nve-button interaction="emphasis">Page action one</nve-button>
117+
<nve-dialog>
118+
<nve-button interaction="emphasis">Dialog action</nve-button>
119+
</nve-dialog>
120+
<nve-button interaction="emphasis">Page action two</nve-button>
121+
<nve-button interaction="emphasis">Page action three</nve-button>`,
122+
errors: [error]
123+
}
124+
]
125+
});
126+
});
127+
83128
it('should count separate tagged templates independently', () => {
84129
const javascriptTester = new RuleTester({
85130
languageOptions: {

‎projects/lint/src/eslint/rules/no-excessive-primary-actions.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@
44
import type { Rule } from 'eslint';
55
import { createVisitors } from '@html-eslint/eslint-plugin/lib/rules/utils/visitors.js';
66
import { findAttr } from '@html-eslint/eslint-plugin/lib/rules/utils/node.js';
7+
import { elements } from '../internals/metadata.js';
78
import type { HtmlTagNode } from '../rule-types.js';
89

910
declare const __ELEMENTS_PAGES_BASE_URL__: string;
1011
const MAX_EMPHASIS_BUTTONS = 2;
12+
const POPOVER_ELEMENTS: ReadonlySet<string> = new Set(
13+
elements
14+
.filter(element => element.manifest?.metadata?.behavior === 'popover')
15+
.map(element => element.name.toLowerCase())
16+
);
17+
18+
function hasPopoverAncestor(node: HtmlTagNode): boolean {
19+
let current = node.parent;
20+
21+
while (current) {
22+
if (current.name && POPOVER_ELEMENTS.has(current.name.toLowerCase())) {
23+
return true;
24+
}
25+
current = current.parent;
26+
}
27+
28+
return false;
29+
}
1130

1231
const rule = {
1332
meta: {
@@ -32,7 +51,7 @@ const rule = {
3251
emphasisButtonCount = 0;
3352
},
3453
Tag(node: HtmlTagNode) {
35-
if (node.name.toLowerCase() !== 'nve-button') {
54+
if (node.name.toLowerCase() !== 'nve-button' || hasPopoverAncestor(node)) {
3655
return;
3756
}
3857

0 commit comments

Comments
 (0)