oxc-project/oxc · warning · OxcDiagnostic

`String.raw` should be used to avoid escaping `\`.

Error message

`String.raw` should be used to avoid escaping `\`.

What it means

This is the oxlint rule `unicorn/prefer-string-raw`. It fires on string literals whose only escape sequences are escaped backslashes (`\\`), suggesting `String.raw` tagged templates so Windows paths and regex-like text stay readable. The rule checks plain string literals used as property keys, enum members, JSX attribute values, and TSLiteral positions via the AST kinds it imports.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_string_raw.rs:14

use oxc_allocator::{GetAddress, UnstableAddress};
use oxc_ast::{
    AstKind,
    ast::{JSXAttributeValue, PropertyKey, TSEnumMemberName, TSLiteral},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::keyword::RESERVED_KEYWORDS;

use crate::{AstNode, context::LintContext, rule::Rule};

fn prefer_string_raw(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(r"`String.raw` should be used to avoid escaping `\`.").with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferStringRaw;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefers use of `String.raw` to avoid escaping `\`.
    ///
    /// ### Why is this bad?
    ///
    /// Excessive backslashes can make string values less readable which can be avoided by using `String.raw`.
    ///
    /// ### Examples
    ///
    /// Examples of **incorrect** code for this rule:
    /// ```javascript

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the literal as a tagged template: `String.raw\`C:\Users\dale\` — no escaping needed.
  2. If the string also escapes quotes or control characters, leave it as-is; the rule should not flag those, and mixing concerns makes `String.raw` wrong.
  3. Disable the rule with `"unicorn/prefer-string-raw": "off"` in `.oxlintrc.json` if the codebase prefers quoted strings everywhere.

Example fix

// before
const path = 'C:\\Users\\dale\\notes.txt';

// after
const path = String.raw`C:\Users\dale\notes.txt`;
Defensive patterns

Strategy: validation

Validate before calling

// Guard readability with a tiny helper if backslash-heavy
const raw = String.raw;
const path = raw`C:\Users\dale`; // no double-backslash noise

Prevention

When it happens

Trigger: Any normal string literal containing `\\` (an escaped backslash), such as `'C:\\Users\\dale'` or `"a\\b"`, when the backslash is the only character being escaped. Applies to string literals in expressions, property keys, TSEnumMember names, and JSX attribute string values.

Common situations: Hard-coded Windows file paths, network UNC paths (`\\\\server\\share`), or user-input patterns written by developers on Windows; note the rule intentionally does not fire on strings that also escape quotes or newlines, and `String.raw` changes behavior if the string is later templated.

Related errors


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