@flint.fyi/ts 0.14.3 → 0.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/lib/createTypeScriptFileFromProgram.js +1 -0
  3. package/lib/language.d.ts +1 -0
  4. package/lib/plugin.d.ts +44 -0
  5. package/lib/plugin.js +22 -0
  6. package/lib/rules/anyReturns.d.ts +6 -0
  7. package/lib/rules/anyReturns.js +187 -0
  8. package/lib/rules/anyReturns.test.d.ts +1 -0
  9. package/lib/rules/anyReturns.test.js +647 -0
  10. package/lib/rules/caseDuplicates.d.ts +6 -0
  11. package/lib/rules/caseDuplicates.js +49 -0
  12. package/lib/rules/caseDuplicates.test.d.ts +1 -0
  13. package/lib/rules/caseDuplicates.test.js +307 -0
  14. package/lib/rules/elseIfDuplicates.d.ts +6 -0
  15. package/lib/rules/elseIfDuplicates.js +53 -0
  16. package/lib/rules/elseIfDuplicates.test.d.ts +1 -0
  17. package/lib/rules/elseIfDuplicates.test.js +176 -0
  18. package/lib/rules/forDirections.d.ts +6 -0
  19. package/lib/rules/forDirections.js +124 -0
  20. package/lib/rules/forDirections.test.d.ts +1 -0
  21. package/lib/rules/forDirections.test.js +99 -0
  22. package/lib/rules/functionNewCalls.d.ts +6 -0
  23. package/lib/rules/functionNewCalls.js +60 -0
  24. package/lib/rules/functionNewCalls.test.d.ts +1 -0
  25. package/lib/rules/functionNewCalls.test.js +115 -0
  26. package/lib/rules/negativeZeroComparisons.js +4 -22
  27. package/lib/rules/nonOctalDecimalEscapes.d.ts +6 -0
  28. package/lib/rules/nonOctalDecimalEscapes.js +57 -0
  29. package/lib/rules/nonOctalDecimalEscapes.test.d.ts +1 -0
  30. package/lib/rules/nonOctalDecimalEscapes.test.js +69 -0
  31. package/lib/rules/numericLiteralParsing.d.ts +6 -0
  32. package/lib/rules/numericLiteralParsing.js +97 -0
  33. package/lib/rules/numericLiteralParsing.test.d.ts +1 -0
  34. package/lib/rules/numericLiteralParsing.test.js +99 -0
  35. package/lib/rules/selfAssignments.d.ts +6 -0
  36. package/lib/rules/selfAssignments.js +44 -0
  37. package/lib/rules/selfAssignments.test.d.ts +1 -0
  38. package/lib/rules/selfAssignments.test.js +66 -0
  39. package/lib/rules/selfComparisons.d.ts +6 -0
  40. package/lib/rules/selfComparisons.js +41 -0
  41. package/lib/rules/selfComparisons.test.d.ts +1 -0
  42. package/lib/rules/selfComparisons.test.js +297 -0
  43. package/lib/rules/sequences.d.ts +6 -0
  44. package/lib/rules/sequences.js +34 -0
  45. package/lib/rules/sequences.test.d.ts +1 -0
  46. package/lib/rules/sequences.test.js +48 -0
  47. package/lib/rules/typeofComparisons.d.ts +6 -0
  48. package/lib/rules/typeofComparisons.js +78 -0
  49. package/lib/rules/typeofComparisons.test.d.ts +1 -0
  50. package/lib/rules/typeofComparisons.test.js +85 -0
  51. package/lib/rules/utils/discriminateAnyType.d.ts +12 -0
  52. package/lib/rules/utils/discriminateAnyType.js +41 -0
  53. package/lib/rules/utils/getThisExpression.d.ts +2 -0
  54. package/lib/rules/utils/getThisExpression.js +23 -0
  55. package/lib/rules/utils/isUnsafeAssignment.d.ts +13 -0
  56. package/lib/rules/utils/isUnsafeAssignment.js +76 -0
  57. package/lib/rules/utils/operators.d.ts +4 -0
  58. package/lib/rules/utils/operators.js +36 -0
  59. package/lib/utils/declarationIncludesGlobal.d.ts +2 -0
  60. package/lib/utils/declarationIncludesGlobal.js +5 -0
  61. package/lib/utils/declarationsIncludeGlobal.js +2 -5
  62. package/lib/utils/hasSameTokens.d.ts +2 -0
  63. package/lib/utils/hasSameTokens.js +30 -0
  64. package/lib/utils/isGlobalDeclarationOfName.d.ts +5 -0
  65. package/lib/utils/isGlobalDeclarationOfName.js +31 -0
  66. package/package.json +1 -1
  67. package/src/createTypeScriptFileFromProgram.ts +1 -0
  68. package/src/language.ts +1 -0
  69. package/src/plugin.ts +22 -0
  70. package/src/rules/anyReturns.test.ts +649 -0
  71. package/src/rules/anyReturns.ts +263 -0
  72. package/src/rules/caseDuplicates.test.ts +308 -0
  73. package/src/rules/caseDuplicates.ts +57 -0
  74. package/src/rules/elseIfDuplicates.test.ts +177 -0
  75. package/src/rules/elseIfDuplicates.ts +69 -0
  76. package/src/rules/forDirections.test.ts +100 -0
  77. package/src/rules/forDirections.ts +163 -0
  78. package/src/rules/functionNewCalls.test.ts +116 -0
  79. package/src/rules/functionNewCalls.ts +75 -0
  80. package/src/rules/negativeZeroComparisons.ts +8 -27
  81. package/src/rules/nonOctalDecimalEscapes.test.ts +70 -0
  82. package/src/rules/nonOctalDecimalEscapes.ts +70 -0
  83. package/src/rules/numericLiteralParsing.test.ts +100 -0
  84. package/src/rules/numericLiteralParsing.ts +116 -0
  85. package/src/rules/selfAssignments.test.ts +67 -0
  86. package/src/rules/selfAssignments.ts +50 -0
  87. package/src/rules/selfComparisons.test.ts +298 -0
  88. package/src/rules/selfComparisons.ts +45 -0
  89. package/src/rules/sequences.test.ts +49 -0
  90. package/src/rules/sequences.ts +37 -0
  91. package/src/rules/typeofComparisons.test.ts +86 -0
  92. package/src/rules/typeofComparisons.ts +88 -0
  93. package/src/rules/utils/discriminateAnyType.ts +66 -0
  94. package/src/rules/utils/getThisExpression.ts +24 -0
  95. package/src/rules/utils/isUnsafeAssignment.ts +113 -0
  96. package/src/rules/utils/operators.ts +39 -0
  97. package/src/utils/declarationIncludesGlobal.ts +9 -0
  98. package/src/utils/declarationsIncludeGlobal.ts +3 -7
  99. package/src/utils/hasSameTokens.ts +45 -0
  100. package/src/utils/isGlobalDeclarationOfName.ts +51 -0
  101. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,44 @@
