|
| 1 | +/* eslint-disable unicorn/consistent-function-scoping, unicorn/prefer-at */ |
| 2 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 3 | +module.exports = { |
| 4 | + meta: { |
| 5 | + type: 'suggestion', |
| 6 | + docs: { |
| 7 | + description: 'require sorted attributes and modifiers', |
| 8 | + category: 'Best Practices', |
| 9 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-sort-invocations.md', |
| 10 | + templateMode: 'both', |
| 11 | + }, |
| 12 | + fixable: null, |
| 13 | + schema: [], |
| 14 | + messages: { |
| 15 | + attributeOrder: '`{{attributeName}}` must appear after `{{expectedAfter}}`', |
| 16 | + modifierOrder: '`{{{{modifierName}}}}` must appear after `{{{{expectedAfter}}}}`', |
| 17 | + hashPairOrder: '`{{hashPairName}}` must appear after `{{expectedAfter}}`', |
| 18 | + splattributesOrder: '`...attributes` must appear after modifiers', |
| 19 | + }, |
| 20 | + originallyFrom: { |
| 21 | + name: 'ember-template-lint', |
| 22 | + rule: 'lib/rules/sort-invocations.js', |
| 23 | + docs: 'docs/rule/sort-invocations.md', |
| 24 | + tests: 'test/unit/rules/sort-invocations-test.js', |
| 25 | + }, |
| 26 | + }, |
| 27 | + |
| 28 | + create(context) { |
| 29 | + function getAttributeName(node) { |
| 30 | + return node.name; |
| 31 | + } |
| 32 | + |
| 33 | + function getAttributePosition(node) { |
| 34 | + const name = getAttributeName(node); |
| 35 | + |
| 36 | + if (name.startsWith('@')) { |
| 37 | + return 1; // Arguments first |
| 38 | + } |
| 39 | + |
| 40 | + if (name === '...attributes') { |
| 41 | + return 3; // Splattributes last |
| 42 | + } |
| 43 | + |
| 44 | + return 2; // Regular attributes in the middle |
| 45 | + } |
| 46 | + |
| 47 | + function getHashPairName(node) { |
| 48 | + return node.key; |
| 49 | + } |
| 50 | + |
| 51 | + function getModifierName(node) { |
| 52 | + if (node.path.type !== 'GlimmerPathExpression') { |
| 53 | + return ''; |
| 54 | + } |
| 55 | + |
| 56 | + return node.path.original; |
| 57 | + } |
| 58 | + |
| 59 | + function compareAttributes(a, b) { |
| 60 | + const positionA = getAttributePosition(a); |
| 61 | + const positionB = getAttributePosition(b); |
| 62 | + |
| 63 | + if (positionA !== positionB) { |
| 64 | + return positionA - positionB; |
| 65 | + } |
| 66 | + |
| 67 | + const nameA = getAttributeName(a); |
| 68 | + const nameB = getAttributeName(b); |
| 69 | + |
| 70 | + return nameA.localeCompare(nameB); |
| 71 | + } |
| 72 | + |
| 73 | + function compareHashPairs(a, b) { |
| 74 | + const nameA = getHashPairName(a); |
| 75 | + const nameB = getHashPairName(b); |
| 76 | + |
| 77 | + return nameA.localeCompare(nameB); |
| 78 | + } |
| 79 | + |
| 80 | + function compareModifiers(a, b) { |
| 81 | + const nameA = getModifierName(a); |
| 82 | + const nameB = getModifierName(b); |
| 83 | + |
| 84 | + if (nameA !== nameB) { |
| 85 | + return nameA.localeCompare(nameB); |
| 86 | + } |
| 87 | + |
| 88 | + // For 'on' modifiers, sort by event name |
| 89 | + if (nameA === 'on' && a.params && b.params && a.params.length > 0 && b.params.length > 0) { |
| 90 | + const eventA = a.params[0]; |
| 91 | + const eventB = b.params[0]; |
| 92 | + |
| 93 | + if (eventA.type === 'GlimmerStringLiteral' && eventB.type === 'GlimmerStringLiteral') { |
| 94 | + return eventA.value.localeCompare(eventB.value); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return 0; |
| 99 | + } |
| 100 | + |
| 101 | + function getUnsortedAttributeIndex(attributes) { |
| 102 | + return attributes.findIndex((attribute, index) => { |
| 103 | + if (index === attributes.length - 1) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + return compareAttributes(attribute, attributes[index + 1]) > 0; |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + function getUnsortedHashPairIndex(pairs) { |
| 112 | + return pairs.findIndex((hashPair, index) => { |
| 113 | + if (index === pairs.length - 1) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + return compareHashPairs(hashPair, pairs[index + 1]) > 0; |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + function getUnsortedModifierIndex(modifiers) { |
| 122 | + return modifiers.findIndex((modifier, index) => { |
| 123 | + if (index === modifiers.length - 1) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + return compareModifiers(modifier, modifiers[index + 1]) > 0; |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + function canSkipSplattributesLast(node) { |
| 132 | + const { attributes, modifiers } = node; |
| 133 | + |
| 134 | + if (!attributes || attributes.length === 0 || !modifiers || modifiers.length === 0) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + const splattributes = attributes.at(-1); |
| 139 | + const lastModifier = modifiers.at(-1); |
| 140 | + |
| 141 | + if (!splattributes || splattributes.name !== '...attributes' || !lastModifier) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + // Check that ...attributes appears after the last modifier |
| 146 | + const splattributesPosition = splattributes.loc.start; |
| 147 | + const lastModifierPosition = lastModifier.loc.start; |
| 148 | + |
| 149 | + if (splattributesPosition.line > lastModifierPosition.line) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + return ( |
| 154 | + splattributesPosition.line === lastModifierPosition.line && |
| 155 | + splattributesPosition.column > lastModifierPosition.column |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + GlimmerElementNode(node) { |
| 161 | + const { attributes, modifiers } = node; |
| 162 | + |
| 163 | + if (attributes && attributes.length > 1) { |
| 164 | + const index = getUnsortedAttributeIndex(attributes); |
| 165 | + |
| 166 | + if (index !== -1) { |
| 167 | + context.report({ |
| 168 | + node: attributes[index], |
| 169 | + messageId: 'attributeOrder', |
| 170 | + data: { |
| 171 | + attributeName: getAttributeName(attributes[index]), |
| 172 | + expectedAfter: getAttributeName(attributes[index + 1]), |
| 173 | + }, |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if (modifiers && modifiers.length > 1) { |
| 179 | + const index = getUnsortedModifierIndex(modifiers); |
| 180 | + |
| 181 | + if (index !== -1) { |
| 182 | + context.report({ |
| 183 | + node: modifiers[index], |
| 184 | + messageId: 'modifierOrder', |
| 185 | + data: { |
| 186 | + modifierName: getModifierName(modifiers[index]), |
| 187 | + expectedAfter: getModifierName(modifiers[index + 1]), |
| 188 | + }, |
| 189 | + }); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + if (!canSkipSplattributesLast(node)) { |
| 194 | + const splattributes = attributes.at(-1); |
| 195 | + |
| 196 | + // When ...attributes is the only attribute, report as attributeOrder |
| 197 | + // (the ordering issue is that ...attributes should appear after modifiers) |
| 198 | + if (attributes.length === 1) { |
| 199 | + context.report({ |
| 200 | + node: splattributes, |
| 201 | + messageId: 'attributeOrder', |
| 202 | + data: { |
| 203 | + attributeName: '...attributes', |
| 204 | + expectedAfter: 'modifiers', |
| 205 | + }, |
| 206 | + }); |
| 207 | + } else { |
| 208 | + context.report({ |
| 209 | + node: splattributes, |
| 210 | + messageId: 'splattributesOrder', |
| 211 | + }); |
| 212 | + } |
| 213 | + } |
| 214 | + }, |
| 215 | + |
| 216 | + GlimmerBlockStatement(node) { |
| 217 | + if (node.hash && node.hash.pairs && node.hash.pairs.length > 1) { |
| 218 | + const index = getUnsortedHashPairIndex(node.hash.pairs); |
| 219 | + |
| 220 | + if (index !== -1) { |
| 221 | + context.report({ |
| 222 | + node: node.hash.pairs[index], |
| 223 | + messageId: 'hashPairOrder', |
| 224 | + data: { |
| 225 | + hashPairName: getHashPairName(node.hash.pairs[index]), |
| 226 | + expectedAfter: getHashPairName(node.hash.pairs[index + 1]), |
| 227 | + }, |
| 228 | + }); |
| 229 | + } |
| 230 | + } |
| 231 | + }, |
| 232 | + |
| 233 | + GlimmerMustacheStatement(node) { |
| 234 | + if (node.hash && node.hash.pairs && node.hash.pairs.length > 1) { |
| 235 | + const index = getUnsortedHashPairIndex(node.hash.pairs); |
| 236 | + |
| 237 | + if (index !== -1) { |
| 238 | + // Component invocations with a string positional param (e.g. {{component "ui/button" ...}}) |
| 239 | + // treat hash pairs as component attributes |
| 240 | + const isComponentInvocation = |
| 241 | + node.path && |
| 242 | + node.path.original === 'component' && |
| 243 | + node.params && |
| 244 | + node.params.length > 0 && |
| 245 | + node.params[0].type === 'GlimmerStringLiteral'; |
| 246 | + |
| 247 | + if (isComponentInvocation) { |
| 248 | + context.report({ |
| 249 | + node: node.hash.pairs[index], |
| 250 | + messageId: 'attributeOrder', |
| 251 | + data: { |
| 252 | + attributeName: getHashPairName(node.hash.pairs[index]), |
| 253 | + expectedAfter: getHashPairName(node.hash.pairs[index + 1]), |
| 254 | + }, |
| 255 | + }); |
| 256 | + } else { |
| 257 | + context.report({ |
| 258 | + node: node.hash.pairs[index], |
| 259 | + messageId: 'hashPairOrder', |
| 260 | + data: { |
| 261 | + hashPairName: getHashPairName(node.hash.pairs[index]), |
| 262 | + expectedAfter: getHashPairName(node.hash.pairs[index + 1]), |
| 263 | + }, |
| 264 | + }); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + }, |
| 269 | + |
| 270 | + GlimmerSubExpression(node) { |
| 271 | + if (node.hash && node.hash.pairs && node.hash.pairs.length > 1) { |
| 272 | + const index = getUnsortedHashPairIndex(node.hash.pairs); |
| 273 | + |
| 274 | + if (index !== -1) { |
| 275 | + context.report({ |
| 276 | + node: node.hash.pairs[index], |
| 277 | + messageId: 'hashPairOrder', |
| 278 | + data: { |
| 279 | + hashPairName: getHashPairName(node.hash.pairs[index]), |
| 280 | + expectedAfter: getHashPairName(node.hash.pairs[index + 1]), |
| 281 | + }, |
| 282 | + }); |
| 283 | + } |
| 284 | + } |
| 285 | + }, |
| 286 | + }; |
| 287 | + }, |
| 288 | +}; |
| 289 | +/* eslint-enable unicorn/consistent-function-scoping, unicorn/prefer-at */ |
0 commit comments