Skip to content

Commit d025282

Browse files
committed
Lints
1 parent 2f42fa1 commit d025282

26 files changed

Lines changed: 1865 additions & 104 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ rules in templates can be disabled with eslint directives with mustache or html
473473
| [order-in-models](docs/rules/order-in-models.md) | enforce proper order of properties in models | | 🔧 | |
474474
| [order-in-routes](docs/rules/order-in-routes.md) | enforce proper order of properties in routes | | 🔧 | |
475475
| [template-attribute-order](docs/rules/template-attribute-order.md) | enforce consistent ordering of attributes in template elements | | | |
476+
| [template-eol-last](docs/rules/template-eol-last.md) | require or disallow newline at the end of template files | | 🔧 | |
477+
| [template-linebreak-style](docs/rules/template-linebreak-style.md) | enforce consistent linebreaks in templates | | 🔧 | |
476478
| [template-modifier-name-case](docs/rules/template-modifier-name-case.md) | require dasherized names for modifiers | | 🔧 | |
477479
| [template-no-only-default-slot](docs/rules/template-no-only-default-slot.md) | disallow using only the default slot | | 🔧 | |
478480
| [template-quotes](docs/rules/template-quotes.md) | enforce consistent quote style in templates | | 🔧 | |

docs/rules/template-eol-last.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ember/template-eol-last
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Require or disallow newline at the end of template files.
8+
9+
## Rule Details
10+
11+
This rule enforces at least one newline (or no newline) at the end of template files.
12+
13+
## Config
14+
15+
This rule accepts a single string option:
16+
17+
- `"always"` (default) — enforces that template files end with a newline
18+
- `"never"` — enforces that template files do not end with a newline
19+
20+
## Examples
21+
22+
Examples of **incorrect** code with the default `"always"` config:
23+
24+
```hbs
25+
<div>test</div>
26+
```
27+
28+
Examples of **correct** code with the default `"always"` config:
29+
30+
```hbs
31+
<div>test</div>
32+
{{! newline at end of file }}
33+
```
34+
35+
Examples of **incorrect** code with the `"never"` config:
36+
37+
```hbs
38+
<div>test</div>
39+
{{! trailing newline not allowed }}
40+
```
41+
42+
Examples of **correct** code with the `"never"` config:
43+
44+
```hbs
45+
<div>test</div>
46+
```
47+
48+
## Related Rules
49+
50+
- [eol-last](https://eslint.org/docs/rules/eol-last) from eslint
51+
52+
## References
53+
54+
- [ember-template-lint eol-last](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/eol-last.md)
55+
- [POSIX standard/line](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206)
56+
- [Wikipedia/newline](https://en.wikipedia.org/wiki/Newline#Interpretation)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ember/template-linebreak-style
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Enforce consistent linebreaks in templates.
8+
9+
Having consistent linebreaks is important to make sure that the source code is rendered correctly in editors.
10+
11+
## Rule Details
12+
13+
This rule enforces consistent line endings in templates, independent of the operating system.
14+
15+
## Config
16+
17+
This rule accepts a single string option:
18+
19+
- `"unix"` (default) — enforces the usage of Unix line endings: `\n` for LF
20+
- `"windows"` — enforces the usage of Windows line endings: `\r\n` for CRLF
21+
- `"system"` — enforces the usage of the current platform's line ending
22+
23+
## Examples
24+
25+
Examples of **incorrect** code with the default `"unix"` config:
26+
27+
```hbs
28+
<div>test</div>\r\n
29+
```
30+
31+
Examples of **correct** code with the default `"unix"` config:
32+
33+
```hbs
34+
<div>test</div>\n
35+
```
36+
37+
Examples of **incorrect** code with the `"windows"` config:
38+
39+
```hbs
40+
<div>test</div>\n
41+
```
42+
43+
Examples of **correct** code with the `"windows"` config:
44+
45+
```hbs
46+
<div>test</div>\r\n
47+
```
48+
49+
## Related Rules
50+
51+
- [linebreak-style](https://eslint.org/docs/rules/linebreak-style) from eslint
52+
53+
## References
54+
55+
- [ember-template-lint linebreak-style](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/linebreak-style.md)
56+
- [Git/line endings](https://docs.github.com/en/github/using-git/configuring-git-to-handle-line-endings)

docs/rules/template-no-action.md

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,43 @@
66
77
Disallows the use of `{{action}}` helper.
88

9+
"Action" is an overloaded term in Ember. Actions are methods on the `actions` hash, element modifiers that set up event handlers, partial-applied functions, and something we both pass downward and send back up. They have served many different purposes over the years, which makes them difficult to learn, teach, and repurpose.
10+
11+
```hbs
12+
<button {{action 'increment' 5}}>Click</button>
13+
<button {{action this.increment 5}}>Click</button>
14+
<button onclick={{action 'increment' 5}}>Click</button>
15+
<button onclick={{action this.increment 5}}>Click</button>
16+
<button {{action (action 'increment' 5)}}>Click</button>
17+
<button {{action (action this.increment 5)}}>Click</button>
18+
```
19+
920
The `{{action}}` helper is deprecated in favor of the `{{on}}` modifier and `{{fn}}` helper, which provide better performance and clearer intent.
1021

1122
## Examples
1223

1324
Examples of **incorrect** code for this rule:
1425

26+
```hbs
27+
<button onclick={{action 'foo'}}></button>
28+
```
29+
30+
```hbs
31+
<button {{action 'submit'}}>Submit</button>
32+
```
33+
34+
```hbs
35+
<FooBar @baz={{action 'submit'}} />
36+
```
37+
38+
```hbs
39+
{{yield (action 'foo')}}
40+
```
41+
42+
```hbs
43+
{{yield (action this.foo)}}
44+
```
45+
1546
```hbs
1647
<button {{on 'click' (action 'save')}}>Save</button>
1748
```
@@ -22,6 +53,14 @@ Examples of **incorrect** code for this rule:
2253

2354
Examples of **correct** code for this rule:
2455

56+
```hbs
57+
<button {{on 'submit' @action}}>Click Me</button>
58+
```
59+
60+
```hbs
61+
<button {{on 'submit' this.action}}>Click Me</button>
62+
```
63+
2564
```hbs
2665
<button {{on 'click' this.save}}>Save</button>
2766
```
@@ -36,8 +75,33 @@ Examples of **correct** code for this rule:
3675

3776
## Migration
3877

39-
- Replace `(action "methodName")` with method references or `(fn this.methodName)`
40-
- Replace `<button onclick={{action ...}}>` with `<button {{on "click" ...}}>`
78+
```hbs
79+
<select onchange={{action this.updateSelection this.options}}>
80+
{{#each this.options as |opt|}}
81+
<option>{{opt.value}}</option>
82+
{{/each}}
83+
</select>
84+
```
85+
86+
to
87+
88+
```hbs
89+
<select {{on 'change' (fn this.updateSelection this.options)}}>
90+
{{#each this.options as |opt|}}
91+
<option>{{opt.value}}</option>
92+
{{/each}}
93+
</select>
94+
```
95+
96+
```hbs
97+
<MyComponent @onValidationChange={{action 'onDateValidation'}} />
98+
```
99+
100+
to
101+
102+
```hbs
103+
<MyComponent @onValidationChange={{this.onDateValidation}} />
104+
```
41105

42106
## Related Rules
43107

@@ -47,6 +111,8 @@ Examples of **correct** code for this rule:
47111

48112
## References
49113

50-
- [eslint-plugin-ember template-no-action](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-action.md)
114+
- [Ember Octane Update: What's up with `@action`?](https://www.pzuraq.com/blog/ember-octane-update-action/)
115+
- [RFC-471 `on` modifier](https://github.com/emberjs/rfcs/blob/master/text/0471-on-modifier.md)
116+
- [RFC-470 `fn` helper](https://github.com/emberjs/rfcs/blob/master/text/0470-fn-helper.md)
51117
- [Ember.js Deprecations - action helper](https://deprecations.emberjs.com/v3.x/#toc_action-helper)
52118
- [Ember Modifier Documentation](https://guides.emberjs.com/release/components/template-lifecycle-dom-and-modifiers/)

docs/rules/template-no-implicit-this.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
77
Require explicit `this` for property access in templates to avoid ambiguity.
88

9-
## Rule Details
10-
11-
This rule requires explicitly using `this.` prefix for component properties and `@` prefix for named arguments in templates, avoiding ambiguous property references.
9+
This rule aides in the migration path for [emberjs/rfcs#308](https://github.com/emberjs/rfcs/pull/308).
1210

1311
## Motivation
1412

@@ -21,6 +19,78 @@ ambiguous, as it could be referring to a local variable (block param), a helper
2119
with no arguments, a closed over component, or a property on the component
2220
class.
2321

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+
2494
## Examples
2595

2696
Examples of **incorrect** code for this rule:
@@ -47,5 +117,7 @@ Examples of **correct** code for this rule:
47117

48118
## References
49119

120+
- [Glimmer components](https://guides.emberjs.com/release/upgrading/current-edition/glimmer-components/)
121+
- [RFC#0276 - Named Args](https://github.com/emberjs/rfcs/blob/master/text/0276-named-args.md#motivation)
122+
- [RFC#308 - Deprecate implicit this](https://github.com/emberjs/rfcs/blob/master/text/0308-deprecate-property-lookup-fallback.md)
50123
- [Ember Octane Guide - Templates](https://guides.emberjs.com/release/components/component-state-and-actions/)
51-
- [RFC #308 - Deprecate implicit this](https://github.com/emberjs/rfcs/blob/master/text/0308-deprecate-property-lookup-fallback.md)

0 commit comments

Comments
 (0)