Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
| [template-no-block-params-for-html-elements](docs/rules/template-no-block-params-for-html-elements.md) | disallow block params on HTML elements | | | |
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

Expand Down
64 changes: 64 additions & 0 deletions docs/rules/template-no-chained-this.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ember/template-no-chained-this

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

Disallow chained property access on `this`.

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.

## Rule Details

This rule disallows chaining property access on `this` in templates (e.g., `this.foo.bar`).

## Examples

### Incorrect ❌

```gjs
<template>
{{this.user.name}}
</template>
```

```gjs
<template>
{{this.model.user.firstName}}
</template>
```

```gjs
<template>
<div>{{this.data.items.length}}</div>
</template>
```

### Correct ✅

```gjs
<template>
{{this.userName}}
</template>
```

```gjs
<template>
{{get this.user "name"}}
</template>
```

```gjs
<template>
{{userName}}
</template>
```

## Related Rules

- [template-no-implicit-this](./template-no-implicit-this.md)

## References

- [Ember Best Practices - Component Design](https://guides.emberjs.com/release/components/)
- [eslint-plugin-ember template-no-this-in-template-only-components](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-this-in-template-only-components.md)
53 changes: 53 additions & 0 deletions lib/rules/template-no-chained-this.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow redundant `this.this` in templates',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-chained-this.md',
},
fixable: 'code',
schema: [],
messages: {
noChainedThis:
'this.this.* is not allowed in templates. This is likely a mistake — remove the redundant this.',
},
},

create(context) {
const sourceCode = context.sourceCode;

return {
GlimmerPathExpression(node) {
const text = sourceCode.getText(node);
if (text.startsWith('this.this.')) {
// Don't autofix if this PathExpression is the path of a BlockStatement,
// because the closing tag wouldn't be updated and the template would break.
const isBlockPath =
node.parent?.type === 'GlimmerBlockStatement' && node.parent.path === node;

context.report({
node,
messageId: 'noChainedThis',
fix: isBlockPath
? undefined
: (fixer) => {
return fixer.replaceText(node, text.replace('this.this.', 'this.'));
},
});
}
},
GlimmerElementNode(node) {
if (node.tag?.startsWith('this.this.')) {
context.report({
node,
messageId: 'noChainedThis',
});
}
},
};
},
};
53 changes: 53 additions & 0 deletions tests/lib/rules/template-no-chained-this.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const rule = require('../../../lib/rules/template-no-chained-this');
const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-chained-this', rule, {
valid: [
'<template>{{this.value}}</template>',
'<template>{{this.thisvalue}}</template>',
'<template>{{@argName}}</template>',
'<template>{{this.user.name}}</template>',
'<template><this.Component /></template>',
'<template>{{component this.dynamicComponent}}</template>',
],

invalid: [
{
code: '<template>{{this.this.value}}</template>',
output: '<template>{{this.value}}</template>',
errors: [{ messageId: 'noChainedThis' }],
},
{
code: '<template>{{helper value=this.this.foo}}</template>',
output: '<template>{{helper value=this.foo}}</template>',
errors: [{ messageId: 'noChainedThis' }],
},
{
code: '<template>{{#if this.this.condition}}true{{/if}}</template>',
output: '<template>{{#if this.condition}}true{{/if}}</template>',
errors: [{ messageId: 'noChainedThis' }],
},
{
code: '<template><this.this.Component /></template>',
output: null,
errors: [{ messageId: 'noChainedThis' }],
},

// Test cases ported from ember-template-lint
{
code: '<template>{{#this.this.value}}woo{{/this.this.value}}</template>',
output: null,
errors: [{ messageId: 'noChainedThis' }],
},
{
code: '<template>{{component this.this.dynamicComponent}}</template>',
output: '<template>{{component this.dynamicComponent}}</template>',
errors: [{ messageId: 'noChainedThis' }],
},
],
});
Loading