/
conditional-complexity.ts
151 lines (131 loc) · 4.04 KB
/
conditional-complexity.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type {
AST,
ASTWithSource,
TmplAstBoundAttribute,
} from '@angular-eslint/bundled-angular-compiler';
import {
Binary,
BindingPipe,
Conditional,
Interpolation,
Lexer,
Parser,
} from '@angular-eslint/bundled-angular-compiler';
import { ensureTemplateParser } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
type Options = [{ maxComplexity: number }];
export type MessageIds = 'conditionalComplexity';
export const RULE_NAME = 'conditional-complexity';
const DEFAULT_MAX_COMPLEXITY = 5;
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'The conditional complexity should not exceed a rational limit',
recommended: false,
},
schema: [
{
type: 'object',
properties: {
maxComplexity: {
minimum: 1,
type: 'number',
},
},
additionalProperties: false,
},
],
messages: {
conditionalComplexity:
'The conditional complexity {{totalComplexity}} exceeds the defined limit {{maxComplexity}}',
},
},
defaultOptions: [{ maxComplexity: DEFAULT_MAX_COMPLEXITY }],
create(context, [{ maxComplexity }]) {
ensureTemplateParser(context);
const sourceCode = context.getSourceCode();
return {
BoundAttribute(node: TmplAstBoundAttribute & { value: ASTWithSource }) {
if (!node.value.source || node.value.ast instanceof Interpolation) {
return;
}
const possibleBinary = extractPossibleBinaryOrConditionalFrom(
getParser().parseBinding(node.value.source, '', 0).ast,
);
const totalComplexity = getTotalComplexity(possibleBinary);
if (totalComplexity <= maxComplexity) {
return;
}
const {
sourceSpan: { start, end },
} = node.value;
context.report({
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
messageId: 'conditionalComplexity',
data: { maxComplexity, totalComplexity },
});
},
Interpolation({ expressions }: Interpolation) {
for (const expression of expressions) {
const totalComplexity = getTotalComplexity(expression);
if (totalComplexity <= maxComplexity) {
continue;
}
const {
sourceSpan: { start, end },
} = expression;
context.report({
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
messageId: 'conditionalComplexity',
data: { maxComplexity, totalComplexity },
});
}
},
};
},
});
function extractPossibleBinaryOrConditionalFrom(node: AST): AST {
return node instanceof BindingPipe ? node.exp : node;
}
let parser: Parser | null = null;
// Instantiate the `Parser` class lazily only when this rule is applied.
function getParser(): Parser {
return parser || (parser = new Parser(new Lexer()));
}
function getTotalComplexity(ast: AST): number {
const possibleBinaryOrConditional =
extractPossibleBinaryOrConditionalFrom(ast);
if (
!(
possibleBinaryOrConditional instanceof Binary ||
possibleBinaryOrConditional instanceof Conditional
)
) {
return 0;
}
let total = 1;
if (possibleBinaryOrConditional instanceof Binary) {
if (possibleBinaryOrConditional.left instanceof Binary) {
total += getTotalComplexity(possibleBinaryOrConditional.left);
}
if (possibleBinaryOrConditional.right instanceof Binary) {
total += getTotalComplexity(possibleBinaryOrConditional.right);
}
}
if (possibleBinaryOrConditional instanceof Conditional) {
total +=
getTotalComplexity(possibleBinaryOrConditional.condition) +
getTotalComplexity(possibleBinaryOrConditional.trueExp) +
getTotalComplexity(possibleBinaryOrConditional.falseExp);
}
return total;
}