diff --git a/changelog_unreleased/handlebars/11524.md b/changelog_unreleased/handlebars/11524.md new file mode 100644 index 000000000..09d8fd6c4 --- /dev/null +++ b/changelog_unreleased/handlebars/11524.md @@ -0,0 +1,14 @@ +#### Uses the opposite quote type for quotes inside mustache statements in attributes (#11524 by @bmaehr) + + +```hbs +{{!-- Input --}} +
+ +{{!-- Prettier main --}} +
+ +{{!-- Prettier stable --}} +
+ +``` diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 7e980950e..6d9a50b30 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -399,6 +399,10 @@ function print(path, options, print) { return [""]; } case "StringLiteral": { + if (needsOppositeQuote(path)) { + const printFavoriteQuote = !options.singleQuote ? "'" : '"'; + return printStringLiteral(node.value, printFavoriteQuote); + } return printStringLiteral(node.value, favoriteQuote); } case "NumberLiteral": { @@ -709,6 +713,23 @@ function printStringLiteral(stringLiteral, favoriteQuote) { return [quote, stringLiteral.replace(regex, `\\${quote}`), quote]; } +function needsOppositeQuote(path) { + let index = 0; + let parentNode = path.getParentNode(index); + while (parentNode && isNodeOfSomeType(parentNode, ["SubExpression"])) { + index++; + parentNode = path.getParentNode(index); + } + if ( + parentNode && + isNodeOfSomeType(path.getParentNode(index + 1), ["ConcatStatement"]) && + isNodeOfSomeType(path.getParentNode(index + 2), ["AttrNode"]) + ) { + return true; + } + return false; +} + /* SubExpression print helpers */ function printSubExpressionPathAndParams(path, print) { diff --git a/tests/format/handlebars/concat-statement/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/concat-statement/__snapshots__/jsfmt.spec.js.snap index 624b8ad34..afbe4aa50 100644 --- a/tests/format/handlebars/concat-statement/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/concat-statement/__snapshots__/jsfmt.spec.js.snap @@ -141,7 +141,7 @@ singleQuote: true >Link @@ -173,7 +173,7 @@ printWidth: 80 >Link diff --git a/tests/format/handlebars/mustache-statement/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/mustache-statement/__snapshots__/jsfmt.spec.js.snap index 6f33746f3..f533fdcc7 100644 --- a/tests/format/handlebars/mustache-statement/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/mustache-statement/__snapshots__/jsfmt.spec.js.snap @@ -110,11 +110,11 @@ singleQuote: true
@@ -122,11 +122,11 @@ singleQuote: true
@@ -271,11 +271,11 @@ printWidth: 80
@@ -283,11 +283,11 @@ printWidth: 80
@@ -562,8 +562,8 @@ foo{{/if}}
  • @@ -650,8 +650,8 @@ foo{{/if}}
  • diff --git a/tests/format/handlebars/single-quote-option/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/single-quote-option/__snapshots__/jsfmt.spec.js.snap index 891b513bc..4229856d7 100644 --- a/tests/format/handlebars/single-quote-option/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/single-quote-option/__snapshots__/jsfmt.spec.js.snap @@ -80,6 +80,9 @@ singleQuote: false
    +
    +
    + @@ -88,31 +91,42 @@ singleQuote: false +
    + =====================================output=====================================
    +
    +
    + + +
    ================================================================================ `; @@ -128,6 +142,9 @@ singleQuote: true
    +
    +
    + @@ -136,31 +153,42 @@ singleQuote: true +
    + =====================================output=====================================
    +
    +
    + + +
    ================================================================================ `; diff --git a/tests/format/handlebars/single-quote-option/attributes.hbs b/tests/format/handlebars/single-quote-option/attributes.hbs index 71dc9cb91..65729cb91 100644 --- a/tests/format/handlebars/single-quote-option/attributes.hbs +++ b/tests/format/handlebars/single-quote-option/attributes.hbs @@ -3,6 +3,9 @@
    +
    +
    + @@ -10,3 +13,5 @@ + +