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

Commit

Permalink
fix(rome_js_analyze): keep comments noUnnecessaryContinue (#4315)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Mar 21, 2023
1 parent 8274551 commit 763fd98
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Ast, Rule
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_syntax::{JsContinueStatement, JsLabeledStatement, JsSyntaxKind, JsSyntaxNode};
use rome_rowan::{AstNode, BatchMutationExt};
use rome_rowan::{chain_trivia_pieces, AstNode, BatchMutationExt};

use crate::{utils, JsRuleAction};

Expand Down Expand Up @@ -101,10 +101,24 @@ impl Rule for NoUnnecessaryContinue {

fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();

let continue_token = node.continue_token().ok()?;
let mut mutation = ctx.root().begin();
if continue_token.has_leading_comments() {
let prev_token = continue_token.prev_token()?;
let leading_trivia = continue_token.leading_trivia().pieces();
let skip_count = leading_trivia.len()
- leading_trivia
.rev()
.position(|x| x.is_comments())
.map(|pos| pos + 1)
.unwrap_or(0);
let new_token = prev_token.with_trailing_trivia_pieces(chain_trivia_pieces(
prev_token.trailing_trivia().pieces(),
continue_token.leading_trivia().pieces().skip(skip_count),
));
mutation.replace_token(prev_token, new_token);
}
utils::remove_statement(&mut mutation, node)?;

Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ loop: for (let i = 0; i < 10; i++) {
}
}
}

for (const x of []) {
if (x) {
// before
continue; // statement
// after
} else {
doSomeStuff();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 99
assertion_line: 91
expression: noUnnecessaryContinue.js
---
# Input
Expand Down Expand Up @@ -79,6 +79,15 @@ loop: for (let i = 0; i < 10; i++) {
}
}

for (const x of []) {
if (x) {
// before
continue; // statement
// after
} else {
doSomeStuff();
}
}
```

# Diagnostics
Expand Down Expand Up @@ -240,4 +249,30 @@ noUnnecessaryContinue.js:28:2 lint/correctness/noUnnecessaryContinue FIXABLE
```

```
noUnnecessaryContinue.js:78:3 lint/correctness/noUnnecessaryContinue FIXABLE ━━━━━━━━━━━━━━━━━━━━━
! Unnecessary continue statement
76 │ if (x) {
77 │ // before
> 78 │ continue; // statement
│ ^^^^^^^^^
79 │ // after
80 │ } else {
i Suggested fix: Delete the unnecessary continue statement
75 75 │ for (const x of []) {
76 76 │ if (x) {
77 │ - → → //·before
78 │ - → → continue;·//·statement
79 │ - → → //·after
77 │ + → → //·after
80 78 │ } else {
81 79 │ doSomeStuff();
```


0 comments on commit 763fd98

Please sign in to comment.