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
This commit is contained in:
Matt authored and GitHub committed 2022-02-23 20:58:21 +09:00
1 parent 10a68937d2
commit c865b198fd
5 files changed
+73 -2

No files matched your search

+21
View File
@@ -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.
<!-- prettier-ignore -->
```hbs
{{! Input }}
<p>\</p>
<p>\\</p>
<p>\\\</p>
{{! Prettier stable }}
<p>\\</p>
<p>\\\</p>
<p>\\\\</p>
{{! Prettier main }}
<p>\</p>
<p>\\</p>
<p>\\\</p>
```
+15 -2
View File
@@ -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(
/\\$/,
"\\\\"
);
}
}
}
},
},
};
@@ -1,5 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`backslashes.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<p>\\</p>
<p>\\\\</p>
<p>\\\\\\</p>
<p>\\\\\\ \\\\{{non-escaped-moustache}}</p>
=====================================output=====================================
<p>\\</p>
<p>\\\\</p>
<p>\\\\\\</p>
<p>\\\\\\ \\\\{{non-escaped-moustache}}</p>
================================================================================
`;
exports[`backslashes-in-attributes.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<p data-attr="backslash \\ in an attribute"></p>
=====================================output=====================================
<p data-attr="backslash \\ in an attribute"></p>
================================================================================
`;
exports[`html-entities.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
@@ -0,0 +1 @@
<p data-attr="backslash \ in an attribute"></p>
@@ -0,0 +1,4 @@
<p>\</p>
<p>\\</p>
<p>\\\</p>
<p>\\\ \\{{non-escaped-moustache}}</p>