Fix the crash in declaration file emit when alias is used before its declaration

Fixes #938
This commit is contained in:
Sheetal Nandi 2014-10-22 10:23:15 -07:00
parent 8392ff49bd
commit 2e36faed28
4 changed files with 68 additions and 6 deletions

View File

@ -345,13 +345,22 @@ module ts {
var oldWriter = writer;
forEach(importDeclarations, aliasToWrite => {
var aliasEmitInfo = forEach(aliasDeclarationEmitInfo, declEmitInfo => declEmitInfo.declaration === aliasToWrite ? declEmitInfo : undefined);
writer = createTextWriter(newLine, trackSymbol);
for (var declarationIndent = aliasEmitInfo.indent; declarationIndent; declarationIndent--) {
writer.increaseIndent();
}
// If the alias was marked as not visible when we saw its declaration, we would have saved the aliasEmitInfo, but if we haven't yet visited the alias declaration
// then we don't need to write it at this point. We will write it when we actually see its declaration
// Eg.
// export function bar(a: foo.Foo) { }
// import foo = require("foo");
// Writing of function bar would mark alias declaration foo as visible but we haven't yet visited that declaration so do nothing,
// we would write alias foo declaration when we visit it since it would now be marked as visible
if (aliasEmitInfo) {
writer = createTextWriter(newLine, trackSymbol);
for (var declarationIndent = aliasEmitInfo.indent; declarationIndent; declarationIndent--) {
writer.increaseIndent();
}
writeImportDeclaration(aliasToWrite);
aliasEmitInfo.asynchronousOutput = writer.getText();
writeImportDeclaration(aliasToWrite);
aliasEmitInfo.asynchronousOutput = writer.getText();
}
});
writer = oldWriter;
}

View File

@ -0,0 +1,29 @@
//// [tests/cases/compiler/declFileAliasUseBeforeDeclaration.ts] ////
//// [declFileAliasUseBeforeDeclaration_foo.ts]
export class Foo { }
//// [declFileAliasUseBeforeDeclaration_test.ts]
export function bar(a: foo.Foo) { }
import foo = require("declFileAliasUseBeforeDeclaration_foo");
//// [declFileAliasUseBeforeDeclaration_foo.js]
var Foo = (function () {
function Foo() {
}
return Foo;
})();
exports.Foo = Foo;
//// [declFileAliasUseBeforeDeclaration_test.js]
function bar(a) {
}
exports.bar = bar;
//// [declFileAliasUseBeforeDeclaration_foo.d.ts]
export declare class Foo {
}
//// [declFileAliasUseBeforeDeclaration_test.d.ts]
export declare function bar(a: foo.Foo): void;
import foo = require("declFileAliasUseBeforeDeclaration_foo");

View File

@ -0,0 +1,15 @@
=== tests/cases/compiler/declFileAliasUseBeforeDeclaration_test.ts ===
export function bar(a: foo.Foo) { }
>bar : (a: foo.Foo) => void
>a : foo.Foo
>foo : unknown
>Foo : foo.Foo
import foo = require("declFileAliasUseBeforeDeclaration_foo");
>foo : typeof foo
=== tests/cases/compiler/declFileAliasUseBeforeDeclaration_foo.ts ===
export class Foo { }
>Foo : Foo

View File

@ -0,0 +1,9 @@
//@module: commonjs
//@declaration: true
// @Filename: declFileAliasUseBeforeDeclaration_foo.ts
export class Foo { }
// @Filename: declFileAliasUseBeforeDeclaration_test.ts
export function bar(a: foo.Foo) { }
import foo = require("declFileAliasUseBeforeDeclaration_foo");