|
| 1 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 2 | +module.exports = { |
| 3 | + meta: { |
| 4 | + type: 'suggestion', |
| 5 | + docs: { |
| 6 | + description: 'disallow element event actions (use {{on}} modifier instead)', |
| 7 | + category: 'Best Practices', |
| 8 | + recommended: false, |
| 9 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-element-event-actions.md', |
| 10 | + }, |
| 11 | + fixable: null, |
| 12 | + schema: [ |
| 13 | + { |
| 14 | + type: 'object', |
| 15 | + properties: { |
| 16 | + requireActionHelper: { type: 'boolean' }, |
| 17 | + }, |
| 18 | + additionalProperties: false, |
| 19 | + }, |
| 20 | + ], |
| 21 | + messages: { |
| 22 | + noElementEventActions: 'Do not use element event actions. Use the `on` modifier instead.', |
| 23 | + }, |
| 24 | + strictGjs: true, |
| 25 | + strictGts: true, |
| 26 | + }, |
| 27 | + |
| 28 | + create(context) { |
| 29 | + const options = context.options[0] || {}; |
| 30 | + const requireActionHelper = options.requireActionHelper || false; |
| 31 | + |
| 32 | + return { |
| 33 | + GlimmerElementNode(node) { |
| 34 | + if (!node.attributes) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + for (const attr of node.attributes) { |
| 39 | + if (attr.type !== 'GlimmerAttrNode' || !attr.name) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + const name = attr.name.toLowerCase(); |
| 43 | + if (!name.startsWith('on')) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + // Skip non-event attributes like "once", "open", etc. |
| 47 | + if (name.length <= 2) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // If requireActionHelper is true, only flag when the value uses {{action ...}} |
| 52 | + if (requireActionHelper) { |
| 53 | + if ( |
| 54 | + attr.value?.type === 'GlimmerMustacheStatement' && |
| 55 | + attr.value.path?.original === 'action' |
| 56 | + ) { |
| 57 | + context.report({ node: attr, messageId: 'noElementEventActions' }); |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Flag any mustache value on event attributes |
| 61 | + if ( |
| 62 | + attr.value?.type === 'GlimmerMustacheStatement' || |
| 63 | + attr.value?.type === 'GlimmerConcatStatement' |
| 64 | + ) { |
| 65 | + context.report({ node: attr, messageId: 'noElementEventActions' }); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + }; |
| 71 | + }, |
| 72 | +}; |
0 commit comments