Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions docs/rules/template-no-shadowed-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -16,6 +18,15 @@ This rule **forbids** the following:
</FooBar>
```

```gjs
const code = (value) => value.toLowerCase();

<template>
{{code @name}}
<code>{{@name}}</code>
</template>
```

This rule **allows** the following:

```hbs
Expand Down
41 changes: 36 additions & 5 deletions lib/rules/template-no-shadowed-elements.js
Original file line number Diff line number Diff line change
@@ -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>)
Expand Down Expand Up @@ -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',
Expand All @@ -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 || []));
}
Expand Down Expand Up @@ -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; <item />`).
if (isLocal(tag) || (ELEMENT_TAGS.has(tag) && isJsBinding(node))) {
context.report({
node,
messageId: 'shadowed',
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/rules/template-no-shadowed-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,65 @@ ruleTester.run('template-no-shadowed-elements', rule, {
'<template><FooBar as |Baz|><Baz /></FooBar></template>',
'<template>{{#with foo=(component "blah-zorz") as |Div|}}<Div></Div>{{/with}}</template>',
'<template><Foo as |bar|><bar.baz /></Foo></template>',
// A JS binding that is not used as a tag shadows nothing.
"const kebab = (value) => value.toLowerCase();\n<template><code>{{kebab 'A'}}</code></template>",
// A JS binding only shadows the tags inside its own scope.
'function unrelated() { const code = 1; return code; }\n<template><code>x</code></template>',
// A lowercase binding that is not an element name is invoked on purpose.
"import item from './item';\n<template><item /></template>",
// A configured global is not a binding the template compiler sees.
{
code: '<template><svg><linearGradient><stop offset="0" /></linearGradient></svg></template>',
globals: { stop: 'readonly' },
},
// Types have no runtime value.
{
filename: 'x.gts',
code: 'type code = string;\ninterface section {}\n<template><code>x</code><section /></template>',
},
{
filename: 'x.gts',
code: "import type code from './code';\nimport { type other as section } from './code';\n<template><code>x</code><section /></template>",
},
{
filename: 'x.gts',
code: 'export function wrap<code>(value: code) {\n return <template><code>{{value}}</code></template>;\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 <code> into a call.
{
code: "const code = (value) => value.toLowerCase();\n<template>{{code 'A'}}<code>x</code></template>",
output: null,
errors: [
{
message: 'Component name "code" shadows HTML element <code>. Use a different name.',
type: 'GlimmerElementNode',
},
],
},
{
code: "import div from './my-component';\n<template><div></div></template>",
output: null,
errors: [
{
message: 'Component name "div" shadows HTML element <div>. Use a different name.',
type: 'GlimmerElementNode',
},
],
},
{
code: 'export default function wrap(section) {\n return <template><section>x</section></template>;\n}',
output: null,
errors: [
{
message: 'Component name "section" shadows HTML element <section>. Use a different name.',
type: 'GlimmerElementNode',
},
],
},
{
code: '<template><FooBar as |div|><div></div></FooBar></template>',
output: null,
Expand Down Expand Up @@ -60,6 +116,7 @@ const hbsRuleTester = new RuleTester({

hbsRuleTester.run('template-no-shadowed-elements', rule, {
valid: [
{ code: '<svg><stop offset="0" /></svg>', globals: { stop: 'readonly' } },
'<div>content</div>',
'<form><input /></form>',
'{{#foo-bar as |Baz|}}<Baz />{{/foo-bar}}',
Expand Down
Loading