@flint.fyi/ts 0.14.1 → 0.14.3

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 (221) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/lib/index.d.ts +1 -0
  3. package/lib/index.js +1 -0
  4. package/lib/nodes.d.ts +1 -0
  5. package/lib/plugin.d.ts +132 -4
  6. package/lib/plugin.js +69 -1
  7. package/lib/rules/asyncPromiseExecutors.d.ts +6 -0
  8. package/lib/rules/asyncPromiseExecutors.js +51 -0
  9. package/lib/rules/asyncPromiseExecutors.test.d.ts +1 -0
  10. package/lib/rules/asyncPromiseExecutors.test.js +71 -0
  11. package/lib/rules/caseDeclarations.d.ts +6 -0
  12. package/lib/rules/caseDeclarations.js +53 -0
  13. package/lib/rules/caseDeclarations.test.d.ts +1 -0
  14. package/lib/rules/caseDeclarations.test.js +227 -0
  15. package/lib/rules/chainedAssignments.d.ts +6 -0
  16. package/lib/rules/chainedAssignments.js +48 -0
  17. package/lib/rules/chainedAssignments.test.d.ts +1 -0
  18. package/lib/rules/chainedAssignments.test.js +238 -0
  19. package/lib/rules/classAssignments.d.ts +6 -0
  20. package/lib/rules/classAssignments.js +42 -0
  21. package/lib/rules/classAssignments.test.d.ts +1 -0
  22. package/lib/rules/classAssignments.test.js +156 -0
  23. package/lib/rules/constantAssignments.d.ts +6 -0
  24. package/lib/rules/constantAssignments.js +61 -0
  25. package/lib/rules/constantAssignments.test.d.ts +1 -0
  26. package/lib/rules/constantAssignments.test.js +221 -0
  27. package/lib/rules/constructorReturns.d.ts +6 -0
  28. package/lib/rules/constructorReturns.js +52 -0
  29. package/lib/rules/constructorReturns.test.d.ts +1 -0
  30. package/lib/rules/constructorReturns.test.js +96 -0
  31. package/lib/rules/debuggerStatements.d.ts +6 -0
  32. package/lib/rules/debuggerStatements.js +43 -0
  33. package/lib/rules/debuggerStatements.test.d.ts +1 -0
  34. package/lib/rules/debuggerStatements.test.js +80 -0
  35. package/lib/rules/defaultCaseLast.d.ts +6 -0
  36. package/lib/rules/defaultCaseLast.js +43 -0
  37. package/lib/rules/defaultCaseLast.test.d.ts +1 -0
  38. package/lib/rules/defaultCaseLast.test.js +195 -0
  39. package/lib/rules/duplicateArguments.d.ts +6 -0
  40. package/lib/rules/duplicateArguments.js +51 -0
  41. package/lib/rules/duplicateArguments.test.d.ts +1 -0
  42. package/lib/rules/duplicateArguments.test.js +139 -0
  43. package/lib/rules/emptyDestructures.d.ts +6 -0
  44. package/lib/rules/emptyDestructures.js +40 -0
  45. package/lib/rules/emptyDestructures.test.d.ts +1 -0
  46. package/lib/rules/emptyDestructures.test.js +186 -0
  47. package/lib/rules/emptyStaticBlocks.d.ts +6 -0
  48. package/lib/rules/emptyStaticBlocks.js +54 -0
  49. package/lib/rules/emptyStaticBlocks.test.d.ts +1 -0
  50. package/lib/rules/emptyStaticBlocks.test.js +167 -0
  51. package/lib/rules/exceptionAssignments.d.ts +6 -0
  52. package/lib/rules/exceptionAssignments.js +61 -0
  53. package/lib/rules/exceptionAssignments.test.d.ts +1 -0
  54. package/lib/rules/exceptionAssignments.test.js +264 -0
  55. package/lib/rules/functionAssignments.d.ts +6 -0
  56. package/lib/rules/functionAssignments.js +42 -0
  57. package/lib/rules/functionAssignments.test.d.ts +1 -0
  58. package/lib/rules/functionAssignments.test.js +209 -0
  59. package/lib/rules/generatorFunctionYields.d.ts +6 -0
  60. package/lib/rules/generatorFunctionYields.js +57 -0
  61. package/lib/rules/generatorFunctionYields.test.d.ts +1 -0
  62. package/lib/rules/generatorFunctionYields.test.js +151 -0
  63. package/lib/rules/globalAssignments.d.ts +6 -0
  64. package/lib/rules/globalAssignments.js +59 -0
  65. package/lib/rules/globalAssignments.test.d.ts +1 -0
  66. package/lib/rules/globalAssignments.test.js +215 -0
  67. package/lib/rules/globalObjectCalls.d.ts +6 -0
  68. package/lib/rules/globalObjectCalls.js +43 -0
  69. package/lib/rules/globalObjectCalls.test.d.ts +1 -0
  70. package/lib/rules/globalObjectCalls.test.js +93 -0
  71. package/lib/rules/negativeZeroComparisons.d.ts +6 -0
  72. package/lib/rules/negativeZeroComparisons.js +83 -0
  73. package/lib/rules/negativeZeroComparisons.test.d.ts +1 -0
  74. package/lib/rules/negativeZeroComparisons.test.js +209 -0
  75. package/lib/rules/newExpressions.d.ts +6 -0
  76. package/lib/rules/newExpressions.js +54 -0
  77. package/lib/rules/newExpressions.test.d.ts +1 -0
  78. package/lib/rules/newExpressions.test.js +79 -0
  79. package/lib/rules/newNativeNonConstructors.d.ts +6 -0
  80. package/lib/rules/newNativeNonConstructors.js +49 -0
  81. package/lib/rules/newNativeNonConstructors.test.d.ts +1 -0
  82. package/lib/rules/newNativeNonConstructors.test.js +94 -0
  83. package/lib/rules/objectProto.d.ts +6 -0
  84. package/lib/rules/objectProto.js +54 -0
  85. package/lib/rules/objectProto.test.d.ts +1 -0
  86. package/lib/rules/objectProto.test.js +96 -0
  87. package/lib/rules/octalEscapes.d.ts +6 -0
  88. package/lib/rules/octalEscapes.js +71 -0
  89. package/lib/rules/octalEscapes.test.d.ts +1 -0
  90. package/lib/rules/octalEscapes.test.js +213 -0
  91. package/lib/rules/octalNumbers.d.ts +6 -0
  92. package/lib/rules/octalNumbers.js +68 -0
  93. package/lib/rules/octalNumbers.test.d.ts +1 -0
  94. package/lib/rules/octalNumbers.test.js +245 -0
  95. package/lib/rules/returnAssignments.d.ts +6 -0
  96. package/lib/rules/returnAssignments.js +50 -0
  97. package/lib/rules/returnAssignments.test.d.ts +1 -0
  98. package/lib/rules/returnAssignments.test.js +185 -0
  99. package/lib/rules/sparseArrays.d.ts +6 -0
  100. package/lib/rules/sparseArrays.js +54 -0
  101. package/lib/rules/sparseArrays.test.d.ts +1 -0
  102. package/lib/rules/sparseArrays.test.js +110 -0
  103. package/lib/rules/symbolDescriptions.d.ts +6 -0
  104. package/lib/rules/symbolDescriptions.js +41 -0
  105. package/lib/rules/symbolDescriptions.test.d.ts +1 -0
  106. package/lib/rules/symbolDescriptions.test.js +89 -0
  107. package/lib/rules/unassignedVariables.d.ts +6 -0
  108. package/lib/rules/unassignedVariables.js +55 -0
  109. package/lib/rules/unassignedVariables.test.d.ts +1 -0
  110. package/lib/rules/unassignedVariables.test.js +72 -0
  111. package/lib/rules/undefinedVariables.d.ts +6 -0
  112. package/lib/rules/undefinedVariables.js +76 -0
  113. package/lib/rules/undefinedVariables.test.d.ts +1 -0
  114. package/lib/rules/undefinedVariables.test.js +80 -0
  115. package/lib/rules/unicodeBOMs.d.ts +6 -0
  116. package/lib/rules/unicodeBOMs.js +41 -0
  117. package/lib/rules/unicodeBOMs.test.d.ts +1 -0
  118. package/lib/rules/unicodeBOMs.test.js +65 -0
  119. package/lib/rules/unnecessaryCatches.d.ts +6 -0
  120. package/lib/rules/unnecessaryCatches.js +68 -0
  121. package/lib/rules/unnecessaryCatches.test.d.ts +1 -0
  122. package/lib/rules/unnecessaryCatches.test.js +210 -0
  123. package/lib/rules/unsafeNegations.d.ts +6 -0
  124. package/lib/rules/unsafeNegations.js +50 -0
  125. package/lib/rules/unsafeNegations.test.d.ts +1 -0
  126. package/lib/rules/unsafeNegations.test.js +63 -0
  127. package/lib/rules/variableDeletions.d.ts +6 -0
  128. package/lib/rules/variableDeletions.js +39 -0
  129. package/lib/rules/variableDeletions.test.d.ts +1 -0
  130. package/lib/rules/variableDeletions.test.js +75 -0
  131. package/lib/rules/voidOperator.d.ts +6 -0
  132. package/lib/rules/voidOperator.js +36 -0
  133. package/lib/rules/voidOperator.test.d.ts +1 -0
  134. package/lib/rules/voidOperator.test.js +98 -0
  135. package/lib/utils/declarationsIncludeGlobal.d.ts +2 -0
  136. package/lib/utils/declarationsIncludeGlobal.js +7 -0
  137. package/lib/utils/getModifyingReferences.d.ts +8 -0
  138. package/lib/utils/getModifyingReferences.js +42 -0
  139. package/lib/utils/isGlobalDeclaration.d.ts +2 -0
  140. package/lib/utils/isGlobalDeclaration.js +5 -0
  141. package/lib/utils/isGlobalVariable.d.ts +8 -0
  142. package/lib/utils/isGlobalVariable.js +21 -0
  143. package/lib/utils/unwrapParentParenthesizedExpressions.d.ts +2 -0
  144. package/lib/utils/unwrapParentParenthesizedExpressions.js +6 -0
  145. package/lib/utils/unwrapParenthesizedExpression.d.ts +2 -0
  146. package/lib/utils/unwrapParenthesizedExpression.js +6 -0
  147. package/package.json +1 -1
  148. package/src/index.ts +1 -0
  149. package/src/nodes.ts +1 -0
  150. package/src/plugin.ts +69 -1
  151. package/src/rules/asyncPromiseExecutors.test.ts +72 -0
  152. package/src/rules/asyncPromiseExecutors.ts +61 -0
  153. package/src/rules/caseDeclarations.test.ts +228 -0
  154. package/src/rules/caseDeclarations.ts +70 -0
  155. package/src/rules/chainedAssignments.test.ts +239 -0
  156. package/src/rules/chainedAssignments.ts +55 -0
  157. package/src/rules/classAssignments.test.ts +157 -0
  158. package/src/rules/classAssignments.ts +50 -0
  159. package/src/rules/constantAssignments.test.ts +222 -0
  160. package/src/rules/constantAssignments.ts +78 -0
  161. package/src/rules/constructorReturns.test.ts +97 -0
  162. package/src/rules/constructorReturns.ts +59 -0
  163. package/src/rules/debuggerStatements.test.ts +81 -0
  164. package/src/rules/debuggerStatements.ts +45 -0
  165. package/src/rules/defaultCaseLast.test.ts +196 -0
  166. package/src/rules/defaultCaseLast.ts +54 -0
  167. package/src/rules/duplicateArguments.test.ts +140 -0
  168. package/src/rules/duplicateArguments.ts +58 -0
  169. package/src/rules/emptyDestructures.test.ts +187 -0
  170. package/src/rules/emptyDestructures.ts +48 -0
  171. package/src/rules/emptyStaticBlocks.test.ts +168 -0
  172. package/src/rules/emptyStaticBlocks.ts +60 -0
  173. package/src/rules/exceptionAssignments.test.ts +265 -0
  174. package/src/rules/exceptionAssignments.ts +77 -0
  175. package/src/rules/functionAssignments.test.ts +210 -0
  176. package/src/rules/functionAssignments.ts +51 -0
  177. package/src/rules/generatorFunctionYields.test.ts +152 -0
  178. package/src/rules/generatorFunctionYields.ts +71 -0
  179. package/src/rules/globalAssignments.test.ts +216 -0
  180. package/src/rules/globalAssignments.ts +67 -0
  181. package/src/rules/globalObjectCalls.test.ts +94 -0
  182. package/src/rules/globalObjectCalls.ts +52 -0
  183. package/src/rules/negativeZeroComparisons.test.ts +210 -0
  184. package/src/rules/negativeZeroComparisons.ts +106 -0
  185. package/src/rules/newExpressions.test.ts +80 -0
  186. package/src/rules/newExpressions.ts +67 -0
  187. package/src/rules/newNativeNonConstructors.test.ts +95 -0
  188. package/src/rules/newNativeNonConstructors.ts +59 -0
  189. package/src/rules/objectProto.test.ts +97 -0
  190. package/src/rules/objectProto.ts +62 -0
  191. package/src/rules/octalEscapes.test.ts +214 -0
  192. package/src/rules/octalEscapes.ts +85 -0
  193. package/src/rules/octalNumbers.test.ts +246 -0
  194. package/src/rules/octalNumbers.ts +75 -0
  195. package/src/rules/returnAssignments.test.ts +186 -0
  196. package/src/rules/returnAssignments.ts +57 -0
  197. package/src/rules/sparseArrays.test.ts +111 -0
  198. package/src/rules/sparseArrays.ts +61 -0
  199. package/src/rules/symbolDescriptions.test.ts +90 -0
  200. package/src/rules/symbolDescriptions.ts +47 -0
  201. package/src/rules/unassignedVariables.test.ts +73 -0
  202. package/src/rules/unassignedVariables.ts +69 -0
  203. package/src/rules/undefinedVariables.test.ts +81 -0
  204. package/src/rules/undefinedVariables.ts +97 -0
  205. package/src/rules/unicodeBOMs.test.ts +66 -0
  206. package/src/rules/unicodeBOMs.ts +44 -0
  207. package/src/rules/unnecessaryCatches.test.ts +211 -0
  208. package/src/rules/unnecessaryCatches.ts +84 -0
  209. package/src/rules/unsafeNegations.test.ts +64 -0
  210. package/src/rules/unsafeNegations.ts +59 -0
  211. package/src/rules/variableDeletions.test.ts +76 -0
  212. package/src/rules/variableDeletions.ts +43 -0
  213. package/src/rules/voidOperator.test.ts +99 -0
  214. package/src/rules/voidOperator.ts +38 -0
  215. package/src/utils/declarationsIncludeGlobal.ts +11 -0
  216. package/src/utils/getModifyingReferences.ts +58 -0
  217. package/src/utils/isGlobalDeclaration.ts +12 -0
  218. package/src/utils/isGlobalVariable.ts +30 -0
  219. package/src/utils/unwrapParentParenthesizedExpressions.ts +7 -0
  220. package/src/utils/unwrapParenthesizedExpression.ts +9 -0
  221. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,216 @@
