-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyzer): implement noBarrelFile (#1912)
- Loading branch information
1 parent
2a84139
commit f73af5d
Showing
26 changed files
with
513 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs
This file contains 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,105 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use biome_analyze::{Ast, RuleSource, RuleSourceKind}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{JsExport, JsExportFromClause, JsExportNamedFromClause, JsModule}; | ||
use biome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Disallow the use of barrel file. | ||
/// | ||
/// A barrel file is a file that re-exports all of the exports from other files in a directory. | ||
/// This structure results in the unnecessary loading of many modules, significantly impacting performance in large-scale applications. | ||
/// Additionally, it complicates the codebase, making it difficult to navigate and understand the project's dependency graph. | ||
/// | ||
/// For a more detailed explanation, check out https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/ | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// export * from "foo"; | ||
/// export * from "bar"; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// export { foo } from "foo"; | ||
/// export { bar } from "bar"; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// export { default as module1 } from "./module1"; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```ts | ||
/// export type * from "foo"; | ||
/// export type { foo } from "foo"; | ||
/// ``` | ||
/// | ||
pub NoBarrelFile { | ||
version: "next", | ||
name: "noBarrelFile", | ||
recommended: false, | ||
source: RuleSource::EslintBarrelFiles("avoid-namespace-import"), | ||
source_kind: RuleSourceKind::Inspired, | ||
} | ||
} | ||
|
||
impl Rule for NoBarrelFile { | ||
type Query = Ast<JsModule>; | ||
type State = JsExport; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let items = ctx.query().items(); | ||
|
||
for i in items { | ||
if let Some(export) = JsExport::cast(i.into()) { | ||
if let Ok(export_from_clause) = export.export_clause() { | ||
if let Some(export_from_clause) = | ||
JsExportFromClause::cast(export_from_clause.clone().into()) | ||
{ | ||
if export_from_clause.type_token().is_some() { | ||
return None; | ||
} | ||
} | ||
|
||
if let Some(export_from_clause) = | ||
JsExportNamedFromClause::cast(export_from_clause.into()) | ||
{ | ||
if export_from_clause.type_token().is_some() { | ||
return None; | ||
} | ||
if export_from_clause | ||
.specifiers() | ||
.into_iter() | ||
.flatten() | ||
.all(|s| s.type_token().is_some()) | ||
{ | ||
return None; | ||
} | ||
} | ||
return Some(export); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, js_export: &Self::State) -> Option<RuleDiagnostic> { | ||
let span = js_export.range(); | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
span, | ||
markup! { | ||
"Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused." | ||
}, | ||
).note( | ||
markup! { | ||
"Check "<Hyperlink href="https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/">"this thorough explanation"</Hyperlink>" to better understand the context." | ||
})) | ||
} | ||
} |
This file contains 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
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid.ts
This file contains 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 @@ | ||
export { foo, type Bar } from "foo"; |
24 changes: 24 additions & 0 deletions
24
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid.ts.snap
This file contains 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,24 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.ts | ||
--- | ||
# Input | ||
```ts | ||
export { foo, type Bar } from "foo"; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export { foo, type Bar } from "foo"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
1 change: 1 addition & 0 deletions
1
...biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_default_named_alias_reexport.ts
This file contains 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 @@ | ||
export { default as module2 } from "./module2"; |
24 changes: 24 additions & 0 deletions
24
..._js_analyze/tests/specs/nursery/noBarrelFile/invalid_default_named_alias_reexport.ts.snap
This file contains 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,24 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid_default_named_alias_reexport.ts | ||
--- | ||
# Input | ||
```ts | ||
export { default as module2 } from "./module2"; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid_default_named_alias_reexport.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export { default as module2 } from "./module2"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_named_alias_reexport.ts
This file contains 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 @@ | ||
export { module as module1 } from "./module1"; |
26 changes: 26 additions & 0 deletions
26
...es/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_named_alias_reexport.ts.snap
This file contains 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,26 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid_named_alias_reexport.ts | ||
--- | ||
# Input | ||
```ts | ||
export { module as module1 } from "./module1"; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid_named_alias_reexport.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export { module as module1 } from "./module1"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_named_reexprt.ts
This file contains 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 @@ | ||
export { baz, qux } from "foobar"; |
26 changes: 26 additions & 0 deletions
26
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_named_reexprt.ts.snap
This file contains 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,26 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid_named_reexprt.ts | ||
--- | ||
# Input | ||
```ts | ||
export { baz, qux } from "foobar"; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid_named_reexprt.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export { baz, qux } from "foobar"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_wild_alias_reexport.ts
This file contains 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 @@ | ||
export * as bar from "foo"; |
26 changes: 26 additions & 0 deletions
26
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_wild_alias_reexport.ts.snap
This file contains 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,26 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid_wild_alias_reexport.ts | ||
--- | ||
# Input | ||
```ts | ||
export * as bar from "foo"; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid_wild_alias_reexport.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export * as bar from "foo"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_wild_reexport.ts
This file contains 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 @@ | ||
export * from "foo"; |
26 changes: 26 additions & 0 deletions
26
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/invalid_wild_reexport.ts.snap
This file contains 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,26 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid_wild_reexport.ts | ||
--- | ||
# Input | ||
```ts | ||
export * from "foo"; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid_wild_reexport.ts:1:1 lint/nursery/noBarrelFile ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused. | ||
> 1 │ export * from "foo"; | ||
│ ^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
i Check this thorough explanation to better understand the context. | ||
``` | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts
This file contains 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,6 @@ | ||
export type * from "foo"; | ||
export type * as bar from "foo"; | ||
export type { foo } from "foo"; | ||
export type { baz, qux } from "foobar"; | ||
export type { moduleType as moduleType1 } from "module1"; | ||
export type { default as moduleType2 } from "module2"; |
15 changes: 15 additions & 0 deletions
15
crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts.snap
This file contains 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,15 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.ts | ||
--- | ||
# Input | ||
```ts | ||
export type * from "foo"; | ||
export type * as bar from "foo"; | ||
export type { foo } from "foo"; | ||
export type { baz, qux } from "foobar"; | ||
export type { moduleType as moduleType1 } from "module1"; | ||
export type { default as moduleType2 } from "module2"; | ||
``` | ||
|
||
|
Oops, something went wrong.