forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jens Vannerum
committed
Sep 18, 2024
1 parent
d3e87c6
commit 4f3009b
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,87 @@ | ||
import {TmplAstBoundAttribute, TmplAstElement} from '@angular-eslint/bundled-angular-compiler'; | ||
import { TemplateParserServices } from '@angular-eslint/utils'; | ||
import { | ||
ESLintUtils, | ||
TSESLint, | ||
} from '@typescript-eslint/utils'; | ||
|
||
import { | ||
DSpaceESLintRuleInfo, | ||
NamedTests, | ||
} from '../../util/structure'; | ||
import { getSourceCode } from '../../util/typescript'; | ||
|
||
export enum Message { | ||
USE_DSBTN_DISABLED = 'mustUseDsBtnDisabled', | ||
} | ||
|
||
export const info = { | ||
name: 'no-disabled-attr', | ||
meta: { | ||
docs: { | ||
description: `Buttons should use the \`dsBtnDisabled\` directive instead of the HTML \`disabled\` attribute for accessibility reasons.`, | ||
}, | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
[Message.USE_DSBTN_DISABLED]: 'Buttons should use the `dsBtnDisabled` directive instead of the `disabled` attribute.', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
} as DSpaceESLintRuleInfo; | ||
|
||
export const rule = ESLintUtils.RuleCreator.withoutDocs({ | ||
...info, | ||
create(context: TSESLint.RuleContext<Message, unknown[]>) { | ||
const parserServices = getSourceCode(context).parserServices as TemplateParserServices; | ||
|
||
return { | ||
'BoundAttribute[name="disabled"]'(node: TmplAstBoundAttribute) { | ||
// Only check for disabled attributes on button elements and ensure it's not a class binding | ||
if ((node as any).parent.name !== 'button' || node.name.includes('class.')) { | ||
return; | ||
} | ||
|
||
const sourceSpan = node.sourceSpan; | ||
console.log('name', node.name); | ||
context.report({ | ||
messageId: Message.USE_DSBTN_DISABLED, | ||
loc: parserServices.convertNodeSourceSpanToLoc(sourceSpan), | ||
fix(fixer) { | ||
const templateText = sourceSpan.start.file.content; | ||
const disabledText = templateText.slice(sourceSpan.start.offset, sourceSpan.end.offset); | ||
const newText = disabledText.replace('disabled', '[dsBtnDisabled]'); | ||
return fixer.replaceTextRange([sourceSpan.start.offset, sourceSpan.end.offset], newText); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export const tests = { | ||
plugin: info.name, | ||
valid: [ | ||
{ | ||
name: 'should use [dsBtnDisabled] in HTML templates', | ||
code: ` | ||
<button [dsBtnDisabled]="isDisabled">Submit</button> | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'should not use disabled attribute in HTML templates', | ||
code: ` | ||
<button disabled="true">Submit</button> | ||
`, | ||
errors: [{ messageId: Message.USE_DSBTN_DISABLED }], | ||
output: ` | ||
<button [dsBtnDisabled]="true">Submit</button> | ||
`, | ||
}, | ||
], | ||
} as NamedTests; | ||
|
||
export default rule; |