|
| 1 | +const htmlTags = require('html-tags'); |
| 2 | + |
| 3 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 4 | +module.exports = { |
| 5 | + meta: { |
| 6 | + type: 'problem', |
| 7 | + docs: { |
| 8 | + description: 'disallow ambiguity with block param names shadowing HTML elements', |
| 9 | + category: 'Possible Errors', |
| 10 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-shadowed-elements.md', |
| 11 | + templateMode: 'both', |
| 12 | + }, |
| 13 | + fixable: null, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + shadowed: 'Component name "{{name}}" shadows HTML element <{{name}}>. Use a different name.', |
| 17 | + }, |
| 18 | + originallyFrom: { |
| 19 | + name: 'ember-template-lint', |
| 20 | + rule: 'lib/rules/no-shadowed-elements.js', |
| 21 | + docs: 'docs/rule/no-shadowed-elements.md', |
| 22 | + tests: 'test/unit/rules/no-shadowed-elements-test.js', |
| 23 | + }, |
| 24 | + }, |
| 25 | + |
| 26 | + create(context) { |
| 27 | + const HTML_ELEMENTS = new Set(htmlTags); |
| 28 | + |
| 29 | + const blockParamScope = []; |
| 30 | + |
| 31 | + function pushScope(params) { |
| 32 | + blockParamScope.push(new Set(params || [])); |
| 33 | + } |
| 34 | + |
| 35 | + function popScope() { |
| 36 | + blockParamScope.pop(); |
| 37 | + } |
| 38 | + |
| 39 | + function isLocal(name) { |
| 40 | + for (const scope of blockParamScope) { |
| 41 | + if (scope.has(name)) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + GlimmerBlockStatement(node) { |
| 50 | + if (node.program && node.program.blockParams) { |
| 51 | + pushScope(node.program.blockParams); |
| 52 | + } |
| 53 | + }, |
| 54 | + 'GlimmerBlockStatement:exit'(node) { |
| 55 | + if (node.program && node.program.blockParams) { |
| 56 | + popScope(); |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + GlimmerElementNode(node) { |
| 61 | + // Push block params for elements with 'as |...|' syntax |
| 62 | + if (node.blockParams && node.blockParams.length > 0) { |
| 63 | + pushScope(node.blockParams); |
| 64 | + } |
| 65 | + |
| 66 | + const tag = node.tag; |
| 67 | + if (!tag) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const containsDot = tag.includes('.'); |
| 72 | + |
| 73 | + if (containsDot) { |
| 74 | + // dot paths like bar.baz are not ambiguous |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Only check lowercase elements — a lowercase tag that is a local |
| 79 | + // block param and also a native HTML element name is shadowed. |
| 80 | + // PascalCase tags (e.g. <Input>, <Form>, <Select>) are Ember/Glimmer |
| 81 | + // component invocations and should not be flagged. |
| 82 | + const firstChar = tag.charAt(0); |
| 83 | + const isLowerCase = |
| 84 | + firstChar === firstChar.toLowerCase() && firstChar !== firstChar.toUpperCase(); |
| 85 | + |
| 86 | + if (isLowerCase && isLocal(tag) && HTML_ELEMENTS.has(tag)) { |
| 87 | + context.report({ |
| 88 | + node, |
| 89 | + messageId: 'shadowed', |
| 90 | + data: { name: tag }, |
| 91 | + }); |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + 'GlimmerElementNode:exit'(node) { |
| 96 | + if (node.blockParams && node.blockParams.length > 0) { |
| 97 | + popScope(); |
| 98 | + } |
| 99 | + }, |
| 100 | + }; |
| 101 | + }, |
| 102 | +}; |
0 commit comments