Skip to content

Commit 17ac6de

Browse files
committed
Extract rule: template-no-block-params-for-html-elements
1 parent 3e664f5 commit 17ac6de

4 files changed

Lines changed: 174 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,15 @@ rules in templates can be disabled with eslint directives with mustache or html
189189

190190
### Best Practices
191191

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

201202
### Components
202203

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ember/template-no-block-params-for-html-elements
2+
3+
<!-- end auto-generated rule header -->
4+
5+
💼 This rule is enabled in the following [configs](https://github.com/ember-cli/eslint-plugin-ember#-configurations): `strict-gjs`, `strict-gts`.
6+
7+
Disallow block params on HTML elements.
8+
9+
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.
10+
11+
## Rule Details
12+
13+
This rule disallows using block params on HTML elements. Use components if you need to pass block params.
14+
15+
## Examples
16+
17+
### Incorrect ❌
18+
19+
```gjs
20+
<template>
21+
<div as |content|>
22+
{{content}}
23+
</div>
24+
</template>
25+
```
26+
27+
```gjs
28+
<template>
29+
<section as |data|>
30+
<p>{{data}}</p>
31+
</section>
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<ul as |items|>
38+
<li>{{items}}</li>
39+
</ul>
40+
</template>
41+
```
42+
43+
### Correct ✅
44+
45+
```gjs
46+
<template>
47+
<div>Content</div>
48+
</template>
49+
```
50+
51+
```gjs
52+
<template>
53+
<MyComponent as |item|>
54+
{{item.name}}
55+
</MyComponent>
56+
</template>
57+
```
58+
59+
```gjs
60+
<template>
61+
{{#each this.items as |item|}}
62+
<li>{{item}}</li>
63+
{{/each}}
64+
</template>
65+
```
66+
67+
## Related Rules
68+
69+
- [template-no-arguments-for-html-elements](./template-no-arguments-for-html-elements.md)
70+
71+
## References
72+
73+
- [Ember Guides - Block Content](https://guides.emberjs.com/release/components/block-content/)
74+
- [eslint-plugin-ember template-no-yield-only](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-yield-only.md)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
const htmlTags = require('html-tags');
3+
4+
module.exports = {
5+
meta: {
6+
type: 'problem',
7+
docs: {
8+
description: 'disallow block params on HTML elements',
9+
category: 'Best Practices',
10+
strictGjs: true,
11+
strictGts: true,
12+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-block-params-for-html-elements.md',
13+
},
14+
fixable: null,
15+
schema: [],
16+
messages: {
17+
noBlockParamsForHtmlElements:
18+
'Block params can only be used with components, not HTML elements.',
19+
},
20+
},
21+
22+
create(context) {
23+
const HTML_ELEMENTS = new Set(htmlTags);
24+
25+
return {
26+
GlimmerElementNode(node) {
27+
// Check if this is an HTML element (lowercase)
28+
if (!HTML_ELEMENTS.has(node.tag)) {
29+
return;
30+
}
31+
32+
// Check for block params
33+
if (node.blockParams && node.blockParams.length > 0) {
34+
context.report({
35+
node,
36+
messageId: 'noBlockParamsForHtmlElements',
37+
});
38+
}
39+
},
40+
};
41+
},
42+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const rule = require('../../../lib/rules/template-no-block-params-for-html-elements');
2+
const RuleTester = require('eslint').RuleTester;
3+
4+
const ruleTester = new RuleTester({
5+
parser: require.resolve('ember-eslint-parser'),
6+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
7+
});
8+
9+
ruleTester.run('template-no-block-params-for-html-elements', rule, {
10+
valid: [
11+
'<template><div>Content</div></template>',
12+
'<template><MyComponent as |item|>{{item.name}}</MyComponent></template>',
13+
'<template>{{#each this.items as |item|}}<li>{{item}}</li>{{/each}}</template>',
14+
'<template><button>Click</button></template>',
15+
],
16+
17+
invalid: [
18+
{
19+
code: '<template><div as |content|>{{content}}</div></template>',
20+
output: null,
21+
errors: [
22+
{
23+
message: 'Block params can only be used with components, not HTML elements.',
24+
type: 'GlimmerElementNode',
25+
},
26+
],
27+
},
28+
{
29+
code: '<template><section as |data|><p>{{data}}</p></section></template>',
30+
output: null,
31+
errors: [
32+
{
33+
message: 'Block params can only be used with components, not HTML elements.',
34+
type: 'GlimmerElementNode',
35+
},
36+
],
37+
},
38+
{
39+
code: '<template><ul as |items|><li>{{items}}</li></ul></template>',
40+
output: null,
41+
errors: [
42+
{
43+
message: 'Block params can only be used with components, not HTML elements.',
44+
type: 'GlimmerElementNode',
45+
},
46+
],
47+
},
48+
],
49+
});

0 commit comments

Comments
 (0)