Skip to content

Commit

Permalink
fix(formatter): correctly handle comments at end of export/import list (
Browse files Browse the repository at this point in the history
  • Loading branch information
spanishpear authored Jan 20, 2024
1 parent efa2911 commit d853402
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#1584](https://github.com/biomejs/biome/issues/1584). Ensure the LSP only registers the formatter once. Contributed by @nhedger

- Fix [#1589](https://github.com/biomejs/biome/issues/1589). Fix invalid formatting of own line comments when they were at the end of an import/export list. Contributed by @spanishpear

### Configuration

### Editors
Expand Down
21 changes: 21 additions & 0 deletions crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,27 @@ fn handle_import_export_specifier_comment(
}
}

JsSyntaxKind::JS_EXPORT_NAMED_FROM_CLAUSE => {
if let Some(following_token) = comment.following_token() {
// if we are at the end of a list of import specifiers
// and encounter a comment
// ```javascript
// import {
// MULTI,
// LINE,
// THING,
// // some comment here
// } from 'foo'
// - then attach it as a trailing comment to `THING`
if matches!(following_token.kind(), JsSyntaxKind::R_CURLY) {
if let Some(preceding) = comment.preceding_node() {
return CommentPlacement::trailing(preceding.clone(), comment);
}
}
}
CommentPlacement::Default(comment)
}

_ => CommentPlacement::Default(comment),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export {
THING,
HERE,
MULTI,
LINE
// comment here should not get moved
} from 'foo';

import {
THING,
HERE,
MULTI,
LINE
// comment here should not get moved
} from 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/comments/import_exports.js
---

# Input

```js
export {
THING,
HERE,
MULTI,
LINE
// comment here should not get moved
} from 'foo';

import {
THING,
HERE,
MULTI,
LINE
// comment here should not get moved
} from 'foo';
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
export {
THING,
HERE,
MULTI,
LINE,
// comment here should not get moved
} from "foo";

import {
THING,
HERE,
MULTI,
LINE,
// comment here should not get moved
} from "foo";
```


2 changes: 2 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#1584](https://github.com/biomejs/biome/issues/1584). Ensure the LSP only registers the formatter once. Contributed by @nhedger

- Fix [#1589](https://github.com/biomejs/biome/issues/1589). Fix invalid formatting of own line comments when they were at the end of an import/export list. Contributed by @spanishpear

### Configuration

### Editors
Expand Down

0 comments on commit d853402

Please sign in to comment.