Skip to content

Commit 6bd4adf

Browse files
committed
Extract rule: template-no-attrs-in-components
1 parent 688cf6b commit 6bd4adf

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ rules in templates can be disabled with eslint directives with mustache or html
251251
| [no-old-shims](docs/rules/no-old-shims.md) | disallow usage of old shims for modules || 🔧 | |
252252
| [no-string-prototype-extensions](docs/rules/no-string-prototype-extensions.md) | disallow usage of `String` prototype extensions || | |
253253
| [template-no-action](docs/rules/template-no-action.md) | disallow {{action}} helper | | | |
254+
| [template-no-attrs-in-components](docs/rules/template-no-attrs-in-components.md) | disallow attrs in component templates | | | |
254255

255256
### Ember Data
256257

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ember/template-no-attrs-in-components
2+
3+
<!-- end auto-generated rule header -->
4+
5+
This rule prevents the usage of `attrs` property to access values passed to the component since all the values can be accessed directly from the template.
6+
7+
## Examples
8+
9+
This rule **forbids** the following:
10+
11+
```hbs
12+
{{attr.foo}}
13+
```
14+
15+
This rule **allows** the following:
16+
17+
```hbs
18+
{{foo}}
19+
```
20+
21+
or if you using Ember 3.1 and above:
22+
23+
```hbs
24+
{{@foo}}
25+
```
26+
27+
## References
28+
29+
- [rfcs/named args](https://github.com/emberjs/rfcs/blob/master/text/0276-named-args.md#motivation)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow attrs in component templates',
7+
category: 'Deprecations',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-attrs-in-components.md',
11+
},
12+
schema: [],
13+
messages: {
14+
noAttrs: 'Component templates should not contain `attrs`.',
15+
},
16+
},
17+
create(context) {
18+
return {
19+
GlimmerPathExpression(node) {
20+
// Check if the path starts with "attrs"
21+
if (node.original?.startsWith('attrs.') || node.original === 'attrs') {
22+
context.report({ node, messageId: 'noAttrs' });
23+
}
24+
},
25+
};
26+
},
27+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const rule = require('../../../lib/rules/template-no-attrs-in-components');
2+
const RuleTester = require('eslint').RuleTester;
3+
4+
const ruleTester = new RuleTester({
5+
parser: require.resolve('ember-eslint-parser'),
6+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
7+
});
8+
9+
ruleTester.run('template-no-attrs-in-components', rule, {
10+
valid: ['<template>{{@value}}</template>', '<template>{{this.value}}</template>'],
11+
invalid: [
12+
{
13+
code: '<template>{{attrs.value}}</template>',
14+
output: null,
15+
errors: [{ messageId: 'noAttrs' }],
16+
},
17+
{
18+
code: '<template>{{attrs}}</template>',
19+
output: null,
20+
errors: [{ messageId: 'noAttrs' }],
21+
},
22+
],
23+
});

0 commit comments

Comments
 (0)