Skip to content

Commit 501afc8

Browse files
committed
Extract rule: template-require-has-block-helper
1 parent fb4e650 commit 501afc8

4 files changed

Lines changed: 422 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ rules in templates can be disabled with eslint directives with mustache or html
253253
| [template-no-obsolete-elements](docs/rules/template-no-obsolete-elements.md) | disallow obsolete HTML elements | | | |
254254
| [template-no-outlet-outside-routes](docs/rules/template-no-outlet-outside-routes.md) | disallow {{outlet}} outside of route templates | | | |
255255
| [template-no-page-title-component](docs/rules/template-no-page-title-component.md) | disallow usage of ember-page-title component | | | |
256+
| [template-require-has-block-helper](docs/rules/template-require-has-block-helper.md) | require (has-block) helper usage instead of hasBlock property | | | |
256257
| [template-require-iframe-src-attribute](docs/rules/template-require-iframe-src-attribute.md) | require iframe elements to have src attribute | | 🔧 | |
257258
| [template-require-splattributes](docs/rules/template-require-splattributes.md) | require splattributes usage in component templates | | | |
258259
| [template-require-strict-mode](docs/rules/template-require-strict-mode.md) | require templates to be in strict mode | | | |
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# ember/template-require-has-block-helper
2+
3+
> **HBS Only**: This rule applies to classic `.hbs` template files only (loose mode). It is not relevant for `gjs`/`gts` files (strict mode), where these patterns cannot occur.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Requires usage of the `(has-block)` helper instead of the `hasBlock` property.
8+
9+
## Rule Details
10+
11+
The `(has-block)` helper is the preferred way to check if a block was provided to a component.
12+
13+
## Examples
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```hbs
18+
{{#if hasBlock}}
19+
{{yield}}
20+
{{/if}}
21+
```
22+
23+
```hbs
24+
{{#if this.hasBlock}}
25+
{{yield}}
26+
{{/if}}
27+
```
28+
29+
Examples of **correct** code for this rule:
30+
31+
```hbs
32+
{{#if (has-block)}}
33+
{{yield}}
34+
{{/if}}
35+
```
36+
37+
```hbs
38+
{{#if (has-block 'inverse')}}
39+
{{yield to='inverse'}}
40+
{{/if}}
41+
```
42+
43+
## Migration
44+
45+
- `{{hasBlock}}`-> `{{has-block}}
46+
- `{{hasBlockParams}}`-> `{{has-block-params}}
47+
- `{{#if hasBlock}} {{/if}}`-> `{{#if (has-block)}} {{/if}}`
48+
- `{{#if (hasBlock "inverse")}} {{/if}}`-> `{{#if (has-block "inverse")}} {{/if}}`
49+
- `{{#if hasBlockParams}} {{/if}}`-> `{{#if (has-block-params)}} {{/if}}`
50+
- `{{#if (hasBlockParams "inverse")}} {{/if}}`-> `{{#if (has-block-params "inverse")}} {{/if}}`
51+
52+
## References
53+
54+
- [eslint-plugin-ember template-require-has-block-helper](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-require-has-block-helper.md)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'require (has-block) helper usage instead of hasBlock property',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-has-block-helper.md',
9+
templateMode: 'loose',
10+
},
11+
fixable: null,
12+
schema: [],
13+
messages: {
14+
useHasBlockHelper: 'Use (has-block) helper instead of hasBlock property.',
15+
},
16+
originallyFrom: {
17+
name: 'ember-template-lint',
18+
rule: 'lib/rules/require-has-block-helper.js',
19+
docs: 'docs/rule/require-has-block-helper.md',
20+
tests: 'test/unit/rules/require-has-block-helper-test.js',
21+
},
22+
},
23+
24+
create(context) {
25+
return {
26+
GlimmerPathExpression(node) {
27+
if (
28+
node.original === 'hasBlock' ||
29+
node.original === 'this.hasBlock' ||
30+
node.original === 'hasBlockParams' ||
31+
node.original === 'this.hasBlockParams'
32+
) {
33+
context.report({
34+
node,
35+
messageId: 'useHasBlockHelper',
36+
});
37+
}
38+
},
39+
};
40+
},
41+
};

0 commit comments

Comments
 (0)