@flint.fyi/ts 0.14.2 → 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.
- package/CHANGELOG.md +7 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/nodes.d.ts +1 -0
- package/lib/plugin.d.ts +121 -1
- package/lib/plugin.js +60 -0
- package/lib/rules/asyncPromiseExecutors.d.ts +6 -0
- package/lib/rules/asyncPromiseExecutors.js +51 -0
- package/lib/rules/asyncPromiseExecutors.test.d.ts +1 -0
- package/lib/rules/asyncPromiseExecutors.test.js +71 -0
- package/lib/rules/caseDeclarations.d.ts +6 -0
- package/lib/rules/caseDeclarations.js +53 -0
- package/lib/rules/caseDeclarations.test.d.ts +1 -0
- package/lib/rules/caseDeclarations.test.js +227 -0
- package/lib/rules/chainedAssignments.d.ts +6 -0
- package/lib/rules/chainedAssignments.js +48 -0
- package/lib/rules/chainedAssignments.test.d.ts +1 -0
- package/lib/rules/chainedAssignments.test.js +238 -0
- package/lib/rules/classAssignments.d.ts +6 -0
- package/lib/rules/classAssignments.js +42 -0
- package/lib/rules/classAssignments.test.d.ts +1 -0
- package/lib/rules/classAssignments.test.js +156 -0
- package/lib/rules/constantAssignments.d.ts +6 -0
- package/lib/rules/constantAssignments.js +61 -0
- package/lib/rules/constantAssignments.test.d.ts +1 -0
- package/lib/rules/constantAssignments.test.js +221 -0
- package/lib/rules/constructorReturns.d.ts +6 -0
- package/lib/rules/constructorReturns.js +52 -0
- package/lib/rules/constructorReturns.test.d.ts +1 -0
- package/lib/rules/constructorReturns.test.js +96 -0
- package/lib/rules/defaultCaseLast.d.ts +6 -0
- package/lib/rules/defaultCaseLast.js +43 -0
- package/lib/rules/defaultCaseLast.test.d.ts +1 -0
- package/lib/rules/defaultCaseLast.test.js +195 -0
- package/lib/rules/duplicateArguments.d.ts +6 -0
- package/lib/rules/duplicateArguments.js +51 -0
- package/lib/rules/duplicateArguments.test.d.ts +1 -0
- package/lib/rules/duplicateArguments.test.js +139 -0
- package/lib/rules/emptyDestructures.d.ts +6 -0
- package/lib/rules/emptyDestructures.js +40 -0
- package/lib/rules/emptyDestructures.test.d.ts +1 -0
- package/lib/rules/emptyDestructures.test.js +186 -0
- package/lib/rules/emptyStaticBlocks.d.ts +6 -0
- package/lib/rules/emptyStaticBlocks.js +54 -0
- package/lib/rules/emptyStaticBlocks.test.d.ts +1 -0
- package/lib/rules/emptyStaticBlocks.test.js +167 -0
- package/lib/rules/exceptionAssignments.d.ts +6 -0
- package/lib/rules/exceptionAssignments.js +61 -0
- package/lib/rules/exceptionAssignments.test.d.ts +1 -0
- package/lib/rules/exceptionAssignments.test.js +264 -0
- package/lib/rules/functionAssignments.d.ts +6 -0
- package/lib/rules/functionAssignments.js +42 -0
- package/lib/rules/functionAssignments.test.d.ts +1 -0
- package/lib/rules/functionAssignments.test.js +209 -0
- package/lib/rules/generatorFunctionYields.d.ts +6 -0
- package/lib/rules/generatorFunctionYields.js +57 -0
- package/lib/rules/generatorFunctionYields.test.d.ts +1 -0
- package/lib/rules/generatorFunctionYields.test.js +151 -0
- package/lib/rules/globalAssignments.d.ts +6 -0
- package/lib/rules/globalAssignments.js +59 -0
- package/lib/rules/globalAssignments.test.d.ts +1 -0
- package/lib/rules/globalAssignments.test.js +215 -0
- package/lib/rules/globalObjectCalls.d.ts +6 -0
- package/lib/rules/globalObjectCalls.js +43 -0
- package/lib/rules/globalObjectCalls.test.d.ts +1 -0
- package/lib/rules/globalObjectCalls.test.js +93 -0
- package/lib/rules/negativeZeroComparisons.d.ts +6 -0
- package/lib/rules/negativeZeroComparisons.js +83 -0
- package/lib/rules/negativeZeroComparisons.test.d.ts +1 -0
- package/lib/rules/negativeZeroComparisons.test.js +209 -0
- package/lib/rules/newExpressions.d.ts +6 -0
- package/lib/rules/newExpressions.js +54 -0
- package/lib/rules/newExpressions.test.d.ts +1 -0
- package/lib/rules/newExpressions.test.js +79 -0
- package/lib/rules/newNativeNonConstructors.d.ts +6 -0
- package/lib/rules/newNativeNonConstructors.js +49 -0
- package/lib/rules/newNativeNonConstructors.test.d.ts +1 -0
- package/lib/rules/newNativeNonConstructors.test.js +94 -0
- package/lib/rules/objectProto.d.ts +6 -0
- package/lib/rules/objectProto.js +54 -0
- package/lib/rules/objectProto.test.d.ts +1 -0
- package/lib/rules/objectProto.test.js +96 -0
- package/lib/rules/octalNumbers.d.ts +6 -0
- package/lib/rules/octalNumbers.js +68 -0
- package/lib/rules/octalNumbers.test.d.ts +1 -0
- package/lib/rules/octalNumbers.test.js +245 -0
- package/lib/rules/returnAssignments.d.ts +6 -0
- package/lib/rules/returnAssignments.js +50 -0
- package/lib/rules/returnAssignments.test.d.ts +1 -0
- package/lib/rules/returnAssignments.test.js +185 -0
- package/lib/rules/sparseArrays.d.ts +6 -0
- package/lib/rules/sparseArrays.js +54 -0
- package/lib/rules/sparseArrays.test.d.ts +1 -0
- package/lib/rules/sparseArrays.test.js +110 -0
- package/lib/rules/symbolDescriptions.d.ts +6 -0
- package/lib/rules/symbolDescriptions.js +41 -0
- package/lib/rules/symbolDescriptions.test.d.ts +1 -0
- package/lib/rules/symbolDescriptions.test.js +89 -0
- package/lib/rules/unassignedVariables.d.ts +6 -0
- package/lib/rules/unassignedVariables.js +55 -0
- package/lib/rules/unassignedVariables.test.d.ts +1 -0
- package/lib/rules/unassignedVariables.test.js +72 -0
- package/lib/rules/undefinedVariables.d.ts +6 -0
- package/lib/rules/undefinedVariables.js +76 -0
- package/lib/rules/undefinedVariables.test.d.ts +1 -0
- package/lib/rules/undefinedVariables.test.js +80 -0
- package/lib/rules/unicodeBOMs.d.ts +6 -0
- package/lib/rules/unicodeBOMs.js +41 -0
- package/lib/rules/unicodeBOMs.test.d.ts +1 -0
- package/lib/rules/unicodeBOMs.test.js +65 -0
- package/lib/rules/unnecessaryCatches.d.ts +6 -0
- package/lib/rules/unnecessaryCatches.js +68 -0
- package/lib/rules/unnecessaryCatches.test.d.ts +1 -0
- package/lib/rules/unnecessaryCatches.test.js +210 -0
- package/lib/rules/unsafeNegations.d.ts +6 -0
- package/lib/rules/unsafeNegations.js +50 -0
- package/lib/rules/unsafeNegations.test.d.ts +1 -0
- package/lib/rules/unsafeNegations.test.js +63 -0
- package/lib/rules/variableDeletions.d.ts +6 -0
- package/lib/rules/variableDeletions.js +39 -0
- package/lib/rules/variableDeletions.test.d.ts +1 -0
- package/lib/rules/variableDeletions.test.js +75 -0
- package/lib/rules/voidOperator.d.ts +6 -0
- package/lib/rules/voidOperator.js +36 -0
- package/lib/rules/voidOperator.test.d.ts +1 -0
- package/lib/rules/voidOperator.test.js +98 -0
- package/lib/utils/declarationsIncludeGlobal.d.ts +2 -0
- package/lib/utils/declarationsIncludeGlobal.js +7 -0
- package/lib/utils/getModifyingReferences.d.ts +8 -0
- package/lib/utils/getModifyingReferences.js +42 -0
- package/lib/utils/isGlobalDeclaration.d.ts +2 -0
- package/lib/utils/isGlobalDeclaration.js +5 -0
- package/lib/utils/isGlobalVariable.d.ts +8 -0
- package/lib/utils/isGlobalVariable.js +21 -0
- package/lib/utils/unwrapParentParenthesizedExpressions.d.ts +2 -0
- package/lib/utils/unwrapParentParenthesizedExpressions.js +6 -0
- package/lib/utils/unwrapParenthesizedExpression.d.ts +2 -0
- package/lib/utils/unwrapParenthesizedExpression.js +6 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/nodes.ts +1 -0
- package/src/plugin.ts +60 -0
- package/src/rules/asyncPromiseExecutors.test.ts +72 -0
- package/src/rules/asyncPromiseExecutors.ts +61 -0
- package/src/rules/caseDeclarations.test.ts +228 -0
- package/src/rules/caseDeclarations.ts +70 -0
- package/src/rules/chainedAssignments.test.ts +239 -0
- package/src/rules/chainedAssignments.ts +55 -0
- package/src/rules/classAssignments.test.ts +157 -0
- package/src/rules/classAssignments.ts +50 -0
- package/src/rules/constantAssignments.test.ts +222 -0
- package/src/rules/constantAssignments.ts +78 -0
- package/src/rules/constructorReturns.test.ts +97 -0
- package/src/rules/constructorReturns.ts +59 -0
- package/src/rules/defaultCaseLast.test.ts +196 -0
- package/src/rules/defaultCaseLast.ts +54 -0
- package/src/rules/duplicateArguments.test.ts +140 -0
- package/src/rules/duplicateArguments.ts +58 -0
- package/src/rules/emptyDestructures.test.ts +187 -0
- package/src/rules/emptyDestructures.ts +48 -0
- package/src/rules/emptyStaticBlocks.test.ts +168 -0
- package/src/rules/emptyStaticBlocks.ts +60 -0
- package/src/rules/exceptionAssignments.test.ts +265 -0
- package/src/rules/exceptionAssignments.ts +77 -0
- package/src/rules/functionAssignments.test.ts +210 -0
- package/src/rules/functionAssignments.ts +51 -0
- package/src/rules/generatorFunctionYields.test.ts +152 -0
- package/src/rules/generatorFunctionYields.ts +71 -0
- package/src/rules/globalAssignments.test.ts +216 -0
- package/src/rules/globalAssignments.ts +67 -0
- package/src/rules/globalObjectCalls.test.ts +94 -0
- package/src/rules/globalObjectCalls.ts +52 -0
- package/src/rules/negativeZeroComparisons.test.ts +210 -0
- package/src/rules/negativeZeroComparisons.ts +106 -0
- package/src/rules/newExpressions.test.ts +80 -0
- package/src/rules/newExpressions.ts +67 -0
- package/src/rules/newNativeNonConstructors.test.ts +95 -0
- package/src/rules/newNativeNonConstructors.ts +59 -0
- package/src/rules/objectProto.test.ts +97 -0
- package/src/rules/objectProto.ts +62 -0
- package/src/rules/octalNumbers.test.ts +246 -0
- package/src/rules/octalNumbers.ts +75 -0
- package/src/rules/returnAssignments.test.ts +186 -0
- package/src/rules/returnAssignments.ts +57 -0
- package/src/rules/sparseArrays.test.ts +111 -0
- package/src/rules/sparseArrays.ts +61 -0
- package/src/rules/symbolDescriptions.test.ts +90 -0
- package/src/rules/symbolDescriptions.ts +47 -0
- package/src/rules/unassignedVariables.test.ts +73 -0
- package/src/rules/unassignedVariables.ts +69 -0
- package/src/rules/undefinedVariables.test.ts +81 -0
- package/src/rules/undefinedVariables.ts +97 -0
- package/src/rules/unicodeBOMs.test.ts +66 -0
- package/src/rules/unicodeBOMs.ts +44 -0
- package/src/rules/unnecessaryCatches.test.ts +211 -0
- package/src/rules/unnecessaryCatches.ts +84 -0
- package/src/rules/unsafeNegations.test.ts +64 -0
- package/src/rules/unsafeNegations.ts +59 -0
- package/src/rules/variableDeletions.test.ts +76 -0
- package/src/rules/variableDeletions.ts +43 -0
- package/src/rules/voidOperator.test.ts +99 -0
- package/src/rules/voidOperator.ts +38 -0
- package/src/utils/declarationsIncludeGlobal.ts +11 -0
- package/src/utils/getModifyingReferences.ts +58 -0
- package/src/utils/isGlobalDeclaration.ts +12 -0
- package/src/utils/isGlobalVariable.ts +30 -0
- package/src/utils/unwrapParentParenthesizedExpressions.ts +7 -0
- package/src/utils/unwrapParenthesizedExpression.ts +9 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -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,152 @@
|
|
|
1
|
+
import rule from "./generatorFunctionYields.js";
|
|
2
|
+
import { ruleTester } from "./ruleTester.js";
|
|
3
|
+
|
|
4
|
+
ruleTester.describe(rule, {
|
|
5
|
+
invalid: [
|
|
6
|
+
{
|
|
7
|
+
code: `
|
|
8
|
+
function* emptyGenerator() {
|
|
9
|
+
console.log("No yield here");
|
|
10
|
+
}
|
|
11
|
+
`,
|
|
12
|
+
snapshot: `
|
|
13
|
+
function* emptyGenerator() {
|
|
14
|
+
~
|
|
15
|
+
Generator functions must contain at least one yield expression to produce values.
|
|
16
|
+
console.log("No yield here");
|
|
17
|
+
}
|
|
18
|
+
`,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
code: `
|
|
22
|
+
const generator = function* () {
|
|
23
|
+
return 42;
|
|
24
|
+
};
|
|
25
|
+
`,
|
|
26
|
+
snapshot: `
|
|
27
|
+
const generator = function* () {
|
|
28
|
+
~
|
|
29
|
+
Generator functions must contain at least one yield expression to produce values.
|
|
30
|
+
return 42;
|
|
31
|
+
};
|
|
32
|
+
`,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
code: `
|
|
36
|
+
class MyClass {
|
|
37
|
+
*generatorMethod() {
|
|
38
|
+
this.doSomething();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
`,
|
|
42
|
+
snapshot: `
|
|
43
|
+
class MyClass {
|
|
44
|
+
*generatorMethod() {
|
|
45
|
+
~
|
|
46
|
+
Generator functions must contain at least one yield expression to produce values.
|
|
47
|
+
this.doSomething();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
`,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
code: `
|
|
54
|
+
function* generatorWithNestedFunction() {
|
|
55
|
+
function inner() {
|
|
56
|
+
yield 42;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
`,
|
|
60
|
+
snapshot: `
|
|
61
|
+
function* generatorWithNestedFunction() {
|
|
62
|
+
~
|
|
63
|
+
Generator functions must contain at least one yield expression to produce values.
|
|
64
|
+
function inner() {
|
|
65
|
+
yield 42;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
`,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
code: `
|
|
72
|
+
function* generatorWithNestedGeneratorFunction() {
|
|
73
|
+
function* inner() {
|
|
74
|
+
yield 42;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
`,
|
|
78
|
+
snapshot: `
|
|
79
|
+
function* generatorWithNestedGeneratorFunction() {
|
|
80
|
+
~
|
|
81
|
+
Generator functions must contain at least one yield expression to produce values.
|
|
82
|
+
function* inner() {
|
|
83
|
+
yield 42;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
`,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
valid: [
|
|
90
|
+
`
|
|
91
|
+
function* validGenerator() {
|
|
92
|
+
yield 1;
|
|
93
|
+
}
|
|
94
|
+
`,
|
|
95
|
+
`
|
|
96
|
+
function* validGeneratorMultiple() {
|
|
97
|
+
yield 1;
|
|
98
|
+
yield 2;
|
|
99
|
+
yield 3;
|
|
100
|
+
}
|
|
101
|
+
`,
|
|
102
|
+
`
|
|
103
|
+
const generator = function* () {
|
|
104
|
+
yield 42;
|
|
105
|
+
};
|
|
106
|
+
`,
|
|
107
|
+
`
|
|
108
|
+
class MyClass {
|
|
109
|
+
*generatorMethod() {
|
|
110
|
+
yield this.value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
`,
|
|
114
|
+
`
|
|
115
|
+
function* conditionalYield(condition: boolean) {
|
|
116
|
+
if (condition) {
|
|
117
|
+
yield 1;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
`,
|
|
121
|
+
`
|
|
122
|
+
function* yieldInLoop() {
|
|
123
|
+
for (let i = 0; i < 10; i++) {
|
|
124
|
+
yield i;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
`,
|
|
128
|
+
`
|
|
129
|
+
function* yieldDelegate() {
|
|
130
|
+
yield* [1, 2, 3];
|
|
131
|
+
}
|
|
132
|
+
`,
|
|
133
|
+
`
|
|
134
|
+
function regularFunction() {
|
|
135
|
+
return 42;
|
|
136
|
+
}
|
|
137
|
+
`,
|
|
138
|
+
`
|
|
139
|
+
const arrow = () => {
|
|
140
|
+
return 42;
|
|
141
|
+
};
|
|
142
|
+
`,
|
|
143
|
+
`
|
|
144
|
+
function* generatorWithNestedGenerator() {
|
|
145
|
+
yield 1;
|
|
146
|
+
function* inner() {
|
|
147
|
+
yield 2;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
`,
|
|
151
|
+
],
|
|
152
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as tsutils from "ts-api-utils";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { typescriptLanguage } from "../language.js";
|
|
5
|
+
|
|
6
|
+
export default typescriptLanguage.createRule({
|
|
7
|
+
about: {
|
|
8
|
+
description: "Reports generator functions that do not yield values.",
|
|
9
|
+
id: "generatorFunctionYields",
|
|
10
|
+
preset: "logical",
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
missingYield: {
|
|
14
|
+
primary:
|
|
15
|
+
"Generator functions must contain at least one yield expression to produce values.",
|
|
16
|
+
secondary: [
|
|
17
|
+
"Generator functions use the `function*` syntax to create iterators that can produce multiple values over time.",
|
|
18
|
+
"Without a yield expression, the generator will not produce any values and behaves like an empty iterator.",
|
|
19
|
+
"This is likely unintentional and indicates incomplete implementation or a misunderstanding of generator functions.",
|
|
20
|
+
],
|
|
21
|
+
suggestions: [
|
|
22
|
+
"Add a `yield` expression to produce values from the generator.",
|
|
23
|
+
"If the function should not be a generator, remove the asterisk (`*`) from the function declaration.",
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
setup(context) {
|
|
28
|
+
return {
|
|
29
|
+
visitors: {
|
|
30
|
+
FunctionDeclaration: checkFunction,
|
|
31
|
+
FunctionExpression: checkFunction,
|
|
32
|
+
MethodDeclaration: checkFunction,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function checkFunction(
|
|
37
|
+
node:
|
|
38
|
+
| ts.FunctionDeclaration
|
|
39
|
+
| ts.FunctionExpression
|
|
40
|
+
| ts.MethodDeclaration,
|
|
41
|
+
): void {
|
|
42
|
+
if (!node.asteriskToken || !node.body || blockContainsYield(node.body)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
context.report({
|
|
47
|
+
message: "missingYield",
|
|
48
|
+
range: {
|
|
49
|
+
begin: node.asteriskToken.getStart(context.sourceFile),
|
|
50
|
+
end: node.asteriskToken.getEnd(),
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
function blockContainsYield(block: ts.Block) {
|
|
58
|
+
function checkForYield(node: ts.Node): boolean | undefined {
|
|
59
|
+
if (ts.isYieldExpression(node)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (tsutils.isFunctionScopeBoundary(node)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return ts.forEachChild(node, checkForYield);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return ts.forEachChild(block, checkForYield);
|
|
71
|
+
}
|
|
@@ -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
|
+
});
|