Skip to content

Commit f0e7547

Browse files
committed
Extract rule: template-no-bare-yield
1 parent 3e664f5 commit f0e7547

4 files changed

Lines changed: 108 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-bare-yield](docs/rules/template-no-bare-yield.md) | disallow {{yield}} without parameters outside of contextual components | | | |
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ember/template-no-bare-yield
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallow `{{yield}}` without parameters outside of contextual components.
6+
7+
## Rule Details
8+
9+
This rule enforces passing parameters to `{{yield}}` to make component APIs more explicit.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
{{yield}}
18+
</template>
19+
```
20+
21+
Examples of **correct** code for this rule:
22+
23+
```gjs
24+
<template>
25+
{{yield this}}
26+
</template>
27+
28+
<template>
29+
{{yield @model}}
30+
</template>
31+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 (node.path.type === 'GlimmerPathExpression' && node.path.original === 'yield') {
22+
if (!node.params || node.params.length === 0) {
23+
context.report({
24+
node,
25+
messageId: 'noBareYield',
26+
});
27+
}
28+
}
29+
},
30+
};
31+
},
32+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// yield with params inside a class
16+
`import Component from '@glimmer/component';
17+
class MyComponent extends Component {
18+
<template>{{yield this}}</template>
19+
}`,
20+
// yield with params inside a function
21+
`function myComponent() {
22+
return <template>{{yield this}}</template>;
23+
}`,
24+
'<template>{{yield @model}}</template>',
25+
'<template><div>Content</div></template>',
26+
// yield this at module level is allowed by this rule (template-no-unavailable-this handles the `this` part)
27+
'<template>{{yield this}}</template>',
28+
],
29+
invalid: [
30+
{
31+
code: '<template>{{yield}}</template>',
32+
output: null,
33+
errors: [{ messageId: 'noBareYield' }],
34+
},
35+
],
36+
});

0 commit comments

Comments
 (0)