forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceMapping.ts
More file actions
270 lines (228 loc) · 12.1 KB
/
Copy pathsourceMapping.ts
File metadata and controls
270 lines (228 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///<reference path='references.ts' />
module TypeScript {
export class SourceMapPosition {
public sourceLine: number;
public sourceColumn: number;
public emittedLine: number;
public emittedColumn: number;
}
export class SourceMapping {
public start = new SourceMapPosition();
public end = new SourceMapPosition();
public nameIndex: number = -1;
public childMappings: SourceMapping[] = [];
}
export class SourceMapEntry {
constructor(
public emittedFile: string,
public emittedLine: number,
public emittedColumn: number,
public sourceFile: string,
public sourceLine: number,
public sourceColumn: number,
public sourceName: string) {
Debug.assert(isFinite(emittedLine));
Debug.assert(isFinite(emittedColumn));
Debug.assert(isFinite(sourceColumn));
Debug.assert(isFinite(sourceLine));
}
}
export class SourceMapper {
static MapFileExtension = ".map";
private jsFileName: string;
private sourceMapPath: string;
private sourceMapDirectory: string;
private sourceRoot: string;
public names: string[] = [];
private mappingLevel: ISpan[] = [];
// Below two arrays represent the information about sourceFile at that index.
private tsFilePaths: string[] = [];
private allSourceMappings: SourceMapping[][] = [];
public currentMappings: SourceMapping[][];
public currentNameIndex: number[];
private sourceMapEntries: SourceMapEntry[] = [];
constructor(private jsFile: TextWriter,
private sourceMapOut: TextWriter,
document: Document,
jsFilePath: string,
emitOptions: EmitOptions,
resolvePath: (path: string) => string) {
this.setSourceMapOptions(document, jsFilePath, emitOptions, resolvePath);
this.setNewSourceFile(document, emitOptions);
}
public getOutputFile(): OutputFile {
var result = this.sourceMapOut.getOutputFile();
result.sourceMapEntries = this.sourceMapEntries;
return result;
}
public increaseMappingLevel(ast: ISpan) {
this.mappingLevel.push(ast);
}
public decreaseMappingLevel(ast: any) {
Debug.assert(this.mappingLevel.length > 0, "Mapping level should never be less than 0. This suggests a missing start call.");
var expectedAst = this.mappingLevel.pop();
if (ast !== expectedAst) {
var expectedAstInfo: any = (<any>expectedAst).kind ? SyntaxKind[(<any>expectedAst).kind] : [expectedAst.start(), expectedAst.end()];
var astInfo: any = (<any>ast).kind ? SyntaxKind[(<any>ast).kind] : [ast.start(), ast.end()]
Debug.fail(
"Provided ast is not the expected ISyntaxElement, Expected: " + expectedAstInfo + " Given: " + astInfo)
}
}
public setNewSourceFile(document: Document, emitOptions: EmitOptions) {
// Set new mappings
var sourceMappings: SourceMapping[] = [];
this.allSourceMappings.push(sourceMappings);
this.currentMappings = [sourceMappings];
this.currentNameIndex = [];
// Set new source file path
this.setNewSourceFilePath(document, emitOptions);
}
private setSourceMapOptions(document: Document, jsFilePath: string, emitOptions: EmitOptions, resolvePath: (path: string) => string) {
// Decode mapRoot and sourceRoot
// Js File Name = pretty name of js file
var prettyJsFileName = TypeScript.getPrettyName(jsFilePath, false, true);
var prettyMapFileName = prettyJsFileName + SourceMapper.MapFileExtension;
this.jsFileName = prettyJsFileName;
// Figure out sourceMapPath and sourceMapDirectory
if (emitOptions.sourceMapRootDirectory()) {
// Get the sourceMap Directory
this.sourceMapDirectory = emitOptions.sourceMapRootDirectory();
if (document.emitToOwnOutputFile()) {
// For modules or multiple emit files the mapRoot will have directory structure like the sources
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
this.sourceMapDirectory = this.sourceMapDirectory + switchToForwardSlashes(getRootFilePath((document.fileName)).replace(emitOptions.commonDirectoryPath(), ""));
}
if (isRelative(this.sourceMapDirectory)) {
// The relative paths are relative to the common directory
this.sourceMapDirectory = emitOptions.commonDirectoryPath() + this.sourceMapDirectory;
this.sourceMapDirectory = convertToDirectoryPath(switchToForwardSlashes(resolvePath(this.sourceMapDirectory)));
this.sourceMapPath = getRelativePathToFixedPath(getRootFilePath(jsFilePath), this.sourceMapDirectory + prettyMapFileName);
}
else {
this.sourceMapPath = this.sourceMapDirectory + prettyMapFileName;
}
}
else {
this.sourceMapPath = prettyMapFileName;
this.sourceMapDirectory = getRootFilePath(jsFilePath);
}
this.sourceRoot = emitOptions.sourceRootDirectory();
}
private setNewSourceFilePath(document: Document, emitOptions: EmitOptions) {
var tsFilePath = switchToForwardSlashes(document.fileName);
if (emitOptions.sourceRootDirectory()) {
// Use the relative path corresponding to the common directory path
tsFilePath = getRelativePathToFixedPath(emitOptions.commonDirectoryPath(), tsFilePath);
}
else {
// Source locations relative to map file location
tsFilePath = getRelativePathToFixedPath(this.sourceMapDirectory, tsFilePath);
}
this.tsFilePaths.push(tsFilePath);
}
// Generate source mapping.
// Creating files can cause exceptions, they will be caught higher up in TypeScriptCompiler.emit
public emitSourceMapping(): void {
Debug.assert(
this.mappingLevel.length === 0,
"Mapping level is not 0. This suggest a missing end call. Value: " +
this.mappingLevel.map(item => ['Node of type', SyntaxKind[(<any>item).kind], 'at', item.start(), 'to', item.end()].join(' ')).join(', '));
// Output map file name into the js file
this.jsFile.WriteLine("//# sourceMappingURL=" + this.sourceMapPath);
// Now output map file
var mappingsString = "";
var prevEmittedColumn = 0;
var prevEmittedLine = 0;
var prevSourceColumn = 0;
var prevSourceLine = 0;
var prevSourceIndex = 0;
var prevNameIndex = 0;
var emitComma = false;
var recordedPosition: SourceMapPosition = null;
for (var sourceIndex = 0; sourceIndex < this.tsFilePaths.length; sourceIndex++) {
var recordSourceMapping = (mappedPosition: SourceMapPosition, nameIndex: number) => {
if (recordedPosition !== null &&
recordedPosition.emittedColumn === mappedPosition.emittedColumn &&
recordedPosition.emittedLine === mappedPosition.emittedLine) {
// This position is already recorded
return;
}
// Record this position
if (prevEmittedLine !== mappedPosition.emittedLine) {
while (prevEmittedLine < mappedPosition.emittedLine) {
prevEmittedColumn = 0;
mappingsString = mappingsString + ";";
prevEmittedLine++;
}
emitComma = false;
}
else if (emitComma) {
mappingsString = mappingsString + ",";
}
this.sourceMapEntries.push(new SourceMapEntry(
this.jsFileName,
mappedPosition.emittedLine + 1,
mappedPosition.emittedColumn + 1,
this.tsFilePaths[sourceIndex],
mappedPosition.sourceLine,
mappedPosition.sourceColumn + 1,
nameIndex >= 0 ? this.names[nameIndex] : undefined));
// 1. Relative Column
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.emittedColumn - prevEmittedColumn);
prevEmittedColumn = mappedPosition.emittedColumn;
// 2. Relative sourceIndex
mappingsString = mappingsString + Base64VLQFormat.encode(sourceIndex - prevSourceIndex);
prevSourceIndex = sourceIndex;
// 3. Relative sourceLine 0 based
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.sourceLine - 1 - prevSourceLine);
prevSourceLine = mappedPosition.sourceLine - 1;
// 4. Relative sourceColumn 0 based
mappingsString = mappingsString + Base64VLQFormat.encode(mappedPosition.sourceColumn - prevSourceColumn);
prevSourceColumn = mappedPosition.sourceColumn;
// 5. Relative namePosition 0 based
if (nameIndex >= 0) {
mappingsString = mappingsString + Base64VLQFormat.encode(nameIndex - prevNameIndex);
prevNameIndex = nameIndex;
}
emitComma = true;
recordedPosition = mappedPosition;
};
// Record starting spans
var recordSourceMappingSiblings = (sourceMappings: SourceMapping[]) => {
for (var i = 0; i < sourceMappings.length; i++) {
var sourceMapping = sourceMappings[i];
recordSourceMapping(sourceMapping.start, sourceMapping.nameIndex);
recordSourceMappingSiblings(sourceMapping.childMappings);
recordSourceMapping(sourceMapping.end, sourceMapping.nameIndex);
}
};
recordSourceMappingSiblings(this.allSourceMappings[sourceIndex]);
}
// Write the actual map file
this.sourceMapOut.Write(JSON.stringify({
version: 3,
file: this.jsFileName,
sourceRoot: this.sourceRoot,
sources: this.tsFilePaths,
names: this.names,
mappings: mappingsString
}));
// Closing files could result in exceptions, report them if they occur
this.sourceMapOut.Close();
}
}
}