Skip to content

Commit

Permalink
fix(linter): not ignore adjacent spans when fixing (#4217)
Browse files Browse the repository at this point in the history
fixes: #4204
  • Loading branch information
mysteryven committed Jul 13, 2024
1 parent 205c259 commit 67240dc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
9 changes: 4 additions & 5 deletions crates/oxc_linter/src/fixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<'a> Fixer<'a> {
if start > end {
return;
}
if i64::from(start) <= last_pos {
if i64::from(start) < last_pos {
return;
}

Expand Down Expand Up @@ -558,14 +558,13 @@ mod test {
}

#[test]
fn apply_one_fix_when_the_start_the_same_as_the_previous_end() {
fn apply_two_fix_when_the_start_the_same_as_the_previous_end() {
let result = get_fix_result(vec![
create_message(remove_start(), Some(REMOVE_START)),
create_message(replace_id(), Some(REPLACE_ID)),
]);
assert_eq!(result.fixed_code, TEST_CODE.replace("var ", ""));
assert_eq!(result.messages.len(), 1);
assert_eq!(result.messages[0].error.to_string(), "foo");
assert_eq!(result.fixed_code, TEST_CODE.replace("var answer", "foo"));
assert_eq!(result.messages.len(), 0);
assert!(result.fixed);
}

Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_useless_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn check_template(string: &str) -> Vec<usize> {

let mut offsets = vec![];
let mut in_escape = false;
let mut prev_char = '`';
let mut prev_non_escape_char = '`';
let mut byte_offset = 1;

let mut chars = string.chars().peekable();
Expand All @@ -199,7 +199,7 @@ fn check_template(string: &str) -> Vec<usize> {
match c {
c if c.is_ascii_digit() || c == '`' => { /* noop */ }
'{' => {
if prev_char != '$' {
if prev_non_escape_char != '$' {
offsets.push(byte_offset - c.len_utf8());
}
}
Expand All @@ -213,10 +213,11 @@ fn check_template(string: &str) -> Vec<usize> {
}
_ => {}
}
prev_non_escape_char = c;
} else if c == '\\' {
in_escape = true;
} else {
prev_char = c;
prev_non_escape_char = c;
}
}

Expand Down Expand Up @@ -400,7 +401,7 @@ fn test() {
("var foo = '\\#';", "var foo = '#';", None),
("var foo = '\\$';", "var foo = '$';", None),
("var foo = '\\p';", "var foo = 'p';", None),
("var foo = '\\p\\a\\@';", "var foo = 'p\\a@';", None),
("var foo = '\\p\\a\\@';", "var foo = 'pa@';", None),
("<foo attr={\"\\d\"}/>", "<foo attr={\"d\"}/>", None),
("var foo = '\\`';", "var foo = '`';", None),
("var foo = `\\\"`;", "var foo = `\"`;", None),
Expand Down
6 changes: 0 additions & 6 deletions crates/oxc_linter/src/snapshots/no_useless_escape.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@ source: crates/oxc_linter/src/tester.rs
· ──
╰────
⚠ eslint(no-useless-escape): Unnecessary escape character '{'
╭─[no_useless_escape.tsx:1:14]
1var foo = `\$\{{${foo}`;
· ──
╰────
⚠ eslint(no-useless-escape): Unnecessary escape character '$'
╭─[no_useless_escape.tsx:1:12]
1 │ var foo = `\$a${foo}`;
Expand Down

0 comments on commit 67240dc

Please sign in to comment.