-
-
Notifications
You must be signed in to change notification settings - Fork 211
Extract rule: template-no-block-params-for-html-elements #2412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NullVoxPopuli
merged 1 commit into
ember-cli:master
from
NullVoxPopuli:nvp/template-lint-extract-rule-template-no-block-params-for-html-elements
Feb 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
54
tests/lib/rules/template-no-block-params-for-html-elements.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
| <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', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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