add extra nil checks and convert get{Test,Benchmark}Functions to async
Noticed this error appear in CI a few times. Even if this doesn't fix anything, it seems like a good change.
Change-Id: Ia3fdb4efa2f34b8370b095d9472c02b7a913723a
GitHub-Last-Rev: df7bd7aeeb28255f2e65650e706a09735e73008a
GitHub-Pull-Request: golang/vscode-go#82
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/234717
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/src/testUtils.ts b/src/testUtils.ts
index cfeb493..4bba6f2 100644
--- a/src/testUtils.ts
+++ b/src/testUtils.ts
@@ -122,24 +122,28 @@
* @param the URI of a Go source file.
* @return test function symbols for the source file.
*/
-export function getTestFunctions(
+export async function getTestFunctions(
doc: vscode.TextDocument,
token: vscode.CancellationToken
-): Thenable<vscode.DocumentSymbol[]> {
+): Promise<vscode.DocumentSymbol[] | undefined> {
const documentSymbolProvider = new GoDocumentSymbolProvider(true);
- return documentSymbolProvider
- .provideDocumentSymbols(doc, token)
- .then((symbols) => symbols[0].children)
- .then((symbols) => {
- const testify = symbols.some(
- (sym) => sym.kind === vscode.SymbolKind.Namespace && sym.name === '"github.com/stretchr/testify/suite"'
- );
- return symbols.filter(
- (sym) =>
- sym.kind === vscode.SymbolKind.Function &&
- (testFuncRegex.test(sym.name) || (testify && testMethodRegex.test(sym.name)))
- );
- });
+ const symbols = await documentSymbolProvider.provideDocumentSymbols(doc, token);
+ if (!symbols || symbols.length === 0) {
+ return;
+ }
+ const symbol = symbols[0];
+ if (!symbol) {
+ return;
+ }
+ const children = symbol.children;
+ const testify = children.some(
+ (sym) => sym.kind === vscode.SymbolKind.Namespace && sym.name === '"github.com/stretchr/testify/suite"'
+ );
+ return children.filter(
+ (sym) =>
+ sym.kind === vscode.SymbolKind.Function &&
+ (testFuncRegex.test(sym.name) || (testify && testMethodRegex.test(sym.name)))
+ );
}
/**
@@ -202,17 +206,21 @@
* @param the URI of a Go source file.
* @return benchmark function symbols for the source file.
*/
-export function getBenchmarkFunctions(
+export async function getBenchmarkFunctions(
doc: vscode.TextDocument,
token: vscode.CancellationToken
-): Thenable<vscode.DocumentSymbol[]> {
+): Promise<vscode.DocumentSymbol[] | undefined> {
const documentSymbolProvider = new GoDocumentSymbolProvider();
- return documentSymbolProvider
- .provideDocumentSymbols(doc, token)
- .then((symbols) => symbols[0].children)
- .then((symbols) =>
- symbols.filter((sym) => sym.kind === vscode.SymbolKind.Function && benchmarkRegex.test(sym.name))
- );
+ const symbols = await documentSymbolProvider.provideDocumentSymbols(doc, token);
+ if (!symbols || symbols.length === 0) {
+ return;
+ }
+ const symbol = symbols[0];
+ if (!symbol) {
+ return;
+ }
+ const children = symbol.children;
+ return children.filter((sym) => sym.kind === vscode.SymbolKind.Function && benchmarkRegex.test(sym.name));
}
/**