Fix(rome_js_formatter): named import
MODIFY
crates/rome_js_formatter/src/js/module/import_named_clause.rs
crates/rome_js_formatter/src/js/module/import_named_clause.rs
use crate::prelude::*;
use crate::FormatNodeFields;
use rome_formatter::write;
+use rome_js_syntax::JsAnyNamedImport;
+use rome_js_syntax::JsAnyNamedImportSpecifier;
use rome_js_syntax::JsImportNamedClause;
use rome_js_syntax::JsImportNamedClauseFields;
+use rome_js_syntax::JsNamedImportSpecifiersFields;
impl FormatNodeFields<JsImportNamedClause> for FormatNodeRule<JsImportNamedClause> {
fn fmt_fields(node: &JsImportNamedClause, f: &mut JsFormatter) -> FormatResult<()> {
@@ -19,14 +22,70 @@ impl FormatNodeFields<JsImportNamedClause> for FormatNodeRule<JsImportNamedClaus
write!(f, [type_token.format(), space_token()])?;
}
+ let is_default_specifier_empty = default_specifier.is_none();
+
if let Some(default_specifier) = default_specifier {
write!(f, [default_specifier.format(), space_token()])?;
}
+ let named_import = named_import?;
+
+ // can_break implementation, return `format_element` instead of boolean to reduce enum conversion overhead.
+ // if `can_break` is true we just use the previous format strategy, otherwise we use the new format strategy.
+ // reference https://github.com/prettier/prettier/blob/5b113e71b1808d6916f446c3aa49c3c53e3bdb98/src/language-js/print/module.js#L173
+
+ // https://github.com/prettier/prettier/blob/5b113e71b1808d6916f446c3aa49c3c53e3bdb98/src/language-js/print/module.js#L184-L209v,
+ // `standaloneSpecifiers` corresponding our `JsDefaultImportSpecifier` + part of `JsNamespaceImportSpecifier`,
+ // `groupedSpecifiers` corresponding our `JsNamedImportSpecifiers`
+
+ // Here we use an opposite way of thinking, we only thinking about the way that can not break
+ // That's to say
+ // 1. `default_specifier` need to be none.
+ // 2. length of `JsNamedImportSpecifiers` at least is one
+ // 3. Surrounding of the only `JsNamedImportSpecifiers` should not have any comments
+ if !is_default_specifier_empty {
+ // `can_break` is true.
+ write![f, [named_import.format()]]
+ } else {
+ match named_import {
+ JsAnyNamedImport::JsNamedImportSpecifiers(ref specifiers)
+ if specifiers.specifiers().len() == 1 =>
+ {
+ // SAFETY: we know that the `specifiers.specifiers().len() == 1`, so unwrap `iter().next()` is safe.
+ let first_specifier = specifiers.specifiers().iter().next().unwrap();
+ match first_specifier {
+ Ok(JsAnyNamedImportSpecifier::JsShorthandNamedImportSpecifier(_)) => {
+ let syntax_node = specifiers.syntax();
+ if syntax_node.has_comments_direct() {
+ write![f, [named_import.format()]]
+ } else {
+ let JsNamedImportSpecifiersFields {
+ l_curly_token,
+ specifiers: _,
+ r_curly_token,
+ } = specifiers.as_fields();
+ write![
+ f,
+ [
+ l_curly_token.format(),
+ space_token(),
+ first_specifier.format(),
+ space_token(),
+ r_curly_token.format()
+ ]
+ ]
+ }
+ }
+ _ => write![f, [named_import.format()]],
+ }
+ }
+ _ => write![f, [named_import.format()]],
+ }
+ }?;
+
write![
f,
[
- named_import.format(),
space_token(),
from_token.format(),
space_token(),

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/import/inline.js
crates/rome_js_formatter/tests/specs/prettier/js/import/inline.js
@@ -2,3 +2,4 @@ import somethingSuperLongsomethingSuperLong from 'somethingSuperLongsomethingSup
import {somethingSuperLongsomethingSuperLong1} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import a, {somethingSuperLongsomethingSuperLong2} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import {a2, somethingSuperLongsomethingSuperLong3} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
+import {somethingSuperLongsomethingSuperLoooooooooooooooooooooooooooooooooooooooooooong} from 'source'
\ No newline at end of file

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/import/inline.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/import/inline.js.snap
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
+assertion_line: 182
expression: inline.js
---
# Input
@@ -8,15 +9,13 @@ import somethingSuperLongsomethingSuperLong from 'somethingSuperLongsomethingSup
import {somethingSuperLongsomethingSuperLong1} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import a, {somethingSuperLongsomethingSuperLong2} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
import {a2, somethingSuperLongsomethingSuperLong3} from 'somethingSuperLongsomethingSuperLongsomethingSuperLong'
-
+import {somethingSuperLongsomethingSuperLoooooooooooooooooooooooooooooooooooooooooooong} from 'source'
```
# Output
```js
import somethingSuperLongsomethingSuperLong from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
-import {
- somethingSuperLongsomethingSuperLong1,
-} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
+import { somethingSuperLongsomethingSuperLong1 } from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
import a, {
somethingSuperLongsomethingSuperLong2,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
@@ -24,11 +23,14 @@ import {
a2,
somethingSuperLongsomethingSuperLong3,
} from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
+import { somethingSuperLongsomethingSuperLoooooooooooooooooooooooooooooooooooooooooooong } from "source";
```
# Lines exceeding max width of 80 characters
```
1: import somethingSuperLongsomethingSuperLong from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
+ 2: import { somethingSuperLongsomethingSuperLong1 } from "somethingSuperLongsomethingSuperLongsomethingSuperLong";
+ 10: import { somethingSuperLongsomethingSuperLoooooooooooooooooooooooooooooooooooooooooooong } from "source";
```

Fix(rome_js_formatter): named import (#2498)