Skip to content

Commit

Permalink
fix: handle export clause correctly in noBarrelFile (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Mar 7, 2024
1 parent b8cf046 commit 63d43be
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
× The file contains diagnostics that needs to be addressed.
```
Contributed by @ematipico
- The command `format` now emits parsing diagnostics if there are any, and it will terminate with a non-zero exit code. Contributed by @ematipico
- The command `format` now emits parsing diagnostics if there are any, and it will terminate with a non-zero exit code. Contributed by @ematipico

### Configuration

Expand Down Expand Up @@ -621,6 +621,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Fix [#1945](https://github.com/biomejs/biome/issues/1945) Allow constructor with default parameters in `noUselessConstructor`

- Fix [#1982](https://github.com/biomejs/biome/issues/1982) Change to iterate over the module item lists and ignore .d.ts files. Contributed by @togami2864

### Parser

#### Bug fixes
Expand Down
24 changes: 17 additions & 7 deletions crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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_js_syntax::{
JsExport, JsExportFromClause, JsExportNamedFromClause, JsFileSource, JsModule,
};
use biome_rowan::AstNode;

declare_rule! {
Expand All @@ -10,6 +12,7 @@ declare_rule! {
/// 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.
/// This rule ignores .d.ts files and type-only exports.
///
/// For a more detailed explanation, check out https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/
///
Expand Down Expand Up @@ -54,6 +57,13 @@ impl Rule for NoBarrelFile {
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
if ctx
.source_type::<JsFileSource>()
.language()
.is_definition_file()
{
return None;
}
let items = ctx.query().items();

for i in items {
Expand All @@ -62,27 +72,27 @@ impl Rule for NoBarrelFile {
if let Some(export_from_clause) =
JsExportFromClause::cast(export_from_clause.clone().into())
{
if export_from_clause.type_token().is_some() {
return None;
if export_from_clause.type_token().is_none() {
return Some(export);
}
}

if let Some(export_from_clause) =
JsExportNamedFromClause::cast(export_from_clause.into())
{
if export_from_clause.type_token().is_some() {
return None;
continue;
}
if export_from_clause
if !export_from_clause
.specifiers()
.into_iter()
.flatten()
.all(|s| s.type_token().is_some())
{
return None;
return Some(export);
}
}
return Some(export);
continue;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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";

export const A = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.d.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";

export const A = 0;

```


Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ 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";
export type { default as moduleType2 } from "module2";

export const A = 0;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ 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";

export const A = 0;

```


4 changes: 3 additions & 1 deletion website/src/content/docs/internals/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
× The file contains diagnostics that needs to be addressed.
```
Contributed by @ematipico
- The command `format` now emits parsing diagnostics if there are any, and it will terminate with a non-zero exit code. Contributed by @ematipico
- The command `format` now emits parsing diagnostics if there are any, and it will terminate with a non-zero exit code. Contributed by @ematipico

### Configuration

Expand Down Expand Up @@ -627,6 +627,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Fix [#1945](https://github.com/biomejs/biome/issues/1945) Allow constructor with default parameters in `noUselessConstructor`

- Fix [#1982](https://github.com/biomejs/biome/issues/1982) Change to iterate over the module item lists and ignore .d.ts files. Contributed by @togami2864

### Parser

#### Bug fixes
Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/linter/rules/no-barrel-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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.
This rule ignores .d.ts files and type-only exports.

For a more detailed explanation, check out https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/

Expand Down

0 comments on commit 63d43be

Please sign in to comment.