1
+ import rule from "./globalAssignments.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ undefined = 1;
9
+ `,
10
+ snapshot: `
11
+ undefined = 1;
12
+ ~~~~~~~~~
13
+ Read-only global variables should not be reassigned or modified.
14
+ `,
15
+ },
16
+ {
17
+ code: `
18
+ NaN = 42;
19
+ `,
20
+ snapshot: `
21
+ NaN = 42;
22
+ ~~~
23
+ Read-only global variables should not be reassigned or modified.
24
+ `,
25
+ },
26
+ {
27
+ code: `
28
+ Infinity = 100;
29
+ `,
30
+ snapshot: `
31
+ Infinity = 100;
32
+ ~~~~~~~~
33
+ Read-only global variables should not be reassigned or modified.
34
+ `,
35
+ },
36
+ {
37
+ code: `
38
+ Object = null;
39
+ `,
40
+ snapshot: `
41
+ Object = null;
42
+ ~~~~~~
43
+ Read-only global variables should not be reassigned or modified.
44
+ `,
45
+ },
46
+ {
47
+ code: `
48
+ Array = function() {};
49
+ `,
50
+ snapshot: `
51
+ Array = function() {};
52
+ ~~~~~
53
+ Read-only global variables should not be reassigned or modified.
54
+ `,
55
+ },
56
+ {
57
+ code: `
58
+ String = "test";
59
+ `,
60
+ snapshot: `
61
+ String = "test";
62
+ ~~~~~~
63
+ Read-only global variables should not be reassigned or modified.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ Number = 123;
69
+ `,
70
+ snapshot: `
71
+ Number = 123;
72
+ ~~~~~~
73
+ Read-only global variables should not be reassigned or modified.
74
+ `,
75
+ },
76
+ {
77
+ code: `
78
+ Boolean = true;
79
+ `,
80
+ snapshot: `
81
+ Boolean = true;
82
+ ~~~~~~~
83
+ Read-only global variables should not be reassigned or modified.
84
+ `,
85
+ },
86
+ {
87
+ code: `
88
+ Math = {};
89
+ `,
90
+ snapshot: `
91
+ Math = {};
92
+ ~~~~
93
+ Read-only global variables should not be reassigned or modified.
94
+ `,
95
+ },
96
+ {
97
+ code: `
98
+ JSON = null;
99
+ `,
100
+ snapshot: `
101
+ JSON = null;
102
+ ~~~~
103
+ Read-only global variables should not be reassigned or modified.
104
+ `,
105
+ },
106
+ {
107
+ code: `
108
+ window = {};
109
+ `,
110
+ snapshot: `
111
+ window = {};
112
+ ~~~~~~
113
+ Read-only global variables should not be reassigned or modified.
114
+ `,
115
+ },
116
+ {
117
+ code: `
118
+ undefined += 1;
119
+ `,
120
+ snapshot: `
121
+ undefined += 1;
122
+ ~~~~~~~~~
123
+ Read-only global variables should not be reassigned or modified.
124
+ `,
125
+ },
126
+ {
127
+ code: `
128
+ NaN *= 2;
129
+ `,
130
+ snapshot: `
131
+ NaN *= 2;
132
+ ~~~
133
+ Read-only global variables should not be reassigned or modified.
134
+ `,
135
+ },
136
+ {
137
+ code: `
138
+ ++undefined;
139
+ `,
140
+ snapshot: `
141
+ ++undefined;
142
+ ~~~~~~~~~
143
+ Read-only global variables should not be reassigned or modified.
144
+ `,
145
+ },
146
+ {
147
+ code: `
148
+ NaN++;
149
+ `,
150
+ snapshot: `
151
+ NaN++;
152
+ ~~~
153
+ Read-only global variables should not be reassigned or modified.
154
+ `,
155
+ },
156
+ {
157
+ code: `
158
+ --Infinity;
159
+ `,
160
+ snapshot: `
161
+ --Infinity;
162
+ ~~~~~~~~
163
+ Read-only global variables should not be reassigned or modified.
164
+ `,
165
+ },
166
+ {
167
+ code: `
168
+ Object--;
169
+ `,
170
+ snapshot: `
171
+ Object--;
172
+ ~~~~~~
173
+ Read-only global variables should not be reassigned or modified.
174
+ `,
175
+ },
176
+ ],
177
+ valid: [
178
+ `let undefined = 1;`,
179
+ `const NaN = 42;`,
180
+ `var Infinity = 100;`,
181
+ `function Object() {}`,
182
+ `let value = undefined;`,
183
+ `const result = NaN;`,
184
+ `if (value === undefined) {}`,
185
+ `const obj = { undefined: 1 };`,
186
+ `obj.undefined = 2;`,
187
+ `const custom = { NaN: 42 }; custom.NaN = 100;`,
188
+ `let myVar = 5; myVar = 10;`,
189
+ `const data = { value: 1 }; data.value = 2;`,
190
+ `let counter = 0; counter++;`,
191
+ `let index = 10; --index;`,
192
+ `
193
+ function test() {
194
+ let undefined = 5;
195
+ undefined = 10;
196
+ }
197
+ `,
198
+ `
199
+ const obj = {
200
+ undefined: 1,
201
+ NaN: 2,
202
+ };
203
+ obj.undefined = 5;
204
+ `,
205
+ `
206
+ const fn = (undefined: number) => {
207
+ undefined = 10;
208
+ };
209
+ `,
210
+ `
211
+ function test(NaN: string) {
212
+ NaN = "updated";
213
+ }
214
+ `,
215
+ ],
216
+ });
@@ -0,0 +1,67 @@
1
+ import * as tsutils from "ts-api-utils";
2
+ import * as ts from "typescript";
3
+
4
+ import { getTSNodeRange } from "../getTSNodeRange.js";
5
+ import { typescriptLanguage } from "../language.js";
6
+ import { isGlobalVariable } from "../utils/isGlobalVariable.js";
7
+
8
+ export default typescriptLanguage.createRule({
9
+ about: {
10
+ description:
11
+ "Reports attempting to assign to read-only global variables such as undefined, NaN, Infinity, Object, etc.",
12
+ id: "globalAssignments",
13
+ preset: "untyped",
14
+ },
15
+ messages: {
16
+ noGlobalAssign: {
17
+ primary:
18
+ "Read-only global variables should not be reassigned or modified.",
19
+ secondary: [
20
+ "Global variables like undefined, NaN, Infinity, and built-in objects like Object and Array are read-only and should not be modified.",
21
+ "Attempting to reassign these globals can lead to confusing behavior and potential bugs in your code.",
22
+ "In strict mode, reassigning these globals will throw a TypeError at runtime.",
23
+ ],
24
+ suggestions: [
25
+ "Use a different variable name instead of shadowing or reassigning globals.",
26
+ "If you need a similar name, consider using a more specific name that doesn't conflict with global variables.",
27
+ ],
28
+ },
29
+ },
30
+ setup(context) {
31
+ return {
32
+ visitors: {
33
+ BinaryExpression: (node) => {
34
+ if (
35
+ tsutils.isAssignmentKind(node.operatorToken.kind) &&
36
+ isGlobalVariable(node.left, context.typeChecker)
37
+ ) {
38
+ context.report({
39
+ message: "noGlobalAssign",
40
+ range: getTSNodeRange(node.left, context.sourceFile),
41
+ });
42
+ }
43
+ },
44
+ PostfixUnaryExpression: (node) => {
45
+ if (isGlobalVariable(node.operand, context.typeChecker)) {
46
+ context.report({
47
+ message: "noGlobalAssign",
48
+ range: getTSNodeRange(node.operand, context.sourceFile),
49
+ });
50
+ }
51
+ },
52
+ PrefixUnaryExpression: (node) => {
53
+ if (
54
+ (node.operator === ts.SyntaxKind.PlusPlusToken ||
55
+ node.operator === ts.SyntaxKind.MinusMinusToken) &&
56
+ isGlobalVariable(node.operand, context.typeChecker)
57
+ ) {
58
+ context.report({
59
+ message: "noGlobalAssign",
60
+ range: getTSNodeRange(node.operand, context.sourceFile),
61
+ });
62
+ }
63
+ },
64
+ },
65
+ };
66
+ },
67
+ });
@@ -0,0 +1,94 @@
1
+ import rule from "./globalObjectCalls.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ const result = Math();
9
+ `,
10
+ snapshot: `
11
+ const result = Math();
12
+ ~~~~
13
+ Math is not a function and cannot be called directly.
14
+ `,
15
+ },
16
+ {
17
+ code: `
18
+ const data = JSON();
19
+ `,
20
+ snapshot: `
21
+ const data = JSON();
22
+ ~~~~
23
+ JSON is not a function and cannot be called directly.
24
+ `,
25
+ },
26
+ {
27
+ code: `
28
+ const reflected = Reflect();
29
+ `,
30
+ snapshot: `
31
+ const reflected = Reflect();
32
+ ~~~~~~~
33
+ Reflect is not a function and cannot be called directly.
34
+ `,
35
+ },
36
+ {
37
+ code: `
38
+ const atomic = Atomics();
39
+ `,
40
+ snapshot: `
41
+ const atomic = Atomics();
42
+ ~~~~~~~
43
+ Atomics is not a function and cannot be called directly.
44
+ `,
45
+ },
46
+ {
47
+ code: `
48
+ const instance = new Math();
49
+ `,
50
+ snapshot: `
51
+ const instance = new Math();
52
+ ~~~~
53
+ Math is not a function and cannot be called directly.
54
+ `,
55
+ },
56
+ {
57
+ code: `
58
+ const instance = new JSON();
59
+ `,
60
+ snapshot: `
61
+ const instance = new JSON();
62
+ ~~~~
63
+ JSON is not a function and cannot be called directly.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ const instance = new Reflect();
69
+ `,
70
+ snapshot: `
71
+ const instance = new Reflect();
72
+ ~~~~~~~
73
+ Reflect is not a function and cannot be called directly.
74
+ `,
75
+ },
76
+ {
77
+ code: `
78
+ const instance = new Atomics();
79
+ `,
80
+ snapshot: `
81
+ const instance = new Atomics();
82
+ ~~~~~~~
83
+ Atomics is not a function and cannot be called directly.
84
+ `,
85
+ },
86
+ ],
87
+ valid: [
88
+ `const value = Math.abs(-5);`,
89
+ `const parsed = JSON.parse("{}");`,
90
+ `const stringified = JSON.stringify({});`,
91
+ `const keys = Reflect.ownKeys({});`,
92
+ `const result = Atomics.add(buffer, 0, 1);`,
93
+ ],
94
+ });
@@ -0,0 +1,52 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { getTSNodeRange } from "../getTSNodeRange.js";
4
+ import { typescriptLanguage } from "../language.js";
5
+
6
+ const globalObjects = new Set(["Atomics", "JSON", "Math", "Reflect"]);
7
+
8
+ export default typescriptLanguage.createRule({
9
+ about: {
10
+ description:
11
+ "Reports calling global objects like Math, JSON, or Reflect as functions.",
12
+ id: "globalObjectCalls",
13
+ preset: "untyped",
14
+ },
15
+ messages: {
16
+ noGlobalObjectCall: {
17
+ primary: "{{ name }} is not a function and cannot be called directly.",
18
+ secondary: [
19
+ "{{ name }} is a built-in global object that provides utility methods and properties.",
20
+ "It is not a constructor or function and cannot be called or instantiated.",
21
+ ],
22
+ suggestions: [
23
+ "Use the static methods available on {{ name }} instead (e.g., {{ name }}.methodName()).",
24
+ ],
25
+ },
26
+ },
27
+ setup(context) {
28
+ function reportGlobalObjectCall(
29
+ expression: ts.Expression,
30
+ name: string,
31
+ ): void {
32
+ context.report({
33
+ data: { name },
34
+ message: "noGlobalObjectCall",
35
+ range: getTSNodeRange(expression, context.sourceFile),
36
+ });
37
+ }
38
+
39
+ function checkNode({ expression }: ts.CallExpression | ts.NewExpression) {
40
+ if (ts.isIdentifier(expression) && globalObjects.has(expression.text)) {
41
+ reportGlobalObjectCall(expression, expression.text);
42
+ }
43
+ }
44
+
45
+ return {
46
+ visitors: {
47
+ CallExpression: checkNode,
48
+ NewExpression: checkNode,
49
+ },
50
+ };
51
+ },
52
+ });
@@ -0,0 +1,210 @@
1
+ import rule from "./negativeZeroComparisons.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ if (value === -0) {}
9
+ `,
10
+ snapshot: `
11
+ if (value === -0) {}
12
+ ~~~~~~~~~~~~
13
+ Comparisons with -0 using === do not distinguish between -0 and +0.
14
+ `,
15
+ suggestions: [
16
+ {
17
+ id: "useObjectIs",
18
+ updated: `
19
+ if (Object.is(value, -0)) {}
20
+ `,
21
+ },
22
+ ],
23
+ },
24
+ {
25
+ code: `
26
+ if (-0 === value) {}
27
+ `,
28
+ snapshot: `
29
+ if (-0 === value) {}
30
+ ~~~~~~~~~~~~
31
+ Comparisons with -0 using === do not distinguish between -0 and +0.
32
+ `,
33
+ suggestions: [
34
+ {
35
+ id: "useObjectIs",
36
+ updated: `
37
+ if (Object.is(-0, value)) {}
38
+ `,
39
+ },
40
+ ],
41
+ },
42
+ {
43
+ code: `
44
+ if (value == -0) {}
45
+ `,
46
+ snapshot: `
47
+ if (value == -0) {}
48
+ ~~~~~~~~~~~
49
+ Comparisons with -0 using == do not distinguish between -0 and +0.
50
+ `,
51
+ suggestions: [
52
+ {
53
+ id: "useObjectIs",
54
+ updated: `
55
+ if (Object.is(value, -0)) {}
56
+ `,
57
+ },
58
+ ],
59
+ },
60
+ {
61
+ code: `
62
+ if (value !== -0) {}
63
+ `,
64
+ snapshot: `
65
+ if (value !== -0) {}
66
+ ~~~~~~~~~~~~
67
+ Comparisons with -0 using !== do not distinguish between -0 and +0.
68
+ `,
69
+ suggestions: [
70
+ {
71
+ id: "useObjectIs",
72
+ updated: `
73
+ if (!Object.is(value, -0)) {}
74
+ `,
75
+ },
76
+ ],
77
+ },
78
+ {
79
+ code: `
80
+ if (value != -0) {}
81
+ `,
82
+ snapshot: `
83
+ if (value != -0) {}
84
+ ~~~~~~~~~~~
85
+ Comparisons with -0 using != do not distinguish between -0 and +0.
86
+ `,
87
+ suggestions: [
88
+ {
89
+ id: "useObjectIs",
90
+ updated: `
91
+ if (!Object.is(value, -0)) {}
92
+ `,
93
+ },
94
+ ],
95
+ },
96
+ {
97
+ code: `
98
+ if (value < -0) {}
99
+ `,
100
+ snapshot: `
101
+ if (value < -0) {}
102
+ ~~~~~~~~~~
103
+ Comparisons with -0 using < do not distinguish between -0 and +0.
104
+ `,
105
+ },
106
+ {
107
+ code: `
108
+ if (value <= -0) {}
109
+ `,
110
+ snapshot: `
111
+ if (value <= -0) {}
112
+ ~~~~~~~~~~~
113
+ Comparisons with -0 using <= do not distinguish between -0 and +0.
114
+ `,
115
+ },
116
+ {
117
+ code: `
118
+ if (value > -0) {}
119
+ `,
120
+ snapshot: `
121
+ if (value > -0) {}
122
+ ~~~~~~~~~~
123
+ Comparisons with -0 using > do not distinguish between -0 and +0.
124
+ `,
125
+ },
126
+ {
127
+ code: `
128
+ if (value >= -0) {}
129
+ `,
130
+ snapshot: `
131
+ if (value >= -0) {}
132
+ ~~~~~~~~~~~
133
+ Comparisons with -0 using >= do not distinguish between -0 and +0.
134
+ `,
135
+ },
136
+ {
137
+ code: `
138
+ const result = value === -0 ? "negative zero" : "other";
139
+ `,
140
+ snapshot: `
141
+ const result = value === -0 ? "negative zero" : "other";
142
+ ~~~~~~~~~~~~
143
+ Comparisons with -0 using === do not distinguish between -0 and +0.
144
+ `,
145
+ suggestions: [
146
+ {
147
+ id: "useObjectIs",
148
+ updated: `
149
+ const result = Object.is(value, -0) ? "negative zero" : "other";
150
+ `,
151
+ },
152
+ ],
153
+ },
154
+ {
155
+ code: `
156
+ while (value !== -0) {
157
+ value++;
158
+ }
159
+ `,
160
+ snapshot: `
161
+ while (value !== -0) {
162
+ ~~~~~~~~~~~~
163
+ Comparisons with -0 using !== do not distinguish between -0 and +0.
164
+ value++;
165
+ }
166
+ `,
167
+ suggestions: [
168
+ {
169
+ id: "useObjectIs",
170
+ updated: `
171
+ while (!Object.is(value, -0)) {
172
+ value++;
173
+ }
174
+ `,
175
+ },
176
+ ],
177
+ },
178
+ {
179
+ code: `
180
+ const isNegativeZero = value === -0;
181
+ `,
182
+ snapshot: `
183
+ const isNegativeZero = value === -0;
184
+ ~~~~~~~~~~~~
185
+ Comparisons with -0 using === do not distinguish between -0 and +0.
186
+ `,
187
+ suggestions: [
188
+ {
189
+ id: "useObjectIs",
190
+ updated: `
191
+ const isNegativeZero = Object.is(value, -0);
192
+ `,
193
+ },
194
+ ],
195
+ },
196
+ ],
197
+ valid: [
198
+ `if (value === 0) {}`,
199
+ `if (value === -1) {}`,
200
+ `if (value === 1) {}`,
201
+ `if (Object.is(value, -0)) {}`,
202
+ `if (Object.is(-0, value)) {}`,
203
+ `const result = -0;`,
204
+ `const value = -0 + 1;`,
205
+ `const value = -0 * 2;`,
206
+ `const value = Math.abs(-0);`,
207
+ `const value = [-0, 0, 1];`,
208
+ `function test() { return -0; }`,
209
+ ],
210
+ });