From 63d43be6773054def7ad82db5456e727f3dcbaf1 Mon Sep 17 00:00:00 2001 From: ty <62130798+togami2864@users.noreply.github.com> Date: Thu, 7 Mar 2024 20:58:58 +0900 Subject: [PATCH] fix: handle export clause correctly in noBarrelFile (#1993) --- CHANGELOG.md | 4 +++- .../src/analyzers/nursery/no_barrel_file.rs | 24 +++++++++++++------ .../specs/nursery/noBarrelFile/valid.d.ts | 8 +++++++ .../nursery/noBarrelFile/valid.d.ts.snap | 18 ++++++++++++++ .../tests/specs/nursery/noBarrelFile/valid.ts | 4 +++- .../specs/nursery/noBarrelFile/valid.ts.snap | 3 +++ .../src/content/docs/internals/changelog.md | 4 +++- .../docs/linter/rules/no-barrel-file.md | 1 + 8 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts create mode 100644 crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d994c4a1ea1..2e7da0983abf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs b/crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs index fb0d89c4b80c..126057617334 100644 --- a/crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs +++ b/crates/biome_js_analyze/src/analyzers/nursery/no_barrel_file.rs @@ -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! { @@ -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/ /// @@ -54,6 +57,13 @@ impl Rule for NoBarrelFile { type Options = (); fn run(ctx: &RuleContext) -> Self::Signals { + if ctx + .source_type::() + .language() + .is_definition_file() + { + return None; + } let items = ctx.query().items(); for i in items { @@ -62,8 +72,8 @@ 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); } } @@ -71,18 +81,18 @@ impl Rule for NoBarrelFile { 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; } } } diff --git a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts new file mode 100644 index 000000000000..8600e1fb719e --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts @@ -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; diff --git a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts.snap b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts.snap new file mode 100644 index 000000000000..3e5f218503ed --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.d.ts.snap @@ -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; + +``` + + diff --git a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts index 996c215e0ff0..8600e1fb719e 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts +++ b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts @@ -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"; \ No newline at end of file +export type { default as moduleType2 } from "module2"; + +export const A = 0; diff --git a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts.snap b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts.snap index d344d487acc7..13bc2bbbc8cf 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/nursery/noBarrelFile/valid.ts.snap @@ -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; + ``` diff --git a/website/src/content/docs/internals/changelog.md b/website/src/content/docs/internals/changelog.md index ffbe61fc2bcc..decf0062e800 100644 --- a/website/src/content/docs/internals/changelog.md +++ b/website/src/content/docs/internals/changelog.md @@ -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 @@ -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 diff --git a/website/src/content/docs/linter/rules/no-barrel-file.md b/website/src/content/docs/linter/rules/no-barrel-file.md index 62a2cd5795b1..f73590878d2c 100644 --- a/website/src/content/docs/linter/rules/no-barrel-file.md +++ b/website/src/content/docs/linter/rules/no-barrel-file.md @@ -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/