Skip to content

Commit

Permalink
Preserve attributes order for element node (#10958)
Browse files Browse the repository at this point in the history
* Preserve attributes order for element node

* Add CHANGELOG

* Add ...arguments in the test

* Apply fisker suggestions

* Simplify further
  • Loading branch information
dcyriller committed Jun 4, 2021
1 parent c3c300d commit 3cd28ca
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
44 changes: 44 additions & 0 deletions changelog_unreleased/handlebars/10958.md
@@ -0,0 +1,44 @@
#### Preserve attributes order for element node (#10958 by @dcyriller)

<!-- prettier-ignore -->
```handlebars
{{!-- Input --}}
<MyComponent
{{! this is a comment for arg 1}}
@arg1="hello"
{{on "clik" this.modify}}
@arg2="hello"
{{! this is a comment for arg 3}}
@arg3="hello"
@arg4="hello"
{{! this is a comment for arg 5}}
@arg5="hello"
...arguments
/>
{{!-- Prettier stable --}}
<MyComponent
@arg1="hello"
@arg2="hello"
@arg3="hello"
@arg4="hello"
@arg5="hello"
...arguments
{{on "clik" this.modify}}
{{! this is a comment for arg 1}}
{{! this is a comment for arg 3}}
{{! this is a comment for arg 5}}
/>
{{!-- Prettier main --}}
<MyComponent
{{! this is a comment for arg 1}}
@arg1="hello"
{{on "clik" this.modify}}
@arg2="hello"
{{! this is a comment for arg 3}}
@arg3="hello"
@arg4="hello"
{{! this is a comment for arg 5}}
@arg5="hello"
...arguments
/>
```
34 changes: 20 additions & 14 deletions src/language-handlebars/printer-glimmer.js
Expand Up @@ -418,24 +418,30 @@ function print(path, options, print) {

/* ElementNode print helpers */

function sortByLoc(a, b) {
return locStart(a) - locStart(b);
}

function printStartingTag(path, print) {
const node = path.getValue();

const attributesLike = ["attributes", "modifiers", "comments", "blockParams"]
.filter((property) => isNonEmptyArray(node[property]))
.map((property) => [
line,
property === "blockParams"
? printBlockParams(node)
: join(line, path.map(print, property)),
]);
const types = ["attributes", "modifiers", "comments"].filter((property) =>
isNonEmptyArray(node[property])
);
const attributes = types.flatMap((type) => node[type]).sort(sortByLoc);

return [
"<",
node.tag,
indent(attributesLike),
printStartingTagEndMarker(node),
];
for (const attributeType of types) {
path.each((attributePath) => {
const index = attributes.indexOf(attributePath.getValue());
attributes.splice(index, 1, [line, print()]);
}, attributeType);
}

if (isNonEmptyArray(node.blockParams)) {
attributes.push(line, printBlockParams(node));
}

return ["<", node.tag, indent(attributes), printStartingTagEndMarker(node)];
}

function printChildren(path, options, print) {
Expand Down
35 changes: 35 additions & 0 deletions tests/format/handlebars/attr-node/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -131,6 +131,41 @@ weirdly formatted
================================================================================
`;

exports[`order.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<MyComponent
{{! this is a comment for arg 1}}
@arg1="hello"
{{on "clik" this.modify}}
@arg2="hello"
{{! this is a comment for arg 3}}
@arg3="hello"
@arg4="hello"
{{! this is a comment for arg 5}}
@arg5="hello"
...arguments
/>
=====================================output=====================================
<MyComponent
{{! this is a comment for arg 1}}
@arg1="hello"
{{on "clik" this.modify}}
@arg2="hello"
{{! this is a comment for arg 3}}
@arg3="hello"
@arg4="hello"
{{! this is a comment for arg 5}}
@arg5="hello"
...arguments
/>
================================================================================
`;

exports[`quotes.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
Expand Down
12 changes: 12 additions & 0 deletions tests/format/handlebars/attr-node/order.hbs
@@ -0,0 +1,12 @@
<MyComponent
{{! this is a comment for arg 1}}
@arg1="hello"
{{on "clik" this.modify}}
@arg2="hello"
{{! this is a comment for arg 3}}
@arg3="hello"
@arg4="hello"
{{! this is a comment for arg 5}}
@arg5="hello"
...arguments
/>

0 comments on commit 3cd28ca

Please sign in to comment.