The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import rule from "@effect/oxc/oxlint/rules/no-unused-internal" |
| 2 | import { assert } from "@effect/vitest" |
| 3 | import * as fs from "node:fs" |
| 4 | import * as os from "node:os" |
| 5 | import * as path from "node:path" |
| 6 | import { describe, expect, it } from "vitest" |
| 7 | import { createTestContext } from "./utils.ts" |
| 8 | |
| 9 | function writeFixture(files: Record<string, string>): string { |
| 10 | const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "no-unused-internal-rule-")) |
| 11 | for (const [file, source] of Object.entries(files)) { |
| 12 | const fileName = path.join(cwd, file) |
| 13 | fs.mkdirSync(path.dirname(fileName), { recursive: true }) |
| 14 | fs.writeFileSync(fileName, source) |
| 15 | } |
| 16 | return cwd |
| 17 | } |
| 18 | |
| 19 | function run(cwd: string, file: string) { |
| 20 | const filename = path.join(cwd, file) |
| 21 | const source = fs.readFileSync(filename, "utf8") |
| 22 | const { context, errors } = createTestContext({ sourceCode: source, cwd, filename }) |
| 23 | const visitors = rule.create(context as never) |
| 24 | visitors.Program?.({ type: "Program", range: [0, source.length] } as never) |
| 25 | return errors.map((error) => error.message) |
| 26 | } |
| 27 | |
| 28 | describe("no-unused-internal", () => { |
| 29 | it("reports unused top-level @internal exports", () => { |
| 30 | const cwd = writeFixture({ |
| 31 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 32 | export const unused = 1 |
| 33 | ` |
| 34 | }) |
| 35 | |
| 36 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([ |
| 37 | `@internal export "unused" is not used by production sources` |
| 38 | ]) |
| 39 | }) |
| 40 | |
| 41 | it("counts same-file references as usage", () => { |
| 42 | const cwd = writeFixture({ |
| 43 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 44 | export const used = 1 |
| 45 | |
| 46 | export const value = used |
| 47 | ` |
| 48 | }) |
| 49 | |
| 50 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 51 | }) |
| 52 | |
| 53 | it("counts type-only references as usage", () => { |
| 54 | const cwd = writeFixture({ |
| 55 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 56 | export interface Internal { |
| 57 | readonly value: string |
| 58 | } |
| 59 | `, |
| 60 | "packages/foo/src/Consumer.ts": `import type { Internal } from "./Internal.ts" |
| 61 | |
| 62 | type Local = Internal |
| 63 | ` |
| 64 | }) |
| 65 | |
| 66 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 67 | }) |
| 68 | |
| 69 | it("counts literal dynamic imports as usage of only the resolved module", () => { |
| 70 | const cwd = writeFixture({ |
| 71 | "packages/foo/src/internal/lazy.ts": `/** @internal */ |
| 72 | export const first = 1 |
| 73 | |
| 74 | /** @internal */ |
| 75 | export const second = 2 |
| 76 | `, |
| 77 | "packages/foo/src/internal/unrelated.ts": `/** @internal */ |
| 78 | export const first = 1 |
| 79 | `, |
| 80 | "packages/foo/src/Consumer.ts": `export const load = () => import("./internal/lazy.ts") |
| 81 | export const unknown = (specifier: string) => import(specifier) |
| 82 | ` |
| 83 | }) |
| 84 | |
| 85 | assert.deepStrictEqual(run(cwd, "packages/foo/src/internal/lazy.ts"), []) |
| 86 | assert.deepStrictEqual(run(cwd, "packages/foo/src/internal/unrelated.ts"), [ |
| 87 | `@internal export "first" is not used by production sources` |
| 88 | ]) |
| 89 | }) |
| 90 | |
| 91 | it("reports @internal exports used in public exported type signatures", () => { |
| 92 | const cwd = writeFixture({ |
| 93 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 94 | export interface Internal { |
| 95 | readonly value: string |
| 96 | } |
| 97 | `, |
| 98 | "packages/foo/src/Public.ts": `import type { Internal } from "./Internal.ts" |
| 99 | |
| 100 | export interface Public { |
| 101 | readonly value: Internal |
| 102 | } |
| 103 | ` |
| 104 | }) |
| 105 | |
| 106 | expect(run(cwd, "packages/foo/src/Public.ts")).toEqual([ |
| 107 | `Do not reference @internal export "Internal" in a public exported type signature` |
| 108 | ]) |
| 109 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 110 | }) |
| 111 | |
| 112 | it("reports public re-exports of @internal exports", () => { |
| 113 | const cwd = writeFixture({ |
| 114 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 115 | export const internal = 1 |
| 116 | `, |
| 117 | "packages/foo/src/Public.ts": `export { internal } from "./Internal.ts" |
| 118 | ` |
| 119 | }) |
| 120 | |
| 121 | expect(run(cwd, "packages/foo/src/Public.ts")).toEqual([ |
| 122 | `Do not re-export @internal export "internal" from a public module` |
| 123 | ]) |
| 124 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 125 | }) |
| 126 | |
| 127 | it("ignores @internal class member type signatures", () => { |
| 128 | const cwd = writeFixture({ |
| 129 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 130 | export interface Internal { |
| 131 | readonly value: string |
| 132 | } |
| 133 | `, |
| 134 | "packages/foo/src/Public.ts": `import type { Internal } from "./Internal.ts" |
| 135 | |
| 136 | export class Public { |
| 137 | /** @internal */ |
| 138 | getInternal(): Internal { |
| 139 | throw new Error("not implemented") |
| 140 | } |
| 141 | } |
| 142 | ` |
| 143 | }) |
| 144 | |
| 145 | expect(run(cwd, "packages/foo/src/Public.ts")).toEqual([]) |
| 146 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 147 | }) |
| 148 | |
| 149 | it("ignores public type signatures in internal directories", () => { |
| 150 | const cwd = writeFixture({ |
| 151 | "packages/foo/src/Internal.ts": `/** @internal */ |
| 152 | export interface Internal { |
| 153 | readonly value: string |
| 154 | } |
| 155 | `, |
| 156 | "packages/foo/src/internal/Consumer.ts": `import type { Internal } from "../Internal.ts" |
| 157 | |
| 158 | export interface PublicInsideInternalDirectory { |
| 159 | readonly value: Internal |
| 160 | } |
| 161 | ` |
| 162 | }) |
| 163 | |
| 164 | expect(run(cwd, "packages/foo/src/internal/Consumer.ts")).toEqual([]) |
| 165 | expect(run(cwd, "packages/foo/src/Internal.ts")).toEqual([]) |
| 166 | }) |
| 167 | }) |