Skip to content

Commit de4f4d0

Browse files
authored
Merge pull request #136 from Xvezda/feature/tests
Support type narrowing for non-callable node
2 parents e17f1a3 + aac5ab6 commit de4f4d0

File tree

3 files changed

+364
-15
lines changed

3 files changed

+364
-15
lines changed

src/rules/no-undocumented-throws.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,23 @@ module.exports = createRule({
143143
const calleeDeclaration = getCalleeDeclaration(services, node);
144144
if (!calleeDeclaration) return;
145145

146-
const signature = getCallSignature(
147-
services,
148-
services.tsNodeToESTreeNodeMap.get(calleeDeclaration)
149-
);
146+
switch (node.type) {
147+
case AST_NODE_TYPES.CallExpression:
148+
case AST_NODE_TYPES.NewExpression: {
149+
if (services.tsNodeToESTreeNodeMap.has(calleeDeclaration)) {
150+
const signature = getCallSignature(
151+
services,
152+
services.tsNodeToESTreeNodeMap.get(calleeDeclaration)
153+
);
150154

151-
const returnType = signature?.getReturnType();
152-
if (returnType && isGeneratorLike(returnType)) return;
155+
const returnType = signature?.getReturnType();
156+
if (returnType && isGeneratorLike(returnType)) return;
157+
}
158+
break;
159+
}
160+
default:
161+
break;
162+
}
153163

154164
/** @type {import('typescript').JSDocThrowsTag[]} */
155165
const comments = [];

src/utils.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,14 @@ const getCalleeDeclaration = (services, node) => {
350350
const calleeNode = getCallee(node);
351351
if (!calleeNode) return null;
352352

353-
const symbol = services
354-
.getTypeAtLocation(calleeNode)
355-
.symbol;
353+
const type = services.getTypeAtLocation(calleeNode);
356354

357-
if (!symbol || !symbol.valueDeclaration) {
355+
if (type.symbol?.valueDeclaration) {
356+
declaration = type.symbol.valueDeclaration;
357+
} else if (type.symbol?.declarations?.length) {
358+
// If there are multiple declarations, we take the first one.
359+
declaration = type.symbol.declarations[0];
360+
} else {
358361
const declarations = services
359362
.getSymbolAtLocation(calleeNode)
360363
?.declarations;
@@ -363,8 +366,6 @@ const getCalleeDeclaration = (services, node) => {
363366

364367
// If there are multiple declarations, we take the first one.
365368
declaration = declarations[0];
366-
} else {
367-
declaration = symbol.valueDeclaration;
368369
}
369370
}
370371
if (!declaration) return null;

0 commit comments

Comments
 (0)