fix(rome_js_formatter): Trailing comma inside import
Fix
MODIFY
crates/rome_js_formatter/src/js/expressions/call_arguments.rs
crates/rome_js_formatter/src/js/expressions/call_arguments.rs
@@ -12,7 +12,8 @@ use rome_formatter::{format_args, format_element, write, VecBuffer};
use rome_js_syntax::{
JsAnyCallArgument, JsAnyExpression, JsAnyFunctionBody, JsAnyLiteralExpression, JsAnyStatement,
JsCallArgumentList, JsCallArguments, JsCallArgumentsFields, JsCallExpression,
- JsExpressionStatement, JsFunctionExpression, JsLanguage, TsAnyReturnType, TsType,
+ JsExpressionStatement, JsFunctionExpression, JsImportCallExpression, JsLanguage,
+ TsAnyReturnType, TsType,
};
use rome_rowan::{AstSeparatedElement, AstSeparatedList, SyntaxResult};
@@ -99,6 +100,7 @@ impl FormatNodeRule<JsCallArguments> for FormatJsCallArguments {
l_paren: &l_paren_token.format(),
args: &arguments,
r_paren: &r_paren_token.format(),
+ node,
expand: true,
}]
);
@@ -124,7 +126,8 @@ impl FormatNodeRule<JsCallArguments> for FormatJsCallArguments {
l_paren: &l_paren_token.format(),
args: &arguments,
r_paren: &r_paren_token.format(),
- expand: false
+ node,
+ expand: false,
}]
)
}
@@ -344,7 +347,8 @@ fn write_grouped_arguments(
l_paren: &l_paren_token.format(),
args: &arguments,
r_paren: &r_paren_token.format(),
- expand: true
+ node: call_arguments,
+ expand: true,
}]
);
}
@@ -385,7 +389,8 @@ fn write_grouped_arguments(
l_paren: &l_paren,
args: &arguments,
r_paren: &r_paren,
- expand: true
+ node: call_arguments,
+ expand: true,
}]
)?;
buffer.write_element(FormatElement::Tag(Tag::EndEntry))?;
@@ -703,10 +708,13 @@ struct FormatAllArgsBrokenOut<'a> {
args: &'a [FormatCallArgument],
r_paren: &'a dyn Format<JsFormatContext>,
expand: bool,
+ node: &'a JsCallArguments,
}
impl<'a> Format<JsFormatContext> for FormatAllArgsBrokenOut<'a> {
fn fmt(&self, f: &mut Formatter<JsFormatContext>) -> FormatResult<()> {
+ let is_inside_import = self.node.parent::<JsImportCallExpression>().is_some();
+
write!(
f,
[group(&format_args![
@@ -723,7 +731,10 @@ impl<'a> Format<JsFormatContext> for FormatAllArgsBrokenOut<'a> {
write!(f, [entry])?;
}
- write!(f, [FormatTrailingComma::All])
+ if !is_inside_import {
+ write!(f, [FormatTrailingComma::All])?;
+ }
+ Ok(())
})),
self.r_paren,
])

MODIFY
crates/rome_js_formatter/src/js/expressions/import_call_expression.rs
crates/rome_js_formatter/src/js/expressions/import_call_expression.rs
@@ -15,7 +15,7 @@ impl FormatNodeRule<JsImportCallExpression> for FormatJsImportCallExpression {
arguments,
} = node.as_fields();
- write![f, [import_token.format(), arguments.format(),]]
+ write![f, [import_token.format(), arguments.format()]]
}
fn needs_parentheses(&self, item: &JsImportCallExpression) -> bool {

MODIFY
crates/rome_js_formatter/tests/specs/js/module/import/import_call.js.snap
crates/rome_js_formatter/tests/specs/js/module/import/import_call.js.snap
@@ -32,7 +32,7 @@ import(x);
import("x");
import(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- { assert: { type: "json" } },
+ { assert: { type: "json" } }
);

MODIFY
crates/rome_js_formatter/tests/specs/js/module/import/trailing-comma/import_trailing_comma.js
crates/rome_js_formatter/tests/specs/js/module/import/trailing-comma/import_trailing_comma.js
@@ -8,3 +8,23 @@ import {
adsadasdasdasdasdasdasdasdasdasdas,
dsadsadasdasdasdasdasdasdasd,
dsadsadasdasdasdasdasdasdasd, } from "W";
+
+const StandaloneBackendWASMModule = await import(/* webpackChunkName: "standalone" */'./StandaloneBackendWASM');
+
+const StandaloneBackendWASMModule = await import(/* webpackChunkName: "standalone" */'./StandaloneBackendWASM',
+ /* webpackChunkName: "standalone" */'./StandaloneBackendWASM');
+
+import(
+ 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
+ );
+
+import(/* Hello */ "something");
+import("something" /* Hello */);
+import(/* Hello */ "something" /* Hello */);
+import("something" /* Hello */ + "else");
+import(
+ /* Hello */
+ "something",
+ /* Hello */
+ );
+wrap(import(/* Hello */ "something"));

MODIFY
crates/rome_js_formatter/tests/specs/js/module/import/trailing-comma/import_trailing_comma.js.snap
crates/rome_js_formatter/tests/specs/js/module/import/trailing-comma/import_trailing_comma.js.snap
@@ -17,6 +17,26 @@ import {
dsadsadasdasdasdasdasdasdasd,
dsadsadasdasdasdasdasdasdasd, } from "W";
+const StandaloneBackendWASMModule = await import(/* webpackChunkName: "standalone" */'./StandaloneBackendWASM');
+
+const StandaloneBackendWASMModule = await import(/* webpackChunkName: "standalone" */'./StandaloneBackendWASM',
+ /* webpackChunkName: "standalone" */'./StandaloneBackendWASM');
+
+import(
+ 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
+ );
+
+import(/* Hello */ "something");
+import("something" /* Hello */);
+import(/* Hello */ "something" /* Hello */);
+import("something" /* Hello */ + "else");
+import(
+ /* Hello */
+ "something",
+ /* Hello */
+ );
+wrap(import(/* Hello */ "something"));
+
```
@@ -46,6 +66,35 @@ import {
dsadsadasdasdasdasdasdasdasd,
dsadsadasdasdasdasdasdasdasd,
} from "W";
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM",
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+import(
+ "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
+);
+
+import(/* Hello */ "something");
+import("something" /* Hello */);
+import(/* Hello */ "something" /* Hello */);
+import("something" /* Hello */ + "else");
+import(
+ /* Hello */
+ "something"
+ /* Hello */
+);
+wrap(import(/* Hello */ "something"));
+
+
+## Lines exceeding width of 80 characters
+
+ 23: "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
```
## Output 2
@@ -70,6 +119,35 @@ import {
dsadsadasdasdasdasdasdasdasd,
dsadsadasdasdasdasdasdasdasd,
} from "W";
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM",
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+import(
+ "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
+);
+
+import(/* Hello */ "something");
+import("something" /* Hello */);
+import(/* Hello */ "something" /* Hello */);
+import("something" /* Hello */ + "else");
+import(
+ /* Hello */
+ "something"
+ /* Hello */
+);
+wrap(import(/* Hello */ "something"));
+
+
+## Lines exceeding width of 80 characters
+
+ 23: "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
```
## Output 3
@@ -94,6 +172,35 @@ import {
dsadsadasdasdasdasdasdasdasd,
dsadsadasdasdasdasdasdasdasd
} from "W";
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+const StandaloneBackendWASMModule = await import(
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM",
+ /* webpackChunkName: "standalone" */ "./StandaloneBackendWASM"
+);
+
+import(
+ "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
+);
+
+import(/* Hello */ "something");
+import("something" /* Hello */);
+import(/* Hello */ "something" /* Hello */);
+import("something" /* Hello */ + "else");
+import(
+ /* Hello */
+ "something"
+ /* Hello */
+);
+wrap(import(/* Hello */ "something"));
+
+
+## Lines exceeding width of 80 characters
+
+ 23: "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
```

DELETE
crates/rome_js_formatter/tests/specs/prettier/js/comments/dynamic_imports.js.snap
----
-source: crates/rome_js_formatter/tests/prettier_tests.rs
-info:
- test_file: js/comments/dynamic_imports.js
----
-
-# Input
-
-```js
-import(/* Hello */ 'something')
-
-import('something' /* Hello */)
-
-import(/* Hello */ 'something' /* Hello */)
-
-import('something' /* Hello */ + 'else')
-
-import(
- /* Hello */
- 'something'
- /* Hello */
-)
-
-wrap(
- import(/* Hello */
- 'something'
- )
-)
-```
-
-
-# Prettier differences
-
-```diff
---- Prettier
-+++ Rome
-@@ -8,7 +8,7 @@
-
- import(
- /* Hello */
-- "something"
-+ "something",
- /* Hello */
- );
-
-```
-
-# Output
-
-```js
-import(/* Hello */ "something");
-
-import("something" /* Hello */);
-
-import(/* Hello */ "something" /* Hello */);
-
-import("something" /* Hello */ + "else");
-
-import(
- /* Hello */
- "something",
- /* Hello */
-);
-
-wrap(import(/* Hello */ "something"));
-```
-
-
-

DELETE
crates/rome_js_formatter/tests/specs/prettier/js/trailing-comma/dynamic-import.js.snap
----
-source: crates/rome_js_formatter/tests/prettier_tests.rs
----
-
-# Input
-
-```js
-import(
- 'myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename'
-);
-```
-
-
-# Prettier differences
-
-```diff
---- Prettier
-+++ Rome
-@@ -1,3 +1,3 @@
- import(
-- "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename"
-+ "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename",
- );
-```
-
-# Output
-
-```js
-import(
- "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename",
-);
-```
-
-
-# Lines exceeding max width of 80 characters
-```
- 2: "myreallylongdynamicallyloadedmodulenamemyreallylongdynamicallyloadedmodulename",
-```
-

fix(rome_js_formatter): Trailing comma inside import #3600 (#3624)

Fix https://github.com/rome/tools/issues/3600