Skip to content

Commit 721cf0f

Browse files
committed
fix(parser): should be treated comments where after ( as leading comments of next token (#6588)
same as #6355 The behaviour is also same as esbuild and babel does
1 parent 16bea12 commit 721cf0f

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

crates/oxc_parser/src/lexer/trivia_builder.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ impl TriviaBuilder {
7878
self.saw_newline = false;
7979
}
8080

81+
/// Determines if the current line comment should be treated as a trailing comment.
82+
///
83+
/// A line comment should be treated as trailing when both of the following conditions are met:
84+
///
85+
/// 1. It is not preceded by a newline.
86+
///
87+
/// ```javascript
88+
/// let x = 5; // This should be treated as a trailing comment
89+
/// foo(); // This should also be treated as a trailing comment
90+
///
91+
/// // This should not be treated as trailing (preceded by newline)
92+
/// let x = 5;
93+
/// ```
94+
///
95+
/// 2. It does not immediately follow an `=` [`Kind::Eq`] or `(` [`Kind::LParen`]
96+
/// token.
97+
///
98+
/// ```javascript
99+
/// let y = // This should not be treated as trailing (follows `=`)
100+
/// 10;
101+
///
102+
/// function foo( // This should not be treated as trailing (follows `(`)
103+
/// param
104+
/// ) {}
105+
/// ```
106+
fn should_be_treated_as_trailing_comment(&self) -> bool {
107+
!self.saw_newline && !matches!(self.previous_kind, Kind::Eq | Kind::LParen)
108+
}
109+
81110
fn add_comment(&mut self, comment: Comment) {
82111
// The comments array is an ordered vec, only add the comment if its not added before,
83112
// to avoid situations where the parser needs to rewind and tries to reinsert the comment.
@@ -93,8 +122,7 @@ impl TriviaBuilder {
93122
if comment.is_line() {
94123
// A line comment is always followed by a newline. This is never set in `handle_newline`.
95124
comment.followed_by_newline = true;
96-
// A line comment is trailing when it is no preceded by a newline and it is not after `=`
97-
if !self.saw_newline && self.previous_kind != Kind::Eq {
125+
if self.should_be_treated_as_trailing_comment() {
98126
self.processed = self.comments.len() + 1; // +1 to include this comment.
99127
}
100128
self.saw_newline = true;
@@ -275,4 +303,34 @@ token /* Trailing 1 */
275303
];
276304
assert_eq!(comments, expected);
277305
}
306+
307+
#[test]
308+
fn leading_comments_after_left_parenthesis() {
309+
let source_text = "
310+
call(// Leading comment 1
311+
arguments)
312+
(// Leading comment 2
313+
arguments)
314+
";
315+
let comments = get_comments(source_text);
316+
let expected = vec![
317+
Comment {
318+
span: Span::new(20, 38),
319+
kind: CommentKind::Line,
320+
position: CommentPosition::Leading,
321+
attached_to: 55,
322+
preceded_by_newline: false,
323+
followed_by_newline: true,
324+
},
325+
Comment {
326+
span: Span::new(81, 99),
327+
kind: CommentKind::Line,
328+
position: CommentPosition::Leading,
329+
attached_to: 116,
330+
preceded_by_newline: false,
331+
followed_by_newline: true,
332+
},
333+
];
334+
assert_eq!(comments, expected);
335+
}
278336
}

0 commit comments

Comments
 (0)