Skip to content

Commit

Permalink
Wrap escaped quote in JSX attribute in JSXExpressionContainer (fixes …
Browse files Browse the repository at this point in the history
…issue mentioned in #73)
  • Loading branch information
rattrayalex committed Jan 12, 2017
1 parent e033326 commit 8f652af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/printer.js
Expand Up @@ -1028,8 +1028,17 @@ function genericPrintNoParens(path, options, print) {
case "JSXAttribute":
parts.push(path.call(print, "name"));

if (n.value)
if (n.value) {
// JSX doesn't allow attributes with escaped quotes, so wrap in JSXExpressionContainer
if (n.value.type === "Literal") {
const str = nodeStr(n.value.value, options);
if (str.match(/\\['"]/)) {
parts.push("=", "{", str, "}");
return concat(parts);
}
}
parts.push("=", path.call(print, "value"));
}

return concat(parts);
case "JSXIdentifier":
Expand Down
12 changes: 12 additions & 0 deletions tests/prettier/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -178,6 +178,18 @@ const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) =>
"
`;

exports[`test jsx-string-escape.js 1`] = `
"<div
a=\"\'\"
b=\'\"\'
c={\"\\\"\"}
d={\'\\\'\'}
/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<div a=\"\'\" b={\"\\\"\"} c={\"\\\"\"} d={\"\'\"} />;
"
`;
exports[`test optional-type-name.js 1`] = `
"type Foo = (any) => string
Expand Down
6 changes: 6 additions & 0 deletions tests/prettier/jsx-string-escape.js
@@ -0,0 +1,6 @@
<div
a="'"
b='"'
c={"\""}
d={'\''}
/>

0 comments on commit 8f652af

Please sign in to comment.