@flint.fyi/ts 0.14.3 → 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.
Files changed (101) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/lib/createTypeScriptFileFromProgram.js +1 -0
  3. package/lib/language.d.ts +1 -0
  4. package/lib/plugin.d.ts +44 -0
  5. package/lib/plugin.js +22 -0
  6. package/lib/rules/anyReturns.d.ts +6 -0
  7. package/lib/rules/anyReturns.js +187 -0
  8. package/lib/rules/anyReturns.test.d.ts +1 -0
  9. package/lib/rules/anyReturns.test.js +647 -0
  10. package/lib/rules/caseDuplicates.d.ts +6 -0
  11. package/lib/rules/caseDuplicates.js +49 -0
  12. package/lib/rules/caseDuplicates.test.d.ts +1 -0
  13. package/lib/rules/caseDuplicates.test.js +307 -0
  14. package/lib/rules/elseIfDuplicates.d.ts +6 -0
  15. package/lib/rules/elseIfDuplicates.js +53 -0
  16. package/lib/rules/elseIfDuplicates.test.d.ts +1 -0
  17. package/lib/rules/elseIfDuplicates.test.js +176 -0
  18. package/lib/rules/forDirections.d.ts +6 -0
  19. package/lib/rules/forDirections.js +124 -0
  20. package/lib/rules/forDirections.test.d.ts +1 -0
  21. package/lib/rules/forDirections.test.js +99 -0
  22. package/lib/rules/functionNewCalls.d.ts +6 -0
  23. package/lib/rules/functionNewCalls.js +60 -0
  24. package/lib/rules/functionNewCalls.test.d.ts +1 -0
  25. package/lib/rules/functionNewCalls.test.js +115 -0
  26. package/lib/rules/negativeZeroComparisons.js +4 -22
  27. package/lib/rules/nonOctalDecimalEscapes.d.ts +6 -0
  28. package/lib/rules/nonOctalDecimalEscapes.js +57 -0
  29. package/lib/rules/nonOctalDecimalEscapes.test.d.ts +1 -0
  30. package/lib/rules/nonOctalDecimalEscapes.test.js +69 -0
  31. package/lib/rules/numericLiteralParsing.d.ts +6 -0
  32. package/lib/rules/numericLiteralParsing.js +97 -0
  33. package/lib/rules/numericLiteralParsing.test.d.ts +1 -0
  34. package/lib/rules/numericLiteralParsing.test.js +99 -0
  35. package/lib/rules/selfAssignments.d.ts +6 -0
  36. package/lib/rules/selfAssignments.js +44 -0
  37. package/lib/rules/selfAssignments.test.d.ts +1 -0
  38. package/lib/rules/selfAssignments.test.js +66 -0
  39. package/lib/rules/selfComparisons.d.ts +6 -0
  40. package/lib/rules/selfComparisons.js +41 -0
  41. package/lib/rules/selfComparisons.test.d.ts +1 -0
  42. package/lib/rules/selfComparisons.test.js +297 -0
  43. package/lib/rules/sequences.d.ts +6 -0
  44. package/lib/rules/sequences.js +34 -0
  45. package/lib/rules/sequences.test.d.ts +1 -0
  46. package/lib/rules/sequences.test.js +48 -0
  47. package/lib/rules/typeofComparisons.d.ts +6 -0
  48. package/lib/rules/typeofComparisons.js +78 -0
  49. package/lib/rules/typeofComparisons.test.d.ts +1 -0
  50. package/lib/rules/typeofComparisons.test.js +85 -0
  51. package/lib/rules/utils/discriminateAnyType.d.ts +12 -0
  52. package/lib/rules/utils/discriminateAnyType.js +41 -0
  53. package/lib/rules/utils/getThisExpression.d.ts +2 -0
  54. package/lib/rules/utils/getThisExpression.js +23 -0
  55. package/lib/rules/utils/isUnsafeAssignment.d.ts +13 -0
  56. package/lib/rules/utils/isUnsafeAssignment.js +76 -0
  57. package/lib/rules/utils/operators.d.ts +4 -0
  58. package/lib/rules/utils/operators.js +36 -0
  59. package/lib/utils/declarationIncludesGlobal.d.ts +2 -0
  60. package/lib/utils/declarationIncludesGlobal.js +5 -0
  61. package/lib/utils/declarationsIncludeGlobal.js +2 -5
  62. package/lib/utils/hasSameTokens.d.ts +2 -0
  63. package/lib/utils/hasSameTokens.js +30 -0
  64. package/lib/utils/isGlobalDeclarationOfName.d.ts +5 -0
  65. package/lib/utils/isGlobalDeclarationOfName.js +31 -0
  66. package/package.json +1 -1
  67. package/src/createTypeScriptFileFromProgram.ts +1 -0
  68. package/src/language.ts +1 -0
  69. package/src/plugin.ts +22 -0
  70. package/src/rules/anyReturns.test.ts +649 -0
  71. package/src/rules/anyReturns.ts +263 -0
  72. package/src/rules/caseDuplicates.test.ts +308 -0
  73. package/src/rules/caseDuplicates.ts +57 -0
  74. package/src/rules/elseIfDuplicates.test.ts +177 -0
  75. package/src/rules/elseIfDuplicates.ts +69 -0
  76. package/src/rules/forDirections.test.ts +100 -0
  77. package/src/rules/forDirections.ts +163 -0
  78. package/src/rules/functionNewCalls.test.ts +116 -0
  79. package/src/rules/functionNewCalls.ts +75 -0
  80. package/src/rules/negativeZeroComparisons.ts +8 -27
  81. package/src/rules/nonOctalDecimalEscapes.test.ts +70 -0
  82. package/src/rules/nonOctalDecimalEscapes.ts +70 -0
  83. package/src/rules/numericLiteralParsing.test.ts +100 -0
  84. package/src/rules/numericLiteralParsing.ts +116 -0
  85. package/src/rules/selfAssignments.test.ts +67 -0
  86. package/src/rules/selfAssignments.ts +50 -0
  87. package/src/rules/selfComparisons.test.ts +298 -0
  88. package/src/rules/selfComparisons.ts +45 -0
  89. package/src/rules/sequences.test.ts +49 -0
  90. package/src/rules/sequences.ts +37 -0
  91. package/src/rules/typeofComparisons.test.ts +86 -0
  92. package/src/rules/typeofComparisons.ts +88 -0
  93. package/src/rules/utils/discriminateAnyType.ts +66 -0
  94. package/src/rules/utils/getThisExpression.ts +24 -0
  95. package/src/rules/utils/isUnsafeAssignment.ts +113 -0
  96. package/src/rules/utils/operators.ts +39 -0
  97. package/src/utils/declarationIncludesGlobal.ts +9 -0
  98. package/src/utils/declarationsIncludeGlobal.ts +3 -7
  99. package/src/utils/hasSameTokens.ts +45 -0
  100. package/src/utils/isGlobalDeclarationOfName.ts +51 -0
  101. package/tsconfig.tsbuildinfo +1 -1
