|
| 1 | +// Comprehensive Ember event handler names |
| 2 | +const EMBER_EVENTS = new Set([ |
| 3 | + 'touchStart', |
| 4 | + 'touchMove', |
| 5 | + 'touchEnd', |
| 6 | + 'touchCancel', |
| 7 | + 'keyDown', |
| 8 | + 'keyUp', |
| 9 | + 'keyPress', |
| 10 | + 'mouseDown', |
| 11 | + 'mouseUp', |
| 12 | + 'contextMenu', |
| 13 | + 'click', |
| 14 | + 'doubleClick', |
| 15 | + 'mouseMove', |
| 16 | + 'mouseEnter', |
| 17 | + 'mouseLeave', |
| 18 | + 'focusIn', |
| 19 | + 'focusOut', |
| 20 | + 'submit', |
| 21 | + 'change', |
| 22 | + 'input', |
| 23 | + 'dragStart', |
| 24 | + 'drag', |
| 25 | + 'dragEnter', |
| 26 | + 'dragLeave', |
| 27 | + 'dragOver', |
| 28 | + 'dragEnd', |
| 29 | + 'drop', |
| 30 | +]); |
| 31 | + |
| 32 | +function isEventName(name) { |
| 33 | + return EMBER_EVENTS.has(name); |
| 34 | +} |
| 35 | + |
| 36 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 37 | +module.exports = { |
| 38 | + meta: { |
| 39 | + type: 'suggestion', |
| 40 | + docs: { |
| 41 | + description: 'disallow passing event handlers directly as component arguments', |
| 42 | + category: 'Best Practices', |
| 43 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-passed-in-event-handlers.md', |
| 44 | + templateMode: 'both', |
| 45 | + }, |
| 46 | + fixable: null, |
| 47 | + schema: [ |
| 48 | + { |
| 49 | + type: 'object', |
| 50 | + properties: { |
| 51 | + ignore: { |
| 52 | + type: 'object', |
| 53 | + additionalProperties: { |
| 54 | + type: 'array', |
| 55 | + items: { type: 'string' }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + additionalProperties: false, |
| 60 | + }, |
| 61 | + ], |
| 62 | + messages: { |
| 63 | + unexpected: |
| 64 | + 'Event handler "@{{name}}" should not be passed as a component argument. Use the `on` modifier instead.', |
| 65 | + }, |
| 66 | + originallyFrom: { |
| 67 | + name: 'ember-template-lint', |
| 68 | + rule: 'lib/rules/no-passed-in-event-handlers.js', |
| 69 | + docs: 'docs/rule/no-passed-in-event-handlers.md', |
| 70 | + tests: 'test/unit/rules/no-passed-in-event-handlers-test.js', |
| 71 | + }, |
| 72 | + }, |
| 73 | + |
| 74 | + create(context) { |
| 75 | + const options = context.options[0] || {}; |
| 76 | + const ignoreConfig = options.ignore || {}; |
| 77 | + |
| 78 | + return { |
| 79 | + GlimmerElementNode(node) { |
| 80 | + // Only check component invocations (PascalCase) |
| 81 | + if (!/^[A-Z]/.test(node.tag)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + // Skip built-in Input/Textarea |
| 85 | + if (node.tag === 'Input' || node.tag === 'Textarea') { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (!node.attributes) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + const ignoredAttrs = ignoreConfig[node.tag] || []; |
| 94 | + |
| 95 | + for (const attr of node.attributes) { |
| 96 | + if (!attr.name || !attr.name.startsWith('@')) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + const argName = attr.name.slice(1); |
| 100 | + |
| 101 | + // Check ignore config |
| 102 | + if (ignoredAttrs.includes(attr.name)) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (isEventName(argName)) { |
| 107 | + context.report({ |
| 108 | + node: attr, |
| 109 | + messageId: 'unexpected', |
| 110 | + data: { name: argName }, |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + GlimmerMustacheStatement(node) { |
| 117 | + const path = node.path; |
| 118 | + if (!path || path.type !== 'GlimmerPathExpression') { |
| 119 | + return; |
| 120 | + } |
| 121 | + // Skip built-in input/textarea |
| 122 | + if (path.original === 'input' || path.original === 'textarea') { |
| 123 | + return; |
| 124 | + } |
| 125 | + // Check hash pairs for event handler names |
| 126 | + if (!node.hash || !node.hash.pairs) { |
| 127 | + return; |
| 128 | + } |
| 129 | + const ignoredAttrs = ignoreConfig[path.original] || []; |
| 130 | + |
| 131 | + for (const pair of node.hash.pairs) { |
| 132 | + if (ignoredAttrs.includes(pair.key)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + if (isEventName(pair.key)) { |
| 136 | + context.report({ |
| 137 | + node: pair, |
| 138 | + messageId: 'unexpected', |
| 139 | + data: { name: pair.key }, |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + }; |
| 145 | + }, |
| 146 | +}; |
0 commit comments