fix(tsetse): avoid double type discover in failure condition for ban-expect-truthy-promise rule
Also optimize must-use-promises rule via removal of unneeded type/node checks
Closes #372
PiperOrigin-RevId: 231994937
diff --git a/internal/tsetse/rules/ban_expect_truthy_promise_rule.ts b/internal/tsetse/rules/ban_expect_truthy_promise_rule.ts
index fbbefd9..d1e79c0 100644
--- a/internal/tsetse/rules/ban_expect_truthy_promise_rule.ts
+++ b/internal/tsetse/rules/ban_expect_truthy_promise_rule.ts
@@ -56,11 +56,11 @@
return;
}
- if (!tsutils.isThenableType(tc, expectCallNode.arguments[0])) {
+ const argType = tc.getTypeAtLocation(expectCallNode.arguments[0]);
+ if (!tsutils.isThenableType(tc, expectCallNode.arguments[0], argType)) {
return;
}
- const argType = tc.getTypeAtLocation(expectCallNode.arguments[0]);
checker.addFailureAtNode(
node,
`Value passed to expect() is of type ${tc.typeToString(argType)}, which` +
diff --git a/internal/tsetse/rules/must_use_promises_rule.ts b/internal/tsetse/rules/must_use_promises_rule.ts
index 5c39fe5..7e4905a 100644
--- a/internal/tsetse/rules/must_use_promises_rule.ts
+++ b/internal/tsetse/rules/must_use_promises_rule.ts
@@ -30,30 +30,29 @@
return;
}
- const signature = checker.typeChecker.getResolvedSignature(node);
- if (signature === undefined) {
- return;
- }
-
- const returnType = checker.typeChecker.getReturnTypeOfSignature(signature);
- if (!!(returnType.flags & ts.TypeFlags.Void)) {
- return;
- }
-
if (tsutils.isThenableType(checker.typeChecker, node)) {
checker.addFailureAtNode(node, FAILURE_STRING);
}
}
function inAsyncFunction(node: ts.Node): boolean {
- const isFunction = tsutils.isFunctionDeclaration(node) ||
- tsutils.isArrowFunction(node) || tsutils.isMethodDeclaration(node) ||
- tsutils.isFunctionExpression(node);
- if (isFunction) {
- return tsutils.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword);
+ for (let inode = node.parent; inode !== undefined; inode = inode.parent) {
+ switch (inode.kind) {
+ case ts.SyntaxKind.ArrowFunction:
+ case ts.SyntaxKind.FunctionDeclaration:
+ case ts.SyntaxKind.FunctionExpression:
+ case ts.SyntaxKind.MethodDeclaration:
+ // Potentially async
+ return tsutils.hasModifier(inode.modifiers, ts.SyntaxKind.AsyncKeyword);
+ case ts.SyntaxKind.GetAccessor:
+ case ts.SyntaxKind.SetAccessor:
+ // These cannot be async
+ return false;
+ default:
+ // Loop and check parent
+ break;
+ }
}
- if (node.parent) {
- return inAsyncFunction(node.parent);
- }
+
return false;
}