diff --git a/lib/rules/no-component-lifecycle-hooks.js b/lib/rules/no-component-lifecycle-hooks.js index bc0a287ff0..67a9f00113 100644 --- a/lib/rules/no-component-lifecycle-hooks.js +++ b/lib/rules/no-component-lifecycle-hooks.js @@ -36,6 +36,7 @@ module.exports = { let isInsideEmberComponent = false; let isInsideGlimmerComponent = false; let currentComponentNode = null; + let isInsideClassDeclaration = false; return { // Native class. @@ -43,15 +44,17 @@ module.exports = { if (isEmberComponent(context, node)) { currentComponentNode = node; isInsideEmberComponent = true; + isInsideClassDeclaration = true; } else if (isGlimmerComponent(context, node)) { currentComponentNode = node; isInsideGlimmerComponent = true; + isInsideClassDeclaration = true; } }, // Classic class (not used by Glimmer components). CallExpression(node) { - if (isEmberComponent(context, node)) { + if (isEmberComponent(context, node) && !isInsideClassDeclaration) { currentComponentNode = node; isInsideEmberComponent = true; } @@ -62,11 +65,12 @@ module.exports = { currentComponentNode = null; isInsideEmberComponent = false; isInsideGlimmerComponent = false; + isInsideClassDeclaration = false; } }, 'CallExpression:exit'(node) { - if (currentComponentNode === node) { + if (currentComponentNode === node && !isInsideClassDeclaration) { currentComponentNode = null; isInsideEmberComponent = false; } diff --git a/tests/lib/rules/no-component-lifecycle-hooks.js b/tests/lib/rules/no-component-lifecycle-hooks.js index 7ea99b0351..a9b83fd956 100644 --- a/tests/lib/rules/no-component-lifecycle-hooks.js +++ b/tests/lib/rules/no-component-lifecycle-hooks.js @@ -87,6 +87,16 @@ ruleTester.run('no-component-lifecycle-hooks', rule, { const someRandomClassOrObject = { didDestroyElement() { } }; `, + ` + import Component from '@ember/component'; + + export default class CommentInput extends Component.extend() { + } + + export class CommentInput2 { + didDestroyElement() {} + } + `, ], invalid: [ @@ -167,5 +177,21 @@ ruleTester.run('no-component-lifecycle-hooks', rule, { }, ], }, + { + code: ` + import Component from '@ember/component'; + + export default class CommentInput extends Component.extend() { + didDestroyElement() {} + } + `, + output: null, + errors: [ + { + message: ERROR_MESSAGE, + type: 'MethodDefinition', + }, + ], + }, ], });