-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
no-negated-async.ts
75 lines (69 loc) · 2.14 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
import {
createESLintRule,
ensureTemplateParser,
} from '../utils/create-eslint-rule';
type Options = [];
export type MessageIds = 'noNegatedAsync' | 'noLooseEquality';
export const RULE_NAME = 'no-negated-async';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Ensures that strict equality is used when evaluating negations on async pipe output',
category: 'Best Practices',
recommended: 'error',
},
schema: [],
messages: {
noNegatedAsync:
'Async pipes should not be negated. Use (observable | async) === (false | null | undefined) to check its value instead',
noLooseEquality:
'Async pipes must use strict equality `===` when comparing with `false`',
},
},
defaultOptions: [],
create(context) {
ensureTemplateParser(context);
const sourceCode = context.getSourceCode();
return {
'PrefixNot > BindingPipe[name=async]'({
parent: {
parent: { type },
sourceSpan: { end, start },
},
}: any) {
const additionalOffset = isInterpolation(type) ? -1 : 0;
const loc = {
start: sourceCode.getLocFromIndex(start + additionalOffset),
end: sourceCode.getLocFromIndex(end + additionalOffset),
} as const;
context.report({
messageId: 'noNegatedAsync',
loc,
});
},
'Binary[operation="=="] > BindingPipe[name=async]'({
parent: {
parent: { type },
sourceSpan: { end, start },
},
}: any) {
const additionalStartOffset = isInterpolation(type) ? -2 : -1;
const additionalEndOffset = isInterpolation(type) ? -1 : 0;
const loc = {
start: sourceCode.getLocFromIndex(start + additionalStartOffset),
end: sourceCode.getLocFromIndex(end + additionalEndOffset),
} as const;
context.report({
messageId: 'noLooseEquality',
loc,
});
},
};
},
});
function isInterpolation(value: string): value is 'Interpolation' {
return value === 'Interpolation';
}