-
Notifications
You must be signed in to change notification settings - Fork 885
fix no-duplicate-super validates with ternary operator #3544
fix no-duplicate-super validates with ternary operator #3544
Conversation
src/rules/noDuplicateSuperRule.ts
Outdated
@@ -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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
src/rules/noDuplicateSuperRule.ts
Outdated
@@ -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), getSuperForNode(whenFalse)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to also handle super()
calls inside the condition:
const inCondition = getSuperForNode(node.condition);
const inBranches = worse(getSuperForNode(whenTrue), getSuperForNode(whenFalse));
if (typeof inCondition !== "number" && typeof inBranches !== number) {
addDuplicateFailure(inCondition.node, inBranches.node);
}
return worse(inCondition, inBranches);
*code is untested and written only in GitHub comment editor.
@@ -259,5 +259,14 @@ declare const n: number; | |||
} | |||
} | |||
|
|||
// With ternary operator | |||
{ | |||
class { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thanks @mateuszwitkowski |
PR checklist
Overview of change:
Added case for ternary operator in no-duplicate-super rule.
CHANGELOG.md entry:
[bugfix] using ternary operator for calling super() now passes no-duplicate-super rule.