Fix mustache quotes inside attribute (#10419) (#11524)

* Fix mustache quotes inside attribute (#10419)

* Add changelog (#10419)

* Update changelog (#10419)

* Updated tests

* Updated tests

* Opimize performance

* Don't format changlog

* Remove additional example
This commit is contained in:
bmaehr authored and GitHub committed 2021-09-28 06:02:59 +09:00
1 parent b86d36a28b
commit 5d774dc667
6 files changed
+102 -34

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
#### Uses the opposite quote type for quotes inside mustache statements in attributes (#11524 by @bmaehr)
<!-- prettier-ignore -->
```hbs
{{!-- Input --}}
<div title="{{t 'login.username.description'}}" />
{{!-- Prettier main --}}
<div title="{{t 'login.username.description'}}" />
{{!-- Prettier stable --}}
<div title="{{t "login.username.description"}}" />
```
@@ -399,6 +399,10 @@ function print(path, options, print) {
return ["<!--", node.value, "-->"]; return ["<!--", node.value, "-->"];
} }
case "StringLiteral": { case "StringLiteral": {
if (needsOppositeQuote(path)) {
const printFavoriteQuote = !options.singleQuote ? "'" : '"';
return printStringLiteral(node.value, printFavoriteQuote);
}
return printStringLiteral(node.value, favoriteQuote); return printStringLiteral(node.value, favoriteQuote);
} }
case "NumberLiteral": { case "NumberLiteral": {
@@ -709,6 +713,23 @@ function printStringLiteral(stringLiteral, favoriteQuote) {
return [quote, stringLiteral.replace(regex, `\\${quote}`), quote]; 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 */ /* SubExpression print helpers */
function printSubExpressionPathAndParams(path, print) { function printSubExpressionPathAndParams(path, print) {
@@ -141,7 +141,7 @@ singleQuote: true
>Link</a> >Link</a>
<button <button
class='uk-padding-remove uk-button uk-button-default uk-button-small uk-width-1-{{intl.locales.length}} class='uk-padding-remove uk-button uk-button-default uk-button-small uk-width-1-{{intl.locales.length}}
{{if (contains locale intl.locale) 'uk-button-primary'}}' {{if (contains locale intl.locale) "uk-button-primary"}}'
> >
test test
</button> </button>
@@ -173,7 +173,7 @@ printWidth: 80
>Link</a> >Link</a>
<button <button
class="uk-padding-remove uk-button uk-button-default uk-button-small uk-width-1-{{intl.locales.length}} class="uk-padding-remove uk-button uk-button-default uk-button-small uk-width-1-{{intl.locales.length}}
{{if (contains locale intl.locale) "uk-button-primary"}}" {{if (contains locale intl.locale) 'uk-button-primary'}}"
> >
test test
</button> </button>
@@ -110,11 +110,11 @@ singleQuote: true
<div <div
class='a-first-class class='a-first-class
{{if this.optionOne 'optionOne'}} {{if this.optionOne "optionOne"}}
{{if this.optionTwo 'optionTwo'}} {{if this.optionTwo "optionTwo"}}
{{if this.optionThree 'optionThree'}} {{if this.optionThree "optionThree"}}
{{if this.optionFour 'optionFour'}} {{if this.optionFour "optionFour"}}
{{if this.optionFive 'optionFive'}} {{if this.optionFive "optionFive"}}
{{this.class}}' {{this.class}}'
...attributes ...attributes
> >
@@ -122,11 +122,11 @@ singleQuote: true
<div <div
class='a-first-class class='a-first-class
{{if this.optionOne 'optionOne'}} {{if this.optionOne "optionOne"}}
{{if this.optionTwo 'optionTwo'}} {{if this.optionTwo "optionTwo"}}
{{if this.optionThree 'optionThree'}} {{if this.optionThree "optionThree"}}
{{if this.optionFour 'optionFour'}} {{if this.optionFour "optionFour"}}
{{if this.optionFive 'optionFive'}} {{if this.optionFive "optionFive"}}
{{this.class}}' {{this.class}}'
...attributes ...attributes
> >
@@ -271,11 +271,11 @@ printWidth: 80
<div <div
class="a-first-class class="a-first-class
{{if this.optionOne "optionOne"}} {{if this.optionOne 'optionOne'}}
{{if this.optionTwo "optionTwo"}} {{if this.optionTwo 'optionTwo'}}
{{if this.optionThree "optionThree"}} {{if this.optionThree 'optionThree'}}
{{if this.optionFour "optionFour"}} {{if this.optionFour 'optionFour'}}
{{if this.optionFive "optionFive"}} {{if this.optionFive 'optionFive'}}
{{this.class}}" {{this.class}}"
...attributes ...attributes
> >
@@ -283,11 +283,11 @@ printWidth: 80
<div <div
class="a-first-class class="a-first-class
{{if this.optionOne "optionOne"}} {{if this.optionOne 'optionOne'}}
{{if this.optionTwo "optionTwo"}} {{if this.optionTwo 'optionTwo'}}
{{if this.optionThree "optionThree"}} {{if this.optionThree 'optionThree'}}
{{if this.optionFour "optionFour"}} {{if this.optionFour 'optionFour'}}
{{if this.optionFive "optionFive"}} {{if this.optionFive 'optionFive'}}
{{this.class}}" {{this.class}}"
...attributes ...attributes
> >
@@ -562,8 +562,8 @@ foo{{/if}}
<li <li
class='{{if class='{{if
showAsCarousel showAsCarousel
'career-interests__pill--slide-item' "career-interests__pill--slide-item"
'career-interests__pill' "career-interests__pill"
}} }}
' '
></li> ></li>
@@ -650,8 +650,8 @@ foo{{/if}}
<li <li
class="{{if class="{{if
showAsCarousel showAsCarousel
"career-interests__pill--slide-item" 'career-interests__pill--slide-item'
"career-interests__pill" 'career-interests__pill'
}} }}
" "
></li> ></li>
@@ -80,6 +80,9 @@ singleQuote: false
<div title='My "title"'></div> <div title='My "title"'></div>
<div title="My other 'title'"></div> <div title="My other 'title'"></div>
<div title="{{t 'my.title'}}" />
<div title='{{t "my.title"}}' />
<a href="/{{url}}/{{url}}"></a> <a href="/{{url}}/{{url}}"></a>
<a href='/{{url}}/{{url}}'></a> <a href='/{{url}}/{{url}}'></a>
@@ -88,31 +91,42 @@ singleQuote: false
<button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button>
<button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button>
<div class="padding {{if foo (if fooAgain "bar" (if fooAgainAgain "bar" "foo"))}} baz" />
=====================================output===================================== =====================================output=====================================
<div title="My title"></div> <div title="My title"></div>
<div title="My title"></div> <div title="My title"></div>
<div title='My "title"'></div> <div title='My "title"'></div>
<div title="My other 'title'"></div> <div title="My other 'title'"></div>
<div title="{{t 'my.title'}}"></div>
<div title="{{t 'my.title'}}"></div>
<a href="/{{url}}/{{url}}"></a> <a href="/{{url}}/{{url}}"></a>
<a href="/{{url}}/{{url}}"></a> <a href="/{{url}}/{{url}}"></a>
<button <button
class="padding width-{{locales.length}} class="padding width-{{locales.length}}
{{if (has locale) "uk-button-primary"}}" {{if (has locale) 'uk-button-primary'}}"
></button> ></button>
<button <button
class="padding width-{{locales.length}} class="padding width-{{locales.length}}
{{if (has locale) "uk-button-primary"}}" {{if (has locale) 'uk-button-primary'}}"
></button> ></button>
<button <button
class="padding width-{{locales.length}} class="padding width-{{locales.length}}
{{if (has locale) "uk-button-primary"}}" {{if (has locale) 'uk-button-primary'}}"
></button> ></button>
<button <button
class="padding width-{{locales.length}} class="padding width-{{locales.length}}
{{if (has locale) "uk-button-primary"}}" {{if (has locale) 'uk-button-primary'}}"
></button> ></button>
<div
class="padding
{{if foo (if fooAgain 'bar' (if fooAgainAgain 'bar' 'foo'))}}
baz"
></div>
================================================================================ ================================================================================
`; `;
@@ -128,6 +142,9 @@ singleQuote: true
<div title='My "title"'></div> <div title='My "title"'></div>
<div title="My other 'title'"></div> <div title="My other 'title'"></div>
<div title="{{t 'my.title'}}" />
<div title='{{t "my.title"}}' />
<a href="/{{url}}/{{url}}"></a> <a href="/{{url}}/{{url}}"></a>
<a href='/{{url}}/{{url}}'></a> <a href='/{{url}}/{{url}}'></a>
@@ -136,31 +153,42 @@ singleQuote: true
<button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button>
<button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button>
<div class="padding {{if foo (if fooAgain "bar" (if fooAgainAgain "bar" "foo"))}} baz" />
=====================================output===================================== =====================================output=====================================
<div title='My title'></div> <div title='My title'></div>
<div title='My title'></div> <div title='My title'></div>
<div title='My "title"'></div> <div title='My "title"'></div>
<div title="My other 'title'"></div> <div title="My other 'title'"></div>
<div title='{{t "my.title"}}'></div>
<div title='{{t "my.title"}}'></div>
<a href='/{{url}}/{{url}}'></a> <a href='/{{url}}/{{url}}'></a>
<a href='/{{url}}/{{url}}'></a> <a href='/{{url}}/{{url}}'></a>
<button <button
class='padding width-{{locales.length}} class='padding width-{{locales.length}}
{{if (has locale) 'uk-button-primary'}}' {{if (has locale) "uk-button-primary"}}'
></button> ></button>
<button <button
class='padding width-{{locales.length}} class='padding width-{{locales.length}}
{{if (has locale) 'uk-button-primary'}}' {{if (has locale) "uk-button-primary"}}'
></button> ></button>
<button <button
class='padding width-{{locales.length}} class='padding width-{{locales.length}}
{{if (has locale) 'uk-button-primary'}}' {{if (has locale) "uk-button-primary"}}'
></button> ></button>
<button <button
class='padding width-{{locales.length}} class='padding width-{{locales.length}}
{{if (has locale) 'uk-button-primary'}}' {{if (has locale) "uk-button-primary"}}'
></button> ></button>
<div
class='padding
{{if foo (if fooAgain "bar" (if fooAgainAgain "bar" "foo"))}}
baz'
></div>
================================================================================ ================================================================================
`; `;
@@ -3,6 +3,9 @@
<div title='My "title"'></div> <div title='My "title"'></div>
<div title="My other 'title'"></div> <div title="My other 'title'"></div>
<div title="{{t 'my.title'}}" />
<div title='{{t "my.title"}}' />
<a href="/{{url}}/{{url}}"></a> <a href="/{{url}}/{{url}}"></a>
<a href='/{{url}}/{{url}}'></a> <a href='/{{url}}/{{url}}'></a>
@@ -10,3 +13,5 @@
<button class="padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}"></button> <button class="padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}"></button>
<button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) "uk-button-primary"}}'></button>
<button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button> <button class='padding width-{{locales.length}} {{if (has locale) 'uk-button-primary'}}'></button>
<div class="padding {{if foo (if fooAgain "bar" (if fooAgainAgain "bar" "foo"))}} baz" />