{"record":{"id":"812a55bbb609f3df","repo":"oxc-project/oxc","slug":"string-raw-should-be-used-to-avoid-escaping","errorCode":null,"errorMessage":"`String.raw` should be used to avoid escaping `\\`.","messagePattern":"`String\\.raw` should be used to avoid escaping `\\\\`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_string_raw.rs","lineNumber":14,"sourceCode":"use oxc_allocator::{GetAddress, UnstableAddress};\nuse oxc_ast::{\n    AstKind,\n    ast::{JSXAttributeValue, PropertyKey, TSEnumMemberName, TSLiteral},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_syntax::keyword::RESERVED_KEYWORDS;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_string_raw(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(r\"`String.raw` should be used to avoid escaping `\\`.\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferStringRaw;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers use of `String.raw` to avoid escaping `\\`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Excessive backslashes can make string values less readable which can be avoided by using `String.raw`.\n    ///\n    /// ### Examples\n    ///\n    /// Examples of **incorrect** code for this rule:\n    /// ```javascript","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_string_raw.rs#L1-L32","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rewrite the literal as a tagged template: `String.raw\\`C:\\Users\\dale\\` — no escaping needed.","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.","Disable the rule with `\"unicorn/prefer-string-raw\": \"off\"` in `.oxlintrc.json` if the codebase prefers quoted strings everywhere."],"exampleFix":"// before\nconst path = 'C:\\\\Users\\\\dale\\\\notes.txt';\n\n// after\nconst path = String.raw`C:\\Users\\dale\\notes.txt`;","handlingStrategy":"validation","validationCode":"// Guard readability with a tiny helper if backslash-heavy\nconst raw = String.raw;\nconst path = raw`C:\\Users\\dale`; // no double-backslash noise","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use String.raw for Windows paths and regex-text constants; keep normal strings for everything else.","Never mix quote escapes into a String.raw candidate — only pure backslash escapes qualify.","Beware template interpolation inside String.raw: `${}` still evaluates, raw-ness applies only to backslashes."],"tags":["oxlint","unicorn","string","readability","template-literal"],"backgroundTag":"string-raw-escaping","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}