Skip to content

Commit 9830b2e

Browse files
committed
Extract rule: template-no-bare-yield
1 parent 4847938 commit 9830b2e

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ rules in templates can be disabled with eslint directives with mustache or html
204204
| [template-no-action-on-submit-button](docs/rules/template-no-action-on-submit-button.md) | disallow action attribute on submit buttons | | | |
205205
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
206206
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
207+
| [template-no-bare-yield](docs/rules/template-no-bare-yield.md) | disallow {{yield}} without parameters outside of contextual components | | | |
207208
| [template-no-block-params-for-html-elements](docs/rules/template-no-block-params-for-html-elements.md) | disallow block params on HTML elements | | | |
208209
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
209210
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
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 (Object greeting="hello there")}}
26+
</template>
27+
28+
<template>
29+
{{yield @model}}
30+
</template>
31+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
templateMode: 'both',
10+
},
11+
schema: [],
12+
messages: {
13+
noBareYield: 'yield should have parameters or be used in contextual components only.',
14+
},
15+
},
16+
17+
create(context) {
18+
return {
19+
GlimmerMustacheStatement(node) {
20+
if (node.path.type === 'GlimmerPathExpression' && node.path.original === 'yield') {
21+
if (!node.params || node.params.length === 0) {
22+
context.report({
23+
node,
24+
messageId: 'noBareYield',
25+
});
26+
}
27+
}
28+
},
29+
};
30+
},
31+
};
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)