@flint.fyi/ts 0.14.4 → 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 (57) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/lib/createTypeScriptFileFromProgram.js +4 -1
  3. package/lib/index.d.ts +3 -0
  4. package/lib/index.js +3 -0
  5. package/lib/plugin.d.ts +17 -1
  6. package/lib/plugin.js +8 -0
  7. package/lib/rules/chainedAssignments.js +2 -1
  8. package/lib/rules/chainedAssignments.test.js +24 -0
  9. package/lib/rules/debuggerStatements.test.js +4 -4
  10. package/lib/rules/emptyBlocks.d.ts +6 -0
  11. package/lib/rules/emptyBlocks.js +66 -0
  12. package/lib/rules/emptyBlocks.test.d.ts +1 -0
  13. package/lib/rules/emptyBlocks.test.js +135 -0
  14. package/lib/rules/octalNumbers.test.js +2 -2
  15. package/lib/rules/shadowedRestrictedNames.d.ts +6 -0
  16. package/lib/rules/shadowedRestrictedNames.js +95 -0
  17. package/lib/rules/shadowedRestrictedNames.test.d.ts +1 -0
  18. package/lib/rules/shadowedRestrictedNames.test.js +147 -0
  19. package/lib/rules/symbolDescriptions.test.js +4 -4
  20. package/lib/rules/unicodeBOMs.test.js +1 -0
  21. package/lib/rules/unnecessaryBlocks.d.ts +6 -0
  22. package/lib/rules/unnecessaryBlocks.js +75 -0
  23. package/lib/rules/unnecessaryBlocks.test.d.ts +1 -0
  24. package/lib/rules/unnecessaryBlocks.test.js +147 -0
  25. package/lib/rules/unnecessaryConcatenation.d.ts +6 -0
  26. package/lib/rules/unnecessaryConcatenation.js +38 -0
  27. package/lib/rules/unnecessaryConcatenation.test.d.ts +1 -0
  28. package/lib/rules/unnecessaryConcatenation.test.js +87 -0
  29. package/lib/rules/voidOperator.test.js +2 -2
  30. package/lib/utils/getDeclarationsIfGlobal.d.ts +2 -0
  31. package/lib/utils/getDeclarationsIfGlobal.js +7 -0
  32. package/lib/utils/isGlobalDeclaration.js +2 -3
  33. package/lib/utils/isGlobalDeclarationOfName.d.ts +1 -1
  34. package/lib/utils/isGlobalDeclarationOfName.js +4 -0
  35. package/package.json +3 -3
  36. package/src/createTypeScriptFileFromProgram.ts +5 -1
  37. package/src/index.ts +3 -0
  38. package/src/plugin.ts +8 -0
  39. package/src/rules/chainedAssignments.test.ts +24 -0
  40. package/src/rules/chainedAssignments.ts +4 -1
  41. package/src/rules/debuggerStatements.test.ts +4 -4
  42. package/src/rules/emptyBlocks.test.ts +136 -0
  43. package/src/rules/emptyBlocks.ts +73 -0
  44. package/src/rules/octalNumbers.test.ts +2 -2
  45. package/src/rules/shadowedRestrictedNames.test.ts +148 -0
  46. package/src/rules/shadowedRestrictedNames.ts +105 -0
  47. package/src/rules/symbolDescriptions.test.ts +4 -4
  48. package/src/rules/unicodeBOMs.test.ts +1 -0
  49. package/src/rules/unnecessaryBlocks.test.ts +148 -0
  50. package/src/rules/unnecessaryBlocks.ts +89 -0
  51. package/src/rules/unnecessaryConcatenation.test.ts +88 -0
  52. package/src/rules/unnecessaryConcatenation.ts +44 -0
  53. package/src/rules/voidOperator.test.ts +2 -2
  54. package/src/utils/getDeclarationsIfGlobal.ts +14 -0
  55. package/src/utils/isGlobalDeclaration.ts +3 -5
  56. package/src/utils/isGlobalDeclarationOfName.ts +6 -1
  57. package/tsconfig.tsbuildinfo +1 -1
@@ -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
+ });
@@ -72,8 +72,8 @@ function process() {
72
72
  snapshot: `
73
73
  function process() {
74
74
  void doSomething();
75
- ~~~~~~~~~~~~~~~~~~
76
- Prefer an explicit value over using the void operator to produce undefined.
75
+ ~~~~~~~~~~~~~~~~~~
76
+ Prefer an explicit value over using the void operator to produce undefined.
77
77
  }
78
78
  `,
79
79
  },
@@ -0,0 +1,14 @@
1
+ import * as ts from "typescript";
2
+
3
+ import { declarationsIncludeGlobal } from "./declarationsIncludeGlobal.js";
4
+
5
+ export function getDeclarationsIfGlobal(
6
+ node: ts.Expression,
7
+ typeChecker: ts.TypeChecker,
8
+ ) {
9
+ const declarations = typeChecker.getSymbolAtLocation(node)?.getDeclarations();
10
+
11
+ return !!declarations && declarationsIncludeGlobal(declarations)
12
+ ? declarations
13
+ : undefined;
14
+ }
@@ -1,12 +1,10 @@
1
1
  import * as ts from "typescript";
2
2
 
3
- import { declarationsIncludeGlobal } from "./declarationsIncludeGlobal.js";
3
+ import { getDeclarationsIfGlobal } from "./getDeclarationsIfGlobal.js";
4
4
 
5
5
  export function isGlobalDeclaration(
6
6
  node: ts.Expression,
7
7
  typeChecker: ts.TypeChecker,
8
- ): boolean {
9
- const declarations = typeChecker.getSymbolAtLocation(node)?.getDeclarations();
10
-
11
- return !!declarations && declarationsIncludeGlobal(declarations);
8
+ ) {
9
+ return !!getDeclarationsIfGlobal(node, typeChecker);
12
10
  }
@@ -6,7 +6,7 @@ import { declarationIncludesGlobal } from "./declarationIncludesGlobal.js";
6
6
  * TODO: Use a scope analyzer (#400).
7
7
  */
8
8
  export function isGlobalDeclarationOfName(
9
- node: ts.Expression,
9
+ node: ts.Node,
10
10
  name: string,
11
11
  typeChecker: ts.TypeChecker,
12
12
  ): boolean {
@@ -30,6 +30,11 @@ export function isGlobalDeclarationOfName(
30
30
  );
31
31
  }
32
32
 
33
+ // Special case: a property of an interface
34
+ if (ts.isPropertySignature(declaration)) {
35
+ return isGlobalDeclarationOfName(declaration.parent, name, typeChecker);
36
+ }
37
+
33
38
  return (
34
39
  isDeclarationOfName(declaration, name) &&
35
40
  declarationIncludesGlobal(declaration)