forked from angular-eslint/angular-eslint
/
use-track-by-function.ts
82 lines (78 loc) · 2.33 KB
/
use-track-by-function.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type {
TmplAstTemplate,
TmplAstTextAttribute,
} from '@angular-eslint/bundled-angular-compiler';
import { TmplAstBoundAttribute } from '@angular-eslint/bundled-angular-compiler';
import { getTemplateParserServices } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
type Options = [];
export type MessageIds = 'useTrackByFunction';
export const RULE_NAME = 'use-track-by-function';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Ensures trackBy function is used',
recommended: false,
},
schema: [],
messages: {
useTrackByFunction: 'Missing trackBy function in ngFor directive',
},
},
defaultOptions: [],
create(context) {
const parserServices = getTemplateParserServices(context);
return {
'BoundAttribute.inputs[name="ngForOf"]'({
parent: { inputs },
sourceSpan,
}: TmplAstBoundAttribute & { parent: TmplAstTemplate }) {
if (inputs.some(isNgForTrackBy)) {
return;
}
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
context.report({
messageId: 'useTrackByFunction',
loc,
});
},
'BoundAttribute.templateAttrs[name="ngForOf"]'({
parent: { templateAttrs },
}: TmplAstBoundAttribute & { parent: TmplAstTemplate }) {
if (templateAttrs.some(isNgForTrackBy)) {
return;
}
const { start } = parserServices.convertNodeSourceSpanToLoc(
templateAttrs[0].sourceSpan,
);
const { end } = parserServices.convertNodeSourceSpanToLoc(
templateAttrs[templateAttrs.length - 1].sourceSpan,
);
const loc = {
start: {
...start,
column: start.column - 1,
},
end: {
...end,
column: end.column + 1,
},
} as const;
context.report({
messageId: 'useTrackByFunction',
loc,
});
},
};
},
});
function isNgForTrackBy(
attribute: TmplAstBoundAttribute | TmplAstTextAttribute,
): attribute is TmplAstBoundAttribute & { name: 'ngForTrackBy' } {
return (
attribute instanceof TmplAstBoundAttribute &&
attribute.name === 'ngForTrackBy'
);
}