Skip to content

Commit f941a1c

Browse files
committed
Extract rule: template-no-chained-this
1 parent ffc4ad8 commit f941a1c

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ rules in templates can be disabled with eslint directives with mustache or html
185185
| Name | Description | 💼 | 🔧 | 💡 |
186186
| :----------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
187187
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
188+
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow chained property access on this | | | |
188189
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
189190
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
190191

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ember/template-no-chained-this
2+
3+
<!-- end auto-generated rule header -->
4+
5+
💼 This rule is enabled in the following [configs](https://github.com/ember-cli/eslint-plugin-ember#-configurations): `strict-gjs`, `strict-gts`.
6+
7+
Disallow chained property access on `this`.
8+
9+
Accessing deeply nested properties through `this` (like `this.user.name`) in templates makes components harder to refactor and test. It also creates tight coupling between the template and the component's internal structure. Use local variables or computed properties instead.
10+
11+
## Rule Details
12+
13+
This rule disallows chaining property access on `this` in templates (e.g., `this.foo.bar`).
14+
15+
## Examples
16+
17+
### Incorrect ❌
18+
19+
```gjs
20+
<template>
21+
{{this.user.name}}
22+
</template>
23+
```
24+
25+
```gjs
26+
<template>
27+
{{this.model.user.firstName}}
28+
</template>
29+
```
30+
31+
```gjs
32+
<template>
33+
<div>{{this.data.items.length}}</div>
34+
</template>
35+
```
36+
37+
### Correct ✅
38+
39+
```gjs
40+
<template>
41+
{{this.userName}}
42+
</template>
43+
```
44+
45+
```gjs
46+
<template>
47+
{{get this.user "name"}}
48+
</template>
49+
```
50+
51+
```gjs
52+
<template>
53+
{{userName}}
54+
</template>
55+
```
56+
57+
## Related Rules
58+
59+
- [template-no-implicit-this](./template-no-implicit-this.md)
60+
61+
## References
62+
63+
- [Ember Best Practices - Component Design](https://guides.emberjs.com/release/components/)
64+
- [eslint-plugin-ember: no-this-in-template-only-components](https://github.com/eslint-plugin-ember/eslint-plugin-ember/blob/master/docs/rule/no-this-in-template-only-components.md)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow chained property access on this',
7+
category: 'Best Practices',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-chained-this.md',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {
15+
noChainedThis:
16+
'Do not chain property access on this ({{path}}). Use local variables or getters instead.',
17+
},
18+
},
19+
20+
create(context) {
21+
return {
22+
GlimmerPathExpression(node) {
23+
// Check if this is a chained this path (this.foo.bar)
24+
if (node.head && node.head.type === 'ThisHead' && node.parts && node.parts.length > 1) {
25+
context.report({
26+
node,
27+
messageId: 'noChainedThis',
28+
data: {
29+
path: node.original,
30+
},
31+
});
32+
}
33+
},
34+
};
35+
},
36+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const rule = require('../../../lib/rules/template-no-chained-this');
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-chained-this', rule, {
10+
valid: [
11+
'<template>{{this.user}}</template>',
12+
'<template>{{userName}}</template>',
13+
'<template>{{@user}}</template>',
14+
'<template>{{get this.user "name"}}</template>',
15+
],
16+
17+
invalid: [
18+
{
19+
code: '<template>{{this.user.name}}</template>',
20+
output: null,
21+
errors: [
22+
{
23+
message:
24+
'Do not chain property access on this (this.user.name). Use local variables or getters instead.',
25+
type: 'GlimmerPathExpression',
26+
},
27+
],
28+
},
29+
{
30+
code: '<template>{{this.model.user.firstName}}</template>',
31+
output: null,
32+
errors: [
33+
{
34+
message:
35+
'Do not chain property access on this (this.model.user.firstName). Use local variables or getters instead.',
36+
type: 'GlimmerPathExpression',
37+
},
38+
],
39+
},
40+
{
41+
code: '<template><div>{{this.data.items.length}}</div></template>',
42+
output: null,
43+
errors: [
44+
{
45+
message:
46+
'Do not chain property access on this (this.data.items.length). Use local variables or getters instead.',
47+
type: 'GlimmerPathExpression',
48+
},
49+
],
50+
},
51+
],
52+
});

0 commit comments

Comments
 (0)