From 8f652af980470ed56ed6f5c91ce278425f53c112 Mon Sep 17 00:00:00 2001 From: Alex Rattray Date: Wed, 11 Jan 2017 18:08:07 -0800 Subject: [PATCH] Wrap escaped quote in JSX attribute in JSXExpressionContainer (fixes issue mentioned in #73) --- src/printer.js | 11 ++++++++++- tests/prettier/__snapshots__/jsfmt.spec.js.snap | 12 ++++++++++++ tests/prettier/jsx-string-escape.js | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/prettier/jsx-string-escape.js diff --git a/src/printer.js b/src/printer.js index 0457538fbf29..6c098fa7a08d 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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": diff --git a/tests/prettier/__snapshots__/jsfmt.spec.js.snap b/tests/prettier/__snapshots__/jsfmt.spec.js.snap index 441a9fcb1625..2e479538e901 100644 --- a/tests/prettier/__snapshots__/jsfmt.spec.js.snap +++ b/tests/prettier/__snapshots__/jsfmt.spec.js.snap @@ -178,6 +178,18 @@ const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => " `; +exports[`test jsx-string-escape.js 1`] = ` +"
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
; +" +`; + exports[`test optional-type-name.js 1`] = ` "type Foo = (any) => string diff --git a/tests/prettier/jsx-string-escape.js b/tests/prettier/jsx-string-escape.js new file mode 100644 index 000000000000..225f1fb49ef9 --- /dev/null +++ b/tests/prettier/jsx-string-escape.js @@ -0,0 +1,6 @@ +