diff --git a/changelog_unreleased/handlebars/9082.md b/changelog_unreleased/handlebars/9082.md new file mode 100644 index 000000000..5c9d6070f --- /dev/null +++ b/changelog_unreleased/handlebars/9082.md @@ -0,0 +1,19 @@ +#### Preserve tilde comments (#9082 by @kangax, @fisker) + + +```hbs +{{!-- Input --}} +{{~! Comment }} +{{! Comment ~}} +{{~! Comment ~}} + +{{!-- Prettier stable --}} +{{! Comment }} +{{! Comment }} +{{! Comment }} + +{{!-- Prettier master --}} +{{~! Comment }} +{{! Comment ~}} +{{~! Comment ~}} +``` diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index ff63ca325..0981f2dd3 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -268,8 +268,26 @@ function print(path, options, print) { ]); } case "MustacheCommentStatement": { + const start = locStart(n); + const end = locEnd(n); + // Starts with `{{~` + const isLeftWhiteSpaceSensitive = + options.originalText.charAt(start + 2) === "~"; + // Ends with `{{~` + const isRightWhitespaceSensitive = + options.originalText.charAt(end - 3) === "~"; + const dashes = n.value.includes("}}") ? "--" : ""; - return concat(["{{!", dashes, n.value, dashes, "}}"]); + return concat([ + "{{", + isLeftWhiteSpaceSensitive ? "~" : "", + "!", + dashes, + n.value, + dashes, + isRightWhitespaceSensitive ? "~" : "", + "}}", + ]); } case "PathExpression": { return n.original; diff --git a/tests/handlebars/comment/__snapshots__/jsfmt.spec.js.snap b/tests/handlebars/comment/__snapshots__/jsfmt.spec.js.snap index 98dedda6a..83d437eee 100644 --- a/tests/handlebars/comment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/handlebars/comment/__snapshots__/jsfmt.spec.js.snap @@ -21,9 +21,22 @@ printWidth: 80
{{! This comment will not be in the output }} {{!-- This comment as }} and will not be in the output --}} + {{~! This comment will not be in the output }} + {{! This comment will not be in the output ~}} + {{~! This comment will not be in the output ~}} + {{~! +comment + ~}} + {{~! 1 ~}}{{~! 2 ~}}
+
+ {{~!~}} + + {{~!~}} +
+ =====================================output=====================================
{{! Foo }} @@ -40,7 +53,20 @@ printWidth: 80
{{! This comment will not be in the output }} {{!-- This comment as }} and will not be in the output --}} + {{~! This comment will not be in the output }} + {{! This comment will not be in the output ~}} + {{~! This comment will not be in the output ~}} + {{~! +comment + ~}} + {{~! 1 ~}}{{~! 2 ~}}
+ +
+ {{~!~}} + + {{~!~}} +
================================================================================ `; diff --git a/tests/handlebars/comment/comments.hbs b/tests/handlebars/comment/comments.hbs index 5df2a8a45..adf0c46b7 100644 --- a/tests/handlebars/comment/comments.hbs +++ b/tests/handlebars/comment/comments.hbs @@ -13,5 +13,18 @@
{{! This comment will not be in the output }} {{!-- This comment as }} and will not be in the output --}} + {{~! This comment will not be in the output }} + {{! This comment will not be in the output ~}} + {{~! This comment will not be in the output ~}} + {{~! +comment + ~}} + {{~! 1 ~}}{{~! 2 ~}}
+ +
+ {{~!~}} + + {{~!~}} +
diff --git a/tests/misc/errors/handlebars/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/handlebars/__snapshots__/jsfmt.spec.js.snap index ab5091dd0..c06026421 100644 --- a/tests/misc/errors/handlebars/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/handlebars/__snapshots__/jsfmt.spec.js.snap @@ -37,3 +37,23 @@ Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got | ^ 4 | " `; + +exports[`tilde-comments-1.hbs [glimmer] format 1`] = ` +"Parse error on line 1: +{{~}} +---^ +Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'CLOSE' (1:4) +> 1 | {{~}} + | ^ + 2 | " +`; + +exports[`tilde-comments-2.hbs [glimmer] format 1`] = ` +"Parse error on line 1: +{{~~}} +---^ +Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'CLOSE' (1:4) +> 1 | {{~~}} + | ^ + 2 | " +`; diff --git a/tests/misc/errors/handlebars/tilde-comments-1.hbs b/tests/misc/errors/handlebars/tilde-comments-1.hbs new file mode 100644 index 000000000..562fbb6b0 --- /dev/null +++ b/tests/misc/errors/handlebars/tilde-comments-1.hbs @@ -0,0 +1 @@ +{{~}} diff --git a/tests/misc/errors/handlebars/tilde-comments-2.hbs b/tests/misc/errors/handlebars/tilde-comments-2.hbs new file mode 100644 index 000000000..d3966458d --- /dev/null +++ b/tests/misc/errors/handlebars/tilde-comments-2.hbs @@ -0,0 +1 @@ +{{~~}}