Skip to content

Commit

Permalink
Fix crash, false-positive in no-statement-after-end (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninevra authored Dec 23, 2020
1 parent 895d215 commit 2b04f84
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
27 changes: 23 additions & 4 deletions rules/no-statement-after-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ const create = context => {

let currentSegmentInfo;

function pathStart() {
if (currentSegmentInfo !== undefined) {
segmentInfoStack.push(currentSegmentInfo);
currentSegmentInfo = undefined;
}
}

function pathEnd() {
currentSegmentInfo = segmentInfoStack.pop();
}

function segmentStart(segment) {
// A new CodePathSegment has started, create an "info" object to track this segments state.
segmentInfoStack.push(currentSegmentInfo);

currentSegmentInfo = {
ended: false,
prev: segment.prevSegments.map(previousSegment => segmentInfoMap.get(previousSegment.id))
Expand All @@ -34,12 +43,14 @@ const create = context => {
}

function segmentEnd() {
currentSegmentInfo = segmentInfoStack.pop();
currentSegmentInfo = undefined;
}

function checkForEndExpression(node) {
if (isEndExpression(node)) {
currentSegmentInfo.ended = true;
if (currentSegmentInfo !== undefined) {
currentSegmentInfo.ended = true;
}
}
}

Expand All @@ -48,6 +59,12 @@ const create = context => {
return;
}

// If there is no current segment (this occurs in unreachable code), then we
// can't check whether `t.end()` was called
if (currentSegmentInfo === undefined) {
return;
}

const ended = [currentSegmentInfo]
.concat(currentSegmentInfo.prev)
.filter(info => info.ended);
Expand Down Expand Up @@ -85,6 +102,8 @@ const create = context => {
checkStatement(node);
}
},
onCodePathStart: pathStart,
onCodePathEnd: pathEnd,
onCodePathSegmentStart: segmentStart,
onCodePathSegmentEnd: segmentEnd,
CallExpression: checkForEndExpression
Expand Down
31 changes: 30 additions & 1 deletion test/no-statement-after-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ ruleTester.run('no-statement-after-end', rule, {
cbTest('return t.end();'),
cbTest('t.end(); return;'),
// Valid because it is not a test file (no header)
cbTest('t.end(); t.is(1, 1);', false)
cbTest('t.end(); t.is(1, 1);', false),
`
const test = require('ava');
throw new Error();
1;
`,
cbTest(`
function newCodePath() {
throw new Error('make some unreachable code');
t.end();
}
1;
`)
],
invalid: [
{
Expand All @@ -48,6 +63,20 @@ ruleTester.run('no-statement-after-end', rule, {
{
code: cbTest('if (t.context.a === 1) { t.end(); }\nt.is(1, 1); t.end();'),
errors
},
{
code: cbTest(`
function newCodePath() {
// ...
}
t.end();
1;
`),
errors
},
{
code: cbTest('t.end(); function newCodePath() {} 1;'),
errors
}
]
});

0 comments on commit 2b04f84

Please sign in to comment.