forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnosticsParser.ts
More file actions
363 lines (274 loc) · 13.9 KB
/
Copy pathdiagnosticsParser.ts
File metadata and controls
363 lines (274 loc) · 13.9 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
///<reference path='..\compiler\io.ts'/>
///<reference path='..\compiler\typescript.ts'/>
///<reference path='..\..\samples\node\node.d.ts'/>
module DiagnosticsParser {
class FourSlashGenerator {
private fsFiles: { [s: string]: IFourSlashFile; };
constructor(private diagnosticsFile: string) {
this.fsFiles = {};
}
public generate(args: string[]): void {
if (IO.fileExists(this.diagnosticsFile)) {
this.parseDiagnostics(args);
if (args !== undefined) {
for (var i = 0; i < args.length; i++) {
for (var fsFile in this.fsFiles) {
var strippedScriptId = fsFile.replace(/.+\\/, '');
if (strippedScriptId === args[i]) {
this.writeFile(fsFile);
}
}
}
} else {
for (var fsFile in this.fsFiles) {
this.writeFile(fsFile);
}
}
}
}
private parseDiagnostics(args: string[]) {
var file: string = IO.readFile(this.diagnosticsFile);
var lines: string[] = file.split('\r\n');
var updateMode: boolean;
var scriptId: string;
var editBlock: string;
var editRange: TypeScript.ScriptEditRange;
var startPosition: number;
var caretPosition: number;
var collapsingBlock: string;
var openEditTag = /^.*<Edit>/;
var closeEditTag = /<Edit\/>.*/;
// Loops through the lines of the diagnostics file
for (var i = 0; i < lines.length; i++) {
// Indicates the user opening a file
if (lines[i].match(/\/\/=New=\\\\/)) {
updateMode = false;
// Indicates the user making updates to an opened file
} else if (lines[i].match(/\/\/=Update=\\\\/)) {
updateMode = true;
// Record the filePath of the opened/modified file
} else if (lines[i].match(/^scriptId: /)) {
var newScriptId = lines[i].replace(/^scriptId: /, '');
if (scriptId !== undefined && newScriptId !== scriptId && collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
startPosition = undefined;
caretPosition = undefined;
collapsingBlock = undefined
}
scriptId = newScriptId;
// Records the editRange (minChar, limChar, deltaOfCaret)
} else if (lines[i].match(/^editRange\(/)) {
//Capture numbers in editRange(minChar=###, limChar=###, delta=###)
var match = /editRange\(minChar=([0-9]+), limChar=([0-9]+), delta=(-?[0-9]+)\)/.exec(lines[i]);
if (match !== null) {
editRange = new TypeScript.ScriptEditRange(parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10));
}
// Indicates the beginning of added text and records
} else if (lines[i].match(openEditTag)) {
var editLines: string[] = [];
lines[i] = lines[i].replace(openEditTag, '');
while (lines[i].match(closeEditTag) === null) {
editLines.push(lines[i]);
i++;
}
editLines.push(lines[i].replace(closeEditTag, ''));
editBlock = editLines.join('\r\n');
// Indicates the end of the edit block, and then adds the text to the relevant file
} else if (lines[i].match(/\\\\=====\/\//) && editRange !== null) {
if (updateMode) {
var range: number = editRange.limChar - editRange.minChar;
var deleting: boolean = (range !== 0);
if (deleting) {
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
caretPosition = editRange.minChar + (editBlock !== undefined ? editBlock.length : 0);
this.fsFiles[scriptId].deleteAt(editRange.limChar, caretPosition, range, editBlock);
startPosition = undefined;
caretPosition = undefined;
collapsingBlock = undefined;
} else {
if (editRange.minChar === caretPosition) {
collapsingBlock += editBlock;
} else {
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
startPosition = editRange.minChar;
collapsingBlock = editBlock;
}
caretPosition = editRange.minChar + editRange.delta;
}
} else {
this.fsFiles[scriptId] = new FourSlashFile(scriptId, editBlock);
}
editBlock = undefined;
}
}
// Add any text that hasn't been stored to its relevant file
if (collapsingBlock !== undefined) {
this.fsFiles[scriptId].insertAt(startPosition, caretPosition, collapsingBlock);
}
}
private writeFile(scriptId: string): void {
if (this.fsFiles[scriptId] !== undefined) {
var fsFileName = this.fsFiles[scriptId].getFsFileName();
IO.writeFile(fsFileName, this.fsFiles[scriptId].getFile());
}
}
}
interface IFourSlashFile {
getFile(): string;
getFsFileName(): string;
getOriginalScriptId(): string;
updateInternalFile(minChar: number, limChar: number, content: string): void;
insertAt(position: number, caretPosition: number, content: string): void;
deleteAt(position: number, caretPosition: number, amount: number, content?: string): void;
addInternalState(caretPosition: number): void;
}
interface IFourSlashNode {
getNode(): string;
}
class FourSlashFile implements IFourSlashFile {
private static header = "/// <reference path=\"fourslash.ts\" />" + '\r\n' + '\r\n';
private fsScriptId: string;
private commands: IFourSlashNode[];
private internalState: string;
constructor(private originalScriptId: string, private startingContent: string) {
this.fsScriptId = 'tests\\cases\\fourslash\\' + originalScriptId.replace(/.+\\(.+).ts/, '$1_generated.ts');
this.commands = [];
this.internalState = startingContent;
}
public getFile(): string {
var file: string;
//Split on \r\n and add ////
var rnlines = this.startingContent.split('\r\n');
rnlines.forEach((line, index, array) => { array[index] = '////' + line });
var rnfile = rnlines.join('\r\n');
//Repeat for \n
var nlines = rnfile.split(/([^\r]\n|\r[^\n])/);
nlines.forEach((line, index, array) => { if (line.indexOf('////') !== 0) { array[index] = '////' + line } });
var nfile = nlines.join('\n');
file = FourSlashFile.header + nfile + '\r\n' + '\r\n';
this.commands.forEach((command, index, array) => { file += command.getNode() });
return file;
}
private getWithWhitespace(text: string) {
return text.replace(/ /g, '\u00B7').replace(/\r/g, '\u00B6').replace(/\n/g, '\u2193\n').replace(/\t/g, '\u2192\ ');
}
public getFsFileName(): string {
return this.fsScriptId;
}
public getOriginalScriptId(): string {
return this.originalScriptId;
}
public updateInternalFile(minChar: number, limChar: number, content: string): void {
this.internalState = this.internalState.substring(0, minChar) + content + this.internalState.substr(limChar);
}
public insertAt(position: number, caretPosition: number, content: string): void {
var posOffset = this.getInsertOffset(position);
var insertNode = new FourSlashInsert(position - posOffset, content.replace(/(\r\n|\r|\n)/g, '\n'));
this.commands.push(insertNode);
this.updateInternalFile(position, position, content);
this.addInternalState(caretPosition);
}
public deleteAt(position: number, caretPosition: number, amount: number, content?: string): void {
var posOffset = this.getInsertOffset(position);
var amountOffset = this.getDeleteOffset(position, amount);
var deleteNode = new FourSlashDelete(position - posOffset, amount - amountOffset, (content !== undefined ? content.replace(/(\r\n|\r|\n)/g, '\n') : undefined));
this.commands.push(deleteNode);
this.updateInternalFile(position - amount, position, content);
this.addInternalState(caretPosition);
}
public addInternalState(caretPosition: number): void {
var caretedState = this.internalState.substr(0, caretPosition) + '|' + this.internalState.substr(caretPosition);
var internalState = new FourSlashStateComment(caretedState);
this.commands.push(internalState);
}
private getInsertOffset(position: number) {
var offset: number = 0;
var match = this.internalState.substring(0, position).match(/\r\n/g);
if (match !== null) {
offset = match.length;
}
offset = (match !== null ? match.length : 0);
return offset;
}
private getDeleteOffset(position: number, amount: number) {
var offset: number = 0;
var match = this.internalState.substring(position - amount, position).match(/\r\n/g);
if (match !== null) {
offset = match.length;
}
return offset;
}
}
class FourSlashInsert implements IFourSlashNode {
constructor(private position: number, private content: string) {
this.content = this.content.replace(/([\/\\"'])/g, '\\$1');
this.content = this.content.replace(/\r\n/g, '\\r\\n');
this.content = this.content.replace(/(\r|\n)/g, '\\n');
}
public getNode(): string {
var insertNode: string = "goTo.position(" + this.position.toString() + ");" + "\n" +
"edit.insert(\'" + this.content + "\');" + "\n";
return insertNode;
}
}
class FourSlashDelete implements IFourSlashNode {
constructor(private position: number, private deletionAmount: number, private content: string) {
if (this.content !== undefined) {
this.content = this.content.replace(/([\/\\"'])/g, '\\$1');
this.content = this.content.replace(/\r\n/g, '\\r\\n');
this.content = this.content.replace(/(\r|\n)/g, '\\n');
}
}
public getNode(): string {
var deleteNode: string = "goTo.position(" + this.position.toString() + ");" + "\n" +
"edit.backspace(" + this.deletionAmount + ");" + "\n";
if (this.content !== undefined && this.content !== "") {
deleteNode += "edit.insert(\'" + this.content + "\');" + "\n";
}
return deleteNode;
}
}
class FourSlashStateComment implements IFourSlashNode {
constructor(private state: string) { }
public getNode(): string {
var lines = this.state.replace(/\r\n|\r|\n/g, '\r\n').split('\n');
lines.forEach((line, index, array) => { array[index] = '//-' + line; });
var stateNode = lines.join('\n');
return stateNode + '\n' + '\n';
}
}
class DiagnosticsLocator {
private diagnosticsFile: string = 'diagnostics.txt';
public find(): string {
return this.findDiagnosticsInFolder("C:/Users");
}
private findDiagnosticsInFolder(path: string): string {
if (IO.directoryExists(path)) {
var diagnosticsPath = undefined;
var dir: string[] = IO.dir(path, undefined, { recursive: true });
for (var i = 0; i < dir.length; i++) {
if (dir[i].indexOf(this.diagnosticsFile) !== -1) {
diagnosticsPath = dir[i];
}
}
return diagnosticsPath;
}
}
}
var diagnosticsFile: string = undefined;
var diagnosticArgs: string[] = undefined
if (process.argv.length > 2) {
if (IO.fileExists(process.argv[2])) {
diagnosticsFile = process.argv[2];
}
//Grab remaining arguments
diagnosticArgs = process.argv.slice(3, process.argv.length - 1);
}
if (diagnosticsFile !== undefined) {
new FourSlashGenerator(diagnosticsFile).generate(diagnosticArgs);
}
}