From 613fa78aa148f471f19b7ea26321e6596dafd9fa Mon Sep 17 00:00:00 2001
From: jjohnnystwsss <33243683+jjohnnystwsss@users.noreply.github.com>
Date: Fri, 20 Nov 2026 04:04:55 +0800
Subject: [PATCH] Keep `...attributes` in place and keep whitespace in
`template-attribute-order` autofix
`...attributes` was sorted like an unknown attribute, so the autofix
moved attributes across it. That changes which attributes can be
overridden by the caller (ember-template-lint's attribute-order never
reorders across it either). It now stays in place, and attributes are
only ordered on each side of it.
The range of a valueless attribute (`disabled`, `data-test-foo`,
`...attributes`) includes the whitespace after it, so swapping it dropped
the space between attributes (`
`),
which does not parse after a mustache value. The swap now leaves that
whitespace in place.
Part of #2889 (item 3).
Co-Authored-By: Claude Opus 5.5
---
docs/rules/template-attribute-order.md | 2 ++
lib/rules/template-attribute-order.js | 28 ++++++++++++++++-----
tests/lib/rules/template-attribute-order.js | 26 +++++++++++++++++++
3 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/docs/rules/template-attribute-order.md b/docs/rules/template-attribute-order.md
index 54b3826e96..b7d838ca45 100644
--- a/docs/rules/template-attribute-order.md
+++ b/docs/rules/template-attribute-order.md
@@ -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:
diff --git a/lib/rules/template-attribute-order.js b/lib/rules/template-attribute-order.js
index 8b9d48b413..41ea047590 100644
--- a/lib/rules/template-attribute-order.js
+++ b/lib/rules/template-attribute-order.js
@@ -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);
@@ -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
+ ),
];
},
});
diff --git a/tests/lib/rules/template-attribute-order.js b/tests/lib/rules/template-attribute-order.js
index b6e585be2a..8b4e93dab7 100644
--- a/tests/lib/rules/template-attribute-order.js
+++ b/tests/lib/rules/template-attribute-order.js
@@ -18,6 +18,10 @@ ruleTester.run('template-attribute-order', rule, {
'',
// Correct full order
'',
+ // `...attributes` stays in place, and attributes are not moved across it
+ '