@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,135 @@
1
+ import rule from "./emptyBlocks.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ if (condition) {}
8
+ `,
9
+ snapshot: `
10
+ if (condition) {}
11
+ ~~
12
+ Empty block statements should be removed or contain code.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ if (condition) {
18
+ } else {}
19
+ `,
20
+ snapshot: `
21
+ if (condition) {
22
+ ~
23
+ Empty block statements should be removed or contain code.
24
+ } else {}
25
+ ~
26
+ ~~
27
+ Empty block statements should be removed or contain code.
28
+ `,
29
+ },
30
+ {
31
+ code: `
32
+ while (condition) {}
33
+ `,
34
+ snapshot: `
35
+ while (condition) {}
36
+ ~~
37
+ Empty block statements should be removed or contain code.
38
+ `,
39
+ },
40
+ {
41
+ code: `
42
+ for (let i = 0; i < 10; i++) {}
43
+ `,
44
+ snapshot: `
45
+ for (let i = 0; i < 10; i++) {}
46
+ ~~
47
+ Empty block statements should be removed or contain code.
48
+ `,
49
+ },
50
+ {
51
+ code: `
52
+ switch (value) {}
53
+ `,
54
+ snapshot: `
55
+ switch (value) {}
56
+ ~~
57
+ Empty block statements should be removed or contain code.
58
+ `,
59
+ },
60
+ {
61
+ code: `
62
+ do {} while (condition);
63
+ `,
64
+ snapshot: `
65
+ do {} while (condition);
66
+ ~~
67
+ Empty block statements should be removed or contain code.
68
+ `,
69
+ },
70
+ {
71
+ code: `
72
+ if (x) {
73
+ doSomething();
74
+ } else if (y) {}
75
+ `,
76
+ snapshot: `
77
+ if (x) {
78
+ doSomething();
79
+ } else if (y) {}
80
+ ~~
81
+ Empty block statements should be removed or contain code.
82
+ `,
83
+ },
84
+ {
85
+ code: `
86
+ try {
87
+ doSomething();
88
+ } finally {}
89
+ `,
90
+ snapshot: `
91
+ try {
92
+ doSomething();
93
+ } finally {}
94
+ ~~
95
+ Empty block statements should be removed or contain code.
96
+ `,
97
+ },
98
+ ],
99
+ valid: [
100
+ `if (condition) { doSomething(); }`,
101
+ `while (condition) { doSomething(); }`,
102
+ `for (let i = 0; i < 10; i++) { doSomething(); }`,
103
+ `switch (value) { case 1: break; }`,
104
+ `do { doSomething(); } while (condition);`,
105
+ `function test() {}`,
106
+ `const fn = function() {};`,
107
+ `const arrow = () => {};`,
108
+ `class MyClass { method() {} }`,
109
+ `class MyClass { constructor() {} }`,
110
+ `class MyClass { get value() {} }`,
111
+ `class MyClass { set value(v) {} }`,
112
+ `
113
+ if (condition) {
114
+ // Intentionally empty
115
+ }
116
+ `,
117
+ `
118
+ while (condition) {
119
+ /* Do nothing */
120
+ }
121
+ `,
122
+ `
123
+ try {
124
+ doSomething();
125
+ } catch (error) {}
126
+ `,
127
+ `
128
+ try {
129
+ doSomething();
130
+ } catch (error) {
131
+ // Ignore errors
132
+ }
133
+ `,
134
+ ],
135
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports for loops with counter variables that move in the wrong direction.";
3
+ readonly id: "forDirections";
4
+ readonly preset: "stylistic";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "wrongDirection", undefined>;
6
+ export default _default;
@@ -0,0 +1,124 @@
1
+ import * as ts from "typescript";
2
+ import { getTSNodeRange } from "../getTSNodeRange.js";
3
+ import { typescriptLanguage } from "../language.js";
4
+ function getConditionDirection(condition, counterName) {
5
+ if (!ts.isBinaryExpression(condition)) {
6
+ return undefined;
7
+ }
8
+ const leftName = getCounterName(condition.left);
9
+ const rightName = getCounterName(condition.right);
10
+ const isCounterLeft = leftName === counterName;
11
+ const isCounterRight = rightName === counterName;
12
+ if (!isCounterLeft && !isCounterRight) {
13
+ return undefined;
14
+ }
15
+ const { operatorToken } = condition;
16
+ if (isCounterLeft) {
17
+ if (operatorToken.kind === ts.SyntaxKind.LessThanToken ||
18
+ operatorToken.kind === ts.SyntaxKind.LessThanEqualsToken) {
19
+ return 1;
20
+ }
21
+ if (operatorToken.kind === ts.SyntaxKind.GreaterThanToken ||
22
+ operatorToken.kind === ts.SyntaxKind.GreaterThanEqualsToken) {
23
+ return -1;
24
+ }
25
+ }
26
+ else {
27
+ if (operatorToken.kind === ts.SyntaxKind.LessThanToken ||
28
+ operatorToken.kind === ts.SyntaxKind.LessThanEqualsToken) {
29
+ return -1;
30
+ }
31
+ if (operatorToken.kind === ts.SyntaxKind.GreaterThanToken ||
32
+ operatorToken.kind === ts.SyntaxKind.GreaterThanEqualsToken) {
33
+ return 1;
34
+ }
35
+ }
36
+ return undefined;
37
+ }
38
+ function getCounterName(node) {
39
+ return ts.isIdentifier(node) ? node.text : undefined;
40
+ }
41
+ function getUpdateDirection(update) {
42
+ if (ts.isPostfixUnaryExpression(update) ||
43
+ ts.isPrefixUnaryExpression(update)) {
44
+ switch (update.operator) {
45
+ case ts.SyntaxKind.MinusMinusToken:
46
+ return -1;
47
+ case ts.SyntaxKind.PlusPlusToken:
48
+ return 1;
49
+ default:
50
+ return undefined;
51
+ }
52
+ }
53
+ if (ts.isBinaryExpression(update)) {
54
+ const { operatorToken, right } = update;
55
+ if (operatorToken.kind === ts.SyntaxKind.PlusEqualsToken ||
56
+ operatorToken.kind === ts.SyntaxKind.MinusEqualsToken) {
57
+ if (ts.isNumericLiteral(right)) {
58
+ const value = Number(right.text);
59
+ if (operatorToken.kind === ts.SyntaxKind.PlusEqualsToken) {
60
+ return value > 0 ? 1 : value < 0 ? -1 : 0;
61
+ }
62
+ return value > 0 ? -1 : value < 0 ? 1 : 0;
63
+ }
64
+ if (ts.isPrefixUnaryExpression(right) &&
65
+ right.operator === ts.SyntaxKind.MinusToken &&
66
+ ts.isNumericLiteral(right.operand)) {
67
+ const value = Number(right.operand.text);
68
+ if (operatorToken.kind === ts.SyntaxKind.PlusEqualsToken) {
69
+ return value > 0 ? -1 : 1;
70
+ }
71
+ return value > 0 ? 1 : -1;
72
+ }
73
+ }
74
+ }
75
+ return undefined;
76
+ }
77
+ export default typescriptLanguage.createRule({
78
+ about: {
79
+ description: "Reports for loops with counter variables that move in the wrong direction.",
80
+ id: "forDirections",
81
+ preset: "stylistic",
82
+ },
83
+ messages: {
84
+ wrongDirection: {
85
+ primary: "The update moves the counter in the wrong direction for this loop condition.",
86
+ secondary: [
87
+ "A for loop with a counter that moves in the wrong direction relative to its stop condition will run infinitely or never execute.",
88
+ "When the counter increases but the condition expects it to decrease (or vice versa), the loop logic is incorrect.",
89
+ ],
90
+ suggestions: [
91
+ "Verify the loop counter update direction matches the condition.",
92
+ ],
93
+ },
94
+ },
95
+ setup(context) {
96
+ return {
97
+ visitors: {
98
+ ForStatement: (node) => {
99
+ if (!node.condition ||
100
+ !node.incrementor ||
101
+ !node.initializer ||
102
+ !ts.isVariableDeclarationList(node.initializer)) {
103
+ return;
104
+ }
105
+ const declaration = node.initializer.declarations.at(0);
106
+ if (!declaration || !ts.isIdentifier(declaration.name)) {
107
+ return;
108
+ }
109
+ const updateDirection = getUpdateDirection(node.incrementor);
110
+ if (!updateDirection) {
111
+ return;
112
+ }
113
+ const conditionDirection = getConditionDirection(node.condition, declaration.name.text);
114
+ if (updateDirection !== conditionDirection) {
115
+ context.report({
116
+ message: "wrongDirection",
117
+ range: getTSNodeRange(node.incrementor, context.sourceFile),
118
+ });
119
+ }
120
+ },
121
+ },
122
+ };
123
+ },
124
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ import rule from "./forDirections.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ for (let i = 0; i < 10; i--) {}
8
+ `,
9
+ snapshot: `
10
+ for (let i = 0; i < 10; i--) {}
11
+ ~~~
12
+ The update moves the counter in the wrong direction for this loop condition.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ for (let index = 0; index < 10; index--) {}
18
+ `,
19
+ snapshot: `
20
+ for (let index = 0; index < 10; index--) {}
21
+ ~~~~~~~
22
+ The update moves the counter in the wrong direction for this loop condition.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ for (let index = 10; index >= 0; index++) {}
28
+ `,
29
+ snapshot: `
30
+ for (let index = 10; index >= 0; index++) {}
31
+ ~~~~~~~
32
+ The update moves the counter in the wrong direction for this loop condition.
33
+ `,
34
+ },
35
+ {
36
+ code: `
37
+ for (let index = 0; index > 10; index++) {}
38
+ `,
39
+ snapshot: `
40
+ for (let index = 0; index > 10; index++) {}
41
+ ~~~~~~~
42
+ The update moves the counter in the wrong direction for this loop condition.
43
+ `,
44
+ },
45
+ {
46
+ code: `
47
+ for (let index = 0; 10 > index; index--) {}
48
+ `,
49
+ snapshot: `
50
+ for (let index = 0; 10 > index; index--) {}
51
+ ~~~~~~~
52
+ The update moves the counter in the wrong direction for this loop condition.
53
+ `,
54
+ },
55
+ {
56
+ code: `
57
+ for (let index = 0; index < 10; index -= 1) {}
58
+ `,
59
+ snapshot: `
60
+ for (let index = 0; index < 10; index -= 1) {}
61
+ ~~~~~~~~~~
62
+ The update moves the counter in the wrong direction for this loop condition.
63
+ `,
64
+ },
65
+ {
66
+ code: `
67
+ for (let index = 10; index > 0; index += 1) {}
68
+ `,
69
+ snapshot: `
70
+ for (let index = 10; index > 0; index += 1) {}
71
+ ~~~~~~~~~~
72
+ The update moves the counter in the wrong direction for this loop condition.
73
+ `,
74
+ },
75
+ {
76
+ code: `
77
+ for (let index = 0; index < 10; index += -1) {}
78
+ `,
79
+ snapshot: `
80
+ for (let index = 0; index < 10; index += -1) {}
81
+ ~~~~~~~~~~~
82
+ The update moves the counter in the wrong direction for this loop condition.
83
+ `,
84
+ },
85
+ ],
86
+ valid: [
87
+ `for (let i = 0; i < 10; i++) { }`,
88
+ `for (let index = 0; index < 10; index++) { }`,
89
+ `for (let index = 10; index >= 0; index--) { }`,
90
+ `for (let index = 0; 10 > index; index++) { }`,
91
+ `for (let index = 10; 0 < index; index--) { }`,
92
+ `for (let index = 10; index >= 0; index += step) { }`,
93
+ `for (let index = 0; index <= 10; index -= 0) { }`,
94
+ `for (let index = 0; index < 10; index += 1) { }`,
95
+ `for (let index = 10; index > 0; index -= 1) { }`,
96
+ `for (let index = 0; index < 10; index += 2) { }`,
97
+ `for (let index = 10; index > 0; index -= 2) { }`,
98
+ ],
99
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports using the Function constructor to create functions from strings.";
3
+ readonly id: "functionNewCalls";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noFunctionConstructor", undefined>;
6
+ export default _default;
@@ -0,0 +1,60 @@
1
+ import * as ts from "typescript";
2
+ import { getTSNodeRange } from "../getTSNodeRange.js";
3
+ import { typescriptLanguage } from "../language.js";
4
+ import { isGlobalDeclaration } from "../utils/isGlobalDeclaration.js";
5
+ import { isGlobalDeclarationOfName } from "../utils/isGlobalDeclarationOfName.js";
6
+ export default typescriptLanguage.createRule({
7
+ about: {
8
+ description: "Reports using the Function constructor to create functions from strings.",
9
+ id: "functionNewCalls",
10
+ preset: "logical",
11
+ },
12
+ messages: {
13
+ noFunctionConstructor: {
14
+ primary: "Dynamically creating functions with the Function constructor is insecure and slow.",
15
+ secondary: [
16
+ "Using the `Function` constructor to create functions from strings is similar to `eval()` and introduces security risks and performance issues.",
17
+ "Code passed to the Function constructor is executed in the global scope, making it harder to optimize and potentially allowing arbitrary code execution if user input is involved.",
18
+ ],
19
+ suggestions: [
20
+ "Define functions using function declarations (`function name() {}`) or arrow functions (`() => {}`) instead.",
21
+ "If dynamic code generation is truly necessary, consider safer alternatives like passing functions as parameters or using a more constrained domain-specific approach.",
22
+ ],
23
+ },
24
+ },
25
+ setup(context) {
26
+ function checkNode(node) {
27
+ if (isFunctionConstructor(node)) {
28
+ context.report({
29
+ message: "noFunctionConstructor",
30
+ range: getTSNodeRange(node.expression, context.sourceFile),
31
+ });
32
+ }
33
+ }
34
+ function isFunctionConstructor(node) {
35
+ if (ts.isIdentifier(node.expression)) {
36
+ if (isGlobalDeclarationOfName(node.expression, "Function", context.typeChecker)) {
37
+ return true;
38
+ }
39
+ }
40
+ else if (ts.isPropertyAccessExpression(node.expression) &&
41
+ isGlobalDeclaration(node.expression, context.typeChecker)) {
42
+ const propertyName = node.expression.name.text;
43
+ if (propertyName !== "Function") {
44
+ return false;
45
+ }
46
+ const object = node.expression.expression;
47
+ if (ts.isIdentifier(object)) {
48
+ return object.text === "globalThis" || object.text === "window";
49
+ }
50
+ }
51
+ return false;
52
+ }
53
+ return {
54
+ visitors: {
55
+ CallExpression: checkNode,
56
+ NewExpression: checkNode,
57
+ },
58
+ };
59
+ },
60
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,115 @@
1
+ import rule from "./functionNewCalls.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ const fn = new Function("a", "b", "return a + b");
8
+ `,
9
+ snapshot: `
10
+ const fn = new Function("a", "b", "return a + b");
11
+ ~~~~~~~~
12
+ Dynamically creating functions with the Function constructor is insecure and slow.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ const fn = Function("a", "return a");
18
+ `,
19
+ snapshot: `
20
+ const fn = Function("a", "return a");
21
+ ~~~~~~~~
22
+ Dynamically creating functions with the Function constructor is insecure and slow.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ const fn = new Function("return 1");
28
+ `,
29
+ snapshot: `
30
+ const fn = new Function("return 1");
31
+ ~~~~~~~~
32
+ Dynamically creating functions with the Function constructor is insecure and slow.
33
+ `,
34
+ },
35
+ {
36
+ code: `
37
+ const fn = Function();
38
+ `,
39
+ snapshot: `
40
+ const fn = Function();
41
+ ~~~~~~~~
42
+ Dynamically creating functions with the Function constructor is insecure and slow.
43
+ `,
44
+ },
45
+ {
46
+ code: `
47
+ const fn = new globalThis.Function("return 1");
48
+ `,
49
+ snapshot: `
50
+ const fn = new globalThis.Function("return 1");
51
+ ~~~~~~~~~~~~~~~~~~~
52
+ Dynamically creating functions with the Function constructor is insecure and slow.
53
+ `,
54
+ },
55
+ {
56
+ code: `
57
+ const fn = globalThis.Function("return 1");
58
+ `,
59
+ snapshot: `
60
+ const fn = globalThis.Function("return 1");
61
+ ~~~~~~~~~~~~~~~~~~~
62
+ Dynamically creating functions with the Function constructor is insecure and slow.
63
+ `,
64
+ },
65
+ {
66
+ code: `
67
+ const fn = new window.Function("return 1");
68
+ `,
69
+ snapshot: `
70
+ const fn = new window.Function("return 1");
71
+ ~~~~~~~~~~~~~~~
72
+ Dynamically creating functions with the Function constructor is insecure and slow.
73
+ `,
74
+ },
75
+ {
76
+ code: `
77
+ const fn = window.Function("return 1");
78
+ `,
79
+ snapshot: `
80
+ const fn = window.Function("return 1");
81
+ ~~~~~~~~~~~~~~~
82
+ Dynamically creating functions with the Function constructor is insecure and slow.
83
+ `,
84
+ },
85
+ {
86
+ code: `
87
+ const result = new Function("a", "b", "return a + b")(1, 2);
88
+ `,
89
+ snapshot: `
90
+ const result = new Function("a", "b", "return a + b")(1, 2);
91
+ ~~~~~~~~
92
+ Dynamically creating functions with the Function constructor is insecure and slow.
93
+ `,
94
+ },
95
+ {
96
+ code: `
97
+ const CustomFunction = Function;
98
+ const fn = new CustomFunction();
99
+ `,
100
+ snapshot: `
101
+ const CustomFunction = Function;
102
+ const fn = new CustomFunction();
103
+ ~~~~~~~~~~~~~~
104
+ Dynamically creating functions with the Function constructor is insecure and slow.
105
+ `,
106
+ },
107
+ ],
108
+ valid: [
109
+ `const fn = function(a, b) { return a + b; };`,
110
+ `const fn = (a, b) => a + b;`,
111
+ `function add(a, b) { return a + b; }`,
112
+ `class MyFunction {}`,
113
+ `const fn = new MyFunction();`,
114
+ ],
115
+ });
@@ -1,24 +1,6 @@
1
1
  import * as ts from "typescript";
2
2
  import { typescriptLanguage } from "../language.js";
3
- const comparisonOperators = new Set([
4
- ts.SyntaxKind.EqualsEqualsEqualsToken,
5
- ts.SyntaxKind.EqualsEqualsToken,
6
- ts.SyntaxKind.ExclamationEqualsEqualsToken,
7
- ts.SyntaxKind.ExclamationEqualsToken,
8
- ts.SyntaxKind.GreaterThanEqualsToken,
9
- ts.SyntaxKind.GreaterThanToken,
10
- ts.SyntaxKind.LessThanEqualsToken,
11
- ts.SyntaxKind.LessThanToken,
12
- ]);
13
- function isEqualityOperator(operator) {
14
- return (operator === "===" ||
15
- operator === "==" ||
16
- operator === "!==" ||
17
- operator === "!=");
18
- }
19
- function isNegatedEqualityOperator(operator) {
20
- return operator === "!==" || operator === "!=";
21
- }
3
+ import { isComparisonOperator, isEqualityOperator, isNegatedEqualityOperator, } from "./utils/operators.js";
22
4
  function isNegativeZero(node) {
23
5
  return (ts.isPrefixUnaryExpression(node) &&
24
6
  node.operator === ts.SyntaxKind.MinusToken &&
@@ -51,7 +33,7 @@ export default typescriptLanguage.createRule({
51
33
  return {
52
34
  visitors: {
53
35
  BinaryExpression: (node) => {
54
- if (!comparisonOperators.has(node.operatorToken.kind) ||
36
+ if (!isComparisonOperator(node.operatorToken) ||
55
37
  (!isNegativeZero(node.left) && !isNegativeZero(node.right))) {
56
38
  return;
57
39
  }
@@ -66,12 +48,12 @@ export default typescriptLanguage.createRule({
66
48
  },
67
49
  message: "unexpectedNegativeZeroComparison",
68
50
  range,
69
- suggestions: isEqualityOperator(operator)
51
+ suggestions: isEqualityOperator(node.operatorToken)
70
52
  ? [
71
53
  {
72
54
  id: "useObjectIs",
73
55
  range,
74
- text: generateObjectIsText(node, isNegatedEqualityOperator(operator)),
56
+ text: generateObjectIsText(node, isNegatedEqualityOperator(node.operatorToken)),
75
57
  },
76
58
  ]
77
59
  : undefined,
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports non-octal decimal escape sequences (\\8 and \\9) in string literals.";
3
+ readonly id: "nonOctalDecimalEscapes";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "unexpectedEscape", undefined>;
6
+ export default _default;
@@ -0,0 +1,57 @@
1
+ import { typescriptLanguage } from "../language.js";
2
+ const nonOctalDecimalEscapePattern = /\\[89]/g;
3
+ export default typescriptLanguage.createRule({
4
+ about: {
5
+ description: "Reports non-octal decimal escape sequences (\\8 and \\9) in string literals.",
6
+ id: "nonOctalDecimalEscapes",
7
+ preset: "logical",
8
+ },
9
+ messages: {
10
+ unexpectedEscape: {
11
+ primary: "Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.",
12
+ secondary: [
13
+ "Non-octal decimal escape sequences \\8 and \\9 are legacy features that are treated as identity escapes (the same as the literal digits).",
14
+ "They are optional in the ECMAScript specification and not supported in strict mode in all environments.",
15
+ "Use the literal digits directly instead.",
16
+ ],
17
+ suggestions: [
18
+ "Remove the backslash to use the literal digit.",
19
+ "If an escape sequence is needed, use a Unicode escape like \\u0038 or \\u0039.",
20
+ ],
21
+ },
22
+ },
23
+ setup(context) {
24
+ function checkNode(node) {
25
+ const text = node.getText(context.sourceFile);
26
+ const matches = [...text.matchAll(nonOctalDecimalEscapePattern)];
27
+ for (const match of matches) {
28
+ // Ignore escapes where the backslash itself is escaped (e.g. "\\\\8" -> literal "\\8").
29
+ // Count the number of consecutive backslashes immediately before the match.
30
+ const matchIndex = match.index;
31
+ let backslashesBefore = 0;
32
+ for (let i = matchIndex - 1; i >= 0 && text[i] === "\\"; i--) {
33
+ backslashesBefore++;
34
+ }
35
+ // If there is an odd number of backslashes before this one, the backslash is escaped
36
+ // and should not be treated as an escape sequence.
37
+ if (backslashesBefore % 2 === 1) {
38
+ continue;
39
+ }
40
+ const start = node.getStart(context.sourceFile) + matchIndex;
41
+ context.report({
42
+ message: "unexpectedEscape",
43
+ range: {
44
+ begin: start,
45
+ end: start + match[0].length,
46
+ },
47
+ });
48
+ }
49
+ }
50
+ return {
51
+ visitors: {
52
+ NoSubstitutionTemplateLiteral: checkNode,
53
+ StringLiteral: checkNode,
54
+ },
55
+ };
56
+ },
57
+ });
@@ -0,0 +1 @@
1
+ export {};