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

  1. Write the escape as `\${` — backslash before the dollar, plain `{` after.
  2. Run `oxlint --fix`; it normalizes every variant (`$\{`, `\$\{`) to `\${` in one pass.
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/ae2dcd493b317413. Report an issue: GitHub.