Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit a7a402c

Browse files
committed
feat(rome_js_formatter): Implement the decorator formatting #4608
1 parent 68d31a7 commit a7a402c

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

crates/rome_js_formatter/src/comments.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,28 @@ fn handle_parameter_comment(comment: DecoratedComment<JsLanguage>) -> CommentPla
10351035
// )
10361036
// ```
10371037
match comment.enclosing_node().kind() {
1038-
JsSyntaxKind::JS_FORMAL_PARAMETER if comment.text_position().is_own_line() => {
1039-
return CommentPlacement::leading(comment.enclosing_node().clone(), comment)
1038+
JsSyntaxKind::JS_FORMAL_PARAMETER | JsSyntaxKind::TS_PROPERTY_PARAMETER => {
1039+
// Keep decorator comments near the decorator
1040+
// Attach leading parameter comments to the last decorator
1041+
// ```javascript
1042+
// class Foo {
1043+
// method(
1044+
// //leading own line
1045+
// /*leading same line*/ @Decorator /*trailing*/
1046+
// //leading own line between
1047+
// /*leading same line between*/ @dec //trailing
1048+
// /*leading parameter*/
1049+
// parameter
1050+
// ) {}
1051+
// }
1052+
// ```
1053+
if let Some(preceding_node) = comment.preceding_node() {
1054+
if comment.following_node().kind() != Some(JsSyntaxKind::JS_DECORATOR) {
1055+
return CommentPlacement::trailing(preceding_node.clone(), comment);
1056+
}
1057+
} else if comment.text_position().is_own_line() {
1058+
return CommentPlacement::leading(comment.enclosing_node().clone(), comment);
1059+
}
10401060
}
10411061
JsSyntaxKind::JS_INITIALIZER_CLAUSE => {
10421062
if let Some(parameter) = comment

crates/rome_js_formatter/tests/quick_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ mod language {
1313
// use this test check if your snippet prints as you wish, without using a snapshot
1414
fn quick_test() {
1515
let src = r#"
16-
class Class3 {
17-
method(
18-
@dec @dec
19-
{ prop1_1, prop1_2 }: Type,
20-
) {
21-
doSomething();
22-
}
23-
}
16+
class Foo {
17+
constructor(
18+
@dec
19+
/*leading parameter*/ private parameter
20+
) { }
21+
}
22+
23+
2424
"#;
2525
let syntax = JsFileSource::tsx();
2626
let tree = parse(

crates/rome_js_formatter/tests/specs/ts/decoartors.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,23 @@ class Class3 {
236236

237237
}
238238

239+
// comments
240+
class Foo {
241+
constructor(
242+
//leading own line
243+
/*leading same line*/ @Decorator /*trailing*/
244+
//leading own line between
245+
/*leading same line between*/ @dec //trailing
246+
/*leading parameter*/
247+
parameter: number
248+
) {}
249+
250+
method(
251+
//leading own line
252+
/*leading same line*/ @Decorator /*trailing*/
253+
//leading own line between
254+
/*leading same line between*/ @dec //trailing
255+
/*leading parameter*/
256+
parameter
257+
) {}
258+
}

crates/rome_js_formatter/tests/specs/ts/decoartors.ts.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,26 @@ class Class3 {
244244
245245
}
246246
247+
// comments
248+
class Foo {
249+
constructor(
250+
//leading own line
251+
/*leading same line*/ @Decorator /*trailing*/
252+
//leading own line between
253+
/*leading same line between*/ @dec //trailing
254+
/*leading parameter*/
255+
parameter: number
256+
) {}
257+
258+
method(
259+
//leading own line
260+
/*leading same line*/ @Decorator /*trailing*/
261+
//leading own line between
262+
/*leading same line between*/ @dec //trailing
263+
/*leading parameter*/
264+
parameter
265+
) {}
266+
}
247267
248268
```
249269
@@ -491,6 +511,27 @@ class Class3 {
491511
@d3("foo") private z: number,
492512
) {}
493513
}
514+
515+
// comments
516+
class Foo {
517+
constructor(
518+
//leading own line
519+
/*leading same line*/ @Decorator /*trailing*/
520+
//leading own line between
521+
/*leading same line between*/ @dec //trailing
522+
/*leading parameter*/
523+
parameter: number,
524+
) {}
525+
526+
method(
527+
//leading own line
528+
/*leading same line*/ @Decorator /*trailing*/
529+
//leading own line between
530+
/*leading same line between*/ @dec //trailing
531+
/*leading parameter*/
532+
parameter,
533+
) {}
534+
}
494535
```
495536
496537

0 commit comments

Comments
 (0)