diff --git a/README.md b/README.md index bf4a82c29b..0854b83ec9 100644 --- a/README.md +++ b/README.md @@ -514,14 +514,14 @@ To disable a rule for an entire `.gjs`/`.gts` file, use a regular ESLint file-le ### Possible Errors -| Name                                                 | Description | 💼 | 🔧 | 💡 | -| :------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------- | :- | :- | :- | -| [template-no-extra-mut-helper-argument](docs/rules/template-no-extra-mut-helper-argument.md) | disallow passing more than one argument to the mut helper | 📋 | | | -| [template-no-jsx-attributes](docs/rules/template-no-jsx-attributes.md) | disallow JSX-style camelCase attributes | | 🔧 | | -| [template-no-scope-outside-table-headings](docs/rules/template-no-scope-outside-table-headings.md) | disallow scope attribute outside th elements | 📋 | | | -| [template-no-shadowed-elements](docs/rules/template-no-shadowed-elements.md) | disallow ambiguity with block param names shadowing HTML elements | 📋 | | | -| [template-no-unbalanced-curlies](docs/rules/template-no-unbalanced-curlies.md) | disallow unbalanced mustache curlies | 📋 | | | -| [template-no-unknown-arguments-for-builtin-components](docs/rules/template-no-unknown-arguments-for-builtin-components.md) | disallow unknown arguments for built-in components | 📋 | 🔧 | | +| Name                                                 | Description | 💼 | 🔧 | 💡 | +| :------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------ | :- | :- | :- | +| [template-no-extra-mut-helper-argument](docs/rules/template-no-extra-mut-helper-argument.md) | disallow passing more than one argument to the mut helper | 📋 | | | +| [template-no-jsx-attributes](docs/rules/template-no-jsx-attributes.md) | disallow JSX-style camelCase attributes | | 🔧 | | +| [template-no-scope-outside-table-headings](docs/rules/template-no-scope-outside-table-headings.md) | disallow scope attribute outside th elements | 📋 | | | +| [template-no-shadowed-elements](docs/rules/template-no-shadowed-elements.md) | disallow block params and JS bindings shadowing HTML elements | 📋 | | | +| [template-no-unbalanced-curlies](docs/rules/template-no-unbalanced-curlies.md) | disallow unbalanced mustache curlies | 📋 | | | +| [template-no-unknown-arguments-for-builtin-components](docs/rules/template-no-unknown-arguments-for-builtin-components.md) | disallow unknown arguments for built-in components | 📋 | 🔧 | | ### Routes diff --git a/docs/rules/template-no-shadowed-elements.md b/docs/rules/template-no-shadowed-elements.md index 76cc407017..13340770bb 100644 --- a/docs/rules/template-no-shadowed-elements.md +++ b/docs/rules/template-no-shadowed-elements.md @@ -6,6 +6,8 @@ This rule prevents ambiguity in situations where a yielded block param which starts with a lower case letter is also used within the block itself as an element name. +In `.gjs`/`.gts` the same applies to JavaScript bindings. A tag whose name is a binding in the surrounding scope (a `const`, a function, an import, a parameter) is compiled as a component invocation, not as an element. The template then fails at render time. + ## Examples This rule **forbids** the following: @@ -16,6 +18,15 @@ This rule **forbids** the following: ``` +```gjs +const code = (value) => value.toLowerCase(); + + +``` + 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; `). + if (isLocal(tag) || (ELEMENT_TAGS.has(tag) && isJsBinding(node))) { context.report({ node, messageId: 'shadowed', diff --git a/tests/lib/rules/template-no-shadowed-elements.js b/tests/lib/rules/template-no-shadowed-elements.js index f23f8323e2..319ce15ad7 100644 --- a/tests/lib/rules/template-no-shadowed-elements.js +++ b/tests/lib/rules/template-no-shadowed-elements.js @@ -22,9 +22,65 @@ ruleTester.run('template-no-shadowed-elements', rule, { '', '', '', + // A JS binding that is not used as a tag shadows nothing. + "const kebab = (value) => value.toLowerCase();\n", + // A JS binding only shadows the tags inside its own scope. + 'function unrelated() { const code = 1; return code; }\n', + // A lowercase binding that is not an element name is invoked on purpose. + "import item from './item';\n", + // A configured global is not a binding the template compiler sees. + { + code: '', + globals: { stop: 'readonly' }, + }, + // Types have no runtime value. + { + filename: 'x.gts', + code: 'type code = string;\ninterface section {}\n', + }, + { + filename: 'x.gts', + code: "import type code from './code';\nimport { type other as section } from './code';\n", + }, + { + filename: 'x.gts', + code: 'export function wrap(value: code) {\n return ;\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", + 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 ;\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}}',