From d8534021cc605a4f83d1320cd97066ad65aebe9c Mon Sep 17 00:00:00 2001 From: Shrey Sudhir Date: Sat, 20 Jan 2024 18:32:01 +1100 Subject: [PATCH] fix(formatter): correctly handle comments at end of export/import list (#1603) --- CHANGELOG.md | 2 + crates/biome_js_formatter/src/comments.rs | 21 ++++++ .../js/module/comments/import_exports.js | 15 +++++ .../js/module/comments/import_exports.js.snap | 66 +++++++++++++++++++ .../src/content/docs/internals/changelog.mdx | 2 + 5 files changed, 106 insertions(+) create mode 100644 crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js create mode 100644 crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd4157bd8ae..2559b62a3f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/biome_js_formatter/src/comments.rs b/crates/biome_js_formatter/src/comments.rs index 4b938e6079c1..3016bc9d1038 100644 --- a/crates/biome_js_formatter/src/comments.rs +++ b/crates/biome_js_formatter/src/comments.rs @@ -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), } } diff --git a/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js b/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js new file mode 100644 index 000000000000..b5636eb99cba --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js @@ -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'; \ No newline at end of file diff --git a/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js.snap b/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js.snap new file mode 100644 index 000000000000..a8ad46c4be0e --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/js/module/comments/import_exports.js.snap @@ -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"; +``` + + diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index e8dc33296ce7..d7e2c0a259e3 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -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