(code: string)
| 191 | * for other expression or invalid code. |
| 192 | */ |
| 193 | export 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 | |
| 214 | export function parseSourceMappingUrl(content: string): string | undefined { |
| 215 | if (!content) return; |
no test coverage detected