Skip to content

Commit d3cdc82

Browse files
committed
fix(linter/escape-case): false positive for String.raw (#20496)
1 parent 50d59e9 commit d3cdc82

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

crates/oxc_linter/src/rules/unicorn/escape_case.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use oxc_diagnostics::OxcDiagnostic;
55
use oxc_macros::declare_oxc_lint;
66
use oxc_span::Span;
77

8-
use crate::{AstNode, context::LintContext, rule::Rule};
8+
use crate::{
9+
AstNode, context::LintContext, rule::Rule, utils::is_string_raw_tagged_template_expression,
10+
};
911

1012
fn escape_case_diagnostic(span: Span) -> OxcDiagnostic {
1113
OxcDiagnostic::warn("Use uppercase characters for the value of the escape sequence.")
@@ -133,7 +135,11 @@ impl Rule for EscapeCase {
133135
});
134136
}
135137
}
136-
AstKind::TemplateLiteral(lit) => {
138+
AstKind::TemplateLiteral(lit)
139+
if !is_string_raw_tagged_template_expression(
140+
&ctx.nodes().parent_kind(lit.node_id()),
141+
) =>
142+
{
137143
lit.quasis.iter().for_each(|quasi| {
138144
let text = quasi.span.source_text(ctx.source_text());
139145
if let Some(fixed) = check_case(text, false) {
@@ -189,6 +195,7 @@ fn test() {
189195
r"const foo = `foo\\\\xbar`;",
190196
r"const foo = `foo\\\\ubarbaz`;",
191197
r"const foo = `\ca`;",
198+
r"const foo = String.raw`\uAaAa`;",
192199
r"const foo = /foo\xA9/",
193200
r"const foo = /foo\uD834/",
194201
r"const foo = /foo\u{1D306}/u",

crates/oxc_linter/src/utils/unicorn.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,15 @@ pub fn get_precedence(expr: &Expression) -> Option<Precedence> {
479479
_ => None,
480480
}
481481
}
482+
483+
pub fn is_string_raw_tagged_template_expression(node: &AstKind) -> bool {
484+
if let AstKind::TaggedTemplateExpression(tagged_template_expression) = node
485+
&& let Expression::StaticMemberExpression(member_expr) = &tagged_template_expression.tag
486+
&& let Expression::Identifier(ident) = &member_expr.object
487+
&& member_expr.property.name == "raw"
488+
&& ident.name == "String"
489+
{
490+
return true;
491+
}
492+
false
493+
}

0 commit comments

Comments
 (0)