1
+ import * as ts from "typescript";
2
+ import { getTSNodeRange } from "../getTSNodeRange.js";
3
+ import { typescriptLanguage } from "../language.js";
4
+ import { hasSameTokens } from "../utils/hasSameTokens.js";
5
+ export default typescriptLanguage.createRule({
6
+ about: {
7
+ description: "Reports self-assignments which have no effect and are likely errors.",
8
+ id: "selfAssignments",
9
+ preset: "logical",
10
+ },
11
+ messages: {
12
+ selfAssignment: {
13
+ primary: "This value is being assigned to itself, which does nothing.",
14
+ secondary: [
15
+ "Self-assignments have no effect and typically indicate an incomplete refactoring or copy-paste error.",
16
+ ],
17
+ suggestions: [
18
+ "Review the assignment and ensure you're assigning the correct value.",
19
+ ],
20
+ },
21
+ },
22
+ setup(context) {
23
+ return {
24
+ visitors: {
25
+ BinaryExpression: (node) => {
26
+ if (node.operatorToken.kind !== ts.SyntaxKind.EqualsToken &&
27
+ node.operatorToken.kind !==
28
+ ts.SyntaxKind.AmpersandAmpersandEqualsToken &&
29
+ node.operatorToken.kind !== ts.SyntaxKind.BarBarEqualsToken &&
30
+ node.operatorToken.kind !==
31
+ ts.SyntaxKind.QuestionQuestionEqualsToken) {
32
+ return;
33
+ }
34
+ if (hasSameTokens(node.left, node.right, context.sourceFile)) {
35
+ context.report({
36
+ message: "selfAssignment",
37
+ range: getTSNodeRange(node, context.sourceFile),
38
+ });
39
+ }
40
+ },
41
+ },
42
+ };
43
+ },
44
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,66 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./selfAssignments.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ value = value;
8
+ `,
9
+ snapshot: `
10
+ value = value;
11
+ ~~~~~~~~~~~~~
12
+ This value is being assigned to itself, which does nothing.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ count &&= count;
18
+ `,
19
+ snapshot: `
20
+ count &&= count;
21
+ ~~~~~~~~~~~~~~~
22
+ This value is being assigned to itself, which does nothing.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ flag ||= flag;
28
+ `,
29
+ snapshot: `
30
+ flag ||= flag;
31
+ ~~~~~~~~~~~~~
32
+ This value is being assigned to itself, which does nothing.
33
+ `,
34
+ },
35
+ {
36
+ code: `
37
+ data ??= data;
38
+ `,
39
+ snapshot: `
40
+ data ??= data;
41
+ ~~~~~~~~~~~~~
42
+ This value is being assigned to itself, which does nothing.
43
+ `,
44
+ },
45
+ {
46
+ code: `
47
+ a.b ??= a.b;
48
+ `,
49
+ snapshot: `
50
+ a.b ??= a.b;
51
+ ~~~~~~~~~~~
52
+ This value is being assigned to itself, which does nothing.
53
+ `,
54
+ },
55
+ ],
56
+ valid: [
57
+ `value = other;`,
58
+ `first = second;`,
59
+ `let value = value;`,
60
+ `const data = data;`,
61
+ `value += value;`,
62
+ `count -= count;`,
63
+ `a ||= b;`,
64
+ `a.b ||= a.c;`,
65
+ ],
66
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports comparing a value to itself.";
3
+ readonly id: "selfComparisons";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noSelfComparison", undefined>;
6
+ export default _default;
@@ -0,0 +1,41 @@
1
+ import { typescriptLanguage } from "../language.js";
2
+ import { hasSameTokens } from "../utils/hasSameTokens.js";
3
+ import { isComparisonOperator } from "./utils/operators.js";
4
+ export default typescriptLanguage.createRule({
5
+ about: {
6
+ description: "Reports comparing a value to itself.",
7
+ id: "selfComparisons",
8
+ preset: "logical",
9
+ },
10
+ messages: {
11
+ noSelfComparison: {
12
+ primary: "Comparing a value to itself is unnecessary and likely indicates a logic error.",
13
+ secondary: [
14
+ "Self-comparisons always evaluate to the same result for a given operator.",
15
+ "This pattern often indicates a copy-paste error or typo where different variables were intended.",
16
+ ],
17
+ suggestions: [
18
+ "Verify that you intended to compare two different values.",
19
+ "If checking for NaN, use `Number.isNaN()` or `isNaN()` instead.",
20
+ ],
21
+ },
22
+ },
23
+ setup(context) {
24
+ return {
25
+ visitors: {
26
+ BinaryExpression: (node) => {
27
+ if (isComparisonOperator(node.operatorToken) &&
28
+ hasSameTokens(node.left, node.right, context.sourceFile)) {
29
+ context.report({
30
+ message: "noSelfComparison",
31
+ range: {
32
+ begin: node.getStart(context.sourceFile),
33
+ end: node.getEnd(),
34
+ },
35
+ });
36
+ }
37
+ },
38
+ },
39
+ };
40
+ },
41
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,297 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./selfComparisons.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ if (value === value) {
8
+ console.log("always true");
9
+ }
10
+ `,
11
+ snapshot: `
12
+ if (value === value) {
13
+ ~~~~~~~~~~~~~~~
14
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
15
+ console.log("always true");
16
+ }
17
+ `,
18
+ },
19
+ {
20
+ code: `
21
+ if (value == value) {
22
+ console.log("always true");
23
+ }
24
+ `,
25
+ snapshot: `
26
+ if (value == value) {
27
+ ~~~~~~~~~~~~~~
28
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
29
+ console.log("always true");
30
+ }
31
+ `,
32
+ },
33
+ {
34
+ code: `
35
+ if (value !== value) {
36
+ console.log("always false");
37
+ }
38
+ `,
39
+ snapshot: `
40
+ if (value !== value) {
41
+ ~~~~~~~~~~~~~~~
42
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
43
+ console.log("always false");
44
+ }
45
+ `,
46
+ },
47
+ {
48
+ code: `
49
+ if (value != value) {
50
+ console.log("always false");
51
+ }
52
+ `,
53
+ snapshot: `
54
+ if (value != value) {
55
+ ~~~~~~~~~~~~~~
56
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
57
+ console.log("always false");
58
+ }
59
+ `,
60
+ },
61
+ {
62
+ code: `
63
+ if (value < value) {
64
+ console.log("always false");
65
+ }
66
+ `,
67
+ snapshot: `
68
+ if (value < value) {
69
+ ~~~~~~~~~~~~~
70
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
71
+ console.log("always false");
72
+ }
73
+ `,
74
+ },
75
+ {
76
+ code: `
77
+ if (value <= value) {
78
+ console.log("always true");
79
+ }
80
+ `,
81
+ snapshot: `
82
+ if (value <= value) {
83
+ ~~~~~~~~~~~~~~
84
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
85
+ console.log("always true");
86
+ }
87
+ `,
88
+ },
89
+ {
90
+ code: `
91
+ if (value > value) {
92
+ console.log("always false");
93
+ }
94
+ `,
95
+ snapshot: `
96
+ if (value > value) {
97
+ ~~~~~~~~~~~~~
98
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
99
+ console.log("always false");
100
+ }
101
+ `,
102
+ },
103
+ {
104
+ code: `
105
+ if (value >= value) {
106
+ console.log("always true");
107
+ }
108
+ `,
109
+ snapshot: `
110
+ if (value >= value) {
111
+ ~~~~~~~~~~~~~~
112
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
113
+ console.log("always true");
114
+ }
115
+ `,
116
+ },
117
+ {
118
+ code: `
119
+ const result = object.property === object.property;
120
+ `,
121
+ snapshot: `
122
+ const result = object.property === object.property;
123
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
125
+ `,
126
+ },
127
+ {
128
+ code: `
129
+ const result = array[0] === array[0];
130
+ `,
131
+ snapshot: `
132
+ const result = array[0] === array[0];
133
+ ~~~~~~~~~~~~~~~~~~~~~
134
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
135
+ `,
136
+ },
137
+ {
138
+ code: `
139
+ if (calculate() === calculate()) {
140
+ console.log("functions");
141
+ }
142
+ `,
143
+ snapshot: `
144
+ if (calculate() === calculate()) {
145
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
146
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
147
+ console.log("functions");
148
+ }
149
+ `,
150
+ },
151
+ {
152
+ code: `
153
+ if (value /* comment */ === value) {
154
+ console.log("with comment");
155
+ }
156
+ `,
157
+ snapshot: `
158
+ if (value /* comment */ === value) {
159
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
161
+ console.log("with comment");
162
+ }
163
+ `,
164
+ },
165
+ {
166
+ code: `
167
+ if (object.property /* comment */ === object.property) {
168
+ console.log("with comment in property access");
169
+ }
170
+ `,
171
+ snapshot: `
172
+ if (object.property /* comment */ === object.property) {
173
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
175
+ console.log("with comment in property access");
176
+ }
177
+ `,
178
+ },
179
+ {
180
+ code: `
181
+ if (array /* comment */ [0] === array[0]) {
182
+ console.log("with comment in array access");
183
+ }
184
+ `,
185
+ snapshot: `
186
+ if (array /* comment */ [0] === array[0]) {
187
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
189
+ console.log("with comment in array access");
190
+ }
191
+ `,
192
+ },
193
+ {
194
+ code: `
195
+ if (value === value /* comment */) {
196
+ console.log("comment on right side");
197
+ }
198
+ `,
199
+ snapshot: `
200
+ if (value === value /* comment */) {
201
+ ~~~~~~~~~~~~~~~
202
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
203
+ console.log("comment on right side");
204
+ }
205
+ `,
206
+ },
207
+ {
208
+ code: `
209
+ if (value /* left */ === value /* right */) {
210
+ console.log("comments on both sides");
211
+ }
212
+ `,
213
+ snapshot: `
214
+ if (value /* left */ === value /* right */) {
215
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
216
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
217
+ console.log("comments on both sides");
218
+ }
219
+ `,
220
+ },
221
+ {
222
+ code: `
223
+ if (object.property === object /* comment */ .property) {
224
+ console.log("comment on right property access");
225
+ }
226
+ `,
227
+ snapshot: `
228
+ if (object.property === object /* comment */ .property) {
229
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
231
+ console.log("comment on right property access");
232
+ }
233
+ `,
234
+ },
235
+ {
236
+ code: `
237
+ if (array[0] === array /* comment */ [0]) {
238
+ console.log("comment on right array access");
239
+ }
240
+ `,
241
+ snapshot: `
242
+ if (array[0] === array /* comment */ [0]) {
243
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
245
+ console.log("comment on right array access");
246
+ }
247
+ `,
248
+ },
249
+ {
250
+ code: `
251
+ if (/* before */ value === value /* after */) {
252
+ console.log("comments before and after");
253
+ }
254
+ `,
255
+ snapshot: `
256
+ if (/* before */ value === value /* after */) {
257
+ ~~~~~~~~~~~~~~~
258
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
259
+ console.log("comments before and after");
260
+ }
261
+ `,
262
+ },
263
+ {
264
+ code: `
265
+ const result = value /* a */ /* b */ === value /* c */ /* d */;
266
+ `,
267
+ snapshot: `
268
+ const result = value /* a */ /* b */ === value /* c */ /* d */;
269
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270
+ Comparing a value to itself is unnecessary and likely indicates a logic error.
271
+ `,
272
+ },
273
+ ],
274
+ valid: [
275
+ `if (value1 === value2) { console.log("different values"); }`,
276
+ `if (value === other) { console.log("different values"); }`,
277
+ `if (object.property === object.otherProperty) { console.log("different properties"); }`,
278
+ `if (array[0] === array[1]) { console.log("different elements"); }`,
279
+ `const result = value1 == value2;`,
280
+ `const result = value1 != value2;`,
281
+ `const result = value1 !== value2;`,
282
+ `const result = value1 < value2;`,
283
+ `const result = value1 <= value2;`,
284
+ `const result = value1 > value2;`,
285
+ `const result = value1 >= value2;`,
286
+ `if (Number.isNaN(value)) { console.log("checking for NaN correctly"); }`,
287
+ `if (value1 /* comment */ === value2) { console.log("different with comment on left"); }`,
288
+ `if (value1 === value2 /* comment */) { console.log("different with comment on right"); }`,
289
+ `if (value1 /* left */ === value2 /* right */) { console.log("different with comments on both"); }`,
290
+ `if (object /* comment */ .property === object.otherProperty) { console.log("different properties with comment"); }`,
291
+ `if (object.property === object /* comment */ .otherProperty) { console.log("different properties with comment on right"); }`,
292
+ `if (array /* comment */ [0] === array[1]) { console.log("different elements with comment on left"); }`,
293
+ `if (array[0] === array /* comment */ [1]) { console.log("different elements with comment on right"); }`,
294
+ `if (/* before */ value1 === value2 /* after */) { console.log("different with surrounding comments"); }`,
295
+ `const result = value1 /* a */ /* b */ === value2 /* c */ /* d */;`,
296
+ ],
297
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports using the comma operator in expressions.";
3
+ readonly id: "sequences";
4
+ readonly preset: "untyped";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noSequences", undefined>;
6
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import * as ts from "typescript";
2
+ import { getTSNodeRange } from "../getTSNodeRange.js";
3
+ import { typescriptLanguage } from "../language.js";
4
+ export default typescriptLanguage.createRule({
5
+ about: {
6
+ description: "Reports using the comma operator in expressions.",
7
+ id: "sequences",
8
+ preset: "untyped",
9
+ },
10
+ messages: {
11
+ noSequences: {
12
+ primary: 'The "sequence" (comma) operator is often confusing and a sign of mistaken logic.',
13
+ secondary: [
14
+ "The comma operator can make code harder to read and may hide side effects.",
15
+ "Prefer separate expressions instead of using a single sequence.",
16
+ ],
17
+ suggestions: ["Split the expression into separate statements."],
18
+ },
19
+ },
20
+ setup(context) {
21
+ return {
22
+ visitors: {
23
+ BinaryExpression: (node) => {
24
+ if (node.operatorToken.kind === ts.SyntaxKind.CommaToken) {
25
+ context.report({
26
+ message: "noSequences",
27
+ range: getTSNodeRange(node.operatorToken, context.sourceFile),
28
+ });
29
+ }
30
+ },
31
+ },
32
+ };
33
+ },
34
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./sequences.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ const a = (1, 2);
8
+ `,
9
+ snapshot: `
10
+ const a = (1, 2);
11
+ ~
12
+ The "sequence" (comma) operator is often confusing and a sign of mistaken logic.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ function f() {
18
+ return (g(), h());
19
+ }
20
+ `,
21
+ snapshot: `
22
+ function f() {
23
+ return (g(), h());
24
+ ~
25
+ The "sequence" (comma) operator is often confusing and a sign of mistaken logic.
26
+ }
27
+ `,
28
+ },
29
+ {
30
+ code: `
31
+ for ((a = 1, b = 2); ; ) {
32
+ }
33
+ `,
34
+ snapshot: `
35
+ for ((a = 1, b = 2); ; ) {
36
+ ~
37
+ The "sequence" (comma) operator is often confusing and a sign of mistaken logic.
38
+ }
39
+ `,
40
+ },
41
+ ],
42
+ valid: [
43
+ `const a = [1, 2];`,
44
+ `const a = (1 + 2);`,
45
+ `function f() { g(); h(); }`,
46
+ `for (let i = 0; i < 10; i += 1) {}`,
47
+ ],
48
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports typeof expressions that compare impossible string literals.";
3
+ readonly id: "typeofComparisons";
4
+ readonly preset: "untyped";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "invalidValue", undefined>;
6
+ export default _default;
@@ -0,0 +1,78 @@
1
+ import * as ts from "typescript";
2
+ import { getTSNodeRange } from "../getTSNodeRange.js";
3
+ import { typescriptLanguage } from "../language.js";
4
+ const validTypeofValues = new Set([
5
+ "bigint",
6
+ "boolean",
7
+ "function",
8
+ "number",
9
+ "object",
10
+ "string",
11
+ "symbol",
12
+ "undefined",
13
+ ]);
14
+ // TODO: Reuse a shared getStaticValue-style utility?
15
+ function getStringValue(node) {
16
+ return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)
17
+ ? node.text
18
+ : undefined;
19
+ }
20
+ function getTypeofOperand(node) {
21
+ return ts.isTypeOfExpression(node) && node.expression;
22
+ }
23
+ export default typescriptLanguage.createRule({
24
+ about: {
25
+ description: "Reports typeof expressions that compare impossible string literals.",
26
+ id: "typeofComparisons",
27
+ preset: "untyped",
28
+ },
29
+ messages: {
30
+ invalidValue: {
31
+ primary: "This string literal is not one that the typeof operator will ever produce.",
32
+ secondary: [
33
+ "The typeof operator returns one of a specific set of string values.",
34
+ "Comparing typeof to an invalid string is usually a typo and will never match.",
35
+ 'The only valid values are: "bigint", "boolean", "function", "number", "object", "string", "symbol", and "undefined".',
36
+ ],
37
+ suggestions: [
38
+ "Check for typos and use one of the valid typeof return values.",
39
+ ],
40
+ },
41
+ },
42
+ setup(context) {
43
+ function checkComparison(node) {
44
+ const leftTypeofOperand = getTypeofOperand(node.left);
45
+ const rightTypeofOperand = getTypeofOperand(node.right);
46
+ let comparisonValue;
47
+ if (leftTypeofOperand) {
48
+ comparisonValue = node.right;
49
+ }
50
+ else if (rightTypeofOperand) {
51
+ comparisonValue = node.left;
52
+ }
53
+ else {
54
+ return;
55
+ }
56
+ const stringValue = getStringValue(comparisonValue);
57
+ if (stringValue != null && !validTypeofValues.has(stringValue)) {
58
+ context.report({
59
+ message: "invalidValue",
60
+ range: getTSNodeRange(comparisonValue, context.sourceFile),
61
+ });
62
+ }
63
+ }
64
+ return {
65
+ visitors: {
66
+ BinaryExpression: (node) => {
67
+ if (node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
68
+ node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
69
+ node.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken ||
70
+ node.operatorToken.kind ===
71
+ ts.SyntaxKind.ExclamationEqualsEqualsToken) {
72
+ checkComparison(node);
73
+ }
74
+ },
75
+ },
76
+ };
77
+ },
78
+ });
@@ -0,0 +1 @@
1
+ export {};