From 65d10819acd9698ed9a20b9a172a14b77c2e458f Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Sun, 27 Sep 2020 18:11:55 -0400 Subject: [PATCH] fix: do not pass ...arguments in autofix for attrs hooks in require-super-in-init rule --- lib/rules/require-super-in-init.js | 9 +++++++-- tests/lib/rules/require-super-in-init.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/rules/require-super-in-init.js b/lib/rules/require-super-in-init.js index ff83dd63ea..4e600c738d 100644 --- a/lib/rules/require-super-in-init.js +++ b/lib/rules/require-super-in-init.js @@ -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); diff --git a/tests/lib/rules/require-super-in-init.js b/tests/lib/rules/require-super-in-init.js index 8c6d0a639d..339fa07734 100644 --- a/tests/lib/rules/require-super-in-init.js +++ b/tests/lib/rules/require-super-in-init.js @@ -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';