@flint.fyi/ts 0.14.3 → 0.14.5

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 (147) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/lib/createTypeScriptFileFromProgram.js +5 -1
  3. package/lib/index.d.ts +3 -0
  4. package/lib/index.js +3 -0
  5. package/lib/language.d.ts +1 -0
  6. package/lib/plugin.d.ts +61 -1
  7. package/lib/plugin.js +30 -0
  8. package/lib/rules/anyReturns.d.ts +6 -0
  9. package/lib/rules/anyReturns.js +187 -0
  10. package/lib/rules/anyReturns.test.d.ts +1 -0
  11. package/lib/rules/anyReturns.test.js +647 -0
  12. package/lib/rules/caseDuplicates.d.ts +6 -0
  13. package/lib/rules/caseDuplicates.js +49 -0
  14. package/lib/rules/caseDuplicates.test.d.ts +1 -0
  15. package/lib/rules/caseDuplicates.test.js +307 -0
  16. package/lib/rules/chainedAssignments.js +2 -1
  17. package/lib/rules/chainedAssignments.test.js +24 -0
  18. package/lib/rules/debuggerStatements.test.js +4 -4
  19. package/lib/rules/elseIfDuplicates.d.ts +6 -0
  20. package/lib/rules/elseIfDuplicates.js +53 -0
  21. package/lib/rules/elseIfDuplicates.test.d.ts +1 -0
  22. package/lib/rules/elseIfDuplicates.test.js +176 -0
  23. package/lib/rules/emptyBlocks.d.ts +6 -0
  24. package/lib/rules/emptyBlocks.js +66 -0
  25. package/lib/rules/emptyBlocks.test.d.ts +1 -0
  26. package/lib/rules/emptyBlocks.test.js +135 -0
  27. package/lib/rules/forDirections.d.ts +6 -0
  28. package/lib/rules/forDirections.js +124 -0
  29. package/lib/rules/forDirections.test.d.ts +1 -0
  30. package/lib/rules/forDirections.test.js +99 -0
  31. package/lib/rules/functionNewCalls.d.ts +6 -0
  32. package/lib/rules/functionNewCalls.js +60 -0
  33. package/lib/rules/functionNewCalls.test.d.ts +1 -0
  34. package/lib/rules/functionNewCalls.test.js +115 -0
  35. package/lib/rules/negativeZeroComparisons.js +4 -22
  36. package/lib/rules/nonOctalDecimalEscapes.d.ts +6 -0
  37. package/lib/rules/nonOctalDecimalEscapes.js +57 -0
  38. package/lib/rules/nonOctalDecimalEscapes.test.d.ts +1 -0
  39. package/lib/rules/nonOctalDecimalEscapes.test.js +69 -0
  40. package/lib/rules/numericLiteralParsing.d.ts +6 -0
  41. package/lib/rules/numericLiteralParsing.js +97 -0
  42. package/lib/rules/numericLiteralParsing.test.d.ts +1 -0
  43. package/lib/rules/numericLiteralParsing.test.js +99 -0
  44. package/lib/rules/octalNumbers.test.js +2 -2
  45. package/lib/rules/selfAssignments.d.ts +6 -0
  46. package/lib/rules/selfAssignments.js +44 -0
  47. package/lib/rules/selfAssignments.test.d.ts +1 -0
  48. package/lib/rules/selfAssignments.test.js +66 -0
  49. package/lib/rules/selfComparisons.d.ts +6 -0
  50. package/lib/rules/selfComparisons.js +41 -0
  51. package/lib/rules/selfComparisons.test.d.ts +1 -0
  52. package/lib/rules/selfComparisons.test.js +297 -0
  53. package/lib/rules/sequences.d.ts +6 -0
  54. package/lib/rules/sequences.js +34 -0
  55. package/lib/rules/sequences.test.d.ts +1 -0
  56. package/lib/rules/sequences.test.js +48 -0
  57. package/lib/rules/shadowedRestrictedNames.d.ts +6 -0
  58. package/lib/rules/shadowedRestrictedNames.js +95 -0
  59. package/lib/rules/shadowedRestrictedNames.test.d.ts +1 -0
  60. package/lib/rules/shadowedRestrictedNames.test.js +147 -0
  61. package/lib/rules/symbolDescriptions.test.js +4 -4
  62. package/lib/rules/typeofComparisons.d.ts +6 -0
  63. package/lib/rules/typeofComparisons.js +78 -0
  64. package/lib/rules/typeofComparisons.test.d.ts +1 -0
  65. package/lib/rules/typeofComparisons.test.js +85 -0
  66. package/lib/rules/unicodeBOMs.test.js +1 -0
  67. package/lib/rules/unnecessaryBlocks.d.ts +6 -0
  68. package/lib/rules/unnecessaryBlocks.js +75 -0
  69. package/lib/rules/unnecessaryBlocks.test.d.ts +1 -0
  70. package/lib/rules/unnecessaryBlocks.test.js +147 -0
  71. package/lib/rules/unnecessaryConcatenation.d.ts +6 -0
  72. package/lib/rules/unnecessaryConcatenation.js +38 -0
  73. package/lib/rules/unnecessaryConcatenation.test.d.ts +1 -0
  74. package/lib/rules/unnecessaryConcatenation.test.js +87 -0
  75. package/lib/rules/utils/discriminateAnyType.d.ts +12 -0
  76. package/lib/rules/utils/discriminateAnyType.js +41 -0
  77. package/lib/rules/utils/getThisExpression.d.ts +2 -0
  78. package/lib/rules/utils/getThisExpression.js +23 -0
  79. package/lib/rules/utils/isUnsafeAssignment.d.ts +13 -0
  80. package/lib/rules/utils/isUnsafeAssignment.js +76 -0
  81. package/lib/rules/utils/operators.d.ts +4 -0
  82. package/lib/rules/utils/operators.js +36 -0
  83. package/lib/rules/voidOperator.test.js +2 -2
  84. package/lib/utils/declarationIncludesGlobal.d.ts +2 -0
  85. package/lib/utils/declarationIncludesGlobal.js +5 -0
  86. package/lib/utils/declarationsIncludeGlobal.js +2 -5
  87. package/lib/utils/getDeclarationsIfGlobal.d.ts +2 -0
  88. package/lib/utils/getDeclarationsIfGlobal.js +7 -0
  89. package/lib/utils/hasSameTokens.d.ts +2 -0
  90. package/lib/utils/hasSameTokens.js +30 -0
  91. package/lib/utils/isGlobalDeclaration.js +2 -3
  92. package/lib/utils/isGlobalDeclarationOfName.d.ts +5 -0
  93. package/lib/utils/isGlobalDeclarationOfName.js +35 -0
  94. package/package.json +3 -3
  95. package/src/createTypeScriptFileFromProgram.ts +6 -1
  96. package/src/index.ts +3 -0
  97. package/src/language.ts +1 -0
  98. package/src/plugin.ts +30 -0
  99. package/src/rules/anyReturns.test.ts +649 -0
  100. package/src/rules/anyReturns.ts +263 -0
  101. package/src/rules/caseDuplicates.test.ts +308 -0
  102. package/src/rules/caseDuplicates.ts +57 -0
  103. package/src/rules/chainedAssignments.test.ts +24 -0
  104. package/src/rules/chainedAssignments.ts +4 -1
  105. package/src/rules/debuggerStatements.test.ts +4 -4
  106. package/src/rules/elseIfDuplicates.test.ts +177 -0
  107. package/src/rules/elseIfDuplicates.ts +69 -0
  108. package/src/rules/emptyBlocks.test.ts +136 -0
  109. package/src/rules/emptyBlocks.ts +73 -0
  110. package/src/rules/forDirections.test.ts +100 -0
  111. package/src/rules/forDirections.ts +163 -0
  112. package/src/rules/functionNewCalls.test.ts +116 -0
  113. package/src/rules/functionNewCalls.ts +75 -0
  114. package/src/rules/negativeZeroComparisons.ts +8 -27
  115. package/src/rules/nonOctalDecimalEscapes.test.ts +70 -0
  116. package/src/rules/nonOctalDecimalEscapes.ts +70 -0
  117. package/src/rules/numericLiteralParsing.test.ts +100 -0
  118. package/src/rules/numericLiteralParsing.ts +116 -0
  119. package/src/rules/octalNumbers.test.ts +2 -2
  120. package/src/rules/selfAssignments.test.ts +67 -0
  121. package/src/rules/selfAssignments.ts +50 -0
  122. package/src/rules/selfComparisons.test.ts +298 -0
  123. package/src/rules/selfComparisons.ts +45 -0
  124. package/src/rules/sequences.test.ts +49 -0
  125. package/src/rules/sequences.ts +37 -0
  126. package/src/rules/shadowedRestrictedNames.test.ts +148 -0
  127. package/src/rules/shadowedRestrictedNames.ts +105 -0
  128. package/src/rules/symbolDescriptions.test.ts +4 -4
  129. package/src/rules/typeofComparisons.test.ts +86 -0
  130. package/src/rules/typeofComparisons.ts +88 -0
  131. package/src/rules/unicodeBOMs.test.ts +1 -0
  132. package/src/rules/unnecessaryBlocks.test.ts +148 -0
  133. package/src/rules/unnecessaryBlocks.ts +89 -0
  134. package/src/rules/unnecessaryConcatenation.test.ts +88 -0
  135. package/src/rules/unnecessaryConcatenation.ts +44 -0
  136. package/src/rules/utils/discriminateAnyType.ts +66 -0
  137. package/src/rules/utils/getThisExpression.ts +24 -0
  138. package/src/rules/utils/isUnsafeAssignment.ts +113 -0
  139. package/src/rules/utils/operators.ts +39 -0
  140. package/src/rules/voidOperator.test.ts +2 -2
  141. package/src/utils/declarationIncludesGlobal.ts +9 -0
  142. package/src/utils/declarationsIncludeGlobal.ts +3 -7
  143. package/src/utils/getDeclarationsIfGlobal.ts +14 -0
  144. package/src/utils/hasSameTokens.ts +45 -0
  145. package/src/utils/isGlobalDeclaration.ts +3 -5
  146. package/src/utils/isGlobalDeclarationOfName.ts +56 -0
  147. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,105 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { getTSNodeRange } from "../getTSNodeRange.js";
