|
| 1 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 2 | +module.exports = { |
| 3 | + meta: { |
| 4 | + type: 'suggestion', |
| 5 | + docs: { |
| 6 | + description: 'enforce consistent ordering of attributes in template elements', |
| 7 | + category: 'Stylistic Issues', |
| 8 | + strictGjs: true, |
| 9 | + strictGts: true, |
| 10 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-attribute-order.md', |
| 11 | + }, |
| 12 | + fixable: null, |
| 13 | + schema: [ |
| 14 | + { |
| 15 | + type: 'object', |
| 16 | + properties: { |
| 17 | + order: { |
| 18 | + type: 'array', |
| 19 | + items: { |
| 20 | + type: 'string', |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + additionalProperties: false, |
| 25 | + }, |
| 26 | + ], |
| 27 | + messages: { |
| 28 | + wrongOrder: 'Attribute "{{currentAttr}}" should come {{position}} "{{expectedAttr}}".', |
| 29 | + }, |
| 30 | + }, |
| 31 | + |
| 32 | + create(context) { |
| 33 | + const options = context.options[0] || {}; |
| 34 | + const order = options.order || [ |
| 35 | + 'class', |
| 36 | + 'id', |
| 37 | + 'role', |
| 38 | + 'aria-', |
| 39 | + 'data-test-', |
| 40 | + 'type', |
| 41 | + 'name', |
| 42 | + 'value', |
| 43 | + 'placeholder', |
| 44 | + 'disabled', |
| 45 | + ]; |
| 46 | + |
| 47 | + function getAttributeCategory(attrName) { |
| 48 | + for (const category of order) { |
| 49 | + if (category.endsWith('-')) { |
| 50 | + if (attrName.startsWith(category)) { |
| 51 | + return category; |
| 52 | + } |
| 53 | + } else if (attrName === category) { |
| 54 | + return category; |
| 55 | + } |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + function getExpectedIndex(attrName) { |
| 61 | + const category = getAttributeCategory(attrName); |
| 62 | + if (category === null) { |
| 63 | + return order.length; // Unknown attributes go last |
| 64 | + } |
| 65 | + return order.indexOf(category); |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + GlimmerElementNode(node) { |
| 70 | + if (!node.attributes || node.attributes.length < 2) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const attributes = node.attributes.filter( |
| 75 | + (attr) => attr.type === 'GlimmerAttrNode' && attr.name |
| 76 | + ); |
| 77 | + |
| 78 | + for (let i = 1; i < attributes.length; i++) { |
| 79 | + const current = attributes[i]; |
| 80 | + const currentIndex = getExpectedIndex(current.name); |
| 81 | + |
| 82 | + for (let j = 0; j < i; j++) { |
| 83 | + const previous = attributes[j]; |
| 84 | + const previousIndex = getExpectedIndex(previous.name); |
| 85 | + |
| 86 | + if (currentIndex < previousIndex) { |
| 87 | + context.report({ |
| 88 | + node: current, |
| 89 | + messageId: 'wrongOrder', |
| 90 | + data: { |
| 91 | + currentAttr: current.name, |
| 92 | + position: 'before', |
| 93 | + expectedAttr: previous.name, |
| 94 | + }, |
| 95 | + }); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + }; |
| 102 | + }, |
| 103 | +}; |
0 commit comments