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
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ rules in templates can be disabled with eslint directives with mustache or html

### Best Practices

| Name | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
| Name | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
| [template-no-block-params-for-html-elements](docs/rules/template-no-block-params-for-html-elements.md) | disallow block params on HTML elements | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

### Components

Expand Down
72 changes: 72 additions & 0 deletions docs/rules/template-no-block-params-for-html-elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ember/template-no-block-params-for-html-elements

<!-- end auto-generated rule header -->

Disallow block params on HTML elements.

Block params (using the `as |param|` syntax) are a feature specific to Ember components and block helpers. They should not be used on regular HTML elements.

## Rule Details

This rule disallows using block params on HTML elements. Use components if you need to pass block params.

## Examples

### Incorrect ❌

```gjs
<template>
<div as |content|>
{{content}}
</div>
</template>
```

```gjs
<template>
<section as |data|>
<p>{{data}}</p>
</section>
</template>
```

```gjs
<template>
<ul as |items|>
<li>{{items}}</li>
</ul>
</template>
```

### Correct ✅

```gjs
<template>
<div>Content</div>
</template>
```

```gjs
<template>
<MyComponent as |item|>
{{item.name}}
</MyComponent>
</template>
```

```gjs
<template>
{{#each this.items as |item|}}
<li>{{item}}</li>
{{/each}}
</template>
```

## Related Rules

- [template-no-arguments-for-html-elements](./template-no-arguments-for-html-elements.md)

## References

- [Ember Guides - Block Content](https://guides.emberjs.com/release/components/block-content/)
- [eslint-plugin-ember template-no-yield-only](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-yield-only.md)
50 changes: 50 additions & 0 deletions lib/rules/template-no-block-params-for-html-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/** @type {import('eslint').Rule.RuleModule} */
const htmlTags = require('html-tags');

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow block params on HTML elements',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-block-params-for-html-elements.md',
},
fixable: null,
schema: [],
messages: {
noBlockParamsForHtmlElements:
'Block params can only be used with components, not HTML elements.',
},
},

create(context) {
const sourceCode = context.sourceCode;
const HTML_ELEMENTS = new Set(htmlTags);

return {
GlimmerElementNode(node) {
// Check if this is an HTML element (lowercase)
if (!HTML_ELEMENTS.has(node.tag)) {
return;
}

// If the tag name is a variable in scope, it's being used as a component, not an HTML element
const scope = sourceCode.getScope(node.parent);
const isVariable = scope.references.some((ref) => ref.identifier === node.parts[0]);
if (isVariable) {
return;
}

// Check for block params
if (node.blockParams && node.blockParams.length > 0) {
context.report({
node,
messageId: 'noBlockParamsForHtmlElements',
});
}
},
};
},
};
54 changes: 54 additions & 0 deletions tests/lib/rules/template-no-block-params-for-html-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const rule = require('../../../lib/rules/template-no-block-params-for-html-elements');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-block-params-for-html-elements', rule, {
valid: [
`let div = <template>{{yield "hello"}}</template>;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, but please don't do this <3

<template>
<div as |greeting|>{{greeting}}</div>
</template>
`,
'<template><div>Content</div></template>',
'<template><MyComponent as |item|>{{item.name}}</MyComponent></template>',
'<template>{{#each this.items as |item|}}<li>{{item}}</li>{{/each}}</template>',
'<template><button>Click</button></template>',
],

invalid: [
{
code: '<template><div as |content|>{{content}}</div></template>',
output: null,
errors: [
{
message: 'Block params can only be used with components, not HTML elements.',
type: 'GlimmerElementNode',
},
],
},
{
code: '<template><section as |data|><p>{{data}}</p></section></template>',
output: null,
errors: [
{
message: 'Block params can only be used with components, not HTML elements.',
type: 'GlimmerElementNode',
},
],
},
{
code: '<template><ul as |items|><li>{{items}}</li></ul></template>',
output: null,
errors: [
{
message: 'Block params can only be used with components, not HTML elements.',
type: 'GlimmerElementNode',
},
],
},
],
});
Loading