From c865b198fd788c3ffb30e663efae2fd2dcb9671d Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 23 Feb 2022 06:58:21 -0500 Subject: [PATCH] Correctly identify which backslashes are removed by glimmer (#12302) * Correctly identify which backslashes are removed by glimmer * Add tests for #12298 * Don't use ?? syntax * Add a changelog entry for #12302 * Add a test for #11401 * Move backslash tests to the escape directory * Backslash test shouldn't have a self-closing p --- changelog_unreleased/handlebars/12302.md | 21 ++++++++++++ src/language-handlebars/parser-glimmer.js | 17 ++++++++-- .../escape/__snapshots__/jsfmt.spec.js.snap | 32 +++++++++++++++++++ .../escape/backslashes-in-attributes.hbs | 1 + .../format/handlebars/escape/backslashes.hbs | 4 +++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/handlebars/12302.md create mode 100644 tests/format/handlebars/escape/backslashes-in-attributes.hbs create mode 100644 tests/format/handlebars/escape/backslashes.hbs 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}}