-
Notifications
You must be signed in to change notification settings - Fork 8
feat(lint): add rule to limit excessive primary actions #191
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
Open
coryrylan
wants to merge
1
commit into
main
Choose a base branch
from
topic-eslint-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
147 changes: 147 additions & 0 deletions
147
projects/lint/src/eslint/rules/no-excessive-primary-actions.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,147 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { RuleTester } from 'eslint'; | ||
| import type { JSRuleDefinition } from 'eslint'; | ||
| import htmlParser from '@html-eslint/parser'; | ||
| import noExcessivePrimaryActions from './no-excessive-primary-actions.js'; | ||
|
|
||
| const rule = noExcessivePrimaryActions as unknown as JSRuleDefinition; | ||
| const error = { | ||
| messageId: 'excessive-primary-action' as const, | ||
| data: { max: '2' } | ||
| }; | ||
|
|
||
| describe('noExcessivePrimaryActions', () => { | ||
| let tester: RuleTester; | ||
|
|
||
| beforeEach(() => { | ||
| tester = new RuleTester({ | ||
| languageOptions: { | ||
| parser: htmlParser, | ||
| parserOptions: { | ||
| frontmatter: true | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('should define rule metadata', () => { | ||
| expect(noExcessivePrimaryActions.meta).toBeDefined(); | ||
| expect(noExcessivePrimaryActions.meta.type).toBe('problem'); | ||
| expect(noExcessivePrimaryActions.meta.docs).toBeDefined(); | ||
| expect(noExcessivePrimaryActions.meta.docs.description).toBe('Limit primary actions to two per page.'); | ||
| expect(noExcessivePrimaryActions.meta.docs.category).toBe('Best Practice'); | ||
| expect(noExcessivePrimaryActions.meta.docs.recommended).toBe(true); | ||
| expect(noExcessivePrimaryActions.meta.docs.url).toContain('/docs/lint/'); | ||
| expect(noExcessivePrimaryActions.meta.schema).toEqual([]); | ||
| expect(noExcessivePrimaryActions.meta.messages['excessive-primary-action']).toBe( | ||
| 'Limit primary actions to {{max}} per page. Reserve interaction="emphasis" for primary calls to action.' | ||
| ); | ||
| }); | ||
|
|
||
| it('should allow no more than two emphasis buttons', () => { | ||
| tester.run('valid emphasis button count', rule, { | ||
| valid: [ | ||
| '<nve-button>Default</nve-button>', | ||
| '<nve-button interaction="emphasis">Primary action</nve-button>', | ||
| `<nve-button interaction="emphasis">Primary action</nve-button> | ||
| <nve-button interaction="emphasis">Secondary action</nve-button>`, | ||
| `<nve-button interaction="emphasis">Primary action</nve-button> | ||
| <nve-button interaction="destructive">Delete</nve-button> | ||
| <nve-button interaction="neutral">Cancel</nve-button>` | ||
| ], | ||
| invalid: [] | ||
| }); | ||
| }); | ||
|
|
||
| it('should ignore emphasis on elements other than nve-button', () => { | ||
| tester.run('non-button elements', rule, { | ||
| valid: [ | ||
| `<nve-icon-button interaction="emphasis" icon-name="menu"></nve-icon-button> | ||
| <custom-button interaction="emphasis">Custom action</custom-button> | ||
| <button interaction="emphasis">Native action</button>` | ||
| ], | ||
| invalid: [] | ||
| }); | ||
| }); | ||
|
|
||
| it('should ignore dynamic interaction values', () => { | ||
| tester.run('dynamic interaction values', rule, { | ||
| valid: [ | ||
| `<nve-button interaction="\${interaction}">Lit attribute</nve-button> | ||
| <nve-button .interaction="\${interaction}">Lit property</nve-button> | ||
| <nve-button [interaction]="interaction">Angular property</nve-button> | ||
| <nve-button interaction="{{ interaction }}">Template binding</nve-button> | ||
| <nve-button interaction="{interaction}">JSX expression</nve-button>` | ||
| ], | ||
| invalid: [] | ||
| }); | ||
| }); | ||
|
|
||
| it('should count separate tagged templates independently', () => { | ||
| const javascriptTester = new RuleTester({ | ||
| languageOptions: { | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'module' | ||
| } | ||
| }); | ||
|
|
||
| javascriptTester.run('separate tagged templates', rule, { | ||
| valid: [ | ||
| `const first = html\` | ||
| <nve-button interaction="emphasis">One</nve-button> | ||
| <nve-button interaction="emphasis">Two</nve-button> | ||
| \`; | ||
| const second = html\` | ||
| <nve-button interaction="emphasis">Three</nve-button> | ||
| <nve-button interaction="emphasis">Four</nve-button> | ||
| \`;` | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: `const template = html\` | ||
| <nve-button interaction="emphasis">One</nve-button> | ||
| <nve-button interaction="emphasis">Two</nve-button> | ||
| <nve-button interaction="emphasis">Three</nve-button> | ||
| \`;`, | ||
| errors: [error] | ||
| } | ||
| ] | ||
| }); | ||
| }); | ||
|
Comment on lines
+83
to
+113
|
||
|
|
||
| it('should report the third emphasis button', () => { | ||
| tester.run('third emphasis button', rule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `<nve-button interaction="emphasis">One</nve-button> | ||
| <nve-button interaction="emphasis">Two</nve-button> | ||
| <nve-button interaction="emphasis">Three</nve-button>`, | ||
| errors: [error] | ||
| } | ||
| ] | ||
| }); | ||
| }); | ||
|
|
||
| it('should report every emphasis button after the second', () => { | ||
| tester.run('multiple excessive emphasis buttons', rule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `<main> | ||
| <nve-button interaction="emphasis">One</nve-button> | ||
| <section> | ||
| <nve-button interaction="emphasis">Two</nve-button> | ||
| <nve-button interaction="emphasis">Three</nve-button> | ||
| </section> | ||
| <nve-button interaction="emphasis">Four</nve-button> | ||
| </main>`, | ||
| errors: [error, error] | ||
| } | ||
| ] | ||
| }); | ||
| }); | ||
| }); | ||
59 changes: 59 additions & 0 deletions
59
projects/lint/src/eslint/rules/no-excessive-primary-actions.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,59 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import type { Rule } from 'eslint'; | ||
| import { createVisitors } from '@html-eslint/eslint-plugin/lib/rules/utils/visitors.js'; | ||
| import { findAttr } from '@html-eslint/eslint-plugin/lib/rules/utils/node.js'; | ||
| import type { HtmlTagNode } from '../rule-types.js'; | ||
|
|
||
| declare const __ELEMENTS_PAGES_BASE_URL__: string; | ||
| const MAX_EMPHASIS_BUTTONS = 2; | ||
|
|
||
| const rule = { | ||
| meta: { | ||
| type: 'problem' as const, | ||
| docs: { | ||
| description: 'Limit primary actions to two per page.', | ||
| category: 'Best Practice', | ||
| recommended: true, | ||
| url: `${__ELEMENTS_PAGES_BASE_URL__}/docs/lint/` | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| ['excessive-primary-action']: | ||
| 'Limit primary actions to {{max}} per page. Reserve interaction="emphasis" for primary calls to action.' | ||
| } | ||
| }, | ||
| create(context: Rule.RuleContext) { | ||
| let emphasisButtonCount = 0; | ||
|
|
||
| return createVisitors(context, { | ||
| Document() { | ||
| emphasisButtonCount = 0; | ||
| }, | ||
| Tag(node: HtmlTagNode) { | ||
| if (node.name.toLowerCase() !== 'nve-button') { | ||
| return; | ||
| } | ||
|
Comment on lines
+34
to
+37
|
||
|
|
||
| const interaction = findAttr(node, 'interaction'); | ||
| if (interaction?.value?.value !== 'emphasis') { | ||
| return; | ||
| } | ||
|
|
||
| emphasisButtonCount += 1; | ||
| if (emphasisButtonCount <= MAX_EMPHASIS_BUTTONS) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| node: interaction, | ||
| messageId: 'excessive-primary-action', | ||
| data: { max: String(MAX_EMPHASIS_BUTTONS) } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } as const; | ||
|
|
||
| export default rule; | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
went with slightly more generic name so the rule has some flexibility to what it checks