{"record":{"id":"9f3cef294592c969","repo":"oxc-project/oxc","slug":"unexpected-string-concatenation","errorCode":null,"errorMessage":"Unexpected string concatenation.","messagePattern":"Unexpected string concatenation\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"info","filePath":"crates/oxc_linter/src/rules/eslint/prefer_template.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{BinaryExpression, BinaryOperator, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, fixer::RuleFixer, rule::Rule};\n\nfn prefer_template_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected string concatenation.\")\n        .with_help(\"Use template literals instead of string concatenation.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferTemplate;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Require template literals instead of string concatenation.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// In ES2015 (ES6), we can use template literals instead of string concatenation.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_template.rs#L1-L30","documentation":"oxlint `eslint/prefer-template`: a binary `+` expression involving strings was flagged ('Unexpected string concatenation.') and the rule wants template literals instead. The diagnostic at prefer_template.rs:12 fires from `prefer_template_diagnostic` when the visitor sees string-typed concatenation in a BinaryExpression. Template literals avoid operator-precedence bugs and read better when interpolating values.","triggerScenarios":"`\"Hello \" + name`, `` `total: ` + count ``, or chained concatenation `a + \" \" + b` where at least one operand is a string literal — especially inside loops or when the rule sees dynamic values joined with strings.","commonSituations":"Message/URL/log builders written pre-ES6; JSX-adjacent string assembly; teams that enable prefer-template in a shared config and legacy code suddenly fails CI.","solutions":["Convert to a template literal: `\"Hello \" + name` becomes `` `Hello ${name}` ``.","For long chains, combine into one template literal rather than nested concatenation.","If concatenation is intentional (hot-path micro-optimization, or numeric operands), add `// oxlint-disable-next-line prefer-template`.","Turn the rule off in `.oxlintrc.json` if the team prefers `+`."],"exampleFix":"// before\nconst msg = \"User \" + user.name + \" has \" + user.points + \" points\";\n\n// after\nconst msg = `User ${user.name} has ${user.points} points`;","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer template literals when joining two or more dynamic pieces into a string.","If your team prefers `+`, leave prefer-template off rather than littering disable comments.","Watch operator precedence in concatenation chains — the bugs prefer-template guards against are real even when the rule is off."],"tags":["eslint","oxlint","template-literals","strings","lint"],"backgroundTag":"prefer-template-literals","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}