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

Method parse

src/valdi_modules/src/valdi/source_map/src/SourceMap.ts:123–222  ·  view source on GitHub ↗
(sourceMapJson: string)

Source from the content-addressed store, hash-verified

121 }
122
123 static parse(sourceMapJson: string): SourceMap {
124 const object = JSON.parse(sourceMapJson) as SourceMapJson;
125 if (object.version !== 3) {
126 throw new Error('SourceMap version must be 3');
127 }
128
129 if (!object.mappings) {
130 throw new Error('Missing mappings');
131 }
132
133 let sources = object.sources ?? [];
134 const names = object.names ?? [];
135
136 sources = sources.map(source => {
137 // Remove leading '/'
138 if (source.charAt(0) === '/') {
139 source = source.substr(1);
140 }
141
142 return source;
143 });
144
145 /**
146 Logic ported from https://github.com/microsoft/sourcemap-toolkit/blob/master/src/SourcemapToolkit.SourcemapParser/MappingListParser.cs
147 */
148
149 const lines: MappedLine[] = [];
150 const allMappings = object.mappings.split(';');
151
152 let generatedLineNumber = 0;
153 let generatedColumnNumber = 0;
154 let sourcesListIndex = 0;
155 let originalLineNumber = 0;
156 let originalColumnNumber = 0;
157 let namesListIndex = 0;
158
159 for (const mappings of allMappings) {
160 const segments = mappings.split(',');
161 const entries: MappedLineEntry[] = [];
162
163 for (const segment of segments) {
164 if (!segment) {
165 continue;
166 }
167
168 const fields = decode(segment);
169
170 if (fields.length !== 1 && fields.length !== 4 && fields.length !== 5) {
171 throw new Error(`Invalid mappings (found ${fields.length} fields in ${segment})`);
172 }
173
174 generatedColumnNumber += fields[0];
175
176 let originalFileName: string | undefined;
177
178 if (fields.length > 1) {
179 sourcesListIndex += fields[1];
180 originalLineNumber += fields[2];

Callers 4

parseFromBase64Method · 0.45
createSourceMapFunction · 0.45
_DateFunction · 0.45
FakeFunction · 0.45

Calls 5

decodeFunction · 0.90
substrMethod · 0.80
splitMethod · 0.80
mapMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected