Skip to content

Commit 398266d

Browse files
committed
Merge pull request #464 from Microsoft/typeWriter
Revive TypeWriter
2 parents 840d6d0 + f949c50 commit 398266d

File tree

1,712 files changed

+142961
-550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,712 files changed

+142961
-550
lines changed

src/compiler/checker.ts

Lines changed: 253 additions & 68 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ module ts {
218218
LastKeyword = StringKeyword,
219219
FirstFutureReservedWord = ImplementsKeyword,
220220
LastFutureReservedWord = YieldKeyword,
221+
FirstTypeNode = TypeReference,
222+
LastTypeNode = ArrayType,
221223
FirstPunctuation= OpenBraceToken,
222224
LastPunctuation = CaretEqualsToken
223225
}
@@ -595,19 +597,17 @@ module ts {
595597
getTypeCount(): number;
596598
checkProgram(): void;
597599
emitFiles(): EmitResult;
598-
getSymbolOfNode(node: Node): Symbol;
599600
getParentOfSymbol(symbol: Symbol): Symbol;
600601
getTypeOfSymbol(symbol: Symbol): Type;
601-
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
602602
getPropertiesOfType(type: Type): Symbol[];
603603
getPropertyOfType(type: Type, propetyName: string): Symbol;
604604
getSignaturesOfType(type: Type, kind: SignatureKind): Signature[];
605605
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
606606
getReturnTypeOfSignature(signature: Signature): Type;
607-
resolveEntityName(location: Node, name: EntityName, meaning: SymbolFlags): Symbol;
608607
getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[];
609608
getSymbolInfo(node: Node): Symbol;
610-
getTypeOfExpression(node: Expression, contextualType?: Type, contextualMapper?: TypeMapper): Type;
609+
getTypeOfNode(node: Node): Type;
610+
getApparentType(type: Type): ApparentType;
611611
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
612612
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;
613613
getAugmentedPropertiesOfApparentType(type: Type): Symbol[];

src/harness/compilerRunner.ts

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class CompilerBaselineRunner extends RunnerBase {
3939

4040
public checkTestCodeOutput(fileName: string) {
4141
describe('compiler tests for ' + fileName, () => {
42-
// strips the fileName from the path.
43-
var justName = fileName.replace(/^.*[\\\/]/, '');
42+
// Mocha holds onto the closure environment of the describe callback even after the test is done.
43+
// Everything declared here should be cleared out in the "after" callback.
44+
var justName = fileName.replace(/^.*[\\\/]/, ''); // strips the fileName from the path.
4445
var content = Harness.IO.readFile(fileName);
4546
var testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(content, fileName);
4647

@@ -52,6 +53,7 @@ class CompilerBaselineRunner extends RunnerBase {
5253
var rootDir = lastUnit.originalFilePath.indexOf('conformance') === -1 ? 'tests/cases/compiler/' : lastUnit.originalFilePath.substring(0, lastUnit.originalFilePath.lastIndexOf('/')) + '/';
5354

5455
var result: Harness.Compiler.CompilerResult;
56+
var checker: ts.TypeChecker;
5557
var options: ts.CompilerOptions;
5658
// equivalent to the files that will be passed on the command line
5759
var toBeCompiled: { unitName: string; content: string }[];
@@ -85,8 +87,10 @@ class CompilerBaselineRunner extends RunnerBase {
8587
});
8688
}
8789

88-
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult) {
90+
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult, _checker) {
8991
result = compileResult;
92+
// The checker will be used by typeWriter
93+
checker = _checker;
9094
}, function (settings) {
9195
harnessCompiler.setCompilerSettings(tcSettings);
9296
});
@@ -97,6 +101,7 @@ class CompilerBaselineRunner extends RunnerBase {
97101
a fresh compiler instance for themselves and then create a fresh one for the next test. Would be nice to get dev fixes
98102
eventually to remove this limitation. */
99103
for (var i = 0; i < tcSettings.length; ++i) {
104+
// noImplicitAny is passed to getCompiler, but target is just passed in the settings blob to setCompilerSettings
100105
if (!createNewInstance && (tcSettings[i].flag == "noimplicitany" || tcSettings[i].flag === 'target')) {
101106
harnessCompiler = Harness.Compiler.getCompiler({
102107
useExistingInstance: false,
@@ -118,6 +123,27 @@ class CompilerBaselineRunner extends RunnerBase {
118123
}
119124
});
120125

126+
after(() => {
127+
// Mocha holds onto the closure environment of the describe callback even after the test is done.
128+
// Therefore we have to clean out large objects after the test is done.
129+
justName = undefined;
130+
content = undefined;
131+
testCaseContent = undefined;
132+
units = undefined;
133+
tcSettings = undefined;
134+
lastUnit = undefined;
135+
rootDir = undefined;
136+
result = undefined;
137+
checker = undefined;
138+
options = undefined;
139+
toBeCompiled = undefined;
140+
otherFiles = undefined;
141+
harnessCompiler = undefined;
142+
declToBeCompiled = undefined;
143+
declOtherFiles = undefined;
144+
declResult = undefined;
145+
});
146+
121147
function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string {
122148
return file.writeByteOrderMark ? "\u00EF\u00BB\u00BF" : "";
123149
}
@@ -251,32 +277,15 @@ class CompilerBaselineRunner extends RunnerBase {
251277

252278
it('Correct type baselines for ' + fileName, () => {
253279
// NEWTODO: Type baselines
254-
if (/* ! */ false && /* ! */ result.errors.length === 0) {
280+
if (result.errors.length === 0) {
255281
Harness.Baseline.runBaseline('Correct expression types for ' + fileName, justName.replace(/\.ts/, '.types'), () => {
256-
// TODO: Rewrite this part
257-
//var compiler = new TypeScript.TypeScriptCompiler(
258-
// new TypeScript.NullLogger(), TypeScript.ImmutableCompilationSettings.defaultSettings());
259-
260-
//compiler.addFile('lib.d.ts', TypeScript.ScriptSnapshot.fromString(Harness.Compiler.libTextMinimal),
261-
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
262-
263-
//var allFiles = toBeCompiled.concat(otherFiles);
264-
//allFiles.forEach(file => {
265-
// compiler.addFile(file.unitName, TypeScript.ScriptSnapshot.fromString(file.content),
266-
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
267-
//});
268-
269-
var allFiles: any[] = [];
270-
var compiler: any = undefined;
271-
272-
var typeBaselineText = '';
282+
var allFiles = toBeCompiled.concat(otherFiles).filter(file => !!checker.getProgram().getSourceFile(file.unitName));
273283
var typeLines: string[] = [];
274284
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
285+
var walker = new TypeWriterWalker(checker);
275286
allFiles.forEach(file => {
276287
var codeLines = file.content.split('\n');
277-
var walker = new TypeWriterWalker(file.unitName, compiler);
278-
walker.run();
279-
walker.results.forEach(result => {
288+
walker.getTypes(file.unitName).forEach(result => {
280289
var formattedLine = result.identifierName + " : " + result.type;
281290
if (!typeMap[file.unitName]) {
282291
typeMap[file.unitName] = {};
@@ -290,23 +299,6 @@ class CompilerBaselineRunner extends RunnerBase {
290299
typeMap[file.unitName][result.line] = typeInfo;
291300
});
292301

293-
var typeBaselineText = '';
294-
var typeLines: string[] = [];
295-
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
296-
allFiles.forEach(file => {
297-
var codeLines = file.content.split('\n');
298-
var walker = new TypeWriterWalker(file.unitName, compiler);
299-
walker.run();
300-
walker.results.forEach(result => {
301-
var formattedLine = result.identifierName + " : " + result.type;
302-
if (!typeMap[file.unitName]) {
303-
typeMap[file.unitName] = {};
304-
} else {
305-
typeLines.push('No type information for this code.');
306-
}
307-
});
308-
});
309-
310302
typeLines.push('=== ' + file.unitName + ' ===\r\n');
311303
for (var i = 0; i < codeLines.length; i++) {
312304
var currentCodeLine = codeLines[i];

src/harness/harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ module Harness {
609609

610610
public compileFiles(inputFiles: { unitName: string; content: string }[],
611611
otherFiles: { unitName: string; content?: string }[],
612-
onComplete: (result: CompilerResult) => void,
612+
onComplete: (result: CompilerResult, checker: ts.TypeChecker) => void,
613613
settingsCallback?: (settings: ts.CompilerOptions) => void,
614614
options?: ts.CompilerOptions) {
615615

@@ -755,7 +755,7 @@ module Harness {
755755
var result = new CompilerResult(fileOutputs, errors, []);
756756
// Covert the source Map data into the baseline
757757
result.updateSourceMapRecord(program, emitResult ? emitResult.sourceMaps : undefined);
758-
onComplete(result);
758+
onComplete(result, checker);
759759

760760
// reset what newline means in case the last test changed it
761761
sys.newLine = '\r\n';

0 commit comments

Comments
 (0)