@flint.fyi/ts 0.14.2 → 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 (300) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/lib/createTypeScriptFileFromProgram.js +1 -0
  3. package/lib/index.d.ts +1 -0
  4. package/lib/index.js +1 -0
  5. package/lib/language.d.ts +1 -0
  6. package/lib/nodes.d.ts +1 -0
  7. package/lib/plugin.d.ts +165 -1
  8. package/lib/plugin.js +82 -0
  9. package/lib/rules/anyReturns.d.ts +6 -0
  10. package/lib/rules/anyReturns.js +187 -0
  11. package/lib/rules/anyReturns.test.d.ts +1 -0
  12. package/lib/rules/anyReturns.test.js +647 -0
  13. package/lib/rules/asyncPromiseExecutors.d.ts +6 -0
  14. package/lib/rules/asyncPromiseExecutors.js +51 -0
  15. package/lib/rules/asyncPromiseExecutors.test.d.ts +1 -0
  16. package/lib/rules/asyncPromiseExecutors.test.js +71 -0
  17. package/lib/rules/caseDeclarations.d.ts +6 -0
  18. package/lib/rules/caseDeclarations.js +53 -0
  19. package/lib/rules/caseDeclarations.test.d.ts +1 -0
  20. package/lib/rules/caseDeclarations.test.js +227 -0
  21. package/lib/rules/caseDuplicates.d.ts +6 -0
  22. package/lib/rules/caseDuplicates.js +49 -0
  23. package/lib/rules/caseDuplicates.test.d.ts +1 -0
  24. package/lib/rules/caseDuplicates.test.js +307 -0
  25. package/lib/rules/chainedAssignments.d.ts +6 -0
  26. package/lib/rules/chainedAssignments.js +48 -0
  27. package/lib/rules/chainedAssignments.test.d.ts +1 -0
  28. package/lib/rules/chainedAssignments.test.js +238 -0
  29. package/lib/rules/classAssignments.d.ts +6 -0
  30. package/lib/rules/classAssignments.js +42 -0
  31. package/lib/rules/classAssignments.test.d.ts +1 -0
  32. package/lib/rules/classAssignments.test.js +156 -0
  33. package/lib/rules/constantAssignments.d.ts +6 -0
  34. package/lib/rules/constantAssignments.js +61 -0
  35. package/lib/rules/constantAssignments.test.d.ts +1 -0
  36. package/lib/rules/constantAssignments.test.js +221 -0
  37. package/lib/rules/constructorReturns.d.ts +6 -0
  38. package/lib/rules/constructorReturns.js +52 -0
  39. package/lib/rules/constructorReturns.test.d.ts +1 -0
  40. package/lib/rules/constructorReturns.test.js +96 -0
  41. package/lib/rules/defaultCaseLast.d.ts +6 -0
  42. package/lib/rules/defaultCaseLast.js +43 -0
  43. package/lib/rules/defaultCaseLast.test.d.ts +1 -0
  44. package/lib/rules/defaultCaseLast.test.js +195 -0
  45. package/lib/rules/duplicateArguments.d.ts +6 -0
  46. package/lib/rules/duplicateArguments.js +51 -0
  47. package/lib/rules/duplicateArguments.test.d.ts +1 -0
  48. package/lib/rules/duplicateArguments.test.js +139 -0
  49. package/lib/rules/elseIfDuplicates.d.ts +6 -0
  50. package/lib/rules/elseIfDuplicates.js +53 -0
  51. package/lib/rules/elseIfDuplicates.test.d.ts +1 -0
  52. package/lib/rules/elseIfDuplicates.test.js +176 -0
  53. package/lib/rules/emptyDestructures.d.ts +6 -0
  54. package/lib/rules/emptyDestructures.js +40 -0
  55. package/lib/rules/emptyDestructures.test.d.ts +1 -0
  56. package/lib/rules/emptyDestructures.test.js +186 -0
  57. package/lib/rules/emptyStaticBlocks.d.ts +6 -0
  58. package/lib/rules/emptyStaticBlocks.js +54 -0
  59. package/lib/rules/emptyStaticBlocks.test.d.ts +1 -0
  60. package/lib/rules/emptyStaticBlocks.test.js +167 -0
  61. package/lib/rules/exceptionAssignments.d.ts +6 -0
  62. package/lib/rules/exceptionAssignments.js +61 -0
  63. package/lib/rules/exceptionAssignments.test.d.ts +1 -0
  64. package/lib/rules/exceptionAssignments.test.js +264 -0
  65. package/lib/rules/forDirections.d.ts +6 -0
  66. package/lib/rules/forDirections.js +124 -0
  67. package/lib/rules/forDirections.test.d.ts +1 -0
  68. package/lib/rules/forDirections.test.js +99 -0
  69. package/lib/rules/functionAssignments.d.ts +6 -0
  70. package/lib/rules/functionAssignments.js +42 -0
  71. package/lib/rules/functionAssignments.test.d.ts +1 -0
  72. package/lib/rules/functionAssignments.test.js +209 -0
  73. package/lib/rules/functionNewCalls.d.ts +6 -0
  74. package/lib/rules/functionNewCalls.js +60 -0
  75. package/lib/rules/functionNewCalls.test.d.ts +1 -0
  76. package/lib/rules/functionNewCalls.test.js +115 -0
  77. package/lib/rules/generatorFunctionYields.d.ts +6 -0
  78. package/lib/rules/generatorFunctionYields.js +57 -0
  79. package/lib/rules/generatorFunctionYields.test.d.ts +1 -0
  80. package/lib/rules/generatorFunctionYields.test.js +151 -0
  81. package/lib/rules/globalAssignments.d.ts +6 -0
  82. package/lib/rules/globalAssignments.js +59 -0
  83. package/lib/rules/globalAssignments.test.d.ts +1 -0
  84. package/lib/rules/globalAssignments.test.js +215 -0
  85. package/lib/rules/globalObjectCalls.d.ts +6 -0
  86. package/lib/rules/globalObjectCalls.js +43 -0
  87. package/lib/rules/globalObjectCalls.test.d.ts +1 -0
  88. package/lib/rules/globalObjectCalls.test.js +93 -0
  89. package/lib/rules/negativeZeroComparisons.d.ts +6 -0
  90. package/lib/rules/negativeZeroComparisons.js +65 -0
  91. package/lib/rules/negativeZeroComparisons.test.d.ts +1 -0
  92. package/lib/rules/negativeZeroComparisons.test.js +209 -0
  93. package/lib/rules/newExpressions.d.ts +6 -0
  94. package/lib/rules/newExpressions.js +54 -0
  95. package/lib/rules/newExpressions.test.d.ts +1 -0
  96. package/lib/rules/newExpressions.test.js +79 -0
  97. package/lib/rules/newNativeNonConstructors.d.ts +6 -0
  98. package/lib/rules/newNativeNonConstructors.js +49 -0
  99. package/lib/rules/newNativeNonConstructors.test.d.ts +1 -0
  100. package/lib/rules/newNativeNonConstructors.test.js +94 -0
  101. package/lib/rules/nonOctalDecimalEscapes.d.ts +6 -0
  102. package/lib/rules/nonOctalDecimalEscapes.js +57 -0
  103. package/lib/rules/nonOctalDecimalEscapes.test.d.ts +1 -0
  104. package/lib/rules/nonOctalDecimalEscapes.test.js +69 -0
  105. package/lib/rules/numericLiteralParsing.d.ts +6 -0
  106. package/lib/rules/numericLiteralParsing.js +97 -0
  107. package/lib/rules/numericLiteralParsing.test.d.ts +1 -0
  108. package/lib/rules/numericLiteralParsing.test.js +99 -0
  109. package/lib/rules/objectProto.d.ts +6 -0
  110. package/lib/rules/objectProto.js +54 -0
  111. package/lib/rules/objectProto.test.d.ts +1 -0
  112. package/lib/rules/objectProto.test.js +96 -0
  113. package/lib/rules/octalNumbers.d.ts +6 -0
  114. package/lib/rules/octalNumbers.js +68 -0
  115. package/lib/rules/octalNumbers.test.d.ts +1 -0
  116. package/lib/rules/octalNumbers.test.js +245 -0
  117. package/lib/rules/returnAssignments.d.ts +6 -0
  118. package/lib/rules/returnAssignments.js +50 -0
  119. package/lib/rules/returnAssignments.test.d.ts +1 -0
  120. package/lib/rules/returnAssignments.test.js +185 -0
  121. package/lib/rules/selfAssignments.d.ts +6 -0
  122. package/lib/rules/selfAssignments.js +44 -0
  123. package/lib/rules/selfAssignments.test.d.ts +1 -0
  124. package/lib/rules/selfAssignments.test.js +66 -0
  125. package/lib/rules/selfComparisons.d.ts +6 -0
  126. package/lib/rules/selfComparisons.js +41 -0
  127. package/lib/rules/selfComparisons.test.d.ts +1 -0
  128. package/lib/rules/selfComparisons.test.js +297 -0
  129. package/lib/rules/sequences.d.ts +6 -0
  130. package/lib/rules/sequences.js +34 -0
  131. package/lib/rules/sequences.test.d.ts +1 -0
  132. package/lib/rules/sequences.test.js +48 -0
  133. package/lib/rules/sparseArrays.d.ts +6 -0
  134. package/lib/rules/sparseArrays.js +54 -0
  135. package/lib/rules/sparseArrays.test.d.ts +1 -0
  136. package/lib/rules/sparseArrays.test.js +110 -0
  137. package/lib/rules/symbolDescriptions.d.ts +6 -0
  138. package/lib/rules/symbolDescriptions.js +41 -0
  139. package/lib/rules/symbolDescriptions.test.d.ts +1 -0
  140. package/lib/rules/symbolDescriptions.test.js +89 -0
  141. package/lib/rules/typeofComparisons.d.ts +6 -0
  142. package/lib/rules/typeofComparisons.js +78 -0
  143. package/lib/rules/typeofComparisons.test.d.ts +1 -0
  144. package/lib/rules/typeofComparisons.test.js +85 -0
  145. package/lib/rules/unassignedVariables.d.ts +6 -0
  146. package/lib/rules/unassignedVariables.js +55 -0
  147. package/lib/rules/unassignedVariables.test.d.ts +1 -0
  148. package/lib/rules/unassignedVariables.test.js +72 -0
  149. package/lib/rules/undefinedVariables.d.ts +6 -0
  150. package/lib/rules/undefinedVariables.js +76 -0
  151. package/lib/rules/undefinedVariables.test.d.ts +1 -0
  152. package/lib/rules/undefinedVariables.test.js +80 -0
  153. package/lib/rules/unicodeBOMs.d.ts +6 -0
  154. package/lib/rules/unicodeBOMs.js +41 -0
  155. package/lib/rules/unicodeBOMs.test.d.ts +1 -0
  156. package/lib/rules/unicodeBOMs.test.js +65 -0
  157. package/lib/rules/unnecessaryCatches.d.ts +6 -0
  158. package/lib/rules/unnecessaryCatches.js +68 -0
  159. package/lib/rules/unnecessaryCatches.test.d.ts +1 -0
  160. package/lib/rules/unnecessaryCatches.test.js +210 -0
  161. package/lib/rules/unsafeNegations.d.ts +6 -0
  162. package/lib/rules/unsafeNegations.js +50 -0
  163. package/lib/rules/unsafeNegations.test.d.ts +1 -0
  164. package/lib/rules/unsafeNegations.test.js +63 -0
  165. package/lib/rules/utils/discriminateAnyType.d.ts +12 -0
  166. package/lib/rules/utils/discriminateAnyType.js +41 -0
  167. package/lib/rules/utils/getThisExpression.d.ts +2 -0
  168. package/lib/rules/utils/getThisExpression.js +23 -0
  169. package/lib/rules/utils/isUnsafeAssignment.d.ts +13 -0
  170. package/lib/rules/utils/isUnsafeAssignment.js +76 -0
  171. package/lib/rules/utils/operators.d.ts +4 -0
  172. package/lib/rules/utils/operators.js +36 -0
  173. package/lib/rules/variableDeletions.d.ts +6 -0
  174. package/lib/rules/variableDeletions.js +39 -0
  175. package/lib/rules/variableDeletions.test.d.ts +1 -0
  176. package/lib/rules/variableDeletions.test.js +75 -0
  177. package/lib/rules/voidOperator.d.ts +6 -0
  178. package/lib/rules/voidOperator.js +36 -0
  179. package/lib/rules/voidOperator.test.d.ts +1 -0
  180. package/lib/rules/voidOperator.test.js +98 -0
  181. package/lib/utils/declarationIncludesGlobal.d.ts +2 -0
  182. package/lib/utils/declarationIncludesGlobal.js +5 -0
  183. package/lib/utils/declarationsIncludeGlobal.d.ts +2 -0
  184. package/lib/utils/declarationsIncludeGlobal.js +4 -0
  185. package/lib/utils/getModifyingReferences.d.ts +8 -0
  186. package/lib/utils/getModifyingReferences.js +42 -0
  187. package/lib/utils/hasSameTokens.d.ts +2 -0
  188. package/lib/utils/hasSameTokens.js +30 -0
  189. package/lib/utils/isGlobalDeclaration.d.ts +2 -0
  190. package/lib/utils/isGlobalDeclaration.js +5 -0
  191. package/lib/utils/isGlobalDeclarationOfName.d.ts +5 -0
  192. package/lib/utils/isGlobalDeclarationOfName.js +31 -0
  193. package/lib/utils/isGlobalVariable.d.ts +8 -0
  194. package/lib/utils/isGlobalVariable.js +21 -0
  195. package/lib/utils/unwrapParentParenthesizedExpressions.d.ts +2 -0
  196. package/lib/utils/unwrapParentParenthesizedExpressions.js +6 -0
  197. package/lib/utils/unwrapParenthesizedExpression.d.ts +2 -0
  198. package/lib/utils/unwrapParenthesizedExpression.js +6 -0
  199. package/package.json +1 -1
  200. package/src/createTypeScriptFileFromProgram.ts +1 -0
  201. package/src/index.ts +1 -0
  202. package/src/language.ts +1 -0
  203. package/src/nodes.ts +1 -0
  204. package/src/plugin.ts +82 -0
  205. package/src/rules/anyReturns.test.ts +649 -0
  206. package/src/rules/anyReturns.ts +263 -0
  207. package/src/rules/asyncPromiseExecutors.test.ts +72 -0
  208. package/src/rules/asyncPromiseExecutors.ts +61 -0
  209. package/src/rules/caseDeclarations.test.ts +228 -0
  210. package/src/rules/caseDeclarations.ts +70 -0
  211. package/src/rules/caseDuplicates.test.ts +308 -0
  212. package/src/rules/caseDuplicates.ts +57 -0
  213. package/src/rules/chainedAssignments.test.ts +239 -0
  214. package/src/rules/chainedAssignments.ts +55 -0
  215. package/src/rules/classAssignments.test.ts +157 -0
  216. package/src/rules/classAssignments.ts +50 -0
  217. package/src/rules/constantAssignments.test.ts +222 -0
  218. package/src/rules/constantAssignments.ts +78 -0
  219. package/src/rules/constructorReturns.test.ts +97 -0
  220. package/src/rules/constructorReturns.ts +59 -0
  221. package/src/rules/defaultCaseLast.test.ts +196 -0
  222. package/src/rules/defaultCaseLast.ts +54 -0
  223. package/src/rules/duplicateArguments.test.ts +140 -0
  224. package/src/rules/duplicateArguments.ts +58 -0
  225. package/src/rules/elseIfDuplicates.test.ts +177 -0
  226. package/src/rules/elseIfDuplicates.ts +69 -0
  227. package/src/rules/emptyDestructures.test.ts +187 -0
  228. package/src/rules/emptyDestructures.ts +48 -0
  229. package/src/rules/emptyStaticBlocks.test.ts +168 -0
  230. package/src/rules/emptyStaticBlocks.ts +60 -0
  231. package/src/rules/exceptionAssignments.test.ts +265 -0
  232. package/src/rules/exceptionAssignments.ts +77 -0
  233. package/src/rules/forDirections.test.ts +100 -0
  234. package/src/rules/forDirections.ts +163 -0
  235. package/src/rules/functionAssignments.test.ts +210 -0
  236. package/src/rules/functionAssignments.ts +51 -0
  237. package/src/rules/functionNewCalls.test.ts +116 -0
  238. package/src/rules/functionNewCalls.ts +75 -0
  239. package/src/rules/generatorFunctionYields.test.ts +152 -0
  240. package/src/rules/generatorFunctionYields.ts +71 -0
  241. package/src/rules/globalAssignments.test.ts +216 -0
  242. package/src/rules/globalAssignments.ts +67 -0
  243. package/src/rules/globalObjectCalls.test.ts +94 -0
  244. package/src/rules/globalObjectCalls.ts +52 -0
  245. package/src/rules/negativeZeroComparisons.test.ts +210 -0
  246. package/src/rules/negativeZeroComparisons.ts +87 -0
  247. package/src/rules/newExpressions.test.ts +80 -0
  248. package/src/rules/newExpressions.ts +67 -0
  249. package/src/rules/newNativeNonConstructors.test.ts +95 -0
  250. package/src/rules/newNativeNonConstructors.ts +59 -0
  251. package/src/rules/nonOctalDecimalEscapes.test.ts +70 -0
  252. package/src/rules/nonOctalDecimalEscapes.ts +70 -0
  253. package/src/rules/numericLiteralParsing.test.ts +100 -0
  254. package/src/rules/numericLiteralParsing.ts +116 -0
  255. package/src/rules/objectProto.test.ts +97 -0
  256. package/src/rules/objectProto.ts +62 -0
  257. package/src/rules/octalNumbers.test.ts +246 -0
  258. package/src/rules/octalNumbers.ts +75 -0
  259. package/src/rules/returnAssignments.test.ts +186 -0
  260. package/src/rules/returnAssignments.ts +57 -0
  261. package/src/rules/selfAssignments.test.ts +67 -0
  262. package/src/rules/selfAssignments.ts +50 -0
  263. package/src/rules/selfComparisons.test.ts +298 -0
  264. package/src/rules/selfComparisons.ts +45 -0
  265. package/src/rules/sequences.test.ts +49 -0
  266. package/src/rules/sequences.ts +37 -0
  267. package/src/rules/sparseArrays.test.ts +111 -0
  268. package/src/rules/sparseArrays.ts +61 -0
  269. package/src/rules/symbolDescriptions.test.ts +90 -0
  270. package/src/rules/symbolDescriptions.ts +47 -0
  271. package/src/rules/typeofComparisons.test.ts +86 -0
  272. package/src/rules/typeofComparisons.ts +88 -0
  273. package/src/rules/unassignedVariables.test.ts +73 -0
  274. package/src/rules/unassignedVariables.ts +69 -0
  275. package/src/rules/undefinedVariables.test.ts +81 -0
  276. package/src/rules/undefinedVariables.ts +97 -0
  277. package/src/rules/unicodeBOMs.test.ts +66 -0
  278. package/src/rules/unicodeBOMs.ts +44 -0
  279. package/src/rules/unnecessaryCatches.test.ts +211 -0
  280. package/src/rules/unnecessaryCatches.ts +84 -0
  281. package/src/rules/unsafeNegations.test.ts +64 -0
  282. package/src/rules/unsafeNegations.ts +59 -0
  283. package/src/rules/utils/discriminateAnyType.ts +66 -0
  284. package/src/rules/utils/getThisExpression.ts +24 -0
  285. package/src/rules/utils/isUnsafeAssignment.ts +113 -0
  286. package/src/rules/utils/operators.ts +39 -0
  287. package/src/rules/variableDeletions.test.ts +76 -0
  288. package/src/rules/variableDeletions.ts +43 -0
  289. package/src/rules/voidOperator.test.ts +99 -0
  290. package/src/rules/voidOperator.ts +38 -0
  291. package/src/utils/declarationIncludesGlobal.ts +9 -0
  292. package/src/utils/declarationsIncludeGlobal.ts +7 -0
  293. package/src/utils/getModifyingReferences.ts +58 -0
  294. package/src/utils/hasSameTokens.ts +45 -0
  295. package/src/utils/isGlobalDeclaration.ts +12 -0
  296. package/src/utils/isGlobalDeclarationOfName.ts +51 -0
  297. package/src/utils/isGlobalVariable.ts +30 -0
  298. package/src/utils/unwrapParentParenthesizedExpressions.ts +7 -0
  299. package/src/utils/unwrapParenthesizedExpression.ts +9 -0
  300. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,110 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./sparseArrays.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ const array = [1, , 3];
