Skip to content

Commit

Permalink
Merge pull request #957 from bmish/require-super-in-init-all-hooks
Browse files Browse the repository at this point in the history
Add `checkInitOnly` (default true) and `checkNativeClasses` (default false) options to `require-super-in-init` rule
  • Loading branch information
bmish authored Sep 25, 2020
2 parents 9f04b3c + 2d8a97a commit b4157c8
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Rules are grouped by category to help you understand their purpose. Each rule ha
| :white_check_mark::wrench: | [no-get-with-default](./docs/rules/no-get-with-default.md) | disallow usage of the Ember's `getWithDefault` function |
| :white_check_mark::wrench: | [no-get](./docs/rules/no-get.md) | require using ES5 getters instead of Ember's `get` / `getProperties` functions |
| | [no-proxies](./docs/rules/no-proxies.md) | disallow using array or object proxies |
| :white_check_mark::wrench: | [require-super-in-init](./docs/rules/require-super-in-init.md) | require `this._super` to be called in `init` hooks |
| :white_check_mark::wrench: | [require-super-in-init](./docs/rules/require-super-in-init.md) | require super to be called in lifecycle hooks |
| :wrench: | [use-ember-get-and-set](./docs/rules/use-ember-get-and-set.md) | enforce usage of `Ember.get` and `Ember.set` |

### Ember Octane
Expand Down
68 changes: 66 additions & 2 deletions docs/rules/require-super-in-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

:wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

Call `_super` in init lifecycle hooks.
Call super in lifecycle hooks.

When overriding the `init` lifecycle hook inside Ember Components, Controllers, Routes or Mixins, it is necessary to include a call to `_super`.
When overriding lifecycle hooks inside Ember Components, Controllers, Routes, Mixins, or Services, it is necessary to include a call to super.

By default, this rule only applies to the `init` lifecycle hook and classic classes (see rule options to configure this).

## Examples

Expand All @@ -22,6 +24,28 @@ export default Component.extend({
});
```

```javascript
// With: checkInitOnly = false
import Component from '@ember/component';

export default Component.extend({
didInsertElement() {
// ...
}
});
```

```javascript
// With: checkNativeClasses = true
import Component from '@ember/component';

class Foo extends Component {
init() {
// ...
}
}
```

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

```javascript
Expand All @@ -34,3 +58,43 @@ export default Component.extend({
}
});
```

```javascript
import Component from '@ember/component';

export default Component.extend({
didInsertElement(...args) {
this._super(...args);
// ...
}
});
```

```javascript
import Component from '@ember/component';

class Foo extends Component {
init(...args) {
super.init(...args);
// ...
}
}
```

```javascript
import Component from '@ember/component';

class Foo extends Component {
didInsertElement(...args) {
super.didInsertElement(...args);
// ...
}
}
```

## Configuration

This rule takes an optional object containing:

* `boolean` -- `checkInitOnly` -- whether the rule should only check the `init` lifecycle hook and not other lifecycle hooks (default `true`, TODO: change default to `false` and rename rule to `require-super-in-lifecycle-hooks` in next major release)
* `boolean` -- `checkNativeClasses` -- whether the rule should check lifecycle hooks in native classes (in addition to classic classes) (default `false`, TODO: change default to `true` in next major release)
Loading

0 comments on commit b4157c8

Please sign in to comment.