From 2db7d05fe994bbf31ff0721140c9f9a37054b51f Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Thu, 25 Apr 2024 21:07:39 +0200 Subject: [PATCH] fix(organizeImports): add space where needed (#2602) --- CHANGELOG.md | 3 +++ .../assists/correctness/organize_imports.rs | 9 ++++++++- .../specs/correctness/organizeImports/space.ts | 1 + .../correctness/organizeImports/space.ts.snap | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts create mode 100644 crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index f2dc2d7fb7b7..b6410a79297c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,9 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b Contributed by @Conaclos +- Import sorting now adds spaces where needed ([#1665](https://github.com/biomejs/biome/issues/1665)) + Contributed by @Conaclos + ### CLI #### Bug fixes diff --git a/crates/biome_js_analyze/src/assists/correctness/organize_imports.rs b/crates/biome_js_analyze/src/assists/correctness/organize_imports.rs index 2ce0be9ea063..02db41071049 100644 --- a/crates/biome_js_analyze/src/assists/correctness/organize_imports.rs +++ b/crates/biome_js_analyze/src/assists/correctness/organize_imports.rs @@ -448,7 +448,14 @@ impl ImportNode { // If the node we're attaching this separator to has no trailing trivia, just create a simple comma token let last_trailing_trivia = match node.syntax().last_trailing_trivia() { Some(trivia) if !trivia.is_empty() => trivia, - _ => return make::token(T![,]), + _ => { + let sep = make::token(T![,]); + return if node.syntax().has_leading_newline() { + sep + } else { + sep.with_trailing_trivia([(TriviaPieceKind::Whitespace, " ")]) + }; + } }; // Otherwise we need to clone the trailing trivia from the node to the separator diff --git a/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts b/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts new file mode 100644 index 000000000000..ef810e5b39bb --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts @@ -0,0 +1 @@ +import {type ContactOption, /* 1 */ loadStripe, /* 2 */ type StripeElementsOptions} from '@stripe/stripe-js'; \ No newline at end of file diff --git a/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts.snap b/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts.snap new file mode 100644 index 000000000000..9b7812ac78ee --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/organizeImports/space.ts.snap @@ -0,0 +1,18 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: space.ts +--- +# Input +```ts +import {type ContactOption, /* 1 */ loadStripe, /* 2 */ type StripeElementsOptions} from '@stripe/stripe-js'; +``` + +# Actions +```diff +@@ -1 +1 @@ +-import {type ContactOption, /* 1 */ loadStripe, /* 2 */ type StripeElementsOptions} from '@stripe/stripe-js'; +\ No newline at end of file ++import {type ContactOption, /* 1 */ type StripeElementsOptions, loadStripe /* 2 */ } from '@stripe/stripe-js'; +\ No newline at end of file + +```