@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.
- package/CHANGELOG.md +17 -0
- package/lib/createTypeScriptFileFromProgram.js +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/language.d.ts +1 -0
- package/lib/nodes.d.ts +1 -0
- package/lib/plugin.d.ts +165 -1
- package/lib/plugin.js +82 -0
- package/lib/rules/anyReturns.d.ts +6 -0
- package/lib/rules/anyReturns.js +187 -0
- package/lib/rules/anyReturns.test.d.ts +1 -0
- package/lib/rules/anyReturns.test.js +647 -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/caseDuplicates.d.ts +6 -0
- package/lib/rules/caseDuplicates.js +49 -0
- package/lib/rules/caseDuplicates.test.d.ts +1 -0
- package/lib/rules/caseDuplicates.test.js +307 -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/elseIfDuplicates.d.ts +6 -0
- package/lib/rules/elseIfDuplicates.js +53 -0
- package/lib/rules/elseIfDuplicates.test.d.ts +1 -0
- package/lib/rules/elseIfDuplicates.test.js +176 -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/forDirections.d.ts +6 -0
- package/lib/rules/forDirections.js +124 -0
- package/lib/rules/forDirections.test.d.ts +1 -0
- package/lib/rules/forDirections.test.js +99 -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/functionNewCalls.d.ts +6 -0
- package/lib/rules/functionNewCalls.js +60 -0
- package/lib/rules/functionNewCalls.test.d.ts +1 -0
- package/lib/rules/functionNewCalls.test.js +115 -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 +65 -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/nonOctalDecimalEscapes.d.ts +6 -0
- package/lib/rules/nonOctalDecimalEscapes.js +57 -0
- package/lib/rules/nonOctalDecimalEscapes.test.d.ts +1 -0
- package/lib/rules/nonOctalDecimalEscapes.test.js +69 -0
- package/lib/rules/numericLiteralParsing.d.ts +6 -0
- package/lib/rules/numericLiteralParsing.js +97 -0
- package/lib/rules/numericLiteralParsing.test.d.ts +1 -0
- package/lib/rules/numericLiteralParsing.test.js +99 -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/selfAssignments.d.ts +6 -0
- package/lib/rules/selfAssignments.js +44 -0
- package/lib/rules/selfAssignments.test.d.ts +1 -0
- package/lib/rules/selfAssignments.test.js +66 -0
- package/lib/rules/selfComparisons.d.ts +6 -0
- package/lib/rules/selfComparisons.js +41 -0
- package/lib/rules/selfComparisons.test.d.ts +1 -0
- package/lib/rules/selfComparisons.test.js +297 -0
- package/lib/rules/sequences.d.ts +6 -0
- package/lib/rules/sequences.js +34 -0
- package/lib/rules/sequences.test.d.ts +1 -0
- package/lib/rules/sequences.test.js +48 -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/typeofComparisons.d.ts +6 -0
- package/lib/rules/typeofComparisons.js +78 -0
- package/lib/rules/typeofComparisons.test.d.ts +1 -0
- package/lib/rules/typeofComparisons.test.js +85 -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/utils/discriminateAnyType.d.ts +12 -0
- package/lib/rules/utils/discriminateAnyType.js +41 -0
- package/lib/rules/utils/getThisExpression.d.ts +2 -0
- package/lib/rules/utils/getThisExpression.js +23 -0
- package/lib/rules/utils/isUnsafeAssignment.d.ts +13 -0
- package/lib/rules/utils/isUnsafeAssignment.js +76 -0
- package/lib/rules/utils/operators.d.ts +4 -0
- package/lib/rules/utils/operators.js +36 -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/declarationIncludesGlobal.d.ts +2 -0
- package/lib/utils/declarationIncludesGlobal.js +5 -0
- package/lib/utils/declarationsIncludeGlobal.d.ts +2 -0
- package/lib/utils/declarationsIncludeGlobal.js +4 -0
- package/lib/utils/getModifyingReferences.d.ts +8 -0
- package/lib/utils/getModifyingReferences.js +42 -0
- package/lib/utils/hasSameTokens.d.ts +2 -0
- package/lib/utils/hasSameTokens.js +30 -0
- package/lib/utils/isGlobalDeclaration.d.ts +2 -0
- package/lib/utils/isGlobalDeclaration.js +5 -0
- package/lib/utils/isGlobalDeclarationOfName.d.ts +5 -0
- package/lib/utils/isGlobalDeclarationOfName.js +31 -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/createTypeScriptFileFromProgram.ts +1 -0
- package/src/index.ts +1 -0
- package/src/language.ts +1 -0
- package/src/nodes.ts +1 -0
- package/src/plugin.ts +82 -0
- package/src/rules/anyReturns.test.ts +649 -0
- package/src/rules/anyReturns.ts +263 -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/caseDuplicates.test.ts +308 -0
- package/src/rules/caseDuplicates.ts +57 -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/elseIfDuplicates.test.ts +177 -0
- package/src/rules/elseIfDuplicates.ts +69 -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/forDirections.test.ts +100 -0
- package/src/rules/forDirections.ts +163 -0
- package/src/rules/functionAssignments.test.ts +210 -0
- package/src/rules/functionAssignments.ts +51 -0
- package/src/rules/functionNewCalls.test.ts +116 -0
- package/src/rules/functionNewCalls.ts +75 -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 +87 -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/nonOctalDecimalEscapes.test.ts +70 -0
- package/src/rules/nonOctalDecimalEscapes.ts +70 -0
- package/src/rules/numericLiteralParsing.test.ts +100 -0
- package/src/rules/numericLiteralParsing.ts +116 -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/selfAssignments.test.ts +67 -0
- package/src/rules/selfAssignments.ts +50 -0
- package/src/rules/selfComparisons.test.ts +298 -0
- package/src/rules/selfComparisons.ts +45 -0
- package/src/rules/sequences.test.ts +49 -0
- package/src/rules/sequences.ts +37 -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/typeofComparisons.test.ts +86 -0
- package/src/rules/typeofComparisons.ts +88 -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/utils/discriminateAnyType.ts +66 -0
- package/src/rules/utils/getThisExpression.ts +24 -0
- package/src/rules/utils/isUnsafeAssignment.ts +113 -0
- package/src/rules/utils/operators.ts +39 -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/declarationIncludesGlobal.ts +9 -0
- package/src/utils/declarationsIncludeGlobal.ts +7 -0
- package/src/utils/getModifyingReferences.ts +58 -0
- package/src/utils/hasSameTokens.ts +45 -0
- package/src/utils/isGlobalDeclaration.ts +12 -0
- package/src/utils/isGlobalDeclarationOfName.ts +51 -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,196 @@
|
|
|
1
|
+
import rule from "./defaultCaseLast.js";
|
|
2
|
+
import { ruleTester } from "./ruleTester.js";
|
|
3
|
+
|
|
4
|
+
ruleTester.describe(rule, {
|
|
5
|
+
invalid: [
|
|
6
|
+
{
|
|
7
|
+
code: `
|
|
8
|
+
switch (value) {
|
|
9
|
+
default:
|
|
10
|
+
break;
|
|
11
|
+
case 1:
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
`,
|
|
15
|
+
snapshot: `
|
|
16
|
+
switch (value) {
|
|
17
|
+
default:
|
|
18
|
+
~~~~~~~
|
|
19
|
+
Default clauses in switch statements should be last.
|
|
20
|
+
break;
|
|
21
|
+
case 1:
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
`,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
code: `
|
|
28
|
+
switch (value) {
|
|
29
|
+
case 1:
|
|
30
|
+
break;
|
|
31
|
+
default:
|
|
32
|
+
break;
|
|
33
|
+
case 2:
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
`,
|
|
37
|
+
snapshot: `
|
|
38
|
+
switch (value) {
|
|
39
|
+
case 1:
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
~~~~~~~
|
|
43
|
+
Default clauses in switch statements should be last.
|
|
44
|
+
break;
|
|
45
|
+
case 2:
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
`,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
code: `
|
|
52
|
+
switch (value) {
|
|
53
|
+
case 1:
|
|
54
|
+
case 2:
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
break;
|
|
58
|
+
case 3:
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
`,
|
|
62
|
+
snapshot: `
|
|
63
|
+
switch (value) {
|
|
64
|
+
case 1:
|
|
65
|
+
case 2:
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
~~~~~~~
|
|
69
|
+
Default clauses in switch statements should be last.
|
|
70
|
+
break;
|
|
71
|
+
case 3:
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
`,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
code: `
|
|
78
|
+
switch (value) {
|
|
79
|
+
default:
|
|
80
|
+
console.log("default");
|
|
81
|
+
break;
|
|
82
|
+
case 1:
|
|
83
|
+
console.log("one");
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
`,
|
|
87
|
+
snapshot: `
|
|
88
|
+
switch (value) {
|
|
89
|
+
default:
|
|
90
|
+
~~~~~~~
|
|
91
|
+
Default clauses in switch statements should be last.
|
|
92
|
+
console.log("default");
|
|
93
|
+
break;
|
|
94
|
+
case 1:
|
|
95
|
+
console.log("one");
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
`,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
code: `
|
|
102
|
+
const result = (value: number) => {
|
|
103
|
+
switch (value) {
|
|
104
|
+
case 1:
|
|
105
|
+
return "one";
|
|
106
|
+
default:
|
|
107
|
+
return "default";
|
|
108
|
+
case 2:
|
|
109
|
+
return "two";
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
`,
|
|
113
|
+
snapshot: `
|
|
114
|
+
const result = (value: number) => {
|
|
115
|
+
switch (value) {
|
|
116
|
+
case 1:
|
|
117
|
+
return "one";
|
|
118
|
+
default:
|
|
119
|
+
~~~~~~~
|
|
120
|
+
Default clauses in switch statements should be last.
|
|
121
|
+
return "default";
|
|
122
|
+
case 2:
|
|
123
|
+
return "two";
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
`,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
code: `
|
|
130
|
+
switch (value) {
|
|
131
|
+
default:
|
|
132
|
+
case 1:
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
`,
|
|
136
|
+
snapshot: `
|
|
137
|
+
switch (value) {
|
|
138
|
+
default:
|
|
139
|
+
~~~~~~~
|
|
140
|
+
Default clauses in switch statements should be last.
|
|
141
|
+
case 1:
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
`,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
valid: [
|
|
148
|
+
`switch (value) { case 1: break; default: break; }`,
|
|
149
|
+
`switch (value) { case 1: break; case 2: break; default: break; }`,
|
|
150
|
+
`switch (value) { case 1: break; case 2: break; }`,
|
|
151
|
+
`switch (value) { default: break; }`,
|
|
152
|
+
`switch (value) { case 1: case 2: break; default: break; }`,
|
|
153
|
+
`
|
|
154
|
+
switch (value) {
|
|
155
|
+
case 1:
|
|
156
|
+
break;
|
|
157
|
+
default:
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
`,
|
|
161
|
+
`
|
|
162
|
+
switch (value) {
|
|
163
|
+
case 1:
|
|
164
|
+
console.log("one");
|
|
165
|
+
break;
|
|
166
|
+
case 2:
|
|
167
|
+
console.log("two");
|
|
168
|
+
break;
|
|
169
|
+
default:
|
|
170
|
+
console.log("default");
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
`,
|
|
174
|
+
`
|
|
175
|
+
const result = (value: number) => {
|
|
176
|
+
switch (value) {
|
|
177
|
+
case 1:
|
|
178
|
+
return "one";
|
|
179
|
+
case 2:
|
|
180
|
+
return "two";
|
|
181
|
+
default:
|
|
182
|
+
return "default";
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
`,
|
|
186
|
+
`
|
|
187
|
+
switch (value) {
|
|
188
|
+
case 1:
|
|
189
|
+
case 2:
|
|
190
|
+
break;
|
|
191
|
+
default:
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
`,
|
|
195
|
+
],
|
|
196
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { typescriptLanguage } from "../language.js";
|
|
4
|
+
|
|
5
|
+
export default typescriptLanguage.createRule({
|
|
6
|
+
about: {
|
|
7
|
+
description:
|
|
8
|
+
"Reports switch statements where the default clause is not last.",
|
|
9
|
+
id: "defaultCaseLast",
|
|
10
|
+
preset: "logical",
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
defaultCaseShouldBeLast: {
|
|
14
|
+
primary: "Default clauses in switch statements should be last.",
|
|
15
|
+
secondary: [
|
|
16
|
+
"Placing the default clause in a position other than last can lead to confusion and unexpected behavior.",
|
|
17
|
+
"While the default clause is executed when no case matches, having it in the middle of other cases makes the control flow harder to follow.",
|
|
18
|
+
],
|
|
19
|
+
suggestions: [
|
|
20
|
+
"Move the default clause to the end of the switch statement to improve code clarity.",
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
setup(context) {
|
|
25
|
+
return {
|
|
26
|
+
visitors: {
|
|
27
|
+
SwitchStatement: (node) => {
|
|
28
|
+
const clauses = node.caseBlock.clauses;
|
|
29
|
+
const defaultClauseIndex = clauses.findIndex(
|
|
30
|
+
(clause) => clause.kind === ts.SyntaxKind.DefaultClause,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
defaultClauseIndex === -1 ||
|
|
35
|
+
defaultClauseIndex === clauses.length - 1
|
|
36
|
+
) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const defaultClause = clauses[defaultClauseIndex];
|
|
41
|
+
|
|
42
|
+
context.report({
|
|
43
|
+
message: "defaultCaseShouldBeLast",
|
|
44
|
+
range: {
|
|
45
|
+
begin: defaultClause.getStart(context.sourceFile),
|
|
46
|
+
end:
|
|
47
|
+
defaultClause.getStart(context.sourceFile) + "default".length,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import rule from "./duplicateArguments.js";
|
|
2
|
+
import { ruleTester } from "./ruleTester.js";
|
|
3
|
+
|
|
4
|
+
ruleTester.describe(rule, {
|
|
5
|
+
invalid: [
|
|
6
|
+
{
|
|
7
|
+
code: `
|
|
8
|
+
function test(value, value) {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
`,
|
|
12
|
+
snapshot: `
|
|
13
|
+
function test(value, value) {
|
|
14
|
+
~~~~~
|
|
15
|
+
This parameter overrides the previous parameter of the same name.
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
`,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
code: `
|
|
22
|
+
function test(first, second, first) {
|
|
23
|
+
return first + second;
|
|
24
|
+
}
|
|
25
|
+
`,
|
|
26
|
+
snapshot: `
|
|
27
|
+
function test(first, second, first) {
|
|
28
|
+
~~~~~
|
|
29
|
+
This parameter overrides the previous parameter of the same name.
|
|
30
|
+
return first + second;
|
|
31
|
+
}
|
|
32
|
+
`,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
code: `
|
|
36
|
+
const arrow = (value, value) => value;
|
|
37
|
+
`,
|
|
38
|
+
snapshot: `
|
|
39
|
+
const arrow = (value, value) => value;
|
|
40
|
+
~~~~~
|
|
41
|
+
This parameter overrides the previous parameter of the same name.
|
|
42
|
+
`,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
code: `
|
|
46
|
+
const func = function (value, value) {
|
|
47
|
+
return value;
|
|
48
|
+
};
|
|
49
|
+
`,
|
|
50
|
+
snapshot: `
|
|
51
|
+
const func = function (value, value) {
|
|
52
|
+
~~~~~
|
|
53
|
+
This parameter overrides the previous parameter of the same name.
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
`,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
code: `
|
|
60
|
+
class Example {
|
|
61
|
+
method(value, value) {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
`,
|
|
66
|
+
snapshot: `
|
|
67
|
+
class Example {
|
|
68
|
+
method(value, value) {
|
|
69
|
+
~~~~~
|
|
70
|
+
This parameter overrides the previous parameter of the same name.
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
`,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
code: `
|
|
78
|
+
class Example {
|
|
79
|
+
constructor(value, value) {
|
|
80
|
+
this.value = value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
`,
|
|
84
|
+
snapshot: `
|
|
85
|
+
class Example {
|
|
86
|
+
constructor(value, value) {
|
|
87
|
+
~~~~~
|
|
88
|
+
This parameter overrides the previous parameter of the same name.
|
|
89
|
+
this.value = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
`,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
code: `
|
|
96
|
+
function test(first, second, third, second) {
|
|
97
|
+
return first + second + third;
|
|
98
|
+
}
|
|
99
|
+
`,
|
|
100
|
+
snapshot: `
|
|
101
|
+
function test(first, second, third, second) {
|
|
102
|
+
~~~~~~
|
|
103
|
+
This parameter overrides the previous parameter of the same name.
|
|
104
|
+
return first + second + third;
|
|
105
|
+
}
|
|
106
|
+
`,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
code: `
|
|
110
|
+
function test(value, value, value) {
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
`,
|
|
114
|
+
snapshot: `
|
|
115
|
+
function test(value, value, value) {
|
|
116
|
+
~~~~~
|
|
117
|
+
This parameter overrides the previous parameter of the same name.
|
|
118
|
+
~~~~~
|
|
119
|
+
This parameter overrides the previous parameter of the same name.
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
`,
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
valid: [
|
|
126
|
+
`function test(value) { return value; }`,
|
|
127
|
+
`function test(first, second) { return first + second; }`,
|
|
128
|
+
`const arrow = (value) => value;`,
|
|
129
|
+
`const arrow = (first, second) => first + second;`,
|
|
130
|
+
`const func = function (value) { return value; };`,
|
|
131
|
+
`const func = function (first, second) { return first + second; };`,
|
|
132
|
+
`class Example { method(value) { return value; } }`,
|
|
133
|
+
`class Example { method(first, second) { return first + second; } }`,
|
|
134
|
+
`class Example { constructor(value) { this.value = value; } }`,
|
|
135
|
+
`class Example { constructor(first, second) { this.first = first; this.second = second; } }`,
|
|
136
|
+
`function test({ value }) { return value; }`,
|
|
137
|
+
`function test({ first, second }) { return first + second; }`,
|
|
138
|
+
`function test([first, second]) { return first + second; }`,
|
|
139
|
+
],
|
|
140
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { getTSNodeRange } from "../getTSNodeRange.js";
|
|
4
|
+
import { typescriptLanguage } from "../language.js";
|
|
5
|
+
|
|
6
|
+
export default typescriptLanguage.createRule({
|
|
7
|
+
about: {
|
|
8
|
+
description:
|
|
9
|
+
"Reports functions with duplicate parameter names in their signatures.",
|
|
10
|
+
id: "duplicateArguments",
|
|
11
|
+
preset: "untyped",
|
|
12
|
+
},
|
|
13
|
+
messages: {
|
|
14
|
+
duplicateParam: {
|
|
15
|
+
primary:
|
|
16
|
+
"This parameter overrides the previous parameter of the same name.",
|
|
17
|
+
secondary: [
|
|
18
|
+
"Duplicate parameter names in functions can lead to unexpected behavior and make code harder to understand.",
|
|
19
|
+
"In strict mode, duplicate parameter names are a syntax error.",
|
|
20
|
+
"Even in non-strict mode, only the last parameter with a given name is accessible.",
|
|
21
|
+
],
|
|
22
|
+
suggestions: ["Rename one of the duplicate parameters to a unique name."],
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
setup(context) {
|
|
26
|
+
function checkNode({ parameters }: ts.FunctionLikeDeclaration) {
|
|
27
|
+
const seenNames = new Set<string>();
|
|
28
|
+
|
|
29
|
+
for (const parameter of parameters) {
|
|
30
|
+
if (!ts.isIdentifier(parameter.name)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const parameterName = parameter.name.text;
|
|
35
|
+
const existingParameter = seenNames.has(parameterName);
|
|
36
|
+
|
|
37
|
+
if (existingParameter) {
|
|
38
|
+
context.report({
|
|
39
|
+
message: "duplicateParam",
|
|
40
|
+
range: getTSNodeRange(parameter.name, context.sourceFile),
|
|
41
|
+
});
|
|
42
|
+
} else {
|
|
43
|
+
seenNames.add(parameterName);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
visitors: {
|
|
50
|
+
ArrowFunction: checkNode,
|
|
51
|
+
Constructor: checkNode,
|
|
52
|
+
FunctionDeclaration: checkNode,
|
|
53
|
+
FunctionExpression: checkNode,
|
|
54
|
+
MethodDeclaration: checkNode,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import rule from "./elseIfDuplicates.js";
|
|
2
|
+
import { ruleTester } from "./ruleTester.js";
|
|
3
|
+
|
|
4
|
+
ruleTester.describe(rule, {
|
|
5
|
+
invalid: [
|
|
6
|
+
{
|
|
7
|
+
code: `
|
|
8
|
+
if (isSomething(value)) {
|
|
9
|
+
doFirst();
|
|
10
|
+
} else if (isSomething(value)) {
|
|
11
|
+
doSecond();
|
|
12
|
+
}
|
|
13
|
+
`,
|
|
14
|
+
snapshot: `
|
|
15
|
+
if (isSomething(value)) {
|
|
16
|
+
doFirst();
|
|
17
|
+
} else if (isSomething(value)) {
|
|
18
|
+
~~~~~~~~~~~~~~~~~~
|
|
19
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
20
|
+
doSecond();
|
|
21
|
+
}
|
|
22
|
+
`,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
code: `
|
|
26
|
+
if (isSomething /* ... */ (value)) {
|
|
27
|
+
doFirst();
|
|
28
|
+
} else if (isSomething(value /* ... */)) {
|
|
29
|
+
doSecond();
|
|
30
|
+
}
|
|
31
|
+
`,
|
|
32
|
+
snapshot: `
|
|
33
|
+
if (isSomething /* ... */ (value)) {
|
|
34
|
+
doFirst();
|
|
35
|
+
} else if (isSomething(value /* ... */)) {
|
|
36
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
37
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
38
|
+
doSecond();
|
|
39
|
+
}
|
|
40
|
+
`,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
code: `
|
|
44
|
+
if (first) {
|
|
45
|
+
handleFirst();
|
|
46
|
+
} else if (second) {
|
|
47
|
+
handleSecond();
|
|
48
|
+
} else if (third && fourth) {
|
|
49
|
+
handleThird();
|
|
50
|
+
} else if (third && fourth) {
|
|
51
|
+
handleFourth();
|
|
52
|
+
}
|
|
53
|
+
`,
|
|
54
|
+
snapshot: `
|
|
55
|
+
if (first) {
|
|
56
|
+
handleFirst();
|
|
57
|
+
} else if (second) {
|
|
58
|
+
handleSecond();
|
|
59
|
+
} else if (third && fourth) {
|
|
60
|
+
handleThird();
|
|
61
|
+
} else if (third && fourth) {
|
|
62
|
+
~~~~~~~~~~~~~~~
|
|
63
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
64
|
+
handleFourth();
|
|
65
|
+
}
|
|
66
|
+
`,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
code: `
|
|
70
|
+
if (count === 1) {
|
|
71
|
+
handleOne();
|
|
72
|
+
} else if (count === 2) {
|
|
73
|
+
handleTwo();
|
|
74
|
+
} else if (count === 3) {
|
|
75
|
+
handleThree();
|
|
76
|
+
} else if (count === 2) {
|
|
77
|
+
handleFour();
|
|
78
|
+
} else if (count === 5) {
|
|
79
|
+
handleFive();
|
|
80
|
+
}
|
|
81
|
+
`,
|
|
82
|
+
snapshot: `
|
|
83
|
+
if (count === 1) {
|
|
84
|
+
handleOne();
|
|
85
|
+
} else if (count === 2) {
|
|
86
|
+
handleTwo();
|
|
87
|
+
} else if (count === 3) {
|
|
88
|
+
handleThree();
|
|
89
|
+
} else if (count === 2) {
|
|
90
|
+
~~~~~~~~~~~
|
|
91
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
92
|
+
handleFour();
|
|
93
|
+
} else if (count === 5) {
|
|
94
|
+
handleFive();
|
|
95
|
+
}
|
|
96
|
+
`,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
code: `
|
|
100
|
+
if (value === "first") {
|
|
101
|
+
process();
|
|
102
|
+
} else if (value === "second") {
|
|
103
|
+
process();
|
|
104
|
+
} else if (value === "first") {
|
|
105
|
+
process();
|
|
106
|
+
}
|
|
107
|
+
`,
|
|
108
|
+
snapshot: `
|
|
109
|
+
if (value === "first") {
|
|
110
|
+
process();
|
|
111
|
+
} else if (value === "second") {
|
|
112
|
+
process();
|
|
113
|
+
} else if (value === "first") {
|
|
114
|
+
~~~~~~~~~~~~~~~~~
|
|
115
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
116
|
+
process();
|
|
117
|
+
}
|
|
118
|
+
`,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
code: `
|
|
122
|
+
if (isValid && isActive) {
|
|
123
|
+
execute();
|
|
124
|
+
} else if (isPending) {
|
|
125
|
+
wait();
|
|
126
|
+
} else if (isValid && isActive) {
|
|
127
|
+
execute();
|
|
128
|
+
}
|
|
129
|
+
`,
|
|
130
|
+
snapshot: `
|
|
131
|
+
if (isValid && isActive) {
|
|
132
|
+
execute();
|
|
133
|
+
} else if (isPending) {
|
|
134
|
+
wait();
|
|
135
|
+
} else if (isValid && isActive) {
|
|
136
|
+
~~~~~~~~~~~~~~~~~~~
|
|
137
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
138
|
+
execute();
|
|
139
|
+
}
|
|
140
|
+
`,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
code: `
|
|
144
|
+
if (value) {
|
|
145
|
+
first();
|
|
146
|
+
} else if (value) {
|
|
147
|
+
second();
|
|
148
|
+
} else if (value) {
|
|
149
|
+
third();
|
|
150
|
+
}
|
|
151
|
+
`,
|
|
152
|
+
snapshot: `
|
|
153
|
+
if (value) {
|
|
154
|
+
first();
|
|
155
|
+
} else if (value) {
|
|
156
|
+
~~~~~
|
|
157
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
158
|
+
second();
|
|
159
|
+
} else if (value) {
|
|
160
|
+
~~~~~
|
|
161
|
+
This condition is identical to a previous condition in the if-else-if chain.
|
|
162
|
+
third();
|
|
163
|
+
}
|
|
164
|
+
`,
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
valid: [
|
|
168
|
+
`if (isSomething(value)) { doFirst(); } else if (isSomethingElse(value)) { doSecond(); }`,
|
|
169
|
+
`if (first) { handleFirst(); } else if (second) { handleSecond(); } else if (third && fourth) { handleThird(); } else if (third && fifth) { handleFourth(); }`,
|
|
170
|
+
`if (count === 1) { handleOne(); } else if (count === 2) { handleTwo(); } else if (count === 3) { handleThree(); } else if (count === 4) { handleFour(); }`,
|
|
171
|
+
`if (value === "first") { process(); } else if (value === "second") { process(); } else if (value === "third") { process(); }`,
|
|
172
|
+
`if (isValid) { execute(); } else if (isPending) { wait(); } else if (isInvalid) { reject(); }`,
|
|
173
|
+
`if (value) { process(); }`,
|
|
174
|
+
`if (first) { doFirst(); } else { doSecond(); }`,
|
|
175
|
+
`if (first) { doFirst(); } else if (second) { doSecond(); } else { doDefault(); }`,
|
|
176
|
+
],
|
|
177
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { getTSNodeRange } from "../getTSNodeRange.js";
|
|
4
|
+
import { typescriptLanguage } from "../language.js";
|
|
5
|
+
import { hasSameTokens } from "../utils/hasSameTokens.js";
|
|
6
|
+
|
|
7
|
+
export default typescriptLanguage.createRule({
|
|
8
|
+
about: {
|
|
9
|
+
description:
|
|
10
|
+
"Reports duplicate conditions in if-else-if chains that make code unreachable.",
|
|
11
|
+
id: "elseIfDuplicates",
|
|
12
|
+
preset: "logical",
|
|
13
|
+
},
|
|
14
|
+
messages: {
|
|
15
|
+
duplicateCondition: {
|
|
16
|
+
primary:
|
|
17
|
+
"This condition is identical to a previous condition in the if-else-if chain.",
|
|
18
|
+
secondary: [
|
|
19
|
+
"When an if-else-if chain has duplicate conditions, the duplicate branch will never execute because the earlier identical condition will always be evaluated first.",
|
|
20
|
+
"This typically indicates a copy-paste error or logic mistake.",
|
|
21
|
+
],
|
|
22
|
+
suggestions: [
|
|
23
|
+
"Verify the condition logic is correct and update to check for the intended condition.",
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
setup(context) {
|
|
28
|
+
function checkIfStatement(node: ts.IfStatement) {
|
|
29
|
+
const seen: ts.Expression[] = [];
|
|
30
|
+
let current: ts.IfStatement = node;
|
|
31
|
+
|
|
32
|
+
while (true) {
|
|
33
|
+
if (
|
|
34
|
+
seen.some((previous) =>
|
|
35
|
+
hasSameTokens(previous, current.expression, context.sourceFile),
|
|
36
|
+
)
|
|
37
|
+
) {
|
|
38
|
+
context.report({
|
|
39
|
+
message: "duplicateCondition",
|
|
40
|
+
range: getTSNodeRange(current.expression, context.sourceFile),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
!current.elseStatement ||
|
|
46
|
+
!ts.isIfStatement(current.elseStatement)
|
|
47
|
+
) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
seen.push(current.expression);
|
|
52
|
+
current = current.elseStatement;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
visitors: {
|
|
58
|
+
IfStatement: (node) => {
|
|
59
|
+
if (
|
|
60
|
+
!ts.isIfStatement(node.parent) ||
|
|
61
|
+
node.parent.elseStatement !== node
|
|
62
|
+
) {
|
|
63
|
+
checkIfStatement(node);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
});
|