/
no-negated-async.ts
77 lines (73 loc) · 2.24 KB
/
no-negated-async.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
import type {
BindingPipe,
PrefixNot,
} from '@angular-eslint/bundled-angular-compiler';
import { ensureTemplateParser } from '@angular-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
type Options = [];
export type MessageIds =
| 'noNegatedAsync'
| 'suggestFalseComparison'
| 'suggestNullComparison'
| 'suggestUndefinedComparison';
export const RULE_NAME = 'no-negated-async';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Ensures that async pipe results are not negated',
recommended: 'error',
},
hasSuggestions: true,
schema: [],
messages: {
noNegatedAsync:
'Async pipe results should not be negated. Use `(observable | async) === false`, `(observable | async) === null`, or `(observable | async) === undefined` to check its value instead',
suggestFalseComparison: 'Compare with `false`',
suggestNullComparison: 'Compare with `null`',
suggestUndefinedComparison: 'Compare with `undefined`',
},
},
defaultOptions: [],
create(context) {
ensureTemplateParser(context);
const sourceCode = context.getSourceCode();
return {
':not(PrefixNot) > PrefixNot > BindingPipe[name="async"]'({
parent: {
sourceSpan: { end, start },
},
}: BindingPipe & {
parent: PrefixNot;
}) {
context.report({
messageId: 'noNegatedAsync',
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
suggest: getSuggestionsSchema().map(
({ messageId, textToInsert }) => ({
messageId,
fix: (fixer) => [
fixer.removeRange([start, start + 1]),
fixer.insertTextAfterRange([end, end], textToInsert),
],
}),
),
});
},
};
},
});
function getSuggestionsSchema() {
return [
{ messageId: 'suggestFalseComparison', textToInsert: ' === false' },
{ messageId: 'suggestNullComparison', textToInsert: ' === null' },
{
messageId: 'suggestUndefinedComparison',
textToInsert: ' === undefined',
},
] as const;
}