Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

fix no-duplicate-super validates with ternary operator #3544

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/rules/noDuplicateSuperRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function walk(ctx: Lint.WalkContext<void>): void {
? { node: node.parent! as ts.CallExpression, break: false }
: Kind.NoSuper;

case ts.SyntaxKind.ConditionalExpression: {
const { whenTrue, whenFalse } = node as ts.ConditionalExpression;
return worse(getSuperForNode(whenTrue), whenFalse !== undefined ? getSuperForNode(whenFalse) : Kind.NoSuper);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whenFalse can't be undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right of course. I'll fix it. There's another issue though. If someone would use nested ternary operators super() will be called twice:

	constructor(props?: any) {
        props ? super(props) : super() ? super() : super(); // if no props passed super() is called twice
	}
}

Do you think such a case should be covered? If so can you give me a tip how to handle it?

}

case ts.SyntaxKind.IfStatement: {
const { thenStatement, elseStatement } = node as ts.IfStatement;
return worse(getSuperForNode(thenStatement), elseStatement !== undefined ? getSuperForNode(elseStatement) : Kind.NoSuper);
Expand Down
9 changes: 9 additions & 0 deletions test/rules/no-duplicate-super/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,14 @@ declare const n: number;
}
}

// With ternary operator
{
class {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw. this is not a valid class declaration. Class declarations need a name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it but that's how it's done in all the other test cases for that rule. I've figured out there's some reason for it and followed it. I'll fix it in all test cases with updated PR.

constructor(props?: any) {
props ? super(props) : super();
}
}
}

[0]: Multiple calls to 'super()' found. It must be called only once.
[1]: 'super()' called in a loop. It must be called only once.