Skip to content

Commit

Permalink
Merge pull request #2200 from wagenet/fix-lifecycle-hook-in-double-ex…
Browse files Browse the repository at this point in the history
…tend

Fix no-component-lifecycle-hook in double extended classic component
  • Loading branch information
NullVoxPopuli authored Oct 25, 2024
2 parents 49ff30c + 4427396 commit cf00326
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rules/no-component-lifecycle-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ module.exports = {
let isInsideEmberComponent = false;
let isInsideGlimmerComponent = false;
let currentComponentNode = null;
let isInsideClassDeclaration = false;

return {
// Native class.
ClassDeclaration(node) {
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;
}
Expand All @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/no-component-lifecycle-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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',
},
],
},
],
});

0 comments on commit cf00326

Please sign in to comment.