@@ -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 {};
@@ -0,0 +1,69 @@
1
+ import rule from "./nonOctalDecimalEscapes.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ "\\8"
8
+ `,
9
+ snapshot: `
10
+ "\\8"
11
+ ~~
12
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ "\\9"
18
+ `,
19
+ snapshot: `
20
+ "\\9"
21
+ ~~
22
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ "w\\8less"
28
+ `,
29
+ snapshot: `
30
+ "w\\8less"
31
+ ~~
32
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
33
+ `,
34
+ },
35
+ {
36
+ code: `
37
+ "December 1\\9"
38
+ `,
39
+ snapshot: `
40
+ "December 1\\9"
41
+ ~~
42
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
43
+ `,
44
+ },
45
+ {
46
+ code: `
47
+ "Don't use \\8 and \\9 escapes."
48
+ `,
49
+ snapshot: `
50
+ "Don't use \\8 and \\9 escapes."
51
+ ~~
52
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
53
+ ~~
54
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
55
+ `,
56
+ },
57
+ {
58
+ code: `
59
+ "\\0\\8"
60
+ `,
61
+ snapshot: `
62
+ "\\0\\8"
63
+ ~~
64
+ Non-octal decimal escape sequences (\\8 and \\9) should not be used in string literals.
65
+ `,
66
+ },
67
+ ],
68
+ valid: ['"8"', '"9"', '"w8less"', '"December 19"', '"\\\\8"', '"\\0\\u0038"'],
69
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports parseInt calls with binary, hexadecimal, or octal strings that can be replaced with numeric literals.";
3
+ readonly id: "numericLiteralParsing";
4
+ readonly preset: "stylistic";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "preferLiteral", undefined>;
6
+ export default _default;
@@ -0,0 +1,97 @@
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
+ function convertToLiteral(value, radix) {
6
+ const parsed = Number.parseInt(value, radix);
7
+ if (Number.isNaN(parsed)) {
8
+ return value;
9
+ }
10
+ switch (radix) {
11
+ case 2:
12
+ return `0b${parsed.toString(2)}`;
13
+ case 8:
14
+ return `0o${parsed.toString(8)}`;
15
+ case 16:
16
+ return `0x${parsed.toString(16).toUpperCase()}`;
17
+ default:
18
+ return value;
19
+ }
20
+ }
21
+ function getRadixValue(node) {
22
+ if (!ts.isNumericLiteral(node)) {
23
+ return undefined;
24
+ }
25
+ const value = Number(node.text);
26
+ if (![2, 8, 16].includes(value)) {
27
+ return undefined;
28
+ }
29
+ return value;
30
+ }
31
+ // TODO: Use a util like getStaticValue
32
+ function getStringValue(node) {
33
+ return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)
34
+ ? node.text
35
+ : undefined;
36
+ }
37
+ export default typescriptLanguage.createRule({
38
+ about: {
39
+ description: "Reports parseInt calls with binary, hexadecimal, or octal strings that can be replaced with numeric literals.",
40
+ id: "numericLiteralParsing",
41
+ preset: "stylistic",
42
+ },
43
+ messages: {
44
+ preferLiteral: {
45
+ primary: "Use {{ literal }} instead of parseInt with radix {{ radix }}.",
46
+ secondary: [
47
+ "Binary, octal, and hexadecimal numeric literals are more readable and direct than using parseInt with a radix.",
48
+ "Numeric literals are supported natively and don't require function calls.",
49
+ ],
50
+ suggestions: ["Replace the parseInt call with the numeric literal."],
51
+ },
52
+ },
53
+ setup(context) {
54
+ function checkParseIntCall(node) {
55
+ if (node.arguments.length !== 2) {
56
+ return;
57
+ }
58
+ const stringValue = getStringValue(node.arguments[0]);
59
+ if (!stringValue) {
60
+ return;
61
+ }
62
+ const radixValue = getRadixValue(node.arguments[1]);
63
+ if (!radixValue) {
64
+ return;
65
+ }
66
+ context.report({
67
+ data: {
68
+ literal: convertToLiteral(stringValue, radixValue),
69
+ radix: String(radixValue),
70
+ },
71
+ message: "preferLiteral",
72
+ range: getTSNodeRange(node, context.sourceFile),
73
+ });
74
+ }
75
+ return {
76
+ visitors: {
77
+ CallExpression: (node) => {
78
+ if (ts.isIdentifier(node.expression)) {
79
+ if (node.expression.text === "parseInt" &&
80
+ isGlobalDeclaration(node.expression, context.typeChecker)) {
81
+ checkParseIntCall(node);
82
+ }
83
+ }
84
+ else if (ts.isPropertyAccessExpression(node.expression)) {
85
+ if (ts.isIdentifier(node.expression.expression) &&
86
+ node.expression.expression.text === "Number" &&
87
+ ts.isIdentifier(node.expression.name) &&
88
+ node.expression.name.text === "parseInt" &&
89
+ isGlobalDeclaration(node.expression.expression, context.typeChecker)) {
90
+ checkParseIntCall(node);
91
+ }
92
+ }
93
+ },
94
+ },
95
+ };
96
+ },
97
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ import rule from "./numericLiteralParsing.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ const binary = parseInt("111110111", 2);
8
+ `,
9
+ snapshot: `
10
+ const binary = parseInt("111110111", 2);
11
+ ~~~~~~~~~~~~~~~~~~~~~~~~
12
+ Use 0b111110111 instead of parseInt with radix 2.
13
+ `,
14
+ },
15
+ {
16
+ code: `
17
+ const octal = parseInt("767", 8);
18
+ `,
19
+ snapshot: `
20
+ const octal = parseInt("767", 8);
21
+ ~~~~~~~~~~~~~~~~~~
22
+ Use 0o767 instead of parseInt with radix 8.
23
+ `,
24
+ },
25
+ {
26
+ code: `
27
+ const hex = parseInt("1F7", 16);
28
+ `,
29
+ snapshot: `
30
+ const hex = parseInt("1F7", 16);
31
+ ~~~~~~~~~~~~~~~~~~~
32
+ Use 0x1F7 instead of parseInt with radix 16.
33
+ `,
34
+ },
35
+ {
36
+ code: `
37
+ const value = Number.parseInt("111110111", 2);
38
+ `,
39
+ snapshot: `
40
+ const value = Number.parseInt("111110111", 2);
41
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
+ Use 0b111110111 instead of parseInt with radix 2.
43
+ `,
44
+ },
45
+ {
46
+ code: `
47
+ const result = Number.parseInt("767", 8);
48
+ `,
49
+ snapshot: `
50
+ const result = Number.parseInt("767", 8);
51
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
52
+ Use 0o767 instead of parseInt with radix 8.
53
+ `,
54
+ },
55
+ {
56
+ code: `
57
+ const num = Number.parseInt("1F7", 16);
58
+ `,
59
+ snapshot: `
60
+ const num = Number.parseInt("1F7", 16);
61
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
62
+ Use 0x1F7 instead of parseInt with radix 16.
63
+ `,
64
+ },
65
+ {
66
+ code: `
67
+ const value = parseInt(\`111110111\`, 2);
68
+ `,
69
+ snapshot: `
70
+ const value = parseInt(\`111110111\`, 2);
71
+ ~~~~~~~~~~~~~~~~~~~~~~~~
72
+ Use 0b111110111 instead of parseInt with radix 2.
73
+ `,
74
+ },
75
+ ],
76
+ valid: [
77
+ `const decimal = parseInt("123");`,
78
+ `const value = parseInt("123", 10);`,
79
+ `const ternary = parseInt("102", 3);`,
80
+ `const binary = 0b111110111;`,
81
+ `const octal = 0o767;`,
82
+ `const hex = 0x1F7;`,
83
+ `const dynamic = parseInt(value, 2);`,
84
+ `const variable = parseInt("111", radix);`,
85
+ `const single = parseInt("111");`,
86
+ `Number.parseInt("123");`,
87
+ `Number.parseInt("123", 10);`,
88
+ `
89
+ function parseInt(...values: unknown[]) {}
90
+ parseInt("1", 2);
91
+ export {};
92
+ `,
93
+ `
94
+ const Number = { parseInt(...values: unknown[]) {} };
95
+ Number.parseInt("1", 2);
96
+ export {};
97
+ `,
98
+ ],
99
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports self-assignments which have no effect and are likely errors.";
3
+ readonly id: "selfAssignments";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "selfAssignment", undefined>;
6
+ export default _default;