Debian Codegraph @

jridgewell-source-map/src/source-map.ts

raw
1 import { AnyMap, originalPositionFor } from '@jridgewell/trace-mapping';
2 import {
3 GenMapping,
4 maybeAddMapping,
5 toDecodedMap,
6 toEncodedMap,
7 setSourceContent,
8 } from '@jridgewell/gen-mapping';
9
10 import type { TraceMap, SectionedSourceMapInput } from '@jridgewell/trace-mapping';
11 export type { TraceMap, SectionedSourceMapInput };
12
13 import type { Mapping, EncodedSourceMap, DecodedSourceMap } from '@jridgewell/gen-mapping';
14 export type { Mapping, EncodedSourceMap, DecodedSourceMap };
15
16 export class SourceMapConsumer {
17 private declare _map: TraceMap;
18 declare file: TraceMap['file'];
19 declare names: TraceMap['names'];
20 declare sourceRoot: TraceMap['sourceRoot'];
21 declare sources: TraceMap['sources'];
22 declare sourcesContent: TraceMap['sourcesContent'];
23
24 constructor(map: ConstructorParameters<typeof AnyMap>[0], mapUrl: Parameters<typeof AnyMap>[1]) {
25 const trace = (this._map = new AnyMap(map, mapUrl));
26
27 this.file = trace.file;
28 this.names = trace.names;
29 this.sourceRoot = trace.sourceRoot;
30 this.sources = trace.resolvedSources;
31 this.sourcesContent = trace.sourcesContent;
32 }
33
34 originalPositionFor(
35 needle: Parameters<typeof originalPositionFor>[1],
36 ): ReturnType<typeof originalPositionFor> {
37 return originalPositionFor(this._map, needle);
38 }
39
40 destroy() {
41 // noop.
42 }
43 }
44
45 export class SourceMapGenerator {
46 private declare _map: GenMapping;
47
48 constructor(opts: ConstructorParameters<typeof GenMapping>[0]) {
49 this._map = new GenMapping(opts);
50 }
51
52 addMapping(mapping: Parameters<typeof maybeAddMapping>[1]): ReturnType<typeof maybeAddMapping> {
53 maybeAddMapping(this._map, mapping);
54 }
55
56 setSourceContent(
57 source: Parameters<typeof setSourceContent>[1],
58 content: Parameters<typeof setSourceContent>[2],
59 ): ReturnType<typeof setSourceContent> {
60 setSourceContent(this._map, source, content);
61 }
62
63 toJSON(): ReturnType<typeof toEncodedMap> {
64 return toEncodedMap(this._map);
65 }
66
67 toDecodedMap(): ReturnType<typeof toDecodedMap> {
68 return toDecodedMap(this._map);
69 }
70 }