Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/icy-results-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@biomejs/biome": patch
---

Fixed [#6569](https://github.com/biomejs/biome/issues/6569): Allow files to export from themselves with `noImportCycles`.

This means the following is now allowed:

**example.js**
```js
export function example() {
return 1;
}

// Re-exports all named exports from the current module under a single namespace
// and then imports the namespace from the current module.
// Allows for encapsulating functions/variables into a namespace instead
// of using a static class.
export * as Example from './example.js';

import { Example } from './example.js';
```
21 changes: 21 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery/no_import_cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ declare_lint_rule! {
/// only go in a single direction, i.e. they don't point "back" to the
/// importing file.
///
/// However, files that import themselves are allowed, and the rule won't trigger for these use cases.
/// This allows for encapsulation of functions/variables into a namespace instead of using a
/// static class (triggers [noStaticOnlyClass](https://biomejs.dev/linter/rules/no-static-only-class)).
///
/// :::note
/// This rule is computationally expensive. If you are particularly
/// pressed for lint time, or don't think you have an issue with dependency
Expand Down Expand Up @@ -79,6 +83,16 @@ declare_lint_rule! {
/// }
/// ```
///
/// ```js,file=foobaz.js
/// export function foo() {
/// console.log("foobaz");
/// }
///
/// export * as baz from './foobaz.js';
///
/// import { baz } from './foobaz.js';
/// ```
///
/// ```ts,file=types.ts
/// import type { bar } from "./qux.ts";
///
Expand Down Expand Up @@ -243,6 +257,12 @@ fn find_cycle(
}

if path == ctx.file_path() {
// https://github.com/biomejs/biome/issues/6569
// prevent flagging on import cycles when they are isolated to a single file
if stack.is_empty() && start_path == path {
continue;
}

// Return all the paths from `start_path` to `resolved_path`:
let paths = Some(start_path.to_string())
.into_iter()
Expand All @@ -254,6 +274,7 @@ fn find_cycle(
)
.chain(Some(path.to_string()))
.collect();

return Some(paths);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
import { foo } from "./invalidFoobar";

foo();

export function bar() {
return 1;
}

export * as bar from "./valid"

import { foobar } from "./valid"

foobar.bar();
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ import { foo } from "./invalidFoobar";
foo();
export function bar() {
return 1;
}
export * as bar from "./valid"
import { foobar } from "./valid"
foobar.bar();
```
Loading