diff --git a/changelog_unreleased/handlebars/12302.md b/changelog_unreleased/handlebars/12302.md new file mode 100644 index 000000000..0798f5dea --- /dev/null +++ b/changelog_unreleased/handlebars/12302.md @@ -0,0 +1,21 @@ +#### Correctly identify which backslashes are removed by glimmer (#12302 by @MattTheNub) + +This caused Prettier to unnecessarily add backslashes every time a file was formatted. + + +```hbs +{{! Input }} +
\
+\\
+\\\
+ +{{! Prettier stable }} +\\
+\\\
+\\\\
+ +{{! Prettier main }} +\
+\\
+\\\
+``` diff --git a/src/language-handlebars/parser-glimmer.js b/src/language-handlebars/parser-glimmer.js index e73970578..191cc494a 100644 --- a/src/language-handlebars/parser-glimmer.js +++ b/src/language-handlebars/parser-glimmer.js @@ -12,8 +12,21 @@ function addBackslash(/* options*/) { return { name: "addBackslash", visitor: { - TextNode(node) { - node.chars = node.chars.replace(/\\/, "\\\\"); + All(node) { + const childrenOrBody = node.children || node.body; + if (childrenOrBody) { + for (let i = 0; i < childrenOrBody.length - 1; i++) { + if ( + childrenOrBody[i].type === "TextNode" && + childrenOrBody[i + 1].type === "MustacheStatement" + ) { + childrenOrBody[i].chars = childrenOrBody[i].chars.replace( + /\\$/, + "\\\\" + ); + } + } + } }, }, }; diff --git a/tests/format/handlebars/escape/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/escape/__snapshots__/jsfmt.spec.js.snap index 9348648cb..2688ed5b4 100644 --- a/tests/format/handlebars/escape/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/escape/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,37 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`backslashes.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 + | printWidth +=====================================input====================================== +\\
+\\\\
+\\\\\\
+\\\\\\ \\\\{{non-escaped-moustache}}
+ +=====================================output===================================== +\\
+\\\\
+\\\\\\
+\\\\\\ \\\\{{non-escaped-moustache}}
+================================================================================ +`; + +exports[`backslashes-in-attributes.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + +================================================================================ +`; + exports[`html-entities.hbs format 1`] = ` ====================================options===================================== parsers: ["glimmer"] diff --git a/tests/format/handlebars/escape/backslashes-in-attributes.hbs b/tests/format/handlebars/escape/backslashes-in-attributes.hbs new file mode 100644 index 000000000..860b8ee28 --- /dev/null +++ b/tests/format/handlebars/escape/backslashes-in-attributes.hbs @@ -0,0 +1 @@ + diff --git a/tests/format/handlebars/escape/backslashes.hbs b/tests/format/handlebars/escape/backslashes.hbs new file mode 100644 index 000000000..c776f6f04 --- /dev/null +++ b/tests/format/handlebars/escape/backslashes.hbs @@ -0,0 +1,4 @@ +\
+\\
+\\\
+\\\ \\{{non-escaped-moustache}}