-
Notifications
You must be signed in to change notification settings - Fork 0
feat(lint): detect import cycles with import-x/no-cycle #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { join, resolve } from "node:path"; | ||
|
|
||
| import { ESLint } from "eslint"; | ||
| import { afterAll, describe, expect, it } from "vitest"; | ||
|
|
||
| /** | ||
| * A guard that `import-x/no-cycle` is actually ON and actually reaching | ||
| * `packages/cli/src`. | ||
| * | ||
| * This does NOT re-implement cycle detection — that would be exactly the | ||
| * "re-derive what the tool already knows" mistake the style guide forbids. | ||
| * Every assertion below asks ESLint, running the repository's real | ||
| * `eslint.config.js`, and checks what it answers. | ||
| * | ||
| * It exists because the rule's failure mode is silence. While this rule was | ||
| * being added, the config resolved correctly, matched the right files, and | ||
| * reported `import-x/no-cycle` as an enabled error — and still found nothing on | ||
| * a tree that provably contained a cycle, because `import-x/extensions` | ||
| * defaults to `['.js', '.mjs', '.cjs']` and so every `.ts` file was dropped | ||
| * before its imports were read. A green `pnpm lint` looked identical whether | ||
| * the rule was working or inert. Nothing but an actual cycle distinguishes | ||
| * those two states, which is why the second test below writes one. | ||
| */ | ||
|
|
||
| const REPO_ROOT = resolve(import.meta.dirname, "..", "..", ".."); | ||
| const CLI_SOURCE = resolve(REPO_ROOT, "packages/cli/src"); | ||
|
|
||
| const temporaryDirectories: string[] = []; | ||
|
|
||
| afterAll(async () => { | ||
| await Promise.all( | ||
| temporaryDirectories.map(async (directory) => | ||
| rm(directory, { recursive: true, force: true }) | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| function createESLint(): ESLint { | ||
| return new ESLint({ cwd: REPO_ROOT }); | ||
| } | ||
|
|
||
| describe("import-x/no-cycle", () => { | ||
| it("is enabled as an error for files in packages/cli/src", async () => { | ||
| const config = (await createESLint().calculateConfigForFile( | ||
| join(CLI_SOURCE, "index.ts") | ||
| )) as { rules?: Record<string, unknown> }; | ||
|
|
||
| // "error" is 2 once ESLint normalizes it. A config block that stopped | ||
| // matching `packages/cli/src` would leave this undefined. | ||
| expect(config.rules?.["import-x/no-cycle"]).toBeDefined(); | ||
| expect((config.rules?.["import-x/no-cycle"] as unknown[])[0]).toBe(2); | ||
| }); | ||
|
|
||
| it("reports a value cycle written into packages/cli/src", async () => { | ||
| // Written inside `packages/cli/src` on purpose: the point of the check is | ||
| // that the rule reaches THIS tree, so linting a fixture parked somewhere | ||
| // the config does not match would prove nothing. The directory name is | ||
| // prefixed so it is obviously not product code if cleanup is ever missed. | ||
| const directory = await mkdtemp(join(CLI_SOURCE, "__cycle-guard-")); | ||
| temporaryDirectories.push(directory); | ||
|
|
||
| // A -> B -> A over VALUE imports. Kept to real value edges because | ||
| // type-only edges are erased before the module runs and the rule ignores | ||
| // them by design; see the note in eslint.config.js. | ||
| await writeFile( | ||
| join(directory, "a.ts"), | ||
| 'import { b } from "./b";\n\nexport const a = (): string => b();\n' | ||
| ); | ||
| await writeFile( | ||
| join(directory, "b.ts"), | ||
| 'import { a } from "./a";\n\nexport const b = (): string => a();\n' | ||
| ); | ||
|
|
||
| const results = await createESLint().lintFiles([join(directory, "*.ts")]); | ||
| const cycleMessages = results.flatMap((result) => | ||
| result.messages.filter( | ||
| (message) => message.ruleId === "import-x/no-cycle" | ||
| ) | ||
| ); | ||
|
|
||
| expect(cycleMessages.length).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.