4
+ import { typescriptLanguage } from "../language.js";
5
+
6
+ const restrictedNames = new Set([
7
+ "arguments",
8
+ "eval",
9
+ "Infinity",
10
+ "NaN",
11
+ "undefined",
12
+ ]);
13
+
14
+ export default typescriptLanguage.createRule({
15
+ about: {
16
+ description:
17
+ "Reports variable declarations that shadow JavaScript's restricted names.",
18
+ id: "shadowedRestrictedNames",
19
+ preset: "untyped",
20
+ },
21
+ messages: {
22
+ shadowedRestrictedName: {
23
+ primary: "This variable misleadingly shadows the global `{{ name }}`.",
24
+ secondary: [
25
+ "JavaScript has certain built-in global identifiers that are considered restricted because shadowing them can lead to confusing or erroneous code.",
26
+ "When you declare a variable with a restricted name, it shadows the global identifier and can cause unexpected behavior.",
27
+ ],
28
+ suggestions: [
29
+ "Use a different variable name that doesn't shadow a restricted name.",
30
+ ],
31
+ },
32
+ },
33
+ setup(context) {
34
+ function checkIdentifier(node: ts.Identifier): void {
35
+ if (restrictedNames.has(node.text)) {
36
+ context.report({
37
+ data: {
38
+ name: node.text,
39
+ },
40
+ message: "shadowedRestrictedName",
41
+ range: getTSNodeRange(node, context.sourceFile),
42
+ });
43
+ }
44
+ }
45
+
46
+ function checkBindingName(name: ts.BindingName): void {
47
+ if (ts.isIdentifier(name)) {
48
+ checkIdentifier(name);
49
+ } else if (
50
+ ts.isObjectBindingPattern(name) ||
51
+ ts.isArrayBindingPattern(name)
52
+ ) {
53
+ for (const element of name.elements) {
54
+ if (ts.isBindingElement(element)) {
55
+ checkBindingName(element.name);
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ function checkParameters(
62
+ parameters: ts.NodeArray<ts.ParameterDeclaration>,
63
+ ): void {
64
+ for (const parameter of parameters) {
65
+ checkBindingName(parameter.name);
66
+ }
67
+ }
68
+
69
+ return {
70
+ visitors: {
71
+ ArrowFunction: (node) => {
72
+ checkParameters(node.parameters);
73
+ },
74
+ ClassDeclaration: (node) => {
75
+ if (node.name) {
76
+ checkIdentifier(node.name);
77
+ }
78
+ },
79
+ ClassExpression: (node) => {
80
+ if (node.name) {
81
+ checkIdentifier(node.name);
82
+ }
83
+ },
84
+ FunctionDeclaration: (node) => {
85
+ if (node.name) {
86
+ checkIdentifier(node.name);
87
+ }
88
+ checkParameters(node.parameters);
89
+ },
90
+ FunctionExpression: (node) => {
91
+ if (node.name) {
92
+ checkIdentifier(node.name);
93
+ }
94
+ checkParameters(node.parameters);
95
+ },
96
+ MethodDeclaration: (node) => {
97
+ checkParameters(node.parameters);
98
+ },
99
+ VariableDeclaration: (node) => {
100
+ checkBindingName(node.name);
101
+ },
102
+ },
103
+ };
104
+ },
105
+ });
@@ -44,8 +44,8 @@ function createSymbol() {
44
44
  snapshot: `
45
45
  function createSymbol() {
46
46
  return Symbol();
47
- ~~~~~~~~
48
- Symbols without descriptions are more difficult to debug or reason about.
47
+ ~~~~~~~~
48
+ Symbols without descriptions are more difficult to debug or reason about.
49
49
  }
50
50
  `,
51
51
  },
@@ -58,8 +58,8 @@ const obj = {
58
58
  snapshot: `
59
59
  const obj = {
60
60
  key: Symbol(),
61
- ~~~~~~~~
62
- Symbols without descriptions are more difficult to debug or reason about.
61
+ ~~~~~~~~
62
+ Symbols without descriptions are more difficult to debug or reason about.
63
63
  };
64
64
  `,
65
65
  },
@@ -0,0 +1,86 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./typeofComparisons.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ if (typeof value === "") {
9
+ process();
10
+ }
11
+ `,
12
+ snapshot: `
13
+ if (typeof value === "") {
14
+ ~~
15
+ This string literal is not one that the typeof operator will ever produce.
16
+ process();
17
+ }
18
+ `,
19
+ },
20
+ {
21
+ code: `
22
+ if (typeof data != "invalid") {
23
+ reject();
24
+ }
25
+ `,
26
+ snapshot: `
27
+ if (typeof data != "invalid") {
28
+ ~~~~~~~~~
29
+ This string literal is not one that the typeof operator will ever produce.
30
+ reject();
31
+ }
32
+ `,
33
+ },
34
+ {
35
+ code: `
36
+ if ("invalid" === typeof flag) {
37
+ toggle();
38
+ }
39
+ `,
40
+ snapshot: `
41
+ if ("invalid" === typeof flag) {
42
+ ~~~~~~~~~
43
+ This string literal is not one that the typeof operator will ever produce.
44
+ toggle();
45
+ }
46
+ `,
47
+ },
48
+ {
49
+ code: `
50
+ if (typeof value === "String") {
51
+ process();
52
+ }
53
+ `,
54
+ snapshot: `
55
+ if (typeof value === "String") {
56
+ ~~~~~~~~
57
+ This string literal is not one that the typeof operator will ever produce.
58
+ process();
59
+ }
60
+ `,
61
+ },
62
+ {
63
+ code: `
64
+ const isValid = typeof input !== "array";
65
+ `,
66
+ snapshot: `
67
+ const isValid = typeof input !== "array";
68
+ ~~~~~~~
69
+ This string literal is not one that the typeof operator will ever produce.
70
+ `,
71
+ },
72
+ ],
73
+ valid: [
74
+ `if (typeof value === "string") { process(); }`,
75
+ `if (typeof variable == "undefined") { handle(); }`,
76
+ `if (typeof data != "number") { reject(); }`,
77
+ `if (typeof callback !== "function") { throw new Error("Invalid callback"); }`,
78
+ `if (typeof flag === "boolean") { toggle(); }`,
79
+ `if (typeof value === "object") { parse(); }`,
80
+ `if (typeof value === "symbol") { handle(); }`,
81
+ `if (typeof value === "bigint") { compute(); }`,
82
+ `if (typeof value === other) { process(); }`,
83
+ `if (typeof value === typeof other) { compare(); }`,
84
+ `const type = typeof value;`,
85
+ ],
86
+ });
@@ -0,0 +1,88 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { getTSNodeRange } from "../getTSNodeRange.js";
4
+ import { typescriptLanguage } from "../language.js";
5
+
6
+ const validTypeofValues = new Set([
7
+ "bigint",
8
+ "boolean",
9
+ "function",
10
+ "number",
11
+ "object",
12
+ "string",
13
+ "symbol",
14
+ "undefined",
15
+ ]);
16
+
17
+ // TODO: Reuse a shared getStaticValue-style utility?
18
+ function getStringValue(node: ts.Expression) {
19
+ return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)
20
+ ? node.text
21
+ : undefined;
22
+ }
23
+
24
+ function getTypeofOperand(node: ts.Expression) {
25
+ return ts.isTypeOfExpression(node) && node.expression;
26
+ }
27
+
28
+ export default typescriptLanguage.createRule({
29
+ about: {
30
+ description:
31
+ "Reports typeof expressions that compare impossible string literals.",
32
+ id: "typeofComparisons",
33
+ preset: "untyped",
34
+ },
35
+ messages: {
36
+ invalidValue: {
37
+ primary:
38
+ "This string literal is not one that the typeof operator will ever produce.",
39
+ secondary: [
40
+ "The typeof operator returns one of a specific set of string values.",
41
+ "Comparing typeof to an invalid string is usually a typo and will never match.",
42
+ 'The only valid values are: "bigint", "boolean", "function", "number", "object", "string", "symbol", and "undefined".',
43
+ ],
44
+ suggestions: [
45
+ "Check for typos and use one of the valid typeof return values.",
46
+ ],
47
+ },
48
+ },
49
+ setup(context) {
50
+ function checkComparison(node: ts.BinaryExpression) {
51
+ const leftTypeofOperand = getTypeofOperand(node.left);
52
+ const rightTypeofOperand = getTypeofOperand(node.right);
53
+
54
+ let comparisonValue: ts.Expression | undefined;
55
+ if (leftTypeofOperand) {
56
+ comparisonValue = node.right;
57
+ } else if (rightTypeofOperand) {
58
+ comparisonValue = node.left;
59
+ } else {
60
+ return;
61
+ }
62
+
63
+ const stringValue = getStringValue(comparisonValue);
64
+ if (stringValue != null && !validTypeofValues.has(stringValue)) {
65
+ context.report({
66
+ message: "invalidValue",
67
+ range: getTSNodeRange(comparisonValue, context.sourceFile),
68
+ });
69
+ }
70
+ }
71
+
72
+ return {
73
+ visitors: {
74
+ BinaryExpression: (node) => {
75
+ if (
76
+ node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
77
+ node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
78
+ node.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken ||
79
+ node.operatorToken.kind ===
80
+ ts.SyntaxKind.ExclamationEqualsEqualsToken
81
+ ) {
82
+ checkComparison(node);
83
+ }
84
+ },
85
+ },
86
+ };
87
+ },
88
+ });
@@ -1,3 +1,4 @@
1
+ // flint-disable-file invalidCodeLines -- This rule checks the first character of code files.
1
2
  import { ruleTester } from "./ruleTester.js";
2
3
  import rule from "./unicodeBOMs.js";
3
4
 
@@ -0,0 +1,148 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./unnecessaryBlocks.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ {
9
+ const x = 1;
10
+ }
11
+ `,
12
+ snapshot: `
13
+ {
14
+ ~
15
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
16
+ const x = 1;
17
+ }
18
+ `,
19
+ },
20
+ {
21
+ code: `
22
+ function test() {
23
+ {
24
+ const y = 2;
25
+ }
26
+ }
27
+ `,
28
+ snapshot: `
29
+ function test() {
30
+ {
31
+ ~
32
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
33
+ const y = 2;
34
+ }
35
+ }
36
+ `,
37
+ },
38
+ {
39
+ code: `
40
+ if (condition) {
41
+ doSomething();
42
+ {
43
+ const nested = true;
44
+ }
45
+ }
46
+ `,
47
+ snapshot: `
48
+ if (condition) {
49
+ doSomething();
50
+ {
51
+ ~
52
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
53
+ const nested = true;
54
+ }
55
+ }
56
+ `,
57
+ },
58
+ {
59
+ code: `
60
+ {
61
+ console.log("standalone");
62
+ }
63
+ `,
64
+ snapshot: `
65
+ {
66
+ ~
67
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
68
+ console.log("standalone");
69
+ }
70
+ `,
71
+ },
72
+ {
73
+ code: `
74
+ function outer() {
75
+ {
76
+ console.log("inner");
77
+ }
78
+ {
79
+ console.log("another");
80
+ }
81
+ }
82
+ `,
83
+ snapshot: `
84
+ function outer() {
85
+ {
86
+ ~
87
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
88
+ console.log("inner");
89
+ }
90
+ {
91
+ ~
92
+ This standalone block statement is unnecessary and doesn't change any variable scopes.
93
+ console.log("another");
94
+ }
95
+ }
96
+ `,
97
+ },
98
+ ],
99
+ valid: [
100
+ `if (condition) { doSomething(); }`,
101
+ `for (let i = 0; i < 10; i++) { console.log(i); }`,
102
+ `while (condition) { doWork(); }`,
103
+ `do { doWork(); } while (condition);`,
104
+ `function test() { return 42; }`,
105
+ `const arrow = () => { return 1; };`,
106
+ `
107
+ switch (value) {
108
+ case 1: {
109
+ const x = 1;
110
+ break;
111
+ }
112
+ case 2: {
113
+ const y = 2;
114
+ break;
115
+ }
116
+ }
117
+ `,
118
+ `
119
+ try {
120
+ doSomething();
121
+ } catch (error) {
122
+ handleError(error);
123
+ }
124
+ `,
125
+ `
126
+ class MyClass {
127
+ method() {
128
+ return 1;
129
+ }
130
+ }
131
+ `,
132
+ `
133
+ for (const item of items) {
134
+ processItem(item);
135
+ }
136
+ `,
137
+ `
138
+ for (const key in object) {
139
+ processKey(key);
140
+ }
141
+ `,
142
+ `
143
+ label: {
144
+ break label;
145
+ }
146
+ `,
147
+ ],
148
+ });
@@ -0,0 +1,89 @@
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 standalone block statements that don't create a meaningful scope.",
9
+ id: "unnecessaryBlocks",
10
+ preset: "stylistic",
11
+ },
12
+ messages: {
13
+ unnecessaryBlock: {
14
+ primary:
15
+ "This standalone block statement is unnecessary and doesn't change any variable scopes.",
16
+ secondary: [
17
+ "Standalone block statements that aren't part of control flow (if/else, loops, switch) or don't create a meaningful lexical scope can be confusing and should be avoided.",
18
+ "In modern JavaScript and TypeScript, blocks primarily serve to create lexical scope for `let` and `const` variables, but this is often better achieved through other means.",
19
+ ],
20
+ suggestions: [
21
+ "Remove the block statement and move its contents to the parent scope.",
22
+ "If you need lexical scoping, consider using an IIFE (Immediately Invoked Function Expression) or extracting to a separate function.",
23
+ ],
24
+ },
25
+ },
26
+ setup(context) {
27
+ function isValidBlock(node: ts.Block): boolean {
28
+ const parent = node.parent;
29
+
30
+ // Valid blocks: function bodies, arrow functions, class/interface bodies, etc.
31
+ if (
32
+ ts.isFunctionDeclaration(parent) ||
33
+ ts.isFunctionExpression(parent) ||
34
+ ts.isArrowFunction(parent) ||
35
+ ts.isMethodDeclaration(parent) ||
36
+ ts.isConstructorDeclaration(parent) ||
37
+ ts.isGetAccessorDeclaration(parent) ||
38
+ ts.isSetAccessorDeclaration(parent) ||
39
+ ts.isModuleBlock(parent)
40
+ ) {
41
+ return true;
42
+ }
43
+
44
+ // Valid blocks: control flow statements
45
+ if (
46
+ ts.isIfStatement(parent) ||
47
+ ts.isForStatement(parent) ||
48
+ ts.isForInStatement(parent) ||
49
+ ts.isForOfStatement(parent) ||
50
+ ts.isWhileStatement(parent) ||
51
+ ts.isDoStatement(parent) ||
52
+ ts.isWithStatement(parent) ||
53
+ ts.isTryStatement(parent) ||
54
+ ts.isCatchClause(parent)
55
+ ) {
56
+ return true;
57
+ }
58
+
59
+ // Valid block: switch case/default clause with block
60
+ // In ES6+, blocks in switch cases create scope for let/const
61
+ if (ts.isCaseClause(parent) || ts.isDefaultClause(parent)) {
62
+ return true;
63
+ }
64
+
65
+ // Valid block: labeled statement
66
+ if (ts.isLabeledStatement(parent)) {
67
+ return true;
68
+ }
69
+
70
+ return false;
71
+ }
72
+
73
+ return {
74
+ visitors: {
75
+ Block: (node) => {
76
+ if (!isValidBlock(node)) {
77
+ context.report({
78
+ message: "unnecessaryBlock",
79
+ range: {
80
+ begin: node.getStart(context.sourceFile),
81
+ end: node.getStart(context.sourceFile) + 1,
82
+ },
83
+ });
84
+ }
85
+ },
86
+ },
87
+ };
88
+ },
89
+ });
@@ -0,0 +1,88 @@
1
+ import { ruleTester } from "./ruleTester.js";
2
+ import rule from "./unnecessaryConcatenation.js";
3
+
4
+ ruleTester.describe(rule, {
5
+ invalid: [
6
+ {
7
+ code: `
8
+ const message = "Hello" + "World";
9
+ `,
10
+ snapshot: `
11
+ const message = "Hello" + "World";
12
+ ~
13
+ This string concatenation can be streamlined into a single string literal.
14
+ `,
15
+ },
16
+ {
17
+ code: `
18
+ const path = 'foo' + 'bar';
19
+ `,
20
+ snapshot: `
21
+ const path = 'foo' + 'bar';
22
+ ~
23
+ This string concatenation can be streamlined into a single string literal.
24
+ `,
25
+ },
26
+ {
27
+ code: `
28
+ const result = "abc" + "def" + "ghi";
29
+ `,
30
+ snapshot: `
31
+ const result = "abc" + "def" + "ghi";
32
+ ~
33
+ This string concatenation can be streamlined into a single string literal.
34
+ `,
35
+ },
36
+ {
37
+ code: `
38
+ const text = "first" + "second";
39
+ `,
40
+ snapshot: `
41
+ const text = "first" + "second";
42
+ ~
43
+ This string concatenation can be streamlined into a single string literal.
44
+ `,
45
+ },
46
+ {
47
+ code: `
48
+ const mixed = 'single' + "double";
49
+ `,
50
+ snapshot: `
51
+ const mixed = 'single' + "double";
52
+ ~
53
+ This string concatenation can be streamlined into a single string literal.
54
+ `,
55
+ },
56
+ {
57
+ code: `
58
+ const withSpace = "Hello " + "World";
59
+ `,
60
+ snapshot: `
61
+ const withSpace = "Hello " + "World";
62
+ ~
63
+ This string concatenation can be streamlined into a single string literal.
64
+ `,
65
+ },
66
+ {
67
+ code: `
68
+ const longString = "This is a very long string that " +
69
+ "continues on the next line";
70
+ `,
71
+ snapshot: `
72
+ const longString = "This is a very long string that " +
73
+ ~
74
+ This string concatenation can be streamlined into a single string literal.
75
+ "continues on the next line";
76
+ `,
77
+ },
78
+ ],
79
+ valid: [
80
+ `const message = "Hello" + variable;`,
81
+ `const message = variable + "World";`,
82
+ `const result = variable1 + variable2;`,
83
+ `const template = \`Hello\${name}World\`;`,
84
+ `const number = 1 + 2;`,
85
+ `const mixed = "Hello" + getName() + "World";`,
86
+ `const value = "prefix" + getValue();`,
87
+ ],
88
+ });
@@ -0,0 +1,44 @@
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 string concatenation using the + operator when both operands are string literals.",
10
+ id: "unnecessaryConcatenation",
11
+ preset: "stylistic",
12
+ },
13
+ messages: {
14
+ unnecessaryConcatenation: {
15
+ primary:
16
+ "This string concatenation can be streamlined into a single string literal.",
17
+ secondary: [
18
+ "Concatenating string literals with the + operator is unnecessary and reduces code readability.",
19
+ "String literals can be combined into a single literal, which is clearer and potentially more performant.",
20
+ ],
21
+ suggestions: [
22
+ "Combine the string literals into a single string literal.",
23
+ ],
24
+ },
25
+ },
26
+ setup(context) {
27
+ return {
28
+ visitors: {
29
+ BinaryExpression: (node) => {
30
+ if (
31
+ node.operatorToken.kind === ts.SyntaxKind.PlusToken &&
32
+ ts.isStringLiteral(node.left) &&
33
+ ts.isStringLiteral(node.right)
34
+ ) {
35
+ context.report({
36
+ message: "unnecessaryConcatenation",
37
+ range: getTSNodeRange(node.operatorToken, context.sourceFile),
38
+ });
39
+ }
40
+ },
41
+ },
42
+ };
43
+ },
44
+ });