MCPcopy Create free account
hub / github.com/Snapchat/Valdi / wrapObjectLiteral

Function wrapObjectLiteral

valdi/vscode_debugger/src/common/sourceUtils.ts:193–212  ·  view source on GitHub ↗
(code: string)

Source from the content-addressed store, hash-verified

191 * for other expression or invalid code.
192 */
193export function wrapObjectLiteral(code: string): string {
194 let src: ts.SourceFile;
195 try {
196 src = ts.createSourceFile('file.js', `return ${code};`, ts.ScriptTarget.ESNext, true);
197 } catch {
198 return code;
199 }
200 const returnStmt = src.statements[0];
201 if (!ts.isReturnStatement(returnStmt) || !returnStmt.expression) {
202 return code; // should never happen, maybe if there's a bizarre parse error
203 }
204
205 const diagnostics = ((src as unknown) as { parseDiagnostics?: ts.DiagnosticMessage[] })
206 .parseDiagnostics;
207 if (diagnostics?.some(d => d.category === ts.DiagnosticCategory.Error)) {
208 return code; // abort on parse errors
209 }
210
211 return ts.isObjectLiteralExpression(returnStmt.expression) ? `(${code})` : code;
212}
213
214export function parseSourceMappingUrl(content: string): string | undefined {
215 if (!content) return;

Callers 1

Calls 1

createSourceFileMethod · 0.45

Tested by

no test coverage detected