forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilerRunner.ts
More file actions
476 lines (416 loc) · 24.4 KB
/
Copy pathcompilerRunner.ts
File metadata and controls
476 lines (416 loc) · 24.4 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/// <reference path='harness.ts' />
/// <reference path='runnerbase.ts' />
/// <reference path='typeWriter.ts' />
/// <reference path='syntacticCleaner.ts' />
enum CompilerTestType {
Conformance,
Regressions,
Test262
}
class CompilerBaselineRunner extends RunnerBase {
private basePath = 'tests/cases';
private errors: boolean;
private emit: boolean;
private decl: boolean;
private output: boolean;
public options: string;
constructor(public testType?: CompilerTestType) {
super();
this.errors = true;
this.emit = true;
this.decl = true;
this.output = true;
if (testType === CompilerTestType.Conformance) {
this.basePath += '/conformance';
}
else if (testType === CompilerTestType.Regressions) {
this.basePath += '/compiler';
}
else if (testType === CompilerTestType.Test262) {
this.basePath += '/test262';
} else {
this.basePath += '/compiler'; // default to this for historical reasons
}
}
public checkTestCodeOutput(fileName: string) {
describe('compiler tests for ' + fileName, () => {
// strips the fileName from the path.
var justName = fileName.replace(/^.*[\\\/]/, '');
var content = Harness.IO.readFile(fileName);
var testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(content, fileName);
var units = testCaseContent.testUnitData;
var tcSettings = testCaseContent.settings;
var createNewInstance = false;
var lastUnit = units[units.length - 1];
var rootDir = lastUnit.originalFilePath.indexOf('conformance') === -1 ? 'tests/cases/compiler/' : lastUnit.originalFilePath.substring(0, lastUnit.originalFilePath.lastIndexOf('/')) + '/';
var result: Harness.Compiler.CompilerResult;
var options: ts.CompilerOptions;
// equivalent to the files that will be passed on the command line
var toBeCompiled: { unitName: string; content: string }[];
// equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
var otherFiles: { unitName: string; content: string }[];
var harnessCompiler: Harness.Compiler.HarnessCompiler;
var declToBeCompiled: { unitName: string; content: string }[] = [];
var declOtherFiles: { unitName: string; content: string }[] = [];
var declResult: Harness.Compiler.CompilerResult;
var createNewInstance = false;
before(() => {
harnessCompiler = Harness.Compiler.getCompiler();
// We need to assemble the list of input files for the compiler and other related files on the 'filesystem' (ie in a multi-file test)
// If the last file in a test uses require or a triple slash reference we'll assume all other files will be brought in via references,
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
toBeCompiled = [];
otherFiles = [];
if (/require\(/.test(lastUnit.content) || /reference\spath/.test(lastUnit.content)) {
toBeCompiled.push({ unitName: rootDir + lastUnit.name, content: lastUnit.content });
units.forEach(unit => {
if (unit.name !== lastUnit.name) {
otherFiles.push({ unitName: rootDir + unit.name, content: unit.content });
}
});
} else {
toBeCompiled = units.map(unit => {
return { unitName: rootDir + unit.name, content: unit.content };
});
}
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult) {
result = compileResult;
}, function (settings) {
harnessCompiler.setCompilerSettings(tcSettings);
});
});
beforeEach(() => {
/* The compiler doesn't handle certain flags flipping during a single compilation setting. Tests on these flags will need
a fresh compiler instance for themselves and then create a fresh one for the next test. Would be nice to get dev fixes
eventually to remove this limitation. */
for (var i = 0; i < tcSettings.length; ++i) {
if (!createNewInstance && (tcSettings[i].flag == "noimplicitany" || tcSettings[i].flag === 'target')) {
harnessCompiler = Harness.Compiler.getCompiler({
useExistingInstance: false,
optionsForFreshInstance: { useMinimalDefaultLib: true, noImplicitAny: tcSettings[i].flag === "noimplicitany" }
});
harnessCompiler.setCompilerSettings(tcSettings);
createNewInstance = true;
}
}
});
afterEach(() => {
if (createNewInstance) {
harnessCompiler = Harness.Compiler.getCompiler({
useExistingInstance: false,
optionsForFreshInstance: { useMinimalDefaultLib: true, noImplicitAny: false }
});
createNewInstance = false;
}
});
function getErrorBaseline(toBeCompiled: { unitName: string; content: string }[],
otherFiles: { unitName: string; content: string }[],
result: Harness.Compiler.CompilerResult
) {
var outputLines: string[] = [];
// Count up all the errors we find so we don't miss any
var totalErrorsReported = 0;
function outputErrorText(error: Harness.Compiler.MinimalDiagnostic) {
var errLines = RunnerBase.removeFullPaths(error.message)
.split('\n')
.map(s => s.length > 0 && s.charAt(s.length - 1) === '\r' ? s.substr(0, s.length - 1) : s)
.filter(s => s.length > 0)
.map(s => '!!! ' + s);
errLines.forEach(e => outputLines.push(e));
totalErrorsReported++;
}
// Report glovbal errors:
var globalErrors = result.errors.filter(err => !err.filename);
globalErrors.forEach(err => outputErrorText(err));
// 'merge' the lines of each input file with any errors associated with it
toBeCompiled.concat(otherFiles).forEach(inputFile => {
// Filter down to the errors in the file
var fileErrors = result.errors.filter(e => {
var errFn = e.filename;
return errFn && errFn === inputFile.unitName;
});
// Header
outputLines.push('==== ' + inputFile.unitName + ' (' + fileErrors.length + ' errors) ====');
// Make sure we emit something for every error
var markedErrorCount = 0;
// For each line, emit the line followed by any error squiggles matching this line
// Note: IE JS engine incorrectly handles consecutive delimiters here when using RegExp split, so
// we have to string-based splitting instead and try to figure out the delimiting chars
var lineStarts = ts.getLineStarts(inputFile.content);
var lines = inputFile.content.split('\n');
lines.forEach((line, lineIndex) => {
if (line.length > 0 && line.charAt(line.length - 1) === '\r') {
line = line.substr(0, line.length - 1);
}
var thisLineStart = lineStarts[lineIndex];
var nextLineStart: number;
// On the last line of the file, fake the next line start number so that we handle errors on the last character of the file correctly
if (lineIndex === lines.length - 1) {
nextLineStart = inputFile.content.length;
} else {
nextLineStart = lineStarts[lineIndex + 1];
}
// Emit this line from the original file
outputLines.push(' ' + line);
fileErrors.forEach(err => {
// Does any error start or continue on to this line? Emit squiggles
if ((err.end >= thisLineStart) && ((err.start < nextLineStart) || (lineIndex === lines.length - 1))) {
// How many characters from the start of this line the error starts at (could be positive or negative)
var relativeOffset = err.start - thisLineStart;
// How many characters of the error are on this line (might be longer than this line in reality)
var length = (err.end - err.start) - Math.max(0, thisLineStart - err.start);
// Calculate the start of the squiggle
var squiggleStart = Math.max(0, relativeOffset);
// TODO/REVIEW: this doesn't work quite right in the browser if a multi file test has files whose names are just the right length relative to one another
outputLines.push(' ' + line.substr(0, squiggleStart).replace(/[^\s]/g, ' ') + new Array(Math.min(length, line.length - squiggleStart) + 1).join('~'));
// If the error ended here, or we're at the end of the file, emit its message
if ((lineIndex === lines.length - 1) || nextLineStart > err.end) {
// Just like above, we need to do a split on a string instead of on a regex
// because the JS engine does regexes wrong
outputErrorText(err);
markedErrorCount++;
}
}
});
});
// Verify we didn't miss any errors in this file
assert.equal(markedErrorCount, fileErrors.length, 'count of errors in ' + inputFile.unitName);
});
// Verify we didn't miss any errors in total
assert.equal(totalErrorsReported, result.errors.length, 'total number of errors');
return outputLines.join('\r\n');
}
// check errors
it('Correct errors for ' + fileName, () => {
if (this.errors) {
Harness.Baseline.runBaseline('Correct errors for ' + fileName, justName.replace(/\.ts$/, '.errors.txt'), (): string => {
if (result.errors.length === 0) return null;
return getErrorBaseline(toBeCompiled, otherFiles, result);
});
}
});
// Source maps?
it('Correct sourcemap content for ' + fileName, () => {
if (result.sourceMapRecord) {
Harness.Baseline.runBaseline('Correct sourcemap content for ' + fileName, justName.replace(/\.ts$/, '.sourcemap.txt'), () => {
return result.sourceMapRecord;
});
}
});
// Compile .d.ts files
it('Correct compiler generated.d.ts for ' + fileName, () => {
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length !== result.files.length) {
throw new Error('There were no errors and declFiles generated did not match number of js files generated');
}
// if the .d.ts is non-empty, confirm it compiles correctly as well
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length > 0) {
function getDtsFile(file: { unitName: string; content: string }) {
if (Harness.Compiler.isDTS(file.unitName)) {
return file;
} else {
var declFile = ts.forEach(result.declFilesCode,
declFile => declFile.fileName === (file.unitName.substr(0, file.unitName.length - ".ts".length) + ".d.ts")
? declFile : undefined);
return { unitName: rootDir + Harness.Path.getFileName(declFile.fileName), content: declFile.code };
}
}
ts.forEach(toBeCompiled, file => { declToBeCompiled.push(getDtsFile(file)); });
ts.forEach(otherFiles, file => { declOtherFiles.push(getDtsFile(file)); });
harnessCompiler.compileFiles(declToBeCompiled, declOtherFiles, function (compileResult) {
declResult = compileResult;
}, function (settings) {
harnessCompiler.setCompilerSettings(tcSettings);
});
}
});
it('Correct JS output for ' + fileName, () => {
if (!ts.fileExtensionIs(lastUnit.name, '.d.ts') && this.emit) {
if (result.files.length === 0 && result.errors.length === 0) {
throw new Error('Expected at least one js file to be emitted or at least one error to be created.');
}
// check js output
Harness.Baseline.runBaseline('Correct JS output for ' + fileName, justName.replace(/\.ts/, '.js'), () => {
var tsCode = '';
var tsSources = otherFiles.concat(toBeCompiled);
if (tsSources.length > 1) {
tsCode += '//// [' + fileName + '] ////\r\n\r\n';
}
for (var i = 0; i < tsSources.length; i++) {
tsCode += '//// [' + Harness.Path.getFileName(tsSources[i].unitName) + ']\r\n';
tsCode += tsSources[i].content + (i < (tsSources.length - 1) ? '\r\n' : '');
}
var jsCode = '';
for (var i = 0; i < result.files.length; i++) {
jsCode += '//// [' + Harness.Path.getFileName(result.files[i].fileName) + ']\r\n';
jsCode += result.files[i].code;
// Re-enable this if we want to do another comparison of old vs new compiler baselines
// jsCode += SyntacticCleaner.clean(result.files[i].code);
}
if (result.declFilesCode.length > 0) {
jsCode += '\r\n\r\n';
for (var i = 0; i < result.files.length; i++) {
jsCode += '//// [' + Harness.Path.getFileName(result.declFilesCode[i].fileName) + ']\r\n';
jsCode += result.declFilesCode[i].code;
}
}
if (declResult && declResult.errors.length) {
jsCode += '\r\n\r\n//// [DtsFileErrors]\r\n';
jsCode += '\r\n\r\n';
jsCode += getErrorBaseline(declToBeCompiled, declOtherFiles, declResult);
}
if (jsCode.length > 0) {
return tsCode + '\r\n\r\n' + jsCode;
} else {
return null;
}
});
}
});
it('Correct Sourcemap output for ' + fileName, () => {
if (options.sourceMap) {
if (result.sourceMaps.length !== result.files.length) {
throw new Error('Number of sourcemap files should be same as js files.');
}
Harness.Baseline.runBaseline('Correct Sourcemap output for ' + fileName, justName.replace(/\.ts/, '.js.map'), () => {
var sourceMapCode = '';
for (var i = 0; i < result.sourceMaps.length; i++) {
sourceMapCode += '//// [' + Harness.Path.getFileName(result.sourceMaps[i].fileName) + ']\r\n';
sourceMapCode += result.sourceMaps[i].code;
}
return sourceMapCode;
});
}
});
it('Correct type baselines for ' + fileName, () => {
// NEWTODO: Type baselines
if (/* ! */ false && /* ! */ result.errors.length === 0) {
Harness.Baseline.runBaseline('Correct expression types for ' + fileName, justName.replace(/\.ts/, '.types'), () => {
// TODO: Rewrite this part
//var compiler = new TypeScript.TypeScriptCompiler(
// new TypeScript.NullLogger(), TypeScript.ImmutableCompilationSettings.defaultSettings());
//compiler.addFile('lib.d.ts', TypeScript.ScriptSnapshot.fromString(Harness.Compiler.libTextMinimal),
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
//var allFiles = toBeCompiled.concat(otherFiles);
//allFiles.forEach(file => {
// compiler.addFile(file.unitName, TypeScript.ScriptSnapshot.fromString(file.content),
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
//});
var allFiles: any[] = [];
var compiler: any = undefined;
var typeBaselineText = '';
var typeLines: string[] = [];
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
allFiles.forEach(file => {
var codeLines = file.content.split('\n');
var walker = new TypeWriterWalker(file.unitName, compiler);
walker.run();
walker.results.forEach(result => {
var formattedLine = result.identifierName + " : " + result.type;
if (!typeMap[file.unitName]) {
typeMap[file.unitName] = {}
}
var typeInfo = [formattedLine];
var existingTypeInfo = typeMap[file.unitName][result.line];
if (existingTypeInfo) {
typeInfo = existingTypeInfo.concat(typeInfo);
}
typeMap[file.unitName][result.line] = typeInfo;
});
var typeBaselineText = '';
var typeLines: string[] = [];
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
allFiles.forEach(file => {
var codeLines = file.content.split('\n');
var walker = new TypeWriterWalker(file.unitName, compiler);
walker.run();
walker.results.forEach(result => {
var formattedLine = result.identifierName + " : " + result.type;
if (!typeMap[file.unitName]) {
typeMap[file.unitName] = {}
} else {
typeLines.push('No type information for this code.');
}
});
});
typeLines.push('=== ' + file.unitName + ' ===\r\n');
for (var i = 0; i < codeLines.length; i++) {
var currentCodeLine = codeLines[i];
var lastLine = typeLines[typeLines.length];
typeLines.push(currentCodeLine + '\r\n');
if (typeMap[file.unitName]) {
var typeInfo = typeMap[file.unitName][i];
if (typeInfo) {
var leadingSpaces = '';
typeInfo.forEach(ty => {
typeLines.push('>' + ty + '\r\n');
});
if (i + 1 < codeLines.length && (codeLines[i + 1].match(/^\s*[{|}]\s*$/) || codeLines[i + 1].trim() === '')) {
} else {
typeLines.push('\r\n');
}
}
} else {
typeLines.push('No type information for this code.');
}
}
});
return typeLines.join('');
});
}
});
});
}
public initializeTests() {
describe("Setup compiler for compiler baselines", () => {
var harnessCompiler = Harness.Compiler.getCompiler({
useExistingInstance: false,
optionsForFreshInstance: { useMinimalDefaultLib: true, noImplicitAny: false }
});
this.parseOptions();
});
// this will set up a series of describe/it blocks to run between the setup and cleanup phases
if (this.tests.length === 0) {
var testFiles = this.enumerateFiles(this.basePath, /\.ts$/, { recursive: true });
testFiles.forEach(fn => {
fn = fn.replace(/\\/g, "/");
this.checkTestCodeOutput(fn);
});
}
else {
this.tests.forEach(test => this.checkTestCodeOutput(test));
}
describe("Cleanup after compiler baselines", () => {
var harnessCompiler = Harness.Compiler.getCompiler({
useExistingInstance: false,
optionsForFreshInstance: { useMinimalDefaultLib: true, noImplicitAny: false }
});
});
}
private parseOptions() {
if (this.options && this.options.length > 0) {
this.errors = false;
this.emit = false;
this.decl = false;
this.output = false;
var opts = this.options.split(',');
for (var i = 0; i < opts.length; i++) {
switch (opts[i]) {
case 'error':
this.errors = true;
break;
case 'emit':
this.emit = true;
break;
case 'decl':
this.decl = true;
break;
case 'output':
this.output = true;
break;
default:
throw new Error('unsupported flag');
}
}
}
}
}