Skip to content

Commit dd91584

Browse files
committed
two-fer: detect unreachable stub throw after return
1 parent 3ae833a commit dd91584

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

src/analyzers/utils/extract_main_method.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,14 @@ function isNewExpression(node: unknown): node is TSESTree.NewExpression {
8989
)
9090
}
9191

92-
export function hasStubThrow(fn: { body?: TSESTree.Node }): boolean {
93-
if (!fn.body) return false
94-
if (fn.body.type !== 'BlockStatement') return false
95-
96-
const block = fn.body
97-
if (block.body.length !== 1) return false
98-
99-
const statement = block.body[0]
92+
function isStubThrowStatement(statement: TSESTree.Statement): boolean {
10093
if (statement.type !== 'ThrowStatement') return false
10194

102-
const argument: unknown = statement.argument
95+
const argument = statement.argument
10396
if (!isNewExpression(argument)) return false
10497

10598
const callee = argument.callee
106-
if (callee.type !== 'Identifier') return false
107-
if (callee.name !== 'Error') return false
99+
if (callee.type !== 'Identifier' || callee.name !== 'Error') return false
108100

109101
const [firstArg] = argument.arguments
110102
if (!firstArg || firstArg.type !== 'Literal') return false
@@ -117,3 +109,31 @@ export function hasStubThrow(fn: { body?: TSESTree.Node }): boolean {
117109
firstArg.value.includes('Remove this statement and implement')
118110
)
119111
}
112+
113+
114+
export function hasStubThrow(fn: { body?: TSESTree.Node }): boolean {
115+
if (!fn.body || fn.body.type !== 'BlockStatement') return false
116+
117+
const statements = fn.body.body
118+
119+
// Case 1: single-line stub throw
120+
if (statements.length === 1 && isStubThrowStatement(statements[0])) {
121+
return true
122+
}
123+
124+
// Case 2: unreachable stub throw after return
125+
for (let i = 0; i < statements.length - 1; i++) {
126+
const current = statements[i]
127+
const next = statements[i + 1]
128+
129+
if (
130+
current.type === 'ReturnStatement' &&
131+
isStubThrowStatement(next)
132+
) {
133+
return true
134+
}
135+
}
136+
137+
return false
138+
}
139+

0 commit comments

Comments
 (0)