mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
parent
b2eeb5e38c
commit
0bcba4fba2
2 changed files with 27 additions and 9 deletions
|
|
@ -19,6 +19,7 @@ pub struct NoUselessEscape;
|
|||
declare_oxc_lint!(
|
||||
/// ### What it does
|
||||
///
|
||||
/// Disallow unnecessary escape characters
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
///
|
||||
|
|
@ -135,15 +136,16 @@ fn check_string(string: &str) -> Vec<usize> {
|
|||
let mut offsets = vec![];
|
||||
let quote_string = string.chars().next();
|
||||
let mut in_escape = false;
|
||||
let mut offset = 0;
|
||||
for c in string[1..].chars() {
|
||||
offset += c.len_utf8();
|
||||
let mut byte_offset = 0;
|
||||
|
||||
for c in string.chars() {
|
||||
byte_offset += c.len_utf8();
|
||||
if in_escape {
|
||||
in_escape = false;
|
||||
match c {
|
||||
c if c.is_ascii_digit() || quote_string == Some(c) => { /* noop */ }
|
||||
c if !VALID_STRING_ESCAPES.contains(c) => {
|
||||
offsets.push(offset);
|
||||
offsets.push(byte_offset - c.len_utf8());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -162,27 +164,29 @@ fn check_template(string: &str) -> Vec<usize> {
|
|||
let mut offsets = vec![];
|
||||
let mut in_escape = false;
|
||||
let mut prev_char = '`';
|
||||
let mut offset = 0;
|
||||
let mut byte_offset = 1;
|
||||
|
||||
let mut chars = string.chars().peekable();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
offset += c.len_utf8();
|
||||
byte_offset += c.len_utf8();
|
||||
|
||||
if in_escape {
|
||||
in_escape = false;
|
||||
match c {
|
||||
c if c.is_ascii_digit() || c == '`' => { /* noop */ }
|
||||
'{' => {
|
||||
if prev_char != '$' {
|
||||
offsets.push(offset);
|
||||
offsets.push(byte_offset - c.len_utf8());
|
||||
}
|
||||
}
|
||||
'$' => {
|
||||
if chars.peek().is_some_and(|c| *c != '{') {
|
||||
offsets.push(offset);
|
||||
offsets.push(byte_offset - c.len_utf8());
|
||||
}
|
||||
}
|
||||
c if !VALID_STRING_ESCAPES.contains(c) => {
|
||||
offsets.push(offset);
|
||||
offsets.push(byte_offset - c.len_utf8());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -356,6 +360,8 @@ fn test() {
|
|||
"`template literal with mixed linebreaks in line continuations \\\n\\\r\\\r\n\\and useless escape`",
|
||||
"`\\a```",
|
||||
r"var foo = /\(([^\)\(]+)\)$|\(([^\)\)]+)\)$/;",
|
||||
r#"var stringLiteralWithNextLine = "line 1\
line 2";"#,
|
||||
r"var stringLiteralWithNextLine = `line 1\
line 2`;",
|
||||
];
|
||||
|
||||
Tester::new_without_config(NoUselessEscape::NAME, pass, fail).test_and_snapshot();
|
||||
|
|
|
|||
|
|
@ -374,4 +374,16 @@ expression: no_useless_escape
|
|||
· ──
|
||||
╰────
|
||||
|
||||
⚠ eslint(no-useless-escape): Unnecessary escape character '\u{85}'
|
||||
╭─[no_useless_escape.tsx:1:1]
|
||||
1 │ var stringLiteralWithNextLine = "line 1\
line 2";
|
||||
· ─
|
||||
╰────
|
||||
|
||||
⚠ eslint(no-useless-escape): Unnecessary escape character '\u{85}'
|
||||
╭─[no_useless_escape.tsx:1:1]
|
||||
1 │ var stringLiteralWithNextLine = `line 1\
line 2`;
|
||||
· ─
|
||||
╰────
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue