fix(handlebars): Use quotes from config in AttrNode (#6377)

master
Cyrille David 2019-09-30 16:08:58 +02:00 committed by Evilebot Tnawi
parent 2bc15e2bed
commit 7cb0c23774
9 changed files with 81 additions and 16 deletions

View File

@ -679,6 +679,22 @@ const fooooooooooooooo: SomeThing<
const fooooooooooooooo: SomeThing<boolean> = looooooooooooooooooooooooooooooongNameFunc(); const fooooooooooooooo: SomeThing<boolean> = looooooooooooooooooooooooooooooongNameFunc();
``` ```
#### Handlebars: Fix --single-quote option on html attributes ([#6377] by [@dcyriller])
Previously, the flag was not applied on html attributes.
<!-- prettier-ignore-->
```hbs
// Input
<div class="a-class-name"></div>
// Prettier (stable with the option --single-quote)
<div class="a-class-name"></div>
// Prettier (master with the option --single-quote)
<div class='a-class-name'></div>
````
[#5910]: https://github.com/prettier/prettier/pull/5910 [#5910]: https://github.com/prettier/prettier/pull/5910
[#6186]: https://github.com/prettier/prettier/pull/6186 [#6186]: https://github.com/prettier/prettier/pull/6186
[#6206]: https://github.com/prettier/prettier/pull/6206 [#6206]: https://github.com/prettier/prettier/pull/6206
@ -703,6 +719,7 @@ const fooooooooooooooo: SomeThing<boolean> = looooooooooooooooooooooooooooooongN
[#6506]: https://github.com/prettier/prettier/pull/6506 [#6506]: https://github.com/prettier/prettier/pull/6506
[#6514]: https://github.com/prettier/prettier/pull/6514 [#6514]: https://github.com/prettier/prettier/pull/6514
[#6467]: https://github.com/prettier/prettier/pull/6467 [#6467]: https://github.com/prettier/prettier/pull/6467
[#6377]: https://github.com/prettier/prettier/pull/6377
[@duailibe]: https://github.com/duailibe [@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce [@gavinjoyce]: https://github.com/gavinjoyce
[@sosukesuzuki]: https://github.com/sosukesuzuki [@sosukesuzuki]: https://github.com/sosukesuzuki
@ -710,3 +727,4 @@ const fooooooooooooooo: SomeThing<boolean> = looooooooooooooooooooooooooooooongN
[@jounqin]: https://github.com/JounQin [@jounqin]: https://github.com/JounQin
[@bakkot]: https://gibhub.com/bakkot [@bakkot]: https://gibhub.com/bakkot
[@thorn0]: https://github.com/thorn0 [@thorn0]: https://github.com/thorn0
[@dcyriller]: https://github.com/dcyriller

View File

@ -195,8 +195,9 @@ function print(path, options, print) {
if (isText && n.value.loc.start.column === n.value.loc.end.column) { if (isText && n.value.loc.start.column === n.value.loc.end.column) {
return concat([n.name]); return concat([n.name]);
} }
const quote = isText ? '"' : ""; const value = path.call(print, "value");
return concat([n.name, "=", quote, path.call(print, "value"), quote]); const quotedValue = isText ? printStringLiteral(value, options) : value;
return concat([n.name, "=", quotedValue]);
} }
case "ConcatStatement": { case "ConcatStatement": {
return concat([ return concat([

View File

@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`brackets.hbs 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<div title='My title'></div>
<div title="My title"></div>
<div title='My "title"'></div>
<div title="My other 'title'"></div>
=====================================output=====================================
<div title="My title"></div>
<div title="My title"></div>
<div title='My "title"'></div>
<div title="My other 'title'"></div>
================================================================================
`;
exports[`brackets.hbs 2`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
<div title='My title'></div>
<div title="My title"></div>
<div title='My "title"'></div>
<div title="My other 'title'"></div>
=====================================output=====================================
<div title='My title'></div>
<div title='My title'></div>
<div title='My "title"'></div>
<div title="My other 'title'"></div>
================================================================================
`;

View File

@ -0,0 +1,4 @@
<div title='My title'></div>
<div title="My title"></div>
<div title='My "title"'></div>
<div title="My other 'title'"></div>

View File

@ -0,0 +1,2 @@
run_spec(__dirname, ["glimmer"]);
run_spec(__dirname, ["glimmer"], { singleQuote: true });

View File

@ -51,12 +51,12 @@ singleQuote: true
<div class="{{hello}} {{world}}"></div> <div class="{{hello}} {{world}}"></div>
=====================================output===================================== =====================================output=====================================
<script id="entry-template" type="text/x-handlebars-template"> <script id='entry-template' type='text/x-handlebars-template'>
<div class="entry"> <div class='entry'>
<h1> <h1>
{{title}} {{title}}
</h1> </h1>
<div class="body"> <div class='body'>
{{body}} {{body}}
</div> </div>
</div> </div>
@ -161,7 +161,7 @@ singleQuote: true
<div ...attributes>Hello</div> <div ...attributes>Hello</div>
=====================================output===================================== =====================================output=====================================
<UserGreeting @name="Ricardo" @greeting="Olá" /> <UserGreeting @name='Ricardo' @greeting='Olá' />
{{@greeting}} {{@greeting}}
, ,
{{@name}} {{@name}}
@ -172,18 +172,18 @@ singleQuote: true
/> />
</div> </div>
<Form as |f|> <Form as |f|>
<f.input @title="hello" /> <f.input @title='hello' />
<f.input> <f.input>
hello hello
</f.input> </f.input>
</Form> </Form>
<this.label @title="hello" /> <this.label @title='hello' />
<button onclick={{action next}}> <button onclick={{action next}}>
Next Next
</button> </button>
<button disabled class="disabled"></button> <button disabled class='disabled'></button>
<button disabled="disabled" class="disabled"></button> <button disabled='disabled' class='disabled'></button>
<img alt="" /> <img alt='' />
<div ...attributes> <div ...attributes>
Hello Hello
</div> </div>
@ -288,14 +288,14 @@ singleQuote: true
</div> </div>
=====================================output===================================== =====================================output=====================================
<div class="entry"> <div class='entry'>
<h1> <h1>
{{title}} {{title}}
</h1> </h1>
<h2> <h2>
By {{author.name}} By {{author.name}}
</h2> </h2>
<div class="body"> <div class='body'>
{{body}} {{body}}
</div> </div>
</div> </div>

View File

@ -337,7 +337,7 @@ singleQuote: true
</div> </div>
=====================================output===================================== =====================================output=====================================
<div id="comments"> <div id='comments'>
{{#each comments}} {{#each comments}}
<h2> <h2>
<a href="/posts/{{permalink}}#{{id}}"> <a href="/posts/{{permalink}}#{{id}}">

View File

@ -113,6 +113,6 @@ singleQuote: true
<div class="class-i {{myClass}}class-j"></div> <div class="class-i {{myClass}}class-j"></div>
<div class="class-k {{myClass}} class-l"></div> <div class="class-k {{myClass}} class-l"></div>
<div class="class-m {{myClass}} class-n {{myClass}}class-o"></div> <div class="class-m {{myClass}} class-n {{myClass}}class-o"></div>
<div class="class-p class-q"></div> <div class='class-p class-q'></div>
================================================================================ ================================================================================
`; `;

View File

@ -120,7 +120,7 @@ singleQuote: true
<img /> <img />
=====================================output===================================== =====================================output=====================================
<div class="attribute" {{modifier}} {{! comment}}> <div class='attribute' {{modifier}} {{! comment}}>
Hello Hello
</div> </div>
<div> <div>