fix bug in lint rule, add docs for rule and update name so it's clear this is only for buttons currently

(cherry picked from commit 2380d4e751)
This commit is contained in:
Jens Vannerum
2024-09-20 16:45:05 +02:00
parent c249afdb3f
commit ee83beae94
4 changed files with 13 additions and 9 deletions

View File

@@ -294,7 +294,7 @@
"rules": { "rules": {
// Custom DSpace Angular rules // Custom DSpace Angular rules
"dspace-angular-html/themed-component-usages": "error", "dspace-angular-html/themed-component-usages": "error",
"dspace-angular-html/no-disabled-attr": "error" "dspace-angular-html/no-disabled-attribute-on-button": "error"
} }
}, },
{ {

View File

@@ -2,3 +2,4 @@
_______ _______
- [`dspace-angular-html/themed-component-usages`](./rules/themed-component-usages.md): Themeable components should be used via the selector of their `ThemedComponent` wrapper class - [`dspace-angular-html/themed-component-usages`](./rules/themed-component-usages.md): Themeable components should be used via the selector of their `ThemedComponent` wrapper class
- [`dspace-angular-html/no-disabled-attribute-on-button`](./rules/no-disabled-attribute-on-button.md): Buttons should use the `dsBtnDisabled` directive instead of the HTML `disabled` attribute.

View File

@@ -10,12 +10,12 @@ import {
bundle, bundle,
RuleExports, RuleExports,
} from '../../util/structure'; } from '../../util/structure';
import * as noDisabledAttr from './no-disabled-attr'; import * as noDisabledAttributeOnButton from './no-disabled-attribute-on-button';
import * as themedComponentUsages from './themed-component-usages'; import * as themedComponentUsages from './themed-component-usages';
const index = [ const index = [
themedComponentUsages, themedComponentUsages,
noDisabledAttr, noDisabledAttributeOnButton,
] as unknown as RuleExports[]; ] as unknown as RuleExports[];

View File

@@ -1,12 +1,13 @@
import { import {
TmplAstBoundAttribute, TmplAstBoundAttribute,
TmplAstTextAttribute TmplAstTextAttribute,
} from '@angular-eslint/bundled-angular-compiler'; } from '@angular-eslint/bundled-angular-compiler';
import { TemplateParserServices } from '@angular-eslint/utils'; import { TemplateParserServices } from '@angular-eslint/utils';
import { import {
ESLintUtils, ESLintUtils,
TSESLint, TSESLint,
} from '@typescript-eslint/utils'; } from '@typescript-eslint/utils';
import { import {
DSpaceESLintRuleInfo, DSpaceESLintRuleInfo,
NamedTests, NamedTests,
@@ -18,10 +19,12 @@ export enum Message {
} }
export const info = { export const info = {
name: 'no-disabled-attr', name: 'no-disabled-attribute-on-button',
meta: { meta: {
docs: { docs: {
description: `Buttons should use the \`dsBtnDisabled\` directive instead of the HTML \`disabled\` attribute for accessibility reasons.`, description: `Buttons should use the \`dsBtnDisabled\` directive instead of the HTML \`disabled\` attribute.
This should be done to ensure that users with a screen reader are able to understand that the a button button is present, and that it is disabled.
The native html disabled attribute does not allow users to navigate to the button by keyboard, and thus they have no way of knowing that the button is present.`,
}, },
type: 'problem', type: 'problem',
fixable: 'code', fixable: 'code',
@@ -52,7 +55,7 @@ export const rule = ESLintUtils.RuleCreator.withoutDocs({
*/ */
function replaceDisabledText(text: string ): string { function replaceDisabledText(text: string ): string {
const hasBrackets = text.includes('[') && text.includes(']'); const hasBrackets = text.includes('[') && text.includes(']');
const newDisabledText = hasBrackets ? 'dsBtnDisabled' : '[dsBtnDisabled]'; const newDisabledText = hasBrackets ? 'dsBtnDisabled' : '[dsBtnDisabled]="true"';
return text.replace('disabled', newDisabledText); return text.replace('disabled', newDisabledText);
} }
@@ -101,7 +104,7 @@ export const tests = {
{ {
name: 'disabled attribute is still valid on non-button elements', name: 'disabled attribute is still valid on non-button elements',
code: ` code: `
<input disabled="true"> <input disabled>
`, `,
}, },
{ {
@@ -121,7 +124,7 @@ export const tests = {
{ {
name: 'should not use disabled attribute in HTML templates', name: 'should not use disabled attribute in HTML templates',
code: ` code: `
<button disabled="true">Submit</button> <button disabled>Submit</button>
`, `,
errors: [{ messageId: Message.USE_DSBTN_DISABLED }], errors: [{ messageId: Message.USE_DSBTN_DISABLED }],
output: ` output: `