-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
use-track-by-function.ts
96 lines (90 loc) · 2.68 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import type {
TmplAstTemplate,
TmplAstTextAttribute,
TmplAstBoundAttribute,
} from '@angular-eslint/bundled-angular-compiler';
import { getTemplateParserServices } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
type Options = [{ readonly alias: readonly string[] }];
export type MessageIds = 'useTrackByFunction';
export const RULE_NAME = 'use-track-by-function';
const DEFAULT_ALIAS = [] as const;
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Ensures trackBy function is used',
},
schema: [
{
type: 'object',
properties: {
alias: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
messages: {
useTrackByFunction: 'Missing trackBy function in ngFor directive',
},
},
defaultOptions: [{ alias: DEFAULT_ALIAS }],
create(context, [{ alias }]) {
const isNgForTrackBy = isNgForTrackByFactory(alias);
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,
});
},
};
},
});
const DEFAULT_NG_FOR_TRACK_BY_ATTRIBUTE_NAME = 'ngForTrackBy';
function isNgForTrackByFactory(alias: readonly string[]) {
const names = [...alias, DEFAULT_NG_FOR_TRACK_BY_ATTRIBUTE_NAME];
return (attribute: TmplAstBoundAttribute | TmplAstTextAttribute) =>
names.includes(attribute.name);
}