Skip to content

Commit

Permalink
fix(rule): allow super lifecycle calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbuhmann committed Apr 25, 2018
1 parent e9f4d23 commit f862454
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/noLifeCycleCallRule.ts
Expand Up @@ -49,9 +49,13 @@ export class ExpressionCallMetadataWalker extends NgWalker {
}

private validateCallExpression(node: ts.CallExpression): void {
const name = (node.expression as any).name;
const name = ts.isPropertyAccessExpression(node.expression) ? node.expression.name : undefined;
const expression = ts.isPropertyAccessExpression(node.expression) ? node.expression.expression : undefined;

if (!name || !lifecycleHooksMethods.has(name.text)) {
const isSuperCall = expression && expression.kind === ts.SyntaxKind.SuperKeyword;
const isLifecycleCall = name && ts.isIdentifier(name) && lifecycleHooksMethods.has(name.text as any);

if (isSuperCall || !isLifecycleCall) {
return;
}

Expand Down
25 changes: 23 additions & 2 deletions test/noLifeCycleCallRule.spec.ts
Expand Up @@ -58,7 +58,7 @@ describe(ruleName, () => {
}
`;

it(`should fail when explicitly call ${lifecycleHookMethod}`, () => {
it(`should fail when explicitly calling ${lifecycleHookMethodCall}`, () => {
assertAnnotated({
ruleName,
message: `Avoid explicitly calls to lifecycle hooks in class "${className}"`,
Expand All @@ -85,7 +85,28 @@ describe(ruleName, () => {
}
`;

it(`should pass when call ${fakeMethod} method`, () => {
it(`should pass when calling ${fakeMethod} method`, () => {
assertSuccess(ruleName, source);
});
});
});
}

// call super lifecycle hook
for (const metadataKey of metadataKeys) {
describe(metadataKey, () => {
metadataPairs[metadataKey].forEach(lifecycleHookMethod => {
const lifecycleHookMethodCall = `super.${lifecycleHookMethod}()`;
const source = `
@${metadataKey}()
class ${className} implements ${lifecycleHookMethod.slice(2)} {
${lifecycleHookMethod}() {
${lifecycleHookMethodCall}
}
}
`;

it(`should pass when explicitly calling ${lifecycleHookMethodCall}`, () => {
assertSuccess(ruleName, source);
});
});
Expand Down

0 comments on commit f862454

Please sign in to comment.