Skip to content

Commit e9da90c

Browse files
committed
Sync with template-lint
1 parent 501afc8 commit e9da90c

4 files changed

Lines changed: 254 additions & 348 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +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 | | | |
256+
| [template-require-has-block-helper](docs/rules/template-require-has-block-helper.md) | require (has-block) helper usage instead of hasBlock property | | 🔧 | |
257257
| [template-require-iframe-src-attribute](docs/rules/template-require-iframe-src-attribute.md) | require iframe elements to have src attribute | | 🔧 | |
258258
| [template-require-splattributes](docs/rules/template-require-splattributes.md) | require splattributes usage in component templates | | | |
259259
| [template-require-strict-mode](docs/rules/template-require-strict-mode.md) | require templates to be in strict mode | | | |
Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,67 @@
11
# ember/template-require-has-block-helper
22

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.
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
44

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

7-
Requires usage of the `(has-block)` helper instead of the `hasBlock` property.
7+
In Ember 3.26 the properties `hasBlock` and `hasBlockParams` were deprecated. Their replacement is to use `has-block` and `has-block-params` helpers instead.
88

9-
## Rule Details
9+
This rule prevents the usage of `hasBlock` and `hasBlockParams` and suggests using `has-block` or `has-block-params` instead.
1010

11-
The `(has-block)` helper is the preferred way to check if a block was provided to a component.
11+
For more information about this deprecation you can view the [RFC](https://github.com/emberjs/rfcs/blob/master/text/0689-deprecate-has-block.md) or its entry on the [Deprecations page](https://deprecations.emberjs.com/v3.x/#toc_has-block-and-has-block-params).
1212

1313
## Examples
1414

15-
Examples of **incorrect** code for this rule:
15+
This rule **forbids** the following:
1616

17-
```hbs
18-
{{#if hasBlock}}
19-
{{yield}}
20-
{{/if}}
17+
```gjs
18+
<template>
19+
{{hasBlock}}
20+
{{#if hasBlock}}
21+
22+
{{/if}}
23+
</template>
2124
```
2225

23-
```hbs
24-
{{#if this.hasBlock}}
25-
{{yield}}
26-
{{/if}}
26+
```gjs
27+
<template>
28+
{{hasBlockParams}}
29+
{{#if hasBlockParams}}
30+
31+
{{/if}}
32+
</template>
2733
```
2834

29-
Examples of **correct** code for this rule:
35+
This rule **allows** the following:
3036

31-
```hbs
32-
{{#if (has-block)}}
33-
{{yield}}
34-
{{/if}}
37+
```gjs
38+
<template>
39+
{{has-block}}
40+
{{#if (has-block)}}
41+
42+
{{/if}}
43+
</template>
3544
```
3645

37-
```hbs
38-
{{#if (has-block 'inverse')}}
39-
{{yield to='inverse'}}
40-
{{/if}}
46+
```gjs
47+
<template>
48+
{{has-block-params}}
49+
{{#if (has-block-params)}}
50+
51+
{{/if}}
52+
</template>
4153
```
4254

4355
## Migration
4456

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}}`
57+
- `{{hasBlock}}` -> `{{has-block}}`
58+
- `{{hasBlockParams}}` -> `{{has-block-params}}`
59+
- `{{#if hasBlock}} {{/if}}` -> `{{#if (has-block)}} {{/if}}`
60+
- `{{#if (hasBlock "inverse")}} {{/if}}` -> `{{#if (has-block "inverse")}} {{/if}}`
61+
- `{{#if hasBlockParams}} {{/if}}` -> `{{#if (has-block-params)}} {{/if}}`
62+
- `{{#if (hasBlockParams "inverse")}} {{/if}}` -> `{{#if (has-block-params "inverse")}} {{/if}}`
5163

5264
## References
5365

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)
66+
- [RFC](https://github.com/emberjs/rfcs/blob/master/text/0689-deprecate-has-block.md)
67+
- [Deprecation information](https://deprecations.emberjs.com/v3.x/#toc_has-block-and-has-block-params)

lib/rules/template-require-has-block-helper.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
1+
const TRANSFORMATIONS = {
2+
hasBlock: 'has-block',
3+
hasBlockParams: 'has-block-params',
4+
};
5+
6+
function getErrorMessage(name) {
7+
return `\`${name}\` is deprecated. Use the \`${TRANSFORMATIONS[name]}\` helper instead.`;
8+
}
9+
10+
function shouldWrapInSubExpression(node) {
11+
const parent = node.parent;
12+
13+
if (!parent) {
14+
return false;
15+
}
16+
17+
if (parent.type === 'GlimmerBlockStatement') {
18+
return true;
19+
}
20+
21+
if (
22+
(parent.type === 'GlimmerMustacheStatement' || parent.type === 'GlimmerSubExpression') &&
23+
parent.path !== node
24+
) {
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
131
/** @type {import('eslint').Rule.RuleModule} */
232
module.exports = {
333
meta: {
434
type: 'suggestion',
535
docs: {
636
description: 'require (has-block) helper usage instead of hasBlock property',
737
category: 'Best Practices',
38+
fixable: true,
839
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-has-block-helper.md',
9-
templateMode: 'loose',
40+
templateMode: 'both',
1041
},
11-
fixable: null,
42+
fixable: 'code',
1243
schema: [],
13-
messages: {
14-
useHasBlockHelper: 'Use (has-block) helper instead of hasBlock property.',
15-
},
1644
originallyFrom: {
1745
name: 'ember-template-lint',
1846
rule: 'lib/rules/require-has-block-helper.js',
@@ -24,15 +52,18 @@ module.exports = {
2452
create(context) {
2553
return {
2654
GlimmerPathExpression(node) {
27-
if (
28-
node.original === 'hasBlock' ||
29-
node.original === 'this.hasBlock' ||
30-
node.original === 'hasBlockParams' ||
31-
node.original === 'this.hasBlockParams'
32-
) {
55+
const replacement = TRANSFORMATIONS[node.original];
56+
57+
if (replacement) {
3358
context.report({
3459
node,
35-
messageId: 'useHasBlockHelper',
60+
message: getErrorMessage(node.original),
61+
fix(fixer) {
62+
return fixer.replaceTextRange(
63+
node.range,
64+
shouldWrapInSubExpression(node) ? `(${replacement})` : replacement
65+
);
66+
},
3667
});
3768
}
3869
},

0 commit comments

Comments
 (0)