Skip to content

Commit 74a0112

Browse files
committed
Extract rule: template-no-bare-yield
1 parent 688cf6b commit 74a0112

4 files changed

Lines changed: 107 additions & 7 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,14 @@ rules in templates can be disabled with eslint directives with mustache or html
187187

188188
### Best Practices
189189

190-
| Name | Description | 💼 | 🔧 | 💡 |
191-
| :----------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
192-
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
193-
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
194-
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
195-
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
196-
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
190+
| Name                                    | Description | 💼 | 🔧 | 💡 |
191+
| :----------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------- | :- | :- | :- |
192+
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
193+
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
194+
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
195+
| [template-no-bare-yield](docs/rules/template-no-bare-yield.md) | disallow {{yield}} without parameters outside of contextual components | | | |
196+
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
197+
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
197198

198199
### Components
199200

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ember/template-no-bare-yield
2+
3+
<!-- end auto-generated rule header -->
4+
5+
✅ The `extends: 'plugin:ember/strict-gjs'` or `extends: 'plugin:ember/strict-gts'` property in a configuration file enables this rule.
6+
7+
Disallow `{{yield}}` without parameters outside of contextual components.
8+
9+
## Rule Details
10+
11+
This rule enforces passing parameters to `{{yield}}` to make component APIs more explicit.
12+
13+
## Examples
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```gjs
18+
<template>
19+
{{yield}}
20+
</template>
21+
```
22+
23+
Examples of **correct** code for this rule:
24+
25+
```gjs
26+
<template>
27+
{{yield this}}
28+
</template>
29+
30+
<template>
31+
{{yield @model}}
32+
</template>
33+
```
34+
35+
<!-- begin auto-generated rule meta list -->
36+
37+
- strictGjs: true
38+
- strictGts: true
39+
<!-- end auto-generated rule meta list -->
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'problem',
5+
docs: {
6+
description: 'disallow {{yield}} without parameters outside of contextual components',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-bare-yield.md',
9+
strictGjs: true,
10+
strictGts: true,
11+
},
12+
schema: [],
13+
messages: {
14+
noBareYield: 'yield should have parameters or be used in contextual components only.',
15+
},
16+
},
17+
18+
create(context) {
19+
return {
20+
GlimmerMustacheStatement(node) {
21+
if (
22+
node.path.type === 'GlimmerPathExpression' &&
23+
node.path.original === 'yield' &&
24+
(!node.params || node.params.length === 0)
25+
) {
26+
context.report({
27+
node,
28+
messageId: 'noBareYield',
29+
});
30+
}
31+
},
32+
};
33+
},
34+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-bare-yield');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
const ruleTester = new RuleTester({
9+
parser: require.resolve('ember-eslint-parser'),
10+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
11+
});
12+
13+
ruleTester.run('template-no-bare-yield', rule, {
14+
valid: [
15+
'<template>{{yield this}}</template>',
16+
'<template>{{yield @model}}</template>',
17+
'<template><div>Content</div></template>',
18+
],
19+
invalid: [
20+
{
21+
code: '<template>{{yield}}</template>',
22+
output: null,
23+
errors: [{ messageId: 'noBareYield' }],
24+
},
25+
],
26+
});

0 commit comments

Comments
 (0)