Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaped double quote in JSX attribute #1115

Closed
aleclarson opened this issue Apr 6, 2021 · 2 comments
Closed

Escaped double quote in JSX attribute #1115

aleclarson opened this issue Apr 6, 2021 · 2 comments

Comments

@aleclarson
Copy link

esbuild.transformSync(`<Test json="{ \\"a\\": 1 }" />`, {loader: 'jsx'})
// => error: Expected ">" but found "\\"
@ihupoo
Copy link

ihupoo commented Apr 6, 2021

Also

esbuild.transformSync(`<Test json="\s" />`, {loader: 'jsx'})

the actual transform result is missing \

{
  warnings: [],
  code: '/* @__PURE__ */ React.createElement(Test, {\n  json: "s"\n});\n',
  map: ''
}

@evanw
Copy link
Owner

evanw commented Apr 6, 2021

esbuild.transformSync(`<Test json="{ \\"a\\": 1 }" />`, {loader: 'jsx'})
// => error: Expected ">" but found "\\"

JSX is not JS, and strings in JSX do not work like strings in JS. There is nothing in the JSX specification that mentions backslash escapes. TypeScript, Babel, and esbuild will all not compile this JSX code because it contains a syntax error. You can escape a quote in JSX using &quot; instead of \". See #959 (comment) and the rest of that thread for more information.

esbuild.transformSync(`<Test json="\s" />`, {loader: 'jsx'})

Your problem is unrelated to the original post. The \s becomes s before it even gets to the call to esbuild.transformSync(), so this has nothing to do with esbuild. For example, console.log(`<Test json="\s" />`) will also print <Test json="s" />.

Closing because this is working as intended.

@evanw evanw closed this as completed Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants