/
accessibility-table-scope.ts
55 lines (51 loc) · 1.64 KB
/
accessibility-table-scope.ts
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
import type {
TmplAstBoundAttribute,
TmplAstTextAttribute,
} from '@angular-eslint/bundled-angular-compiler';
import { getTemplateParserServices } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { getDomElements } from '../utils/get-dom-elements';
import { toPattern } from '../utils/to-pattern';
type Options = [];
export type MessageIds = 'accessibilityTableScope';
export const RULE_NAME = 'accessibility-table-scope';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Ensures that the `scope` attribute is only used on the `<th>` element',
recommended: false,
},
fixable: 'code',
schema: [],
messages: {
accessibilityTableScope:
'The `scope` attribute should only be on the `<th>` element',
},
},
defaultOptions: [],
create(context) {
const parserServices = getTemplateParserServices(context);
const domElementsPattern = toPattern(
[...getDomElements()].filter((domElement) => domElement !== 'th'),
);
return {
[`Element$1[name=${domElementsPattern}] > :matches(BoundAttribute, TextAttribute)[name='scope']`]({
sourceSpan,
}: TmplAstBoundAttribute | TmplAstTextAttribute) {
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
context.report({
loc,
messageId: 'accessibilityTableScope',
fix: (fixer) =>
fixer.removeRange([
sourceSpan.start.offset - 1,
sourceSpan.end.offset,
]),
});
},
};
},
});