Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/template-attribute-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This rule enforces a consistent order for attributes on template elements. By de
9. `placeholder`
10. `disabled`

`...attributes` is left in place, and attributes are only ordered on each side of it, because attributes before `...attributes` can be overridden by it and attributes after it can't.

## Examples

Examples of **incorrect** code for this rule:
Expand Down
28 changes: 22 additions & 6 deletions lib/rules/template-attribute-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ module.exports = {
(attr) => attr.type === 'GlimmerAttrNode' && attr.name
);

for (let i = 1; i < attributes.length; i++) {
// Attributes before `...attributes` can be overridden by it and attributes after it can't,
// so leave `...attributes` in place and only order the attributes on each side of it.
let groupStart = 0;

for (let i = 0; i < attributes.length; i++) {
const current = attributes[i];
if (current.name === '...attributes') {
groupStart = i + 1;
continue;
}
const currentIndex = getExpectedIndex(current.name);

for (let j = 0; j < i; j++) {
for (let j = groupStart; j < i; j++) {
const previous = attributes[j];
const previousIndex = getExpectedIndex(previous.name);

Expand All @@ -100,11 +108,19 @@ module.exports = {
expectedAttr: previous.name,
},
fix(fixer) {
const currentText = sourceCode.getText(current);
const previousText = sourceCode.getText(previous);
// The range of a valueless attribute (e.g. `disabled`) includes the whitespace
// after it, so swap the attributes without it.
const currentText = sourceCode.getText(current).trimEnd();
const previousText = sourceCode.getText(previous).trimEnd();
return [
fixer.replaceTextRange(previous.range, currentText),
fixer.replaceTextRange(current.range, previousText),
fixer.replaceTextRange(
[previous.range[0], previous.range[0] + previousText.length],
currentText
),
fixer.replaceTextRange(
[current.range[0], current.range[0] + currentText.length],
previousText
),
];
},
});
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/template-attribute-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ ruleTester.run('template-attribute-order', rule, {
'<template><div aria-label="x" aria-hidden="true"></div></template>',
// Correct full order
'<template><input class="x" id="y" role="r" aria-label="l" data-test-foo="1" type="text" name="n" value="v" placeholder="p" disabled></template>',
// `...attributes` stays in place, and attributes are not moved across it
'<template><div ...attributes class={{this.cls}}>x</div></template>',
'<template><input ...attributes type="text" /></template>',
'<template><div id="b" ...attributes class="a"></div></template>',
],

invalid: [
Expand Down Expand Up @@ -60,5 +64,27 @@ ruleTester.run('template-attribute-order', rule, {
output: '<template><div\n class="y"\n name="x"\n></div></template>',
errors: [{ messageId: 'wrongOrder' }],
},
// Attributes on each side of `...attributes` are still ordered
{
code: '<template><div ...attributes id="b" class="a"></div></template>',
output: '<template><div ...attributes class="a" id="b"></div></template>',
errors: [{ messageId: 'wrongOrder' }],
},
{
code: '<template><div id="b" class="a" ...attributes></div></template>',
output: '<template><div class="a" id="b" ...attributes></div></template>',
errors: [{ messageId: 'wrongOrder' }],
},
// Valueless attributes keep the whitespace after them
{
code: '<template><input disabled class="a"></template>',
output: '<template><input class="a" disabled></template>',
errors: [{ messageId: 'wrongOrder' }],
},
{
code: '<template><div data-test-foo class={{this.cls}}></div></template>',
output: '<template><div class={{this.cls}} data-test-foo></div></template>',
errors: [{ messageId: 'wrongOrder' }],
},
],
});
Loading