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

Function prettyPrintAsSourceMap

valdi/vscode_debugger/src/common/sourceUtils.ts:13–45  ·  view source on GitHub ↗
(
  fileName: string,
  minified: string,
  compiledPath: string,
  sourceMapUrl: string,
)

Source from the content-addressed store, hash-verified

11import { LineColumn } from '../adapter/breakpoints/breakpointBase';
12
13export async function prettyPrintAsSourceMap(
14 fileName: string,
15 minified: string,
16 compiledPath: string,
17 sourceMapUrl: string,
18): Promise<SourceMap | undefined> {
19 const source = beautify(minified);
20 const from = generatePositions(source);
21 const to = generatePositions(minified);
22 if (from.length !== to.length) return Promise.resolve(undefined);
23
24 const generator = new sourceMap.SourceMapGenerator();
25 generator.setSourceContent(fileName, source);
26
27 // We know that AST for both sources is the same, so we can
28 // walk them together to generate mapping.
29 for (let i = 0; i < from.length; i += 2) {
30 generator.addMapping({
31 source: fileName,
32 original: { line: from[i], column: from[i + 1] },
33 generated: { line: to[i], column: to[i + 1] },
34 });
35 }
36 return new SourceMap(
37 await sourceMap.SourceMapConsumer.fromSourceMap(generator),
38 {
39 sourceMapUrl,
40 compiledPath,
41 },
42 '',
43 [fileName],
44 );
45}
46
47function generatePositions(text: string) {
48 const sourceFile = ts.createSourceFile(

Callers 1

prettyPrintMethod · 0.90

Calls 2

generatePositionsFunction · 0.85
resolveMethod · 0.65

Tested by

no test coverage detected