{{@name}}
+
+```
+
This rule **allows** the following:
```hbs
diff --git a/lib/rules/template-no-shadowed-elements.js b/lib/rules/template-no-shadowed-elements.js
index fa63d08852..592db9ecfa 100644
--- a/lib/rules/template-no-shadowed-elements.js
+++ b/lib/rules/template-no-shadowed-elements.js
@@ -1,3 +1,17 @@
+const { ELEMENT_TAGS } = require('../utils/is-native-element');
+
+// A configured global has no definition, and a type has no runtime value:
+// neither is a binding the template compiler can see.
+function isValueDefinition(def) {
+ if (def.type === 'Type' || def.type === 'TSEnumMember') {
+ return false;
+ }
+ if (def.type === 'ImportBinding') {
+ return def.node.importKind !== 'type' && def.parent.importKind !== 'type';
+ }
+ return true;
+}
+
// Mirror upstream ember-template-lint's inverse-of-isAngleBracketComponent logic.
// A tag is treated as an HTML element only when it:
// - does NOT contain ':' (named blocks like <:slot>)
@@ -33,7 +47,7 @@ module.exports = {
meta: {
type: 'problem',
docs: {
- description: 'disallow ambiguity with block param names shadowing HTML elements',
+ description: 'disallow block params and JS bindings shadowing HTML elements',
category: 'Possible Errors',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-shadowed-elements.md',
templateMode: 'both',
@@ -52,8 +66,22 @@ module.exports = {
},
create(context) {
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
const blockParamScope = [];
+ // In gjs/gts the template compiler asks the surrounding JS scope: a tag
+ // whose name is bound there (const, function, import, parameter) is
+ // compiled as a component invocation, not as an element. The parser has
+ // already resolved the tag against that scope, as a reference.
+ function isJsBinding(node) {
+ const head = node.parts && node.parts[0];
+ const reference =
+ head && sourceCode.getScope(node).references.find((ref) => ref.identifier === head);
+ return Boolean(
+ reference && reference.resolved && reference.resolved.defs.some(isValueDefinition)
+ );
+ }
+
function pushScope(params) {
blockParamScope.push(new Set(params || []));
}
@@ -97,14 +125,17 @@ module.exports = {
// Mirror upstream: if the tag is an angle-bracket-component (i.e.
// not a plain HTML element — contains '.', is PascalCase, has a
// hyphen, etc.) it cannot be a shadow of a native HTML element.
- // Only a lowercase / simple tag that is a local block param is
- // considered shadowed. This also covers tags not in any static
- // html-tags list (upstream does not restrict to a known set).
+ // Only a lowercase / simple tag that is a local block param or a
+ // JS binding is considered shadowed. This also covers tags not in
+ // any static html-tags list (upstream does not restrict to a known
+ // set).
if (!isHtmlElement(tag)) {
return;
}
- if (isLocal(tag)) {
+ // A lowercase JS binding that is not an element name is a component
+ // invoked on purpose (`const item = Item; {{kebab 'A'}}",
+ // A JS binding only shadows the tags inside its own scope.
+ 'function unrelated() { const code = 1; return code; }\nx',
+ // A lowercase binding that is not an element name is invoked on purpose.
+ "import item from './item';\nx',
+ },
+ {
+ filename: 'x.gts',
+ code: "import type code from './code';\nimport { type other as section } from './code';\nx",
+ },
+ {
+ filename: 'x.gts',
+ code: 'export function wrap(value: code) {\n return {{value}};\n}',
+ },
],
invalid: [
+ // In gjs/gts a tag whose name is a JS binding in scope is compiled as a
+ // component invocation, so a module-level helper turns into a call.
+ {
+ code: "const code = (value) => value.toLowerCase();\n{{code 'A'}}x",
+ output: null,
+ errors: [
+ {
+ message: 'Component name "code" shadows HTML element . Use a different name.',
+ type: 'GlimmerElementNode',
+ },
+ ],
+ },
+ {
+ code: "import div from './my-component';\n",
+ output: null,
+ errors: [
+ {
+ message: 'Component name "div" shadows HTML element . Use a different name.',
+ type: 'GlimmerElementNode',
+ },
+ ],
+ },
+ {
+ code: 'export default function wrap(section) {\n return x ;\n}',
+ output: null,
+ errors: [
+ {
+ message: 'Component name "section" shadows HTML element . Use a different name.',
+ type: 'GlimmerElementNode',
+ },
+ ],
+ },
{
code: ' ',
output: null,
@@ -60,6 +116,7 @@ const hbsRuleTester = new RuleTester({
hbsRuleTester.run('template-no-shadowed-elements', rule, {
valid: [
+ { code: '', globals: { stop: 'readonly' } },
'content',
'',
'{{#foo-bar as |Baz|}} {{/foo-bar}}',