8
+ `,
9
+ snapshot: `
10
+ const array = [1, , 3];
11
+ ~
12
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ const array = [, 2, 3];
18
+ `,
19
+ snapshot: `
20
+ const array = [, 2, 3];
21
+ ~
22
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ const array = [1, , , 4];
28
+ `,
29
+ snapshot: `
30
+ const array = [1, , , 4];
31
+ ~
32
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
33
+ ~
34
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
35
+ `,
36
+ },
37
+ {
38
+ code: `
39
+ const nested = [[1, , 3], [4, 5, 6]];
40
+ `,
41
+ snapshot: `
42
+ const nested = [[1, , 3], [4, 5, 6]];
43
+ ~
44
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
45
+ `,
46
+ },
47
+ {
48
+ code: `
49
+ const result = [
50
+ 1,
51
+ ,
52
+ 3
53
+ ];
54
+ `,
55
+ snapshot: `
56
+ const result = [
57
+ 1,
58
+ ,
59
+ ~
60
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
61
+ 3
62
+ ];
63
+ `,
64
+ },
65
+ {
66
+ code: `
67
+ const matrix = [
68
+ [1, 2, 3],
69
+ [4, , 6],
70
+ [7, 8, 9]
71
+ ];
72
+ `,
73
+ snapshot: `
74
+ const matrix = [
75
+ [1, 2, 3],
76
+ [4, , 6],
77
+ ~
78
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
79
+ [7, 8, 9]
80
+ ];
81
+ `,
82
+ },
83
+ {
84
+ code: `
85
+ function getArray() {
86
+ return [1, , 3];
87
+ }
88
+ `,
89
+ snapshot: `
90
+ function getArray() {
91
+ return [1, , 3];
92
+ ~
93
+ Sparse arrays with "holes" (empty slots) are misleading and behave differently from \`undefined\` values.
94
+ }
95
+ `,
96
+ },
97
+ ],
98
+ valid: [
99
+ `const array = [1, 2, 3];`,
100
+ `const array = [];`,
101
+ `const array = [1];`,
102
+ `const array = [1, undefined, 3];`,
103
+ `const array = [undefined, 2, 3];`,
104
+ `const array = [1, 2, undefined];`,
105
+ `const array = [1, 2, 3,];`,
106
+ `const nested = [[1, undefined, 3], [4, 5, 6]];`,
107
+ `const matrix = [[1, 2, 3], [4, undefined, 6], [7, 8, 9]];`,
108
+ `const values = [null, undefined, 0, false, ""];`,
109
+ ],
110
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports Symbol() calls without description arguments.";
3
+ readonly id: "symbolDescriptions";
4
+ readonly preset: "stylistic";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "missingDescription", undefined>;
6
+ export default _default;
@@ -0,0 +1,41 @@
1
+ import * as ts from "typescript";
2
+ import { typescriptLanguage } from "../language.js";
3
+ export default typescriptLanguage.createRule({
4
+ about: {
5
+ description: "Reports Symbol() calls without description arguments.",
6
+ id: "symbolDescriptions",
7
+ preset: "stylistic",
8
+ },
9
+ messages: {
10
+ missingDescription: {
11
+ primary: "Symbols without descriptions are more difficult to debug or reason about.",
12
+ secondary: [
13
+ "Creating a symbol without a description makes debugging difficult, as the symbol's purpose isn't clear when inspecting it.",
14
+ "While the description doesn't affect the symbol's uniqueness, it appears in the symbol's string representation and can be accessed via `Symbol.prototype.description`.",
15
+ ],
16
+ suggestions: [
17
+ "Add a descriptive string argument to the Symbol constructor that explains the symbol's purpose.",
18
+ ],
19
+ },
20
+ },
21
+ setup(context) {
22
+ return {
23
+ visitors: {
24
+ CallExpression: (node) => {
25
+ if (!ts.isIdentifier(node.expression) ||
26
+ node.expression.text !== "Symbol" ||
27
+ node.arguments.length) {
28
+ return;
29
+ }
30
+ context.report({
31
+ message: "missingDescription",
32
+ range: {
33
+ begin: node.getStart(context.sourceFile),
34
+ end: node.getEnd(),
35
+ },
36
+ });
37
+ },
38
+ },
39
+ };
40
+ },
41
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,89 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./symbolDescriptions.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ const symbol = Symbol();
8
+ `,
9
+ snapshot: `
10
+ const symbol = Symbol();
11
+ ~~~~~~~~
12
+ Symbols without descriptions are more difficult to debug or reason about.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ const uniqueKey = Symbol();
18
+ `,
19
+ snapshot: `
20
+ const uniqueKey = Symbol();
21
+ ~~~~~~~~
22
+ Symbols without descriptions are more difficult to debug or reason about.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ const symbols = [Symbol(), Symbol()];
28
+ `,
29
+ snapshot: `
30
+ const symbols = [Symbol(), Symbol()];
31
+ ~~~~~~~~
32
+ Symbols without descriptions are more difficult to debug or reason about.
33
+ ~~~~~~~~
34
+ Symbols without descriptions are more difficult to debug or reason about.
35
+ `,
36
+ },
37
+ {
38
+ code: `
39
+ function createSymbol() {
40
+ return Symbol();
41
+ }
42
+ `,
43
+ snapshot: `
44
+ function createSymbol() {
45
+ return Symbol();
46
+ ~~~~~~~~
47
+ Symbols without descriptions are more difficult to debug or reason about.
48
+ }
49
+ `,
50
+ },
51
+ {
52
+ code: `
53
+ const obj = {
54
+ key: Symbol(),
55
+ };
56
+ `,
57
+ snapshot: `
58
+ const obj = {
59
+ key: Symbol(),
60
+ ~~~~~~~~
61
+ Symbols without descriptions are more difficult to debug or reason about.
62
+ };
63
+ `,
64
+ },
65
+ {
66
+ code: `
67
+ const value = condition ? Symbol() : null;
68
+ `,
69
+ snapshot: `
70
+ const value = condition ? Symbol() : null;
71
+ ~~~~~~~~
72
+ Symbols without descriptions are more difficult to debug or reason about.
73
+ `,
74
+ },
75
+ ],
76
+ valid: [
77
+ `const symbol = Symbol("description");`,
78
+ `const uniqueKey = Symbol("uniqueKey");`,
79
+ `const mySymbol = Symbol("my symbol");`,
80
+ `const symbols = [Symbol("first"), Symbol("second")];`,
81
+ `function createSymbol() { return Symbol("created"); }`,
82
+ `const obj = { key: Symbol("key") };`,
83
+ `const value = Symbol.for("global");`,
84
+ `const iterator = Symbol.iterator;`,
85
+ `const hasInstance = Symbol.hasInstance;`,
86
+ `const primitiveValue = Symbol.toPrimitive;`,
87
+ `Symbol("valid")`,
88
+ ],
89
+ });
@@ -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 {};
@@ -0,0 +1,85 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./typeofComparisons.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ if (typeof value === "") {
8
+ process();
9
+ }
10
+ `,
11
+ snapshot: `
12
+ if (typeof value === "") {
13
+ ~~
14
+ This string literal is not one that the typeof operator will ever produce.
15
+ process();
16
+ }
17
+ `,
18
+ },
19
+ {
20
+ code: `
21
+ if (typeof data != "invalid") {
22
+ reject();
23
+ }
24
+ `,
25
+ snapshot: `
26
+ if (typeof data != "invalid") {
27
+ ~~~~~~~~~
28
+ This string literal is not one that the typeof operator will ever produce.
29
+ reject();
30
+ }
31
+ `,
32
+ },
33
+ {
34
+ code: `
35
+ if ("invalid" === typeof flag) {
36
+ toggle();
37
+ }
38
+ `,
39
+ snapshot: `
40
+ if ("invalid" === typeof flag) {
41
+ ~~~~~~~~~
42
+ This string literal is not one that the typeof operator will ever produce.
43
+ toggle();
44
+ }
45
+ `,
46
+ },
47
+ {
48
+ code: `
49
+ if (typeof value === "String") {
50
+ process();
51
+ }
52
+ `,
53
+ snapshot: `
54
+ if (typeof value === "String") {
55
+ ~~~~~~~~
56
+ This string literal is not one that the typeof operator will ever produce.
57
+ process();
58
+ }
59
+ `,
60
+ },
61
+ {
62
+ code: `
63
+ const isValid = typeof input !== "array";
64
+ `,
65
+ snapshot: `
66
+ const isValid = typeof input !== "array";
67
+ ~~~~~~~
68
+ This string literal is not one that the typeof operator will ever produce.
69
+ `,
70
+ },
71
+ ],
72
+ valid: [
73
+ `if (typeof value === "string") { process(); }`,
74
+ `if (typeof variable == "undefined") { handle(); }`,
75
+ `if (typeof data != "number") { reject(); }`,
76
+ `if (typeof callback !== "function") { throw new Error("Invalid callback"); }`,
77
+ `if (typeof flag === "boolean") { toggle(); }`,
78
+ `if (typeof value === "object") { parse(); }`,
79
+ `if (typeof value === "symbol") { handle(); }`,
80
+ `if (typeof value === "bigint") { compute(); }`,
81
+ `if (typeof value === other) { process(); }`,
82
+ `if (typeof value === typeof other) { compare(); }`,
83
+ `const type = typeof value;`,
84
+ ],
85
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports variables that are declared but never assigned a value.";
3
+ readonly id: "unassignedVariables";
4
+ readonly preset: "untyped";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noUnassigned", undefined>;
6
+ export default _default;
@@ -0,0 +1,55 @@
1
+ import * as ts from "typescript";
2
+ import { typescriptLanguage } from "../language.js";
3
+ import { getModifyingReferences } from "../utils/getModifyingReferences.js";
4
+ export default typescriptLanguage.createRule({
5
+ about: {
6
+ description: "Reports variables that are declared but never assigned a value.",
7
+ id: "unassignedVariables",
8
+ preset: "untyped",
9
+ },
10
+ messages: {
11
+ noUnassigned: {
12
+ primary: "Variable '{{ name }}' is declared but never assigned a value.",
13
+ secondary: [
14
+ "Declaring a variable without ever assigning it a value means it will always be `undefined`.",
15
+ "This is often a mistake, such as forgetting to initialize the variable or assign to it later.",
16
+ ],
17
+ suggestions: [
18
+ "Assign a value to the variable, either at declaration or before it is used.",
19
+ "If the variable is meant to be `undefined`, consider making that explicit for clarity.",
20
+ ],
21
+ },
22
+ },
23
+ setup(context) {
24
+ function hasAssignments(identifier, sourceFile, typeChecker) {
25
+ // TODO (#400): Switch to scope analysis
26
+ return !!getModifyingReferences(identifier, sourceFile, typeChecker)
27
+ .length;
28
+ }
29
+ return {
30
+ visitors: {
31
+ VariableDeclaration: (node) => {
32
+ if (node.initializer || !ts.isIdentifier(node.name)) {
33
+ return;
34
+ }
35
+ if (ts.isVariableDeclarationList(node.parent) &&
36
+ !!(node.parent.flags & ts.NodeFlags.Const)) {
37
+ return;
38
+ }
39
+ if (!hasAssignments(node.name, context.sourceFile, context.typeChecker)) {
40
+ context.report({
41
+ data: {
42
+ name: node.name.text,
43
+ },
44
+ message: "noUnassigned",
45
+ range: {
46
+ begin: node.name.getStart(context.sourceFile),
47
+ end: node.name.getEnd(),
48
+ },
49
+ });
50
+ }
51
+ },
52
+ },
53
+ };
54
+ },
55
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,72 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./unassignedVariables.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ let value;
8
+ `,
9
+ snapshot: `
10
+ let value;
11
+ ~~~~~
12
+ Variable 'value' is declared but never assigned a value.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ var variable;
18
+ console.log(variable);
19
+ `,
20
+ snapshot: `
21
+ var variable;
22
+ ~~~~~~~~
23
+ Variable 'variable' is declared but never assigned a value.
24
+ console.log(variable);
25
+ `,
26
+ },
27
+ {
28
+ code: `
29
+ let unassigned: number;
30
+ console.log(unassigned);
31
+ `,
32
+ snapshot: `
33
+ let unassigned: number;
34
+ ~~~~~~~~~~
35
+ Variable 'unassigned' is declared but never assigned a value.
36
+ console.log(unassigned);
37
+ `,
38
+ },
39
+ {
40
+ code: `
41
+ function example() {
42
+ let local;
43
+ return local;
44
+ }
45
+ `,
46
+ snapshot: `
47
+ function example() {
48
+ let local;
49
+ ~~~~~
50
+ Variable 'local' is declared but never assigned a value.
51
+ return local;
52
+ }
53
+ `,
54
+ },
55
+ ],
56
+ valid: [
57
+ `let assigned = 5;`,
58
+ `const constant = 10;`,
59
+ `var variable = "hello";`,
60
+ `let value: number = 42;`,
61
+ `let x; x = 5;`,
62
+ `let y; y = 10; console.log(y);`,
63
+ `var z; z += 1;`,
64
+ `let counter; counter++;`,
65
+ `let value; value--;`,
66
+ `for (let index = 0; index < 10; index++) {}`,
67
+ `for (let item; item < 10; item++) {}`,
68
+ `let result; result ||= "default";`,
69
+ `let data; data &&= "value";`,
70
+ `let nullish; nullish ??= "fallback";`,
71
+ ],
72
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports using variables that are not defined.";
3
+ readonly id: "undefinedVariables";
4
+ readonly preset: "untyped";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "undefinedVariable", undefined>;
6
+ export default _default;
@@ -0,0 +1,76 @@
1
+ import * as ts from "typescript";
2
+ import { typescriptLanguage } from "../language.js";
3
+ export default typescriptLanguage.createRule({
4
+ about: {
5
+ description: "Reports using variables that are not defined.",
6
+ id: "undefinedVariables",
7
+ preset: "untyped",
8
+ },
9
+ messages: {
10
+ undefinedVariable: {
11
+ primary: "Variable '{{ name }}' is used but was never defined.",
12
+ secondary: [
13
+ "Variables must be declared before they can be used.",
14
+ "Using undefined variables will cause a ReferenceError at runtime.",
15
+ ],
16
+ suggestions: [
17
+ "Declare the variable before using it, or check if the variable name is spelled correctly.",
18
+ ],
19
+ },
20
+ },
21
+ setup(context) {
22
+ return {
23
+ visitors: {
24
+ Identifier: (node) => {
25
+ if (ts.isVariableDeclaration(node.parent) &&
26
+ node.parent.name === node) {
27
+ return;
28
+ }
29
+ if (ts.isParameter(node.parent) && node.parent.name === node) {
30
+ return;
31
+ }
32
+ if ((ts.isFunctionDeclaration(node.parent) ||
33
+ ts.isClassDeclaration(node.parent) ||
34
+ ts.isInterfaceDeclaration(node.parent) ||
35
+ ts.isTypeAliasDeclaration(node.parent) ||
36
+ ts.isEnumDeclaration(node.parent) ||
37
+ ts.isModuleDeclaration(node.parent)) &&
38
+ node.parent.name === node) {
39
+ return;
40
+ }
41
+ if (ts.isImportSpecifier(node.parent) ||
42
+ ts.isImportClause(node.parent) ||
43
+ ts.isNamespaceImport(node.parent)) {
44
+ return;
45
+ }
46
+ if (ts.isPropertyAccessExpression(node.parent) &&
47
+ node.parent.name === node) {
48
+ return;
49
+ }
50
+ if (ts.isPropertyAssignment(node.parent) &&
51
+ node.parent.name === node) {
52
+ return;
53
+ }
54
+ if (ts.isTypeOfExpression(node.parent) &&
55
+ node.parent.expression === node) {
56
+ return;
57
+ }
58
+ // TODO: This rule is untyped, so it should use scope analysis
59
+ // https://github.com/JoshuaKGoldberg/flint/issues/400
60
+ if (!context.typeChecker.getSymbolAtLocation(node)) {
61
+ context.report({
62
+ data: {
63
+ name: node.text,
64
+ },
65
+ message: "undefinedVariable",
66
+ range: {
67
+ begin: node.getStart(context.sourceFile),
68
+ end: node.getEnd(),
69
+ },
70
+ });
71
+ }
72
+ },
73
+ },
74
+ };
75
+ },
76
+ });
@@ -0,0 +1 @@
1
+ export {};