oxc-project/oxc · warning · OxcDiagnostic
Invalid escape sequence in template literal.
Error message
Invalid escape sequence in template literal.
What it means
Diagnostic from oxlint's `unicorn/consistent-template-literal-escape` rule. Inside template literals the canonical way to write a literal `${` is `\${`; writing `$\{` (escaping the brace instead of the dollar) or `\$\{` is inconsistent and flagged as an invalid escape sequence. The scanner walks each template chunk's raw text looking for the `$\{` sequence, tracking backslash parity so doubled backslashes are handled, and the autofix rewrites the matched range to `\${`. Tagged templates such as `String.raw` and `html` are skipped because their escapes are meaningful.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/consistent_template_literal_escape.rs:9
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule};
fn consistent_template_literal_escape_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Invalid escape sequence in template literal.")
.with_help("Use '\\${' to escape '${' in template literals.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct ConsistentTemplateLiteralEscape;
declare_oxc_lint!(
/// ### What it does
///
/// Enforce consistent style for escaping `${` in template literals.
///
/// ### Why is this bad?
/// Using `\${` instead of `${` can improve readability and prevent confusion.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:View on GitHub (pinned to e1e7af627c)
Solutions
- Write the escape as `\${` — backslash before the dollar, plain `{` after.
- Run `oxlint --fix`; it normalizes every variant (`$\{`, `\$\{`) to `\${` in one pass.
- For tagged templates that must keep raw text, ensure they are tagged (e.g. String.raw) so the rule skips them.
Example fix
// before
const s = `$\{a}`;
// after
const s = `\${a}`; Defensive patterns
Strategy: validation
Prevention
- Always write a literal `${` inside template literals as `\${` — escape the dollar, not the brace.
- Never write `$\{`; it renders the same but the rule (and readers) treat it as inconsistent.
- Use tagged templates (e.g. String.raw) when raw backslash sequences must survive verbatim.
When it happens
Trigger: Template literals whose raw text contains `$\{` or `\$\{` — e.g. `` const s = `$\{a}`; `` or `` const s = `\$\{a}`; `` — including when interpolations surround the sequence (`${expr}$\{a}`). Plain string literals and tagged templates are not flagged.
Common situations: Code generating template-literal source text, shader/GraphQL snippets, or docs examples where authors escaped the brace reflexively; migration from string concatenation where `$` and `{` were separate; enabling the unicorn style preset.
Related errors
- Prefer `{} {}` over `{} {}` to check {}.
- No spaces inside empty pair of braces allowed
- Use uppercase characters for the value of the escape sequenc
- The {expr_type} is useless
- Don't use a zero fraction in the number.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/ae2dcd493b317413.
Report an issue: GitHub.