Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve attributes order for element node #10958

Merged
merged 5 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
/>
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this example a little shorter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Sorry, missed this comment, but IMHO it's good as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is odd, I thought I applied the change. If you think it's worth correcting, I'll open a PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be a minor release without a blog post, so it's okay, don't bother

{{!-- 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
/>