Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,35 @@ doMore();`,
invalid: [],
});
});

it("valid: trailing hoisted function declaration is not a continuation (FP fix)", () => {
ruleTester.run("require-return-after-core-setfailed", requireReturnAfterCoreSetFailedRule, {
valid: [
// FunctionDeclaration is hoisted and has no sequential runtime effect
`core.setFailed("bad"); function helper() {}`,
],
invalid: [],
});
});

it("invalid: bare switch-case fall-through after setFailed is flagged (FN fix)", () => {
ruleTester.run("require-return-after-core-setfailed", requireReturnAfterCoreSetFailedRule, {
valid: [
// Terminating break prevents fall-through — must remain valid
`switch (x) { case 1: core.setFailed("bad"); break; case 2: doMore(); }`,
// Fall-through to a case that only contains hoisted declarations — no executable continuation
`switch (x) { case 1: core.setFailed("bad"); case 2: function helper() {} }`,
],
invalid: [
{
code: `switch (x) { case 1: core.setFailed("bad"); case 2: doMore(); }`,
errors: [{ messageId: "missingReturnAfterSetFailed" }],
},
{
code: `function f(x) { switch (x) { case 1: core.setFailed("bad"); case 2: doMore(); } }`,
errors: [{ messageId: "missingReturnAfterSetFailed", suggestions: [{ messageId: "addReturn", output: `function f(x) { switch (x) { case 1: core.setFailed("bad"); return; case 2: doMore(); } }` }] }],
},
],
});
});
});
18 changes: 15 additions & 3 deletions eslint-factory/src/rules/require-return-after-core-setfailed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function isExecutableStatement(node: TSESTree.ProgramStatement): node is TSESTre
node.type !== AST_NODE_TYPES.ExportAllDeclaration &&
node.type !== AST_NODE_TYPES.ExportDefaultDeclaration &&
node.type !== AST_NODE_TYPES.ExportNamedDeclaration &&
node.type !== AST_NODE_TYPES.TSModuleDeclaration
node.type !== AST_NODE_TYPES.TSModuleDeclaration &&
// Hoisted declarations have no sequential runtime effect — not a continuation
node.type !== AST_NODE_TYPES.FunctionDeclaration &&
node.type !== AST_NODE_TYPES.TSInterfaceDeclaration &&
node.type !== AST_NODE_TYPES.TSTypeAliasDeclaration
);
}

Expand Down Expand Up @@ -131,8 +135,10 @@ function findContinuationOutsideBlock(setFailedNode: TSESTree.Statement, ancesto
if (currentIndex >= 0) {
for (let nextCaseIndex = currentIndex + 1; nextCaseIndex < ancestor.cases.length; nextCaseIndex++) {
const nextCase = ancestor.cases[nextCaseIndex];
const nextStmt = nextCase.consequent[0];
if (nextStmt) {
// Skip hoisted declarations (FunctionDeclaration, etc.) — they have no sequential
// runtime effect and should not count as fall-through continuations.
const nextStmt = nextCase.consequent.find(s => isExecutableStatement(s as TSESTree.ProgramStatement));
if (nextStmt !== undefined) {
return nextStmt;
}
}
Expand Down Expand Up @@ -337,6 +343,12 @@ export const requireReturnAfterCoreSetFailedRule = createRule({
// The main case is BlockStatement above.
SwitchCase(node: TSESTree.SwitchCase) {
checkStatementList(node.consequent, isCoreSetFailedStatement, report);
// Fall-through: when setFailed is the last consequent statement with no terminator,
// execution falls through to the next case — same pattern as BlockStatement above.
const lastStmt = node.consequent[node.consequent.length - 1];
if (lastStmt && isCoreSetFailedStatement(lastStmt)) {
checkNestedContinuation(lastStmt);
Comment on lines +349 to +350
}
},
WhileStatement(node: TSESTree.WhileStatement) {
checkDirectControlBody(node.body);
Expand Down
Loading