Skip to content

Commit c0617e9

Browse files
authored
fix: playground error when returning a function declaration VSCODE-669 (#925)
1 parent 22d5337 commit c0617e9

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/language/worker.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver
22
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
33
import { parentPort } from 'worker_threads';
44
import { ServerCommands } from './serverCommands';
5-
import type { Document } from 'bson';
65

76
import type {
87
ShellEvaluateResult,
@@ -17,7 +16,7 @@ interface EvaluationResult {
1716
type: string | null;
1817
}
1918

20-
const getContent = ({ type, printable }: EvaluationResult): Document => {
19+
const getContent = ({ type, printable }: EvaluationResult): unknown => {
2120
if (type === 'Cursor' || type === 'AggregationCursor') {
2221
return getEJSON(printable.documents);
2322
}
@@ -27,11 +26,7 @@ const getContent = ({ type, printable }: EvaluationResult): Document => {
2726
: getEJSON(printable);
2827
};
2928

30-
export const getLanguage = (
31-
evaluationResult: EvaluationResult
32-
): 'json' | 'plaintext' => {
33-
const content = getContent(evaluationResult);
34-
29+
export const getLanguage = (content: unknown): 'json' | 'plaintext' => {
3530
if (typeof content === 'object' && content !== null) {
3631
return 'json';
3732
}
@@ -105,12 +100,19 @@ export const execute = async ({
105100
? `${source.namespace.db}.${source.namespace.collection}`
106101
: undefined;
107102

103+
// The RPC protocol can't handle functions and it wouldn't make sense to return them anyway. Since just
104+
// declaring a function doesn't execute it, the best thing we can do is return undefined, similarly to
105+
// what we do when there's no return value from the script.
106+
const rpcSafePrintable =
107+
typeof printable !== 'function' ? printable : undefined;
108+
108109
// Prepare a playground result.
110+
const content = getContent({ type, printable: rpcSafePrintable });
109111
const result = {
110112
namespace,
111-
type: type ? type : typeof printable,
112-
content: getContent({ type, printable }),
113-
language: getLanguage({ type, printable }),
113+
type: type ? type : typeof rpcSafePrintable,
114+
content,
115+
language: getLanguage(content),
114116
};
115117

116118
return { data: { result } };

0 commit comments

Comments
 (0)