@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,77 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { typescriptLanguage } from "../language.js";
4
+ import { getModifyingReferences } from "../utils/getModifyingReferences.js";
5
+
6
+ export default typescriptLanguage.createRule({
7
+ about: {
8
+ description: "Reports reassigning exception parameters in catch clauses.",
9
+ id: "exceptionAssignments",
10
+ preset: "logical",
11
+ },
12
+ messages: {
13
+ noExAssign: {
14
+ primary:
15
+ "Exception parameters in catch clauses should not be reassigned.",
16
+ secondary: [
17
+ "Reassigning an exception parameter can make debugging more difficult by obscuring the original error.",
18
+ "The exception parameter contains important information about what went wrong, and reassigning it can make it harder to understand the root cause of an error.",
19
+ ],
20
+ suggestions: [
21
+ "Use a different variable name for the new value instead of reassigning the exception parameter.",
22
+ ],
23
+ },
24
+ },
25
+ setup(context) {
26
+ function collectBindingElements(name: ts.BindingName): ts.Identifier[] {
27
+ const identifiers: ts.Identifier[] = [];
28
+
29
+ if (ts.isIdentifier(name)) {
30
+ identifiers.push(name);
31
+ } else if (
32
+ ts.isObjectBindingPattern(name) ||
33
+ ts.isArrayBindingPattern(name)
34
+ ) {
35
+ for (const element of name.elements) {
36
+ if (ts.isBindingElement(element)) {
37
+ identifiers.push(...collectBindingElements(element.name));
38
+ }
39
+ }
40
+ }
41
+
42
+ return identifiers;
43
+ }
44
+
45
+ return {
46
+ visitors: {
47
+ CatchClause: (node) => {
48
+ if (!node.variableDeclaration?.name) {
49
+ return;
50
+ }
51
+
52
+ const identifiers = collectBindingElements(
53
+ node.variableDeclaration.name,
54
+ );
55
+
56
+ for (const identifier of identifiers) {
57
+ const modifyingReferences = getModifyingReferences(
58
+ identifier,
59
+ context.sourceFile,
60
+ context.typeChecker,
61
+ );
62
+
63
+ for (const reference of modifyingReferences) {
64
+ context.report({
65
+ message: "noExAssign",
66
+ range: {
67
+ begin: reference.getStart(context.sourceFile),
68
+ end: reference.getEnd(),
69
+ },
70
+ });
71
+ }
72
+ }
73
+ },
74
+ },
75
+ };
76
+ },
77
+ });
@@ -0,0 +1,100 @@
1
+ import rule from "./forDirections.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ for (let i = 0; i < 10; i--) {}
9
+ `,
10
+ snapshot: `
11
+ for (let i = 0; i < 10; i--) {}
12
+ ~~~
13
+ The update moves the counter in the wrong direction for this loop condition.
14
+ `,
15
+ },
16
+ {
17
+ code: `
18
+ for (let index = 0; index < 10; index--) {}
19
+ `,
20
+ snapshot: `
21
+ for (let index = 0; index < 10; index--) {}
22
+ ~~~~~~~
23
+ The update moves the counter in the wrong direction for this loop condition.
24
+ `,
25
+ },
26
+ {
27
+ code: `
28
+ for (let index = 10; index >= 0; index++) {}
29
+ `,
30
+ snapshot: `
31
+ for (let index = 10; index >= 0; index++) {}
32
+ ~~~~~~~
33
+ The update moves the counter in the wrong direction for this loop condition.
34
+ `,
35
+ },
36
+ {
37
+ code: `
38
+ for (let index = 0; index > 10; index++) {}
39
+ `,
40
+ snapshot: `
41
+ for (let index = 0; index > 10; index++) {}
42
+ ~~~~~~~
43
+ The update moves the counter in the wrong direction for this loop condition.
44
+ `,
45
+ },
46
+ {
47
+ code: `
48
+ for (let index = 0; 10 > index; index--) {}
49
+ `,
50
+ snapshot: `
51
+ for (let index = 0; 10 > index; index--) {}
52
+ ~~~~~~~
53
+ The update moves the counter in the wrong direction for this loop condition.
54
+ `,
55
+ },
56
+ {
57
+ code: `
58
+ for (let index = 0; index < 10; index -= 1) {}
59
+ `,
60
+ snapshot: `
61
+ for (let index = 0; index < 10; index -= 1) {}
62
+ ~~~~~~~~~~
63
+ The update moves the counter in the wrong direction for this loop condition.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ for (let index = 10; index > 0; index += 1) {}
69
+ `,
70
+ snapshot: `
71
+ for (let index = 10; index > 0; index += 1) {}
72
+ ~~~~~~~~~~
73
+ The update moves the counter in the wrong direction for this loop condition.
74
+ `,
75
+ },
76
+ {
77
+ code: `
78
+ for (let index = 0; index < 10; index += -1) {}
79
+ `,
80
+ snapshot: `
81
+ for (let index = 0; index < 10; index += -1) {}
82
+ ~~~~~~~~~~~
83
+ The update moves the counter in the wrong direction for this loop condition.
84
+ `,
85
+ },
86
+ ],
87
+ valid: [
88
+ `for (let i = 0; i < 10; i++) { }`,
89
+ `for (let index = 0; index < 10; index++) { }`,
90
+ `for (let index = 10; index >= 0; index--) { }`,
91
+ `for (let index = 0; 10 > index; index++) { }`,
92
+ `for (let index = 10; 0 < index; index--) { }`,
93
+ `for (let index = 10; index >= 0; index += step) { }`,
94
+ `for (let index = 0; index <= 10; index -= 0) { }`,
95
+ `for (let index = 0; index < 10; index += 1) { }`,
96
+ `for (let index = 10; index > 0; index -= 1) { }`,
97
+ `for (let index = 0; index < 10; index += 2) { }`,
98
+ `for (let index = 10; index > 0; index -= 2) { }`,
99
+ ],
100
+ });
@@ -0,0 +1,163 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { getTSNodeRange } from "../getTSNodeRange.js";
4
+ import { typescriptLanguage } from "../language.js";
5
+
6
+ function getConditionDirection(condition: ts.Expression, counterName: string) {
7
+ if (!ts.isBinaryExpression(condition)) {
8
+ return undefined;
9
+ }
10
+
11
+ const leftName = getCounterName(condition.left);
12
+ const rightName = getCounterName(condition.right);
13
+
14
+ const isCounterLeft = leftName === counterName;
15
+ const isCounterRight = rightName === counterName;
16
+
17
+ if (!isCounterLeft && !isCounterRight) {
18
+ return undefined;
19
+ }
20
+
21
+ const { operatorToken } = condition;
22
+
23
+ if (isCounterLeft) {
24
+ if (
25
+ operatorToken.kind === ts.SyntaxKind.LessThanToken ||
26
+ operatorToken.kind === ts.SyntaxKind.LessThanEqualsToken
27
+ ) {
28
+ return 1;
29
+ }
30
+ if (
31
+ operatorToken.kind === ts.SyntaxKind.GreaterThanToken ||
32
+ operatorToken.kind === ts.SyntaxKind.GreaterThanEqualsToken
33
+ ) {
34
+ return -1;
35
+ }
36
+ } else {
37
+ if (
38
+ operatorToken.kind === ts.SyntaxKind.LessThanToken ||
39
+ operatorToken.kind === ts.SyntaxKind.LessThanEqualsToken
40
+ ) {
41
+ return -1;
42
+ }
43
+ if (
44
+ operatorToken.kind === ts.SyntaxKind.GreaterThanToken ||
45
+ operatorToken.kind === ts.SyntaxKind.GreaterThanEqualsToken
46
+ ) {
47
+ return 1;
48
+ }
49
+ }
50
+
51
+ return undefined;
52
+ }
53
+
54
+ function getCounterName(node: ts.Expression) {
55
+ return ts.isIdentifier(node) ? node.text : undefined;
56
+ }
57
+
58
+ function getUpdateDirection(update: ts.Expression) {
59
+ if (
60
+ ts.isPostfixUnaryExpression(update) ||
61
+ ts.isPrefixUnaryExpression(update)
62
+ ) {
63
+ switch (update.operator) {
64
+ case ts.SyntaxKind.MinusMinusToken:
65
+ return -1;
66
+ case ts.SyntaxKind.PlusPlusToken:
67
+ return 1;
68
+ default:
69
+ return undefined;
70
+ }
71
+ }
72
+
73
+ if (ts.isBinaryExpression(update)) {
74
+ const { operatorToken, right } = update;
75
+
76
+ if (
77
+ operatorToken.kind === ts.SyntaxKind.PlusEqualsToken ||
78
+ operatorToken.kind === ts.SyntaxKind.MinusEqualsToken
79
+ ) {
80
+ if (ts.isNumericLiteral(right)) {
81
+ const value = Number(right.text);
82
+ if (operatorToken.kind === ts.SyntaxKind.PlusEqualsToken) {
83
+ return value > 0 ? 1 : value < 0 ? -1 : 0;
84
+ }
85
+ return value > 0 ? -1 : value < 0 ? 1 : 0;
86
+ }
87
+
88
+ if (
89
+ ts.isPrefixUnaryExpression(right) &&
90
+ right.operator === ts.SyntaxKind.MinusToken &&
91
+ ts.isNumericLiteral(right.operand)
92
+ ) {
93
+ const value = Number(right.operand.text);
94
+ if (operatorToken.kind === ts.SyntaxKind.PlusEqualsToken) {
95
+ return value > 0 ? -1 : 1;
96
+ }
97
+ return value > 0 ? 1 : -1;
98
+ }
99
+ }
100
+ }
101
+
102
+ return undefined;
103
+ }
104
+
105
+ export default typescriptLanguage.createRule({
106
+ about: {
107
+ description:
108
+ "Reports for loops with counter variables that move in the wrong direction.",
109
+ id: "forDirections",
110
+ preset: "stylistic",
111
+ },
112
+ messages: {
113
+ wrongDirection: {
114
+ primary:
115
+ "The update moves the counter in the wrong direction for this loop condition.",
116
+ secondary: [
117
+ "A for loop with a counter that moves in the wrong direction relative to its stop condition will run infinitely or never execute.",
118
+ "When the counter increases but the condition expects it to decrease (or vice versa), the loop logic is incorrect.",
119
+ ],
120
+ suggestions: [
121
+ "Verify the loop counter update direction matches the condition.",
122
+ ],
123
+ },
124
+ },
125
+ setup(context) {
126
+ return {
127
+ visitors: {
128
+ ForStatement: (node) => {
129
+ if (
130
+ !node.condition ||
131
+ !node.incrementor ||
132
+ !node.initializer ||
133
+ !ts.isVariableDeclarationList(node.initializer)
134
+ ) {
135
+ return;
136
+ }
137
+
138
+ const declaration = node.initializer.declarations.at(0);
139
+ if (!declaration || !ts.isIdentifier(declaration.name)) {
140
+ return;
141
+ }
142
+
143
+ const updateDirection = getUpdateDirection(node.incrementor);
144
+ if (!updateDirection) {
145
+ return;
146
+ }
147
+
148
+ const conditionDirection = getConditionDirection(
149
+ node.condition,
150
+ declaration.name.text,
151
+ );
152
+
153
+ if (updateDirection !== conditionDirection) {
154
+ context.report({
155
+ message: "wrongDirection",
156
+ range: getTSNodeRange(node.incrementor, context.sourceFile),
157
+ });
158
+ }
159
+ },
160
+ },
161
+ };
162
+ },
163
+ });
@@ -0,0 +1,210 @@
1
+ import rule from "./functionAssignments.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ function myFunction() {}
9
+ myFunction = function() {};
10
+ `,
11
+ snapshot: `
12
+ function myFunction() {}
13
+ myFunction = function() {};
14
+ ~~~~~~~~~~
15
+ Variables declared with function declarations should not be reassigned.
16
+ `,
17
+ },
18
+ {
19
+ code: `
20
+ function myFunction() {}
21
+ myFunction = 123;
22
+ `,
23
+ snapshot: `
24
+ function myFunction() {}
25
+ myFunction = 123;
26
+ ~~~~~~~~~~
27
+ Variables declared with function declarations should not be reassigned.
28
+ `,
29
+ },
30
+ {
31
+ code: `
32
+ function myFunction() {}
33
+ myFunction += 1;
34
+ `,
35
+ snapshot: `
36
+ function myFunction() {}
37
+ myFunction += 1;
38
+ ~~~~~~~~~~
39
+ Variables declared with function declarations should not be reassigned.
40
+ `,
41
+ },
42
+ {
43
+ code: `
44
+ function myFunction() {}
45
+ myFunction++;
46
+ `,
47
+ snapshot: `
48
+ function myFunction() {}
49
+ myFunction++;
50
+ ~~~~~~~~~~
51
+ Variables declared with function declarations should not be reassigned.
52
+ `,
53
+ },
54
+ {
55
+ code: `
56
+ function myFunction() {}
57
+ ++myFunction;
58
+ `,
59
+ snapshot: `
60
+ function myFunction() {}
61
+ ++myFunction;
62
+ ~~~~~~~~~~
63
+ Variables declared with function declarations should not be reassigned.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ function myFunction() {
69
+ return 42;
70
+ }
71
+ myFunction = () => 100;
72
+ `,
73
+ snapshot: `
74
+ function myFunction() {
75
+ return 42;
76
+ }
77
+ myFunction = () => 100;
78
+ ~~~~~~~~~~
79
+ Variables declared with function declarations should not be reassigned.
80
+ `,
81
+ },
82
+ {
83
+ code: `
84
+ function outer() {
85
+ function inner() {}
86
+ inner = function() {};
87
+ }
88
+ `,
89
+ snapshot: `
90
+ function outer() {
91
+ function inner() {}
92
+ inner = function() {};
93
+ ~~~~~
94
+ Variables declared with function declarations should not be reassigned.
95
+ }
96
+ `,
97
+ },
98
+ {
99
+ code: `
100
+ function myFunction() {}
101
+ if (condition) {
102
+ myFunction = null;
103
+ }
104
+ `,
105
+ snapshot: `
106
+ function myFunction() {}
107
+ if (condition) {
108
+ myFunction = null;
109
+ ~~~~~~~~~~
110
+ Variables declared with function declarations should not be reassigned.
111
+ }
112
+ `,
113
+ },
114
+ {
115
+ code: `
116
+ function getValue() {
117
+ return 1;
118
+ }
119
+ getValue ??= function() { return 0; };
120
+ `,
121
+ snapshot: `
122
+ function getValue() {
123
+ return 1;
124
+ }
125
+ getValue ??= function() { return 0; };
126
+ ~~~~~~~~
127
+ Variables declared with function declarations should not be reassigned.
128
+ `,
129
+ },
130
+ {
131
+ code: `
132
+ function getValue() {
133
+ return true;
134
+ }
135
+ getValue &&= false;
136
+ `,
137
+ snapshot: `
138
+ function getValue() {
139
+ return true;
140
+ }
141
+ getValue &&= false;
142
+ ~~~~~~~~
143
+ Variables declared with function declarations should not be reassigned.
144
+ `,
145
+ },
146
+ {
147
+ code: `
148
+ function getValue() {
149
+ return false;
150
+ }
151
+ getValue ||= () => true;
152
+ `,
153
+ snapshot: `
154
+ function getValue() {
155
+ return false;
156
+ }
157
+ getValue ||= () => true;
158
+ ~~~~~~~~
159
+ Variables declared with function declarations should not be reassigned.
160
+ `,
161
+ },
162
+ {
163
+ code: `
164
+ function calculate(value: number): number {
165
+ return value * 2;
166
+ }
167
+ calculate = (value: number) => value * 3;
168
+ `,
169
+ snapshot: `
170
+ function calculate(value: number): number {
171
+ return value * 2;
172
+ }
173
+ calculate = (value: number) => value * 3;
174
+ ~~~~~~~~~
175
+ Variables declared with function declarations should not be reassigned.
176
+ `,
177
+ },
178
+ ],
179
+ valid: [
180
+ `function myFunction() { return 42; }`,
181
+ `function myFunction() {} const result = myFunction();`,
182
+ `const myFunction = function() {}; myFunction = () => {};`,
183
+ `const myFunction = () => {}; myFunction = function() {};`,
184
+ `let myFunction = function() {}; myFunction = () => {};`,
185
+ `function outer() { const inner = function() {}; inner = () => {}; }`,
186
+ `
187
+ function outer() {
188
+ const myFunction = "shadowed variable";
189
+ myFunction = "reassigning shadowed variable is ok";
190
+ }
191
+ `,
192
+ `
193
+ let myFunction = "outer variable";
194
+ function scope() {
195
+ function myFunction() {}
196
+ console.log(myFunction);
197
+ }
198
+ myFunction = "reassigning outer is ok";
199
+ `,
200
+ `function myFunction() {} const copy = myFunction;`,
201
+ `function myFunction() {} if (condition) { callWith(myFunction); }`,
202
+ `
203
+ function myFunction() {
204
+ return function nested() {
205
+ return 42;
206
+ };
207
+ }
208
+ `,
209
+ ],
210
+ });
@@ -0,0 +1,51 @@
1
+ import { typescriptLanguage } from "../language.js";
2
+ import { getModifyingReferences } from "../utils/getModifyingReferences.js";
3
+
4
+ export default typescriptLanguage.createRule({
5
+ about: {
6
+ description:
7
+ "Reports reassigning variables declared with function declarations.",
8
+ id: "functionAssignments",
9
+ preset: "untyped",
10
+ },
11
+ messages: {
12
+ noFunctionAssignment: {
13
+ primary:
14
+ "Variables declared with function declarations should not be reassigned.",
15
+ secondary: [
16
+ "Reassigning a function declaration can make code harder to understand and maintain.",
17
+ "Function declarations are hoisted to the top of their scope, and reassigning them can lead to unexpected behavior.",
18
+ ],
19
+ suggestions: [
20
+ "Use a function expression or const variable instead if you need to reassign the function.",
21
+ ],
22
+ },
23
+ },
24
+ setup(context) {
25
+ return {
26
+ visitors: {
27
+ FunctionDeclaration: (node) => {
28
+ if (!node.name) {
29
+ return;
30
+ }
31
+
32
+ const modifyingReferences = getModifyingReferences(
33
+ node.name,
34
+ context.sourceFile,
35
+ context.typeChecker,
36
+ );
37
+
38
+ for (const reference of modifyingReferences) {
39
+ context.report({
40
+ message: "noFunctionAssignment",
41
+ range: {
42
+ begin: reference.getStart(context.sourceFile),
43
+ end: reference.getEnd(),
44
+ },
45
+ });
46
+ }
47
+ },
48
+ },
49
+ };
50
+ },
51
+ });
@@ -0,0 +1,116 @@
1
+ import rule from "./functionNewCalls.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ const fn = new Function("a", "b", "return a + b");
9
+ `,
10
+ snapshot: `
11
+ const fn = new Function("a", "b", "return a + b");
12
+ ~~~~~~~~
13
+ Dynamically creating functions with the Function constructor is insecure and slow.
14
+ `,
15
+ },
16
+ {
17
+ code: `
18
+ const fn = Function("a", "return a");
19
+ `,
20
+ snapshot: `
21
+ const fn = Function("a", "return a");
22
+ ~~~~~~~~
23
+ Dynamically creating functions with the Function constructor is insecure and slow.
24
+ `,
25
+ },
26
+ {
27
+ code: `
28
+ const fn = new Function("return 1");
29
+ `,
30
+ snapshot: `
31
+ const fn = new Function("return 1");
32
+ ~~~~~~~~
33
+ Dynamically creating functions with the Function constructor is insecure and slow.
34
+ `,
35
+ },
36
+ {
37
+ code: `
38
+ const fn = Function();
39
+ `,
40
+ snapshot: `
41
+ const fn = Function();
42
+ ~~~~~~~~
43
+ Dynamically creating functions with the Function constructor is insecure and slow.
44
+ `,
45
+ },
46
+ {
47
+ code: `
48
+ const fn = new globalThis.Function("return 1");
49
+ `,
50
+ snapshot: `
51
+ const fn = new globalThis.Function("return 1");
52
+ ~~~~~~~~~~~~~~~~~~~
53
+ Dynamically creating functions with the Function constructor is insecure and slow.
54
+ `,
55
+ },
56
+ {
57
+ code: `
58
+ const fn = globalThis.Function("return 1");
59
+ `,
60
+ snapshot: `
61
+ const fn = globalThis.Function("return 1");
62
+ ~~~~~~~~~~~~~~~~~~~
63
+ Dynamically creating functions with the Function constructor is insecure and slow.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ const fn = new window.Function("return 1");
69
+ `,
70
+ snapshot: `
71
+ const fn = new window.Function("return 1");
72
+ ~~~~~~~~~~~~~~~
73
+ Dynamically creating functions with the Function constructor is insecure and slow.
74
+ `,
75
+ },
76
+ {
77
+ code: `
78
+ const fn = window.Function("return 1");
79
+ `,
80
+ snapshot: `
81
+ const fn = window.Function("return 1");
82
+ ~~~~~~~~~~~~~~~
83
+ Dynamically creating functions with the Function constructor is insecure and slow.
84
+ `,
85
+ },
86
+ {
87
+ code: `
88
+ const result = new Function("a", "b", "return a + b")(1, 2);
89
+ `,
90
+ snapshot: `
91
+ const result = new Function("a", "b", "return a + b")(1, 2);
92
+ ~~~~~~~~
93
+ Dynamically creating functions with the Function constructor is insecure and slow.
94
+ `,
95
+ },
96
+ {
97
+ code: `
98
+ const CustomFunction = Function;
99
+ const fn = new CustomFunction();
100
+ `,
101
+ snapshot: `
102
+ const CustomFunction = Function;
103
+ const fn = new CustomFunction();
104
+ ~~~~~~~~~~~~~~
105
+ Dynamically creating functions with the Function constructor is insecure and slow.
106
+ `,
107
+ },
108
+ ],
109
+ valid: [
110
+ `const fn = function(a, b) { return a + b; };`,
111
+ `const fn = (a, b) => a + b;`,
112
+ `function add(a, b) { return a + b; }`,
113
+ `class MyFunction {}`,
114
+ `const fn = new MyFunction();`,
115
+ ],
116
+ });