-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtsslint.config.ts
133 lines (131 loc) · 3.79 KB
/
tsslint.config.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
import { defineConfig } from '@tsslint/config';
import type * as ts from 'typescript';
export default defineConfig({
rules: {
'number-equality'({
typescript: ts,
sourceFile,
reportWarning,
languageService,
}) {
const checker = languageService.getProgram()!.getTypeChecker();
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isBinaryExpression(node) &&
node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken &&
ts.isNumericLiteral(node.right) &&
node.right.text === '0'
) {
const type = checker.getTypeAtLocation(node.left);
if (type.flags & ts.TypeFlags.Number) {
reportWarning(
`Replace "x === 0" with "!x" for numeric variables to clarify boolean usage.`,
node.getStart(sourceFile),
node.getEnd(),
).withFix('Use exclamation instead', () => [
{
fileName: sourceFile.fileName,
textChanges: [
{
newText: `!(${node.left.getText(sourceFile)})`,
span: {
start: node.getStart(sourceFile),
length: node.getWidth(),
},
},
],
},
]);
}
}
ts.forEachChild(node, visit);
});
},
'object-equality'({
typescript: ts,
sourceFile,
reportWarning,
languageService,
}) {
const checker = languageService.getProgram()!.getTypeChecker();
const checkFlags = [ts.TypeFlags.Undefined, ts.TypeFlags.Null];
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isPrefixUnaryExpression(node) &&
node.operator === ts.SyntaxKind.ExclamationToken
) {
const type = checker.getTypeAtLocation(node.operand);
for (const checkFlag of checkFlags) {
if (isObjectOrNullableUnion(type, checkFlag)) {
const flagText =
checkFlag === ts.TypeFlags.Undefined ? 'undefined' : 'null';
if (
ts.isPrefixUnaryExpression(node.parent) &&
node.parent.operator === ts.SyntaxKind.ExclamationToken
) {
reportWarning(
`Do not use "!!" for a variable of type "object | ${flagText}". Replace with "!== ${flagText}" for clarity.`,
node.parent.getStart(sourceFile),
node.getEnd(),
).withFix(`Replace with !== ${flagText}`, () => [
{
fileName: sourceFile.fileName,
textChanges: [
{
newText: `${node.operand.getText(sourceFile)} !== ${flagText}`,
span: {
start: node.parent.getStart(sourceFile),
length:
node.getEnd() - node.parent.getStart(sourceFile),
},
},
],
},
]);
} else {
reportWarning(
`Do not use "!" for a variable of type "object | ${flagText}". Replace with "=== ${flagText}" for clarity.`,
node.getStart(sourceFile),
node.getEnd(),
).withFix(`Replace with === ${flagText}`, () => [
{
fileName: sourceFile.fileName,
textChanges: [
{
newText: `${node.operand.getText(sourceFile)} === ${flagText}`,
span: {
start: node.getStart(sourceFile),
length: node.getWidth(),
},
},
],
},
]);
}
}
}
}
ts.forEachChild(node, visit);
});
function isObjectOrNullableUnion(
type: ts.Type,
nullableFlag: ts.TypeFlags,
) {
if (!(type.flags & ts.TypeFlags.Union)) return false;
const unionType = type;
let hasObject = false;
let hasNullable = false;
for (const sub of (unionType as ts.UnionType).types) {
if (sub.flags & nullableFlag) {
hasNullable = true;
} else if (sub.flags & ts.TypeFlags.Object) {
hasObject = true;
} else {
return false;
}
}
return hasObject && hasNullable;
}
},
},
});