Skip to content

Commit

Permalink
fix no-duplicate-super validates with ternary operator (palantir#3544)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszwitkowski authored and ajafff committed Dec 5, 2017
1 parent 91f2d25 commit ee501e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/rules/noDuplicateSuperRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ function walk(ctx: Lint.WalkContext<void>): void {
? { node: node.parent! as ts.CallExpression, break: false }
: Kind.NoSuper;

case ts.SyntaxKind.ConditionalExpression: {
const { condition, whenTrue, whenFalse } = node as ts.ConditionalExpression;
const inCondition = getSuperForNode(condition);
const inBranches = worse(getSuperForNode(whenTrue), getSuperForNode(whenFalse));
if (typeof inCondition !== "number" && typeof inBranches !== "number") {
addDuplicateFailure(inCondition.node, inBranches.node);
}
return worse(inCondition, inBranches);
}

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

// Simple
{
class {
class A {
constructor() {
super();
~~~~~~~~
Expand All @@ -13,14 +13,14 @@ declare const n: number;
}
}

class {
class B {
constructor() {
super();
super.foo();
}
}

class {
class C1 {
constructor() {
super();

Expand All @@ -32,7 +32,7 @@ declare const n: number;
}
}

class {
class D {
constructor() {
super(super());
~~~~~~~~~~~~~ [0]
Expand All @@ -42,7 +42,7 @@ declare const n: number;

// If/else
{
class {
class A {
constructor() {
if (b) {
super();
Expand All @@ -52,7 +52,7 @@ declare const n: number;
}
}

class {
class B {
constructor() {
if (b) {
super();
Expand All @@ -62,7 +62,7 @@ declare const n: number;
}
}

class {
class C {
constructor() {
if (b) {
super();
Expand All @@ -77,7 +77,7 @@ declare const n: number;
}
}

class {
class D {
constructor() {
if (b) {
super();
Expand All @@ -93,7 +93,7 @@ declare const n: number;

// Loop
{
class {
class A {
constructor() {
while (b) {
if (b) {
Expand All @@ -104,7 +104,7 @@ declare const n: number;
}
}

class {
class B {
constructor() {
while (b) {
if (b) {
Expand All @@ -121,7 +121,7 @@ declare const n: number;
}
}

class {
class C {
constructor() {
while (b) {
if (b) {
Expand All @@ -136,7 +136,7 @@ declare const n: number;
}
}

class {
class D {
constructor() {
while (b) {
if (b) {
Expand All @@ -156,7 +156,7 @@ declare const n: number;
}
}

class {
class E {
constructor() {
while (b) {
if (b) {
Expand All @@ -182,7 +182,7 @@ declare const n: number;

// Switch
{
class {
class A {
constructor() {
switch (n) {
case 0:
Expand All @@ -196,7 +196,7 @@ declare const n: number;
}
}

class {
class B {
constructor() {
switch (n) {
case 0:
Expand All @@ -209,7 +209,7 @@ declare const n: number;
}
}

class {
class C {
constructor() {
switch (n) {
case 0:
Expand All @@ -230,7 +230,7 @@ declare const n: number;

// Wierd
{
class {
class A {
constructor() {
if (b) {
super();
Expand All @@ -242,7 +242,7 @@ declare const n: number;
}
}

class {
class B {
constructor() {
switch (n) {
case 0:
Expand All @@ -259,5 +259,22 @@ declare const n: number;
}
}

// With ternary operator
{
class A {
constructor(props?: any) {
props ? super(props) : super();
}
}

class B {
constructor(props?: any) {
props ? super(props) : super() ? super() : super();
~~~~~~~~~~~~~~~~~ [0]
}
}
}


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

0 comments on commit ee501e4

Please sign in to comment.