Skip to content

Commit d978ec9

Browse files
committed
fix: #65 - Fix comment after last object property getting moved around.
1 parent 22659f8 commit d978ec9

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "dprint-plugin-typescript"
33
description = "TypeScript code formatting plugin for dprint."
44
keywords = ["formatting", "formatter", "typescript"]
5-
version = "0.32.3"
5+
version = "0.32.4"
66
authors = ["David Sherret <[email protected]>"]
77
edition = "2018"
88
license = "MIT"

src/parsing/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5232,7 +5232,7 @@ fn parse_node_with_separator<'a>(value: Option<Node<'a>>, parsed_separator: Prin
52325232
if let Some(element) = element {
52335233
match context.token_finder.get_next_token_if_comma(element) {
52345234
Some(comma) => Some(comma),
5235-
None => context.token_finder.get_last_comma_token_within(element), // may occur for type literals
5235+
None => context.token_finder.get_last_token_within_if_comma(element), // may occur for type literals
52365236
}
52375237
} else {
52385238
None // not a comma separated node

src/parsing/tokens.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl<'a> TokenFinder<'a> {
7676
self.get_last_token_within(node, |token| token.token == Token::RBrace)
7777
}
7878

79-
pub fn get_last_comma_token_within(&mut self, node: &dyn Ranged) -> Option<&'a TokenAndSpan> {
80-
self.get_last_token_within(node, |token| token.token == Token::Comma)
79+
pub fn get_last_token_within_if_comma(&mut self, node: &dyn Ranged) -> Option<&'a TokenAndSpan> {
80+
self.get_last_token_within_if(node, |token| token.token == Token::Comma)
8181
}
8282

8383
pub fn get_first_open_bracket_token_within(&mut self, node: &dyn Ranged) -> Option<&'a TokenAndSpan> {
@@ -193,6 +193,13 @@ impl<'a> TokenFinder<'a> {
193193
let node_span_data = node.span_data();
194194
self.inner.get_last_token_within(node_span_data.lo, node_span_data.hi, is_match)
195195
}
196+
197+
#[inline]
198+
fn get_last_token_within_if(&mut self, node: &dyn Ranged, is_match: impl Fn(&'a TokenAndSpan) -> bool) -> Option<&'a TokenAndSpan> {
199+
let node_span_data = node.span_data();
200+
let last_token = self.inner.get_last_token_within(node_span_data.lo, node_span_data.hi, |_| true);
201+
last_token.filter(|token| is_match(&token))
202+
}
196203
}
197204

198205
fn get_text<'a>(file_bytes: &'a [u8], span_data: &Span) -> &'a str {

tests/specs/issues/issue0065.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
== should not move comment ==
2+
this.test.push({
3+
name: test,
4+
count: 5, // update 5 times
5+
});
6+
7+
[expect]
8+
this.test.push({
9+
name: test,
10+
count: 5, // update 5 times
11+
});

0 commit comments

Comments
 (0)