Skip to content

Commit

Permalink
Merge pull request #962 from bmish/require-super-in-init-fix-attrs
Browse files Browse the repository at this point in the history
Do not pass `...arguments` in autofix for attrs hooks in `require-super-in-init` rule
  • Loading branch information
bmish authored Sep 27, 2020
2 parents f291f24 + 65d1081 commit 06282c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rules/require-super-in-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ module.exports = {
node,
message: ERROR_MESSAGE,
fix(fixer) {
// attrs hooks should call super without ...arguments to satisfy ember/no-attrs-snapshot rule.
const replacementArgs = ['didReceiveAttrs', 'didUpdateAttrs'].includes(lifecycleHookName)
? ''
: '...arguments';

const replacement = isNativeClass
? `super.${lifecycleHookName}(...arguments);`
: 'this._super(...arguments);';
? `super.${lifecycleHookName}(${replacementArgs});`
: `this._super(${replacementArgs});`;
// Insert right after function curly brace.
const sourceCode = context.getSourceCode();
const startOfBlockStatement = sourceCode.getFirstToken(node.value.body);
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/require-super-in-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,24 @@ this._super(...arguments);
errors: [{ message, line: 2 }],
},

// attrs hooks should call super without ...arguments to satisfy ember/no-attrs-snapshot rule.
{
code: 'Component({ didReceiveAttrs() {} })',
output: `Component({ didReceiveAttrs() {
this._super();} })`,
options: [{ checkInitOnly: false }],
errors: [{ message, line: 1 }],
},
{
code: `import Component from '@ember/component';
class Foo extends Component { didUpdateAttrs() {} }`,
output: `import Component from '@ember/component';
class Foo extends Component { didUpdateAttrs() {
super.didUpdateAttrs();} }`,
options: [{ checkNativeClasses: true, checkInitOnly: false }],
errors: [{ message, line: 2 }],
},

// Native classes:
{
code: `import Service from '@ember/service';
Expand Down

0 comments on commit 06282c4

Please sign in to comment.