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 + '', + '', + '', ], invalid: [ @@ -60,5 +64,27 @@ ruleTester.run('template-attribute-order', rule, { output: '', errors: [{ messageId: 'wrongOrder' }], }, + // Attributes on each side of `...attributes` are still ordered + { + code: '', + output: '', + errors: [{ messageId: 'wrongOrder' }], + }, + { + code: '', + output: '', + errors: [{ messageId: 'wrongOrder' }], + }, + // Valueless attributes keep the whitespace after them + { + code: '', + output: '', + errors: [{ messageId: 'wrongOrder' }], + }, + { + code: '', + output: '', + errors: [{ messageId: 'wrongOrder' }], + }, ], });