Skip to content

Commit

Permalink
Don't show when there is no else branch (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
xsburg committed Sep 14, 2019
1 parent a4700fc commit fa4c807
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/src/codemods/remove-redundant-else.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ codeMod.canRun = (fileInfo, api, options) => {
const target = options.target;
const node = target.firstNode();

if (!j.IfStatement.check(node)) {
if (!j.IfStatement.check(node) || !node.alternate) {
return false;
}

Expand Down
40 changes: 28 additions & 12 deletions server/tests/__codemod-fixtures__/remove-redundant-else.check.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ if (a) { /*# { pos: 2 } #*/

/*$ { fixture: 'should-trigger-at-else', expected: true } $*/

const a = 1;
{
const a = 1;

if (a) {
let b = 2;
} else { /*# { pos: 3 } #*/
if (a) {
let b = 2;
} else { /*# { pos: 7 } #*/
}
}

/*$ { fixture: 'should-trigger-when-if-returns', expected: true } $*/
Expand All @@ -29,18 +31,32 @@ function test() {

/*$ { fixture: 'should-not-trigger-else-not-empty', expected: false } $*/

const a = 1;
{
const a = 1;

if (a) { /*# { pos: 1 } #*/
let b = 2;
} else {
let c = 3;
if (a) { /*# { pos: 5 } #*/
let b = 2;
} else {
let c = 3;
}
}

/*$ { fixture: 'should-not-trigger-no-else', expected: false } $*/

const a = 1;
{
const a = 1;

if (a) { /*# { pos: 1 } #*/
let b = 2;
if (a) { /*# { pos: 5 } #*/
let b = 2;
}
}

/*$ { fixture: 'should-not-trigger-no-else-return', expected: false } $*/

function test2() {
const a = 1;

if (a) { /*# { pos: 5 } #*/
return 5;
}
}

0 comments on commit fa4c807

Please sign in to comment.