forked from angular-eslint/angular-eslint
/
eqeqeq.ts
157 lines (142 loc) · 4.08 KB
/
eqeqeq.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
152
153
154
155
156
157
import type { AST, Binary } from '@angular-eslint/bundled-angular-compiler';
import {
ASTWithSource,
LiteralPrimitive,
} from '@angular-eslint/bundled-angular-compiler';
import { ensureTemplateParser } from '@angular-eslint/utils';
import type { TSESLint } from '@typescript-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import { getNearestNodeFrom } from '../utils/get-nearest-node-from';
type Options = [{ readonly allowNullOrUndefined?: boolean }];
export type MessageIds = 'eqeqeq' | 'suggestStrictEquality';
export const RULE_NAME = 'eqeqeq';
const DEFAULT_OPTIONS: Options[0] = { allowNullOrUndefined: false };
export default createESLintRule<Options, MessageIds>({
name: 'eqeqeq',
meta: {
type: 'suggestion',
docs: {
description: 'Requires `===` and `!==` in place of `==` and `!=`',
recommended: 'error',
},
hasSuggestions: true,
fixable: 'code',
schema: [
{
type: 'object',
properties: {
allowNullOrUndefined: {
type: 'boolean',
default: DEFAULT_OPTIONS.allowNullOrUndefined,
},
},
additionalProperties: false,
},
],
messages: {
eqeqeq:
'Expected `{{expectedOperation}}` but received `{{actualOperation}}`',
suggestStrictEquality:
'Replace `{{actualOperation}}` with `{{expectedOperation}}`',
},
},
defaultOptions: [DEFAULT_OPTIONS],
create(context, [{ allowNullOrUndefined }]) {
ensureTemplateParser(context);
const sourceCode = context.getSourceCode();
return {
'Binary[operation=/^(==|!=)$/]'(node: Binary) {
const {
left,
operation,
right,
sourceSpan: { start, end },
} = node;
if (allowNullOrUndefined && (isNilValue(left) || isNilValue(right))) {
return;
}
const data = {
actualOperation: operation,
expectedOperation: `${operation}=`,
} as const;
context.report({
loc: {
start: sourceCode.getLocFromIndex(start),
end: sourceCode.getLocFromIndex(end),
},
messageId: 'eqeqeq',
data,
...(isStringNonNumericValue(left) || isStringNonNumericValue(right)
? {
fix: (fixer) =>
getFix({ node, left, right, start, end, fixer }),
}
: {
suggest: [
{
messageId: 'suggestStrictEquality',
fix: (fixer) =>
getFix({ node, left, right, start, end, fixer }),
data,
},
],
}),
});
},
};
},
});
function getSpanLength({ span: { start, end } }: AST): number {
return end - start;
}
const getFix = ({
node,
left,
right,
start,
end,
fixer,
}: {
node: Binary;
left: AST;
right: AST;
start: number;
end: number;
fixer: TSESLint.RuleFixer;
}): TSESLint.RuleFix | null => {
const { source } = getNearestNodeFrom(node, isASTWithSource) ?? {};
if (!source) return null;
return fixer.insertTextAfterRange(
[start + getSpanLength(left) + 1, end - getSpanLength(right) - 1],
'=',
);
};
function isASTWithSource(node: unknown): node is ASTWithSource {
return node instanceof ASTWithSource;
}
function isLiteralPrimitive(node: unknown): node is LiteralPrimitive {
return node instanceof LiteralPrimitive;
}
function isNumeric(value: unknown): value is number | string {
return (
!Number.isNaN(Number.parseFloat(String(value))) &&
Number.isFinite(Number(value))
);
}
function isString(value: unknown): value is string {
return typeof value === 'string';
}
function isStringNonNumericValue(
ast: AST,
): ast is LiteralPrimitive & { value: string } {
return (
isLiteralPrimitive(ast) && isString(ast.value) && !isNumeric(ast.value)
);
}
function isNilValue(
ast: AST,
): ast is LiteralPrimitive & { value: null | undefined } {
return (
isLiteralPrimitive(ast) && (ast.value === null || ast.value === undefined)
);
}