forked from angular-eslint/angular-eslint
/
no-call-expression.ts
55 lines (49 loc) · 1.54 KB
/
no-call-expression.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 { Call } from '@angular-eslint/bundled-angular-compiler';
import { TmplAstBoundEvent } from '@angular-eslint/bundled-angular-compiler';
import { ensureTemplateParser } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { getNearestNodeFrom } from '../utils/get-nearest-node-from';
type Options = [];
export type MessageIds = 'noCallExpression';
export const RULE_NAME = 'no-call-expression';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Disallows calling expressions in templates, except for output handlers',
recommended: false,
},
schema: [],
messages: {
noCallExpression: 'Avoid calling expressions in templates',
},
},
defaultOptions: [],
create(context) {
ensureTemplateParser(context);
const sourceCode = context.getSourceCode();
return {
'Call[receiver.name!="$any"]'(node: Call) {
const isChildOfBoundEvent = Boolean(
getNearestNodeFrom(node, isBoundEvent),
);
if (isChildOfBoundEvent) return;
const {
sourceSpan: { start, end },
} = node;
context.report({
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
messageId: 'noCallExpression',
});
},
};
},
});
function isBoundEvent(node: unknown): node is TmplAstBoundEvent {
return node instanceof TmplAstBoundEvent;
}