@flint.fyi/ts 0.14.0 → 0.14.2

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 (35) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/createTypeScriptFileFromProgram.js +3 -1
  3. package/lib/getFirstEnumValues.d.ts +35 -0
  4. package/lib/getFirstEnumValues.js +51 -0
  5. package/lib/getFirstEnumValues.test.d.ts +1 -0
  6. package/lib/getFirstEnumValues.test.js +45 -0
  7. package/lib/plugin.d.ts +16 -5
  8. package/lib/plugin.js +9 -1
  9. package/lib/rules/consecutiveNonNullAssertions.d.ts +1 -0
  10. package/lib/rules/consecutiveNonNullAssertions.js +3 -2
  11. package/lib/rules/debuggerStatements.d.ts +6 -0
  12. package/lib/rules/debuggerStatements.js +43 -0
  13. package/lib/rules/debuggerStatements.test.d.ts +1 -0
  14. package/lib/rules/debuggerStatements.test.js +80 -0
  15. package/lib/rules/forInArrays.d.ts +1 -0
  16. package/lib/rules/forInArrays.js +1 -0
  17. package/lib/rules/namespaceDeclarations.d.ts +1 -0
  18. package/lib/rules/namespaceDeclarations.js +9 -2
  19. package/lib/rules/octalEscapes.d.ts +6 -0
  20. package/lib/rules/octalEscapes.js +71 -0
  21. package/lib/rules/octalEscapes.test.d.ts +1 -0
  22. package/lib/rules/octalEscapes.test.js +213 -0
  23. package/package.json +1 -1
  24. package/src/createTypeScriptFileFromProgram.ts +4 -1
  25. package/src/getFirstEnumValues.test.ts +51 -0
  26. package/src/getFirstEnumValues.ts +59 -0
  27. package/src/plugin.ts +9 -1
  28. package/src/rules/consecutiveNonNullAssertions.ts +3 -2
  29. package/src/rules/debuggerStatements.test.ts +81 -0
  30. package/src/rules/debuggerStatements.ts +45 -0
  31. package/src/rules/forInArrays.ts +1 -0
  32. package/src/rules/namespaceDeclarations.ts +13 -2
  33. package/src/rules/octalEscapes.test.ts +214 -0
  34. package/src/rules/octalEscapes.ts +85 -0
  35. package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @flint/ts
2
2
 
3
+ ## 0.14.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 6415134: feat: implement debuggerStatements rule for TypeScript plugin
8
+ - Updated dependencies [b58d145]
9
+ - @flint.fyi/rule-tester@0.14.1
10
+
11
+ ## 0.14.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 27280d3: use first names for ts.SyntaxKind visitors, not aliases
16
+
3
17
  ## 0.14.0
4
18
 
5
19
  ### Minor Changes
@@ -1,7 +1,9 @@
1
1
  import * as ts from "typescript";
2
2
  import { collectReferencedFilePaths } from "./collectReferencedFilePaths.js";
3
3
  import { formatDiagnostic } from "./formatDiagnostic.js";
4
+ import { getFirstEnumValues } from "./getFirstEnumValues.js";
4
5
  import { normalizeRange } from "./normalizeRange.js";
