Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: playground error when returning a function declaration VSCODE-669 #925

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/language/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { parentPort } from 'worker_threads';
import { ServerCommands } from './serverCommands';
import type { Document } from 'bson';

import type {
ShellEvaluateResult,
Expand All @@ -17,7 +16,7 @@ interface EvaluationResult {
type: string | null;
}

const getContent = ({ type, printable }: EvaluationResult): Document => {
const getContent = ({ type, printable }: EvaluationResult): unknown => {
if (type === 'Cursor' || type === 'AggregationCursor') {
return getEJSON(printable.documents);
}
Expand All @@ -27,11 +26,7 @@ const getContent = ({ type, printable }: EvaluationResult): Document => {
: getEJSON(printable);
};

export const getLanguage = (
evaluationResult: EvaluationResult
): 'json' | 'plaintext' => {
const content = getContent(evaluationResult);

export const getLanguage = (content: unknown): 'json' | 'plaintext' => {
if (typeof content === 'object' && content !== null) {
return 'json';
}
Expand Down Expand Up @@ -105,12 +100,19 @@ export const execute = async ({
? `${source.namespace.db}.${source.namespace.collection}`
: undefined;

// The RPC protocol can't handle functions and it wouldn't make sense to return them anyway. Since just
// declaring a function doesn't execute it, the best thing we can do is return undefined, similarly to
// what we do when there's no return value from the script.
const rpcSafePrintable =
typeof printable !== 'function' ? printable : undefined;

// Prepare a playground result.
const content = getContent({ type, printable: rpcSafePrintable });
const result = {
namespace,
type: type ? type : typeof printable,
content: getContent({ type, printable }),
language: getLanguage({ type, printable }),
type: type ? type : typeof rpcSafePrintable,
content,
language: getLanguage(content),
};

return { data: { result } };
Expand Down