{"record":{"id":"0b591c4056dda952","repo":"evanw/esbuild","slug":"unexpected-backslash-in-jsx-element","errorCode":null,"errorMessage":"Unexpected backslash in JSX element","messagePattern":"Unexpected backslash in JSX element","errorType":"panic","errorClass":"LexerPanic","httpStatus":null,"severity":"error","filePath":"internal/js_parser/js_parser.go","lineNumber":5422,"sourceCode":"\t\t\txmlEscape = \"&apos;\"\n\t\t}\n\t\tif xmlEscape != \"\" {\n\t\t\tdata := p.tracker.MsgData(p.lexer.PreviousBackslashQuoteInJSX,\n\t\t\t\t\"Quoted JSX attributes use XML-style escapes instead of JavaScript-style escapes:\")\n\t\t\tdata.Location.Suggestion = xmlEscape\n\t\t\tmsg.Notes = append(msg.Notes, data)\n\t\t}\n\n\t\t// Option 2: Suggest using a JavaScript string\n\t\tif stringRange := p.source.RangeOfString(previousStringWithBackslashLoc); stringRange.Len > 0 {\n\t\t\tdata := p.tracker.MsgData(stringRange,\n\t\t\t\t\"Consider using a JavaScript string inside {...} instead of a quoted JSX attribute:\")\n\t\t\tdata.Location.Suggestion = fmt.Sprintf(\"{%s}\", p.source.TextForRange(stringRange))\n\t\t\tmsg.Notes = append(msg.Notes, data)\n\t\t}\n\n\t\tp.log.AddMsg(msg)\n\t\tpanic(js_lexer.LexerPanic{})\n\t}\n\n\t// A slash here is a self-closing element\n\tif p.lexer.Token == js_lexer.TSlash {\n\t\t// Use NextInsideJSXElement() not Next() so we can parse \">>\" as \">\"\n\t\tcloseLoc := p.lexer.Loc()\n\t\tp.lexer.NextInsideJSXElement()\n\t\tif p.lexer.Token != js_lexer.TGreaterThan {\n\t\t\tp.lexer.Expected(js_lexer.TGreaterThan)\n\t\t}\n\t\treturn js_ast.Expr{Loc: loc, Data: &js_ast.EJSXElement{\n\t\t\tTagOrNil:        startTagOrNil,\n\t\t\tProperties:      properties,\n\t\t\tCloseLoc:        closeLoc,\n\t\t\tIsTagSingleLine: isSingleLine,\n\t\t}}\n\t}\n","sourceCodeStart":5404,"sourceCodeEnd":5440,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/internal/js_parser/js_parser.go#L5404-L5440","documentation":"JSX quoted attributes follow XML escaping rules, not JavaScript: a backslash inside the quoted value is a literal backslash and does not escape the quote. esbuild special-cases the common mistake of pasting JSON.stringify output (e.g. content=\"say \\\"hi\\\"\") and reports this message, suggesting &quot;/&apos; or a {...} JS expression.","triggerScenarios":"<Button content=\"some so-called \\\"text\\\"\" /> — a JS-style backslash escape inside a double-quoted JSX attribute. The lexer surfaces TSyntaxError with raw '\\' and a previous-string-with-backslash location.","commonSituations":"Auto-generating JSX from JSON.stringify; pasting JS string escapes into JSX attributes; mixing JS and XML escaping mental models.","solutions":["Use XML entities (&quot; for \", &apos; for ').","Wrap the value in a JS expression: content={\"say \\\"hi\\\"\"}.","Use the opposite quote style for the attribute so no escaping is needed."],"exampleFix":"// before\n<Button content=\"say \\\"hi\\\"\" />\n// after\n<Button content={\"say \\\"hi\\\"\"} />","handlingStrategy":"validation","validationCode":"import \"github.com/evanw/esbuild/pkg/api\"\nres := api.Transform(jsx, api.TransformOptions{Loader: api.LoaderJSX})\nfor _, m := range res.Errors {\n  if strings.Contains(m.Text, \"backslash in JSX\") {\n    // a JS-style \\' or \\\" was used inside a quoted JSX attribute\n  }\n}","typeGuard":"// Flag a quoted JSX attribute containing a JS-style backslash escape.\nvar reJsxEscapedQuote = regexp.MustCompile(`<[-A-Za-z0-9.]+[^>]*\\b[-:A-Za-z][-:A-Za-z0-9]*\\\\s*=\\\\s*\"[^\"]*\\\\\\\\[\"'][^\"]*\"`)\nfunc jsxAttrHasBackslash(src string) bool { return reJsxEscapedQuote.MatchString(src) }","tryCatchPattern":null,"preventionTips":["Never paste JSON.stringify output into a JSX attribute directly; wrap in {...}.","Use XML entities (&quot;, &apos;) for quotes inside JSX attributes.","In codegen, emit JSX attributes as {...} expressions when the value may contain quotes."],"tags":["syntax-error","parser","jsx","escaping"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}