Skip to content

Commit 2daf6f0

Browse files
Merge pull request #2535 from johanrd/autofix-complex/attribute-order
Restore autofix: `template-attribute-order`
2 parents 143cb7f + 571d45b commit 2daf6f0

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ rules in templates can be disabled with eslint directives with mustache or html
405405
| [order-in-models](docs/rules/order-in-models.md) | enforce proper order of properties in models | | 🔧 | |
406406
| [order-in-routes](docs/rules/order-in-routes.md) | enforce proper order of properties in routes | | 🔧 | |
407407
| [template-attribute-indentation](docs/rules/template-attribute-indentation.md) | enforce proper indentation of attributes and arguments in multi-line templates | | | |
408-
| [template-attribute-order](docs/rules/template-attribute-order.md) | enforce consistent ordering of attributes in template elements | | | |
408+
| [template-attribute-order](docs/rules/template-attribute-order.md) | enforce consistent ordering of attributes in template elements | | 🔧 | |
409409
| [template-block-indentation](docs/rules/template-block-indentation.md) | enforce consistent indentation for block statements and their children | | | |
410410
| [template-eol-last](docs/rules/template-eol-last.md) | require or disallow newline at the end of template files | | 🔧 | |
411411
| [template-linebreak-style](docs/rules/template-linebreak-style.md) | enforce consistent linebreaks in templates | | 🔧 | |

docs/rules/template-attribute-order.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ember/template-attribute-order
22

3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
35
<!-- end auto-generated rule header -->
46

57
Enforces a consistent ordering of attributes in template elements. This helps improve readability and maintainability of templates.

lib/rules/template-attribute-order.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
strictGts: true,
1010
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-attribute-order.md',
1111
},
12-
fixable: null,
12+
fixable: 'code',
1313
schema: [
1414
{
1515
type: 'object',
@@ -30,6 +30,8 @@ module.exports = {
3030
},
3131

3232
create(context) {
33+
const sourceCode = context.sourceCode || context.getSourceCode();
34+
3335
const options = context.options[0] || {};
3436
const order = options.order || [
3537
'class',
@@ -92,6 +94,14 @@ module.exports = {
9294
position: 'before',
9395
expectedAttr: previous.name,
9496
},
97+
fix(fixer) {
98+
const currentText = sourceCode.getText(current);
99+
const previousText = sourceCode.getText(previous);
100+
return [
101+
fixer.replaceTextRange(previous.range, currentText),
102+
fixer.replaceTextRange(current.range, previousText),
103+
];
104+
},
95105
});
96106
break;
97107
}

tests/lib/rules/template-attribute-order.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,52 @@ ruleTester.run('template-attribute-order', rule, {
1212
'<template><button class="btn" role="button" aria-label="Submit"></button></template>',
1313
'<template><input type="text" name="username" value=""></template>',
1414
'<template><div data-test-id="foo"></div></template>',
15+
// Single attribute - no ordering needed
16+
'<template><div class="foo"></div></template>',
17+
// All same category
18+
'<template><div aria-label="x" aria-hidden="true"></div></template>',
19+
// Correct full order
20+
'<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>',
1521
],
1622

1723
invalid: [
1824
{
1925
code: '<template><div id="bar" class="foo"></div></template>',
20-
output: null,
26+
output: '<template><div class="foo" id="bar"></div></template>',
2127
errors: [{ messageId: 'wrongOrder' }],
2228
},
2329
{
2430
code: '<template><button aria-label="Submit" role="button"></button></template>',
25-
output: null,
31+
output: '<template><button role="button" aria-label="Submit"></button></template>',
2632
errors: [{ messageId: 'wrongOrder' }],
2733
},
2834
{
2935
code: '<template><input name="username" type="text"></template>',
30-
output: null,
36+
output: '<template><input type="text" name="username"></template>',
37+
errors: [{ messageId: 'wrongOrder' }],
38+
},
39+
// Multiple attributes out of order (3 attrs, fully reversed)
40+
{
41+
code: '<template><div name="x" id="y" class="z"></div></template>',
42+
output: '<template><div id="y" name="x" class="z"></div></template>',
43+
errors: [{ messageId: 'wrongOrder' }, { messageId: 'wrongOrder' }],
44+
},
45+
// Unknown attributes go last
46+
{
47+
code: '<template><div custom="x" class="y"></div></template>',
48+
output: '<template><div class="y" custom="x"></div></template>',
49+
errors: [{ messageId: 'wrongOrder' }],
50+
},
51+
// data-test- prefix before type
52+
{
53+
code: '<template><input type="text" data-test-input="true"></template>',
54+
output: '<template><input data-test-input="true" type="text"></template>',
55+
errors: [{ messageId: 'wrongOrder' }],
56+
},
57+
// Multiline attributes
58+
{
59+
code: '<template><div\n name="x"\n class="y"\n></div></template>',
60+
output: '<template><div\n class="y"\n name="x"\n></div></template>',
3161
errors: [{ messageId: 'wrongOrder' }],
3262
},
3363
],

0 commit comments

Comments
 (0)