Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash, false-positive in no-statement-after-end #316

Merged
merged 4 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
}
]
});