6
+ const NodeSyntaxKinds = getFirstEnumValues(ts.SyntaxKind);
5
7
  export function createTypeScriptFileFromProgram(program, sourceFile) {
6
8
  return {
7
9
  cache: {
@@ -47,7 +49,7 @@ export function createTypeScriptFileFromProgram(program, sourceFile) {
47
49
  }
48
50
  const { visitors } = runtime;
49
51
  const visit = (node) => {
50
- visitors[ts.SyntaxKind[node.kind]]?.(node);
52
+ visitors[NodeSyntaxKinds[node.kind]]?.(node);
51
53
  node.forEachChild(visit);
52
54
  };
53
55
  sourceFile.forEachChild(visit);
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Removes TypeScript enum values that alias earlier values.
3
+ * @param original Any TypeScript enum with numeric values.
4
+ * @returns A version of the enum with only the first occurrence of each value.
5
+ * @example
6
+ * Given the following enum:
7
+ * ```ts
8
+ * enum Example {
9
+ * A = 1,
10
+ * B = 2,
11
+ * C = A
12
+ * }
13
+ * ```
14
+ * Its original value looks like:
15
+ * ```ts
16
+ * {
17
+ * 1: 'C', // <-- Points to 'C', the alias of 'A'
18
+ * 2: 'B',
19
+ * A: 1,
20
+ * B: 2,
21
+ * C: 1,
22
+ * }
23
+ * ```
24
+ * Calling `getFirstEnumValues(Example)` would return:
25
+ * ```ts
26
+ * {
27
+ * 1: 'A', // <-- Corrected to the original 'A'
28
+ * 2: 'B',
29
+ * A: 1,
30
+ * B: 2,
31
+ * C: 1,
32
+ * }
33
+ * ```
34
+ */
35
+ export declare function getFirstEnumValues<Keys extends string, Values extends number, Original extends Record<Keys | Values, Keys | Values>>(original: Original): Original;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Removes TypeScript enum values that alias earlier values.
3
+ * @param original Any TypeScript enum with numeric values.
4
+ * @returns A version of the enum with only the first occurrence of each value.
5
+ * @example
6
+ * Given the following enum:
7
+ * ```ts
8
+ * enum Example {
9
+ * A = 1,
10
+ * B = 2,
11
+ * C = A
12
+ * }
13
+ * ```
14
+ * Its original value looks like:
15
+ * ```ts
16
+ * {
17
+ * 1: 'C', // <-- Points to 'C', the alias of 'A'
18
+ * 2: 'B',
19
+ * A: 1,
20
+ * B: 2,
21
+ * C: 1,
22
+ * }
23
+ * ```
24
+ * Calling `getFirstEnumValues(Example)` would return:
25
+ * ```ts
26
+ * {
27
+ * 1: 'A', // <-- Corrected to the original 'A'
28
+ * 2: 'B',
29
+ * A: 1,
30
+ * B: 2,
31
+ * C: 1,
32
+ * }
33
+ * ```
34
+ */
35
+ export function getFirstEnumValues(original) {
36
+ const result = {};
37
+ for (const key in original) {
38
+ if (typeof original[key] !== "number") {
39
+ continue;
40
+ }
41
+ if (!(key in result)) {
42
+ result[key] = original[key];
43
+ }
44
+ if (!(original[key] in result)) {
45
+ // 🤷 enums are tricky to type.
46
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
47
+ result[original[key]] = key;
48
+ }
49
+ }
50
+ return result;
51
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,45 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { getFirstEnumValues } from "./getFirstEnumValues.js";
3
+ describe("getFirstEnumValues", () => {
4
+ it("returns an empty object when given an empty enum", () => {
5
+ let Empty;
6
+ (function (Empty) {
7
+ })(Empty || (Empty = {}));
8
+ const result = getFirstEnumValues(Empty);
9
+ expect(result).toEqual({});
10
+ });
11
+ it("returns all original pairs when when given a non-repeating enum", () => {
12
+ let NonRepeating;
13
+ (function (NonRepeating) {
14
+ NonRepeating[NonRepeating["A"] = 1] = "A";
15
+ NonRepeating[NonRepeating["B"] = 2] = "B";
16
+ NonRepeating[NonRepeating["C"] = 3] = "C";
17
+ })(NonRepeating || (NonRepeating = {}));
18
+ const result = getFirstEnumValues(NonRepeating);
19
+ expect(result).toEqual({
20
+ 1: "A",
21
+ 2: "B",
22
+ 3: "C",
23
+ A: 1,
24
+ B: 2,
25
+ C: 3,
26
+ });
27
+ });
28
+ it("returns the first key when when given a repeating-value enum", () => {
29
+ let Repeating;
30
+ (function (Repeating) {
31
+ Repeating[Repeating["A"] = 1] = "A";
32
+ Repeating[Repeating["B"] = 2] = "B";
33
+ // eslint-disable-next-line @typescript-eslint/prefer-literal-enum-member
34
+ Repeating[Repeating["C"] = 1] = "C";
35
+ })(Repeating || (Repeating = {}));
36
+ const result = getFirstEnumValues(Repeating);
37
+ expect(result).toEqual({
38
+ 1: "A",
39
+ 2: "B",
40
+ A: 1,
41
+ B: 2,
42
+ C: 1,
43
+ });
44
+ });
45
+ });
package/lib/plugin.d.ts CHANGED
@@ -1,13 +1,24 @@
1
- export declare const ts: import("@flint.fyi/core").Plugin<import("@flint.fyi/core").BaseAbout, string | undefined, [import("@flint.fyi/core").Rule<{
2
- readonly id: "forInArrays";
3
- readonly preset: "logical";
4
- }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "forIn", undefined>, import("@flint.fyi/core").Rule<{
1
+ export declare const ts: import("@flint.fyi/core").Plugin<import("@flint.fyi/core").RuleAbout, string | undefined, [import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports unnecessary extra non-null assertions.";
5
3
  readonly id: "consecutiveNonNullAssertions";
6
4
  readonly preset: "logical";
7
5
  }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "consecutiveNonNullAssertion", undefined>, import("@flint.fyi/core").Rule<{
6
+ readonly description: "Reports using debugger statements.";
7
+ readonly id: "debuggerStatements";
8
+ readonly preset: "logical";
9
+ }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "noDebugger", undefined>, import("@flint.fyi/core").Rule<{
10
+ readonly description: "Reports iterating over an array with a for-in loop.";
11
+ readonly id: "forInArrays";
12
+ readonly preset: "logical";
13
+ }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "forIn", undefined>, import("@flint.fyi/core").Rule<{
14
+ readonly description: "Reports using legacy `namespace` declarations.";
8
15
  readonly id: "namespaceDeclarations";
9
16
  readonly preset: "logical";
10
17
  }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "preferModules", {
11
18
  readonly allowDeclarations: import("zod").ZodDefault<import("zod").ZodBoolean>;
12
19
  readonly allowDefinitionFiles: import("zod").ZodDefault<import("zod").ZodBoolean>;
13
- }>]>;
20
+ }>, import("@flint.fyi/core").Rule<{
21
+ readonly description: "Reports using octal escape sequences in string literals.";
22
+ readonly id: "octalEscapes";
23
+ readonly preset: "logical";
24
+ }, import("./nodes.js").TSNodesByName, import("./language.js").TypeScriptServices, "noOctalEscape", undefined>]>;
package/lib/plugin.js CHANGED
@@ -1,11 +1,19 @@
1
1
  import { createPlugin } from "@flint.fyi/core";
2
2
  import consecutiveNonNullAssertions from "./rules/consecutiveNonNullAssertions.js";
3
+ import debuggerStatements from "./rules/debuggerStatements.js";
3
4
  import forInArrays from "./rules/forInArrays.js";
4
5
  import namespaceDeclarations from "./rules/namespaceDeclarations.js";
6
+ import octalEscapes from "./rules/octalEscapes.js";
5
7
  export const ts = createPlugin({
6
8
  files: {
7
9
  all: ["**/*.{cjs,js,jsx,mjs,ts,tsx}"],
8
10
  },
9
11
  name: "ts",
10
- rules: [forInArrays, consecutiveNonNullAssertions, namespaceDeclarations],
12
+ rules: [
13
+ consecutiveNonNullAssertions,
14
+ debuggerStatements,
15
+ forInArrays,
16
+ namespaceDeclarations,
17
+ octalEscapes,
18
+ ],
11
19
  });
@@ -1,4 +1,5 @@
1
1
  declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports unnecessary extra non-null assertions.";
2
3
  readonly id: "consecutiveNonNullAssertions";
3
4
  readonly preset: "logical";
4
5
  }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "consecutiveNonNullAssertion", undefined>;
@@ -2,6 +2,7 @@ import * as ts from "typescript";
2
2
  import { typescriptLanguage } from "../language.js";
3
3
  export default typescriptLanguage.createRule({
4
4
  about: {
5
+ description: "Reports unnecessary extra non-null assertions.",
5
6
  id: "consecutiveNonNullAssertions",
6
7
  preset: "logical",
7
8
  },
@@ -23,8 +24,8 @@ export default typescriptLanguage.createRule({
23
24
  return;
24
25
  }
25
26
  const range = {
26
- begin: node.end,
27
- end: node.parent.end + 1,
27
+ begin: node.end - 1,
28
+ end: node.parent.end,
28
29
  };
29
30
  context.report({
30
31
  fix: {
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports using debugger statements.";
3
+ readonly id: "debuggerStatements";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noDebugger", undefined>;
6
+ export default _default;
@@ -0,0 +1,43 @@
1
+ import { typescriptLanguage } from "../language.js";
2
+ export default typescriptLanguage.createRule({
3
+ about: {
4
+ description: "Reports using debugger statements.",
5
+ id: "debuggerStatements",
6
+ preset: "logical",
7
+ },
8
+ messages: {
9
+ noDebugger: {
10
+ primary: "Debugger statements should not be used in production code.",
11
+ secondary: [
12
+ "The `debugger` statement causes the JavaScript runtime to pause execution and start a debugger if one is available, such as when browser developer tools are open.",
13
+ "This can be useful during development, but should not be left in production code.",
14
+ ],
15
+ suggestions: [
16
+ "Remove the `debugger` statement before shipping this code to users.",
17
+ ],
18
+ },
19
+ },
20
+ setup(context) {
21
+ return {
22
+ visitors: {
23
+ DebuggerStatement: (node) => {
24
+ const range = {
25
+ begin: node.getStart(),
26
+ end: node.getEnd(),
27
+ };
28
+ context.report({
29
+ message: "noDebugger",
30
+ range,
31
+ suggestions: [
32
+ {
33
+ id: "removeDebugger",
34
+ range,
35
+ text: "",
36
+ },
37
+ ],
38
+ });
39
+ },
40
+ },
41
+ };
42
+ },
43
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ import rule from "./debuggerStatements.js";
2
+ import { ruleTester } from "./ruleTester.js";
3
+ ruleTester.describe(rule, {
4
+ invalid: [
5
+ {
6
+ code: `
7
+ debugger;
8
+ `,
9
+ snapshot: `
10
+ debugger;
11
+ ~~~~~~~~~
12
+ Debugger statements should not be used in production code.
13
+ `,
14
+ suggestions: [
15
+ {
16
+ id: "removeDebugger",
17
+ updated: `
18
+
19
+ `,
20
+ },
21
+ ],
22
+ },
23
+ {
24
+ code: `
25
+ function test() {
26
+ debugger;
27
+ }
28
+ `,
29
+ snapshot: `
30
+ function test() {
31
+ debugger;
32
+ ~~~~~~~~~
33
+ Debugger statements should not be used in production code.
34
+ }
35
+ `,
36
+ suggestions: [
37
+ {
38
+ id: "removeDebugger",
39
+ updated: `
40
+ function test() {
41
+
42
+ }
43
+ `,
44
+ },
45
+ ],
46
+ },
47
+ {
48
+ code: `
49
+ if (condition) {
50
+ debugger;
51
+ }
52
+ `,
53
+ snapshot: `
54
+ if (condition) {
55
+ debugger;
56
+ ~~~~~~~~~
57
+ Debugger statements should not be used in production code.
58
+ }
59
+ `,
60
+ suggestions: [
61
+ {
62
+ id: "removeDebugger",
63
+ updated: `
64
+ if (condition) {
65
+
66
+ }
67
+ `,
68
+ },
69
+ ],
70
+ },
71
+ ],
72
+ valid: [
73
+ `console.log("debugging");`,
74
+ `function test() { console.log("test"); }`,
75
+ `for (let i = 0; i < 10; i++) { break; }`,
76
+ `for (let i = 0; i < 10; i++) { continue; }`,
77
+ `function test() { return; }`,
78
+ `throw new Error("test");`,
79
+ ],
80
+ });
@@ -1,4 +1,5 @@
1
1
  declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports iterating over an array with a for-in loop.";
2
3
  readonly id: "forInArrays";
3
4
  readonly preset: "logical";
4
5
  }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "forIn", undefined>;
@@ -5,6 +5,7 @@ import { getConstrainedTypeAtLocation } from "./utils/getConstrainedType.js";
5
5
  import { isTypeRecursive } from "./utils/isTypeRecursive.js";
6
6
  export default typescriptLanguage.createRule({
7
7
  about: {
8
+ description: "Reports iterating over an array with a for-in loop.",
8
9
  id: "forInArrays",
9
10
  preset: "logical",
10
11
  },
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  declare const _default: import("@flint.fyi/core").Rule<{
3
+ readonly description: "Reports using legacy `namespace` declarations.";
3
4
  readonly id: "namespaceDeclarations";
4
5
  readonly preset: "logical";
5
6
  }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "preferModules", {
@@ -5,6 +5,7 @@ import { getTSNodeRange } from "../getTSNodeRange.js";
5
5
  import { typescriptLanguage } from "../language.js";
6
6
  export default typescriptLanguage.createRule({
7
7
  about: {
8
+ description: "Reports using legacy `namespace` declarations.",
8
9
  id: "namespaceDeclarations",
9
10
  preset: "logical",
10
11
  },
@@ -20,8 +21,14 @@ export default typescriptLanguage.createRule({
20
21
  },
21
22
  },
22
23
  options: {
23
- allowDeclarations: z.boolean().default(false),
24
- allowDefinitionFiles: z.boolean().default(false),
24
+ allowDeclarations: z
25
+ .boolean()
26
+ .default(false)
27
+ .describe("Whether to allow namespaces declared with the `declare` keyword."),
28
+ allowDefinitionFiles: z
29
+ .boolean()
30
+ .default(false)
31
+ .describe("Whether to allow namespaces in `.d.ts` and other definition files."),
25
32
  },
26
33
  setup(context, { allowDeclarations, allowDefinitionFiles }) {
27
34
  if (allowDefinitionFiles && context.sourceFile.isDeclarationFile) {
@@ -0,0 +1,6 @@
1
+ declare const _default: import("@flint.fyi/core").Rule<{
2
+ readonly description: "Reports using octal escape sequences in string literals.";
3
+ readonly id: "octalEscapes";
4
+ readonly preset: "logical";
5
+ }, import("../nodes.js").TSNodesByName, import("../language.js").TypeScriptServices, "noOctalEscape", undefined>;
6
+ export default _default;
@@ -0,0 +1,71 @@
1
+ import { typescriptLanguage } from "../language.js";
2
+ /**
3
+ * Finds the position and length of an octal escape sequence in a string.
4
+ * Returns undefined if no octal escape is found.
5
+ */
6
+ function findOctalEscape(text) {
7
+ // Remove quotes from the string literal
8
+ const content = text.slice(1, -1);
9
+ // Match octal escapes: \0 followed by [0-7], or \1-7 optionally followed by [0-7]
10
+ // But exclude escaped backslashes (\\)
11
+ const octalEscapePattern = /(?<!\\)\\0[0-7]|(?<!\\)\\[1-7][0-7]*/;
12
+ const match = octalEscapePattern.exec(content);
13
+ if (!match) {
14
+ return undefined;
15
+ }
16
+ // Add 1 to account for the opening quote
17
+ return {
18
+ index: match.index + 1,
19
+ length: match[0].length,
20
+ };
21
+ }
22
+ export default typescriptLanguage.createRule({
23
+ about: {
24
+ description: "Reports using octal escape sequences in string literals.",
25
+ id: "octalEscapes",
26
+ preset: "logical",
27
+ },
28
+ messages: {
29
+ noOctalEscape: {
30
+ primary: "Prefer hexadecimal or Unicode escape sequences over legacy octal escape sequences.",
31
+ secondary: [
32
+ "Octal escape sequences (e.g., `\\01`, `\\02`, `\\377`) are a deprecated feature in JavaScript that can lead to confusion and are forbidden in strict mode and template literals.",
33
+ "They are less readable than their modern alternatives and can cause portability issues.",
34
+ ],
35
+ suggestions: [
36
+ "Use hexadecimal escape sequences (e.g., `\\x01`) or Unicode escape sequences (e.g., `\\u0001`) instead.",
37
+ ],
38
+ },
39
+ },
40
+ setup(context) {
41
+ function checkNode(node) {
42
+ const text = node.getText(context.sourceFile);
43
+ const octalEscape = findOctalEscape(text);
44
+ if (!octalEscape) {
45
+ return;
46
+ }
47
+ const nodeStart = node.getStart(context.sourceFile);
48
+ context.report({
49
+ message: "noOctalEscape",
50
+ range: {
51
+ begin: nodeStart + octalEscape.index,
52
+ end: nodeStart + octalEscape.index + octalEscape.length,
53
+ },
54
+ });
55
+ }
56
+ return {
57
+ visitors: {
58
+ NoSubstitutionTemplateLiteral: checkNode,
59
+ StringLiteral: checkNode,
60
+ TemplateExpression: (node) => {
61
+ // Check the head of the template
62
+ checkNode(node.head);
63
+ // Check each template span
64
+ for (const span of node.templateSpans) {
65
+ checkNode(span.literal);
66
+ }
67
+ },
68
+ },
69
+ };
70
+ },
71
+ });
@@ -0,0 +1 @@
1
+ export {};