|
| 1 | +# ember/template-no-implicit-this |
| 2 | + |
| 3 | +> **HBS Only**: This rule applies to classic `.hbs` template files only (loose mode). It is not relevant for `gjs`/`gts` files (strict mode), where these patterns cannot occur. |
| 4 | +
|
| 5 | +<!-- end auto-generated rule header --> |
| 6 | + |
| 7 | +Require explicit `this` for property access in templates to avoid ambiguity. |
| 8 | + |
| 9 | +This rule aides in the migration path for [emberjs/rfcs#308](https://github.com/emberjs/rfcs/pull/308). |
| 10 | + |
| 11 | +## Motivation |
| 12 | + |
| 13 | +Currently, the way to access properties on a components class is `{{greeting}}` |
| 14 | +from a template. This works because the component class is one of the objects |
| 15 | +we resolve against during the evaluation of the expression. |
| 16 | + |
| 17 | +The first problem with this approach is that the `{{greeting}}` syntax is |
| 18 | +ambiguous, as it could be referring to a local variable (block param), a helper |
| 19 | +with no arguments, a closed over component, or a property on the component |
| 20 | +class. |
| 21 | + |
| 22 | +Consider the following example where the ambiguity can cause issues: |
| 23 | + |
| 24 | +You have a component class that looks like the following component and template: |
| 25 | + |
| 26 | +```js |
| 27 | +import Component from '@ember/component'; |
| 28 | +import computed from '@ember/computed'; |
| 29 | + |
| 30 | +export default Component.extend({ |
| 31 | + formatName: computed('firstName', 'lastName', function () { |
| 32 | + return `${this.firstName} ${this.lastName}`; |
| 33 | + }), |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +```hbs |
| 38 | +<h1>Hello {{formatName}}!</h1> |
| 39 | +``` |
| 40 | + |
| 41 | +Given `{ firstName: 'Chad', lastName: 'Hietala' }`, Ember will render the |
| 42 | +following: |
| 43 | + |
| 44 | +```html |
| 45 | +<h1>Hello Chad Hietala!</h1> |
| 46 | +``` |
| 47 | + |
| 48 | +Now some time goes on and someone adds a `formatName` helper at |
| 49 | +`app/helpers/formatName.js` that looks like the following: |
| 50 | + |
| 51 | +```js |
| 52 | +export default function formatName([firstName, lastName]) { |
| 53 | + return `${firstName} ${lastName}`; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Due to the fact that helpers take precedence over property lookups, our |
| 58 | +`{{formatName}}` now resolves to a helper. When the helper runs it doesn't have |
| 59 | +any arguments so our template now renders the following: |
| 60 | + |
| 61 | +```html |
| 62 | +<h1>Hello !</h1> |
| 63 | +``` |
| 64 | + |
| 65 | +This can be a refactoring hazard and can often lead to confusion for readers of |
| 66 | +the template. Upon encountering `{{greeting}}` in a component's template, the |
| 67 | +reader has to check all of these places: first, you need to scan the |
| 68 | +surrounding lines for block params with that name; next, you check in the |
| 69 | +helpers folder to see if there is a helper with that name (it could also be |
| 70 | +coming from an addon!); finally, you check the component's JavaScript class to |
| 71 | +look for a (computed) property. |
| 72 | + |
| 73 | +Like |
| 74 | +[RFC#0276](https://github.com/emberjs/rfcs/blob/master/text/0276-named-args.md) |
| 75 | +made argument usage explicit through the `@` prefix, the `this` prefix will |
| 76 | +resolve the ambiguity and greatly improve clarity, especially in big projects |
| 77 | +with a lot of files (and uses a lot of addons). |
| 78 | + |
| 79 | +As an aside, the ambiguity that causes confusion for human readers is also a |
| 80 | +problem for the compiler. While it is not the main goal of this proposal, |
| 81 | +resolving this ambiguity also helps the rendering system. Currently, the |
| 82 | +"runtime" template compiler has to perform a helper lookup for every |
| 83 | +`{{greeting}}` in each template. It will be able to skip this resolution |
| 84 | +process and perform other optimizations (such as reusing the internal |
| 85 | +[reference](https://github.com/glimmerjs/glimmer-vm/blob/master/guides/04-references.md) |
| 86 | +object and caches) with this addition. |
| 87 | + |
| 88 | +Furthermore, by enforcing the `this` prefix, tooling like the [Ember Language |
| 89 | +Server](https://github.com/emberwatch/ember-language-server) does not need to |
| 90 | +know about fallback resolution rules. This makes common features like ["Go To |
| 91 | +Definition"](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition) |
| 92 | +much easier to implement since we have semantics that mean "property on class". |
| 93 | + |
| 94 | +## Examples |
| 95 | + |
| 96 | +Examples of **incorrect** code for this rule: |
| 97 | + |
| 98 | +```hbs |
| 99 | +{{property}} |
| 100 | +{{someValue}} |
| 101 | +``` |
| 102 | + |
| 103 | +Examples of **correct** code for this rule: |
| 104 | + |
| 105 | +```hbs |
| 106 | +{{this.property}} |
| 107 | +{{@namedArg}} |
| 108 | +{{yield}} |
| 109 | +{{if this.condition 'yes' 'no'}} |
| 110 | +``` |
| 111 | + |
| 112 | +## Options |
| 113 | + |
| 114 | +- object -- An object with the following keys: |
| 115 | + - `allow` -- An array of string names to allow as implicit this. Paths that exactly match an entry in this list will not be flagged. |
| 116 | + |
| 117 | +Example: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "ember/template-no-implicit-this": [ |
| 122 | + "error", |
| 123 | + { |
| 124 | + "allow": ["book-details"] |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Migration |
| 131 | + |
| 132 | +- use [ember-no-implicit-this-codemod](https://github.com/ember-codemods/ember-no-implicit-this-codemod) |
| 133 | +- [upgrade to Glimmer components](https://guides.emberjs.com/release/upgrading/current-edition/glimmer-components/), which don't allow ambiguous access |
| 134 | + - classic components have [auto-reflection](https://github.com/emberjs/rfcs/blob/master/text/0276-named-args.md#motivation), and can use `this.myArgName` or `this.args.myArgNme` or `@myArgName` interchangeably |
| 135 | + |
| 136 | +## References |
| 137 | + |
| 138 | +- [Glimmer components](https://guides.emberjs.com/release/upgrading/current-edition/glimmer-components/) |
| 139 | +- [RFC#0276 - Named Args](https://github.com/emberjs/rfcs/blob/master/text/0276-named-args.md#motivation) |
| 140 | +- [RFC#308 - Deprecate implicit this](https://github.com/emberjs/rfcs/blob/master/text/0308-deprecate-property-lookup-fallback.md) |
| 141 | +- [Ember Octane Guide - Templates](https://guides.emberjs.com/release/components/component-state-and-actions/) |
0 commit comments