Skip to content

Commit

Permalink
fix(organizeImport): ignorer side effect imports
Browse files Browse the repository at this point in the history
Fixes: #817
  • Loading branch information
Conaclos committed Apr 22, 2024
1 parent b150000 commit 8f7fc02
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
13 changes: 7 additions & 6 deletions crates/biome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ fn check_stdin_apply_unsafe_successfully() {
let mut console = BufferConsole::default();

console.in_buffer.push(
"import 'zod'; import 'lodash'; function f() {var x = 1; return{x}} class Foo {}"
"import zod from 'zod'; import _ from 'lodash'; function f() {var x = 1; return{x}} class Foo {}"
.to_string(),
);

Expand Down Expand Up @@ -2236,7 +2236,7 @@ fn check_stdin_apply_unsafe_successfully() {

assert_eq!(
content,
"import \"lodash\";\nimport \"zod\";\nfunction f() {\n\tconst x = 1;\n\treturn { x };\n}\nclass Foo {}\n"
"import _ from \"lodash\";\nimport zod from \"zod\";\nfunction f() {\n\tconst x = 1;\n\treturn { x };\n}\nclass Foo {}\n"
);

assert_cli_snapshot(SnapshotPayload::new(
Expand All @@ -2253,9 +2253,10 @@ fn check_stdin_apply_unsafe_only_organize_imports() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

console
.in_buffer
.push("import 'zod'; import 'lodash'; function f() {return{}} class Foo {}".to_string());
console.in_buffer.push(
"import zod from 'zod'; import _ from 'lodash'; function f() {return{}} class Foo {}"
.to_string(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
Expand Down Expand Up @@ -2287,7 +2288,7 @@ fn check_stdin_apply_unsafe_only_organize_imports() {

assert_eq!(
content,
"import 'lodash'; import 'zod'; function f() {return{}} class Foo {}"
"import _ from 'lodash'; import zod from 'zod'; function f() {return{}} class Foo {}"
);

assert_cli_snapshot(SnapshotPayload::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ expression: content
# Input messages

```block
import 'zod'; import 'lodash'; function f() {return{}} class Foo {}
import zod from 'zod'; import _ from 'lodash'; function f() {return{}} class Foo {}
```

# Emitted Messages

```block
import 'lodash'; import 'zod'; function f() {return{}} class Foo {}
import _ from 'lodash'; import zod from 'zod'; function f() {return{}} class Foo {}
```


Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ expression: content
# Input messages

```block
import 'zod'; import 'lodash'; function f() {var x = 1; return{x}} class Foo {}
import zod from 'zod'; import _ from 'lodash'; function f() {var x = 1; return{x}} class Foo {}
```

# Emitted Messages

```block
import "lodash";
import "zod";
import _ from "lodash";
import zod from "zod";
function f() {
const x = 1;
return { x };
}
class Foo {}
```


Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ impl Rule for OrganizeImports {
continue;
};

let is_side_effect_import = matches!(
import.import_clause(),
Ok(AnyJsImportClause::JsImportBareClause(_))
);
if is_side_effect_import {
if let Some(first_node) = first_node.take() {
groups.push(ImportGroup {
first_node,
nodes: take(&mut nodes),
});
}
// A side effect import creates its own import group
let mut nodes = BTreeMap::new();
nodes.insert(
ImportKey(import.source_text().ok()?),
vec![ImportNode::from(import.clone())],
);
groups.push(ImportGroup {
first_node: import.clone(),
nodes,
});
continue;
}

// If this is not the first import in the group, check for a group break
if has_empty_line(&import.import_token().ok()?.leading_trivia()) {
if let Some(first_node) = first_node.take() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "z"
import { D } from "d";
import { C } from "c";
import "y"
import "x"
import { B } from "b";
import { A } from "a";
import "w"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: side-effect-imports.js
---
# Input
```jsx
import "z"
import { D } from "d";
import { C } from "c";
import "y"
import "x"
import { B } from "b";
import { A } from "a";
import "w"
```

# Actions
```diff
@@ -1,8 +1,8 @@
import "z"
+import { C } from "c";
import { D } from "d";
-import { C } from "c";
import "y"
import "x"
+import { A } from "a";
import { B } from "b";
-import { A } from "a";
import "w"
\ No newline at end of file

```

0 comments on commit 8f7fc02

Please sign in to comment.