Skip to content

Commit 5925422

Browse files
committed
feat: ignore comments work even if there is another comment in-between
1 parent 9ee079e commit 5925422

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

src/parsing/parser.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -374,41 +374,48 @@ fn parse_node_with_inner_parse<'a>(
374374
}
375375

376376
fn get_has_ignore_comment<'a>(leading_comments: &CommentsIterator<'a>, node: &Node<'a>, context: &mut Context<'a>) -> bool {
377-
return if let Some(last_comment) = get_last_comment(leading_comments, node, context) {
378-
parser_helpers::text_has_dprint_ignore(&last_comment.text, &context.config.ignore_node_comment_text)
379-
} else {
380-
false
377+
let comments = match node.parent() {
378+
Some(Node::JSXElement(jsx_element)) => get_comments_for_jsx_children(&jsx_element.children, &node.lo(), context),
379+
Some(Node::JSXFragment(jsx_fragment)) => get_comments_for_jsx_children(&jsx_fragment.children, &node.lo(), context),
380+
_ => leading_comments.clone(),
381381
};
382382

383-
#[inline]
384-
fn get_last_comment<'a>(leading_comments: &CommentsIterator<'a>, node: &Node, context: &mut Context<'a>) -> Option<&'a Comment> {
385-
return match node.parent() {
386-
Some(Node::JSXElement(jsx_element)) => get_last_comment_for_jsx_children(&jsx_element.children, &node.lo(), context),
387-
Some(Node::JSXFragment(jsx_fragment)) => get_last_comment_for_jsx_children(&jsx_fragment.children, &node.lo(), context),
388-
_ => leading_comments.peek_last_comment(),
383+
for comment in comments.into_iter() {
384+
if parser_helpers::text_has_dprint_ignore(&comment.text, &context.config.ignore_node_comment_text) {
385+
return true;
386+
}
387+
}
388+
389+
return false;
390+
391+
fn get_comments_for_jsx_children<'a>(children: &[JSXElementChild], node_lo: &BytePos, context: &mut Context<'a>) -> CommentsIterator<'a> {
392+
let mut iterator = CommentsIterator::empty();
393+
let index = if let Some(index) = children.binary_search_by_key(node_lo, |child| child.lo()).ok() {
394+
index
395+
} else {
396+
return iterator;
389397
};
390398

391-
fn get_last_comment_for_jsx_children<'a>(children: &[JSXElementChild], node_lo: &BytePos, context: &mut Context<'a>) -> Option<&'a Comment> {
392-
let index = children.binary_search_by_key(node_lo, |child| child.lo()).ok()?;
393-
for i in (0..index).rev() {
394-
match children.get(i)? {
395-
JSXElementChild::JSXExprContainer(expr_container) => {
396-
return match expr_container.expr {
397-
JSXExpr::JSXEmptyExpr(empty_expr) => get_jsx_empty_expr_comments(&empty_expr, context).last(),
398-
_ => None,
399-
};
400-
}
401-
JSXElementChild::JSXText(jsx_text) => {
402-
if !jsx_text.text_fast(context.program).trim().is_empty() {
403-
return None;
399+
for i in (0..index).rev() {
400+
match children.get(i).unwrap() {
401+
JSXElementChild::JSXExprContainer(expr_container) => {
402+
match expr_container.expr {
403+
JSXExpr::JSXEmptyExpr(empty_expr) => {
404+
iterator.extend(get_jsx_empty_expr_comments(&empty_expr, context));
404405
}
406+
_ => break,
407+
};
408+
}
409+
JSXElementChild::JSXText(jsx_text) => {
410+
if !jsx_text.text_fast(context.program).trim().is_empty() {
411+
break;
405412
}
406-
_ => return None,
407413
}
414+
_ => break,
408415
}
409-
410-
None
411416
}
417+
418+
iterator
412419
}
413420
}
414421

tests/specs/comments/IgnoreComments_All.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ call(5, 6, 7,
5050
5, 6, 7
5151
]
5252

53-
== should not ignore when it's not the immediately preceding comment ==
53+
== should ignore even when it's not the immediately preceding comment ==
5454
// dprint-ignore
5555
// test
5656
[
@@ -61,8 +61,7 @@ call(5, 6, 7,
6161
// dprint-ignore
6262
// test
6363
[
64-
5,
65-
6,
64+
5, 6
6665
];
6766

6867
== ignore comment at top of file should not ignore everything ==

tests/specs/comments/IgnoreComments_Jsx.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,21 @@ const t = (
3030
<Test />
3131
</>
3232
);
33+
34+
== should ignore element after ignore comment in a previous empty element ==
35+
const t = (
36+
<Element>
37+
{/* dprint-ignore */}
38+
{/* deno-lint-ignore */}
39+
<Test />
40+
</Element>
41+
);
42+
43+
[expect]
44+
const t = (
45+
<Element>
46+
{/* dprint-ignore */}
47+
{/* deno-lint-ignore */}
48+
<Test />
49+
</Element>
50+
);

tests/specs/file/File_IgnoreFileComment.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@
7171
== should not skip when it's not before the first statement -- it doesn't bother searching the rest of the file ==
7272
testing ;
7373
/* dprint-ignore-file */
74+
asdf;
7475
// test
7576
testing( 1, 5 );
7677

7778
[expect]
7879
testing;
7980
/* dprint-ignore-file */
81+
asdf;
8082
// test
8183
testing(1, 5);

0 commit comments

Comments
 (0)