Merge pull request #15740 from Microsoft/emit-expression-in-extends-nested

Correctly emit expression in extends in namespace
This commit is contained in:
Nathan Shively-Sanders
2017-05-10 12:41:11 -07:00
committed by GitHub
5 changed files with 163 additions and 8 deletions

View File

@@ -67,7 +67,7 @@ namespace ts {
let errorNameNode: DeclarationName;
const emitJsDocComments = compilerOptions.removeComments ? noop : writeJsDocComments;
const emit = compilerOptions.stripInternal ? stripInternal : emitNode;
let noDeclare: boolean;
let needsDeclare = true;
let moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = [];
let asynchronousSubModuleDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[];
@@ -110,11 +110,11 @@ namespace ts {
resultHasExternalModuleIndicator = false;
if (!isBundledEmit || !isExternalModule(sourceFile)) {
noDeclare = false;
needsDeclare = true;
emitSourceFile(sourceFile);
}
else if (isExternalModule(sourceFile)) {
noDeclare = true;
needsDeclare = false;
write(`declare module "${getResolvedExternalModuleName(host, sourceFile)}" {`);
writeLine();
increaseIndent();
@@ -612,9 +612,9 @@ namespace ts {
}
}
function emitTempVariableDeclaration(expr: Expression, baseName: string, diagnostic: SymbolAccessibilityDiagnostic): string {
function emitTempVariableDeclaration(expr: Expression, baseName: string, diagnostic: SymbolAccessibilityDiagnostic, needsDeclare: boolean): string {
const tempVarName = getExportTempVariableName(baseName);
if (!noDeclare) {
if (needsDeclare) {
write("declare ");
}
write("const ");
@@ -636,7 +636,7 @@ namespace ts {
const tempVarName = emitTempVariableDeclaration(node.expression, "_default", {
diagnosticMessage: Diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
errorNode: node
});
}, needsDeclare);
write(node.isExportEquals ? "export = " : "export default ");
write(tempVarName);
}
@@ -728,7 +728,7 @@ namespace ts {
if (modifiers & ModifierFlags.Default) {
write("default ");
}
else if (node.kind !== SyntaxKind.InterfaceDeclaration && !noDeclare) {
else if (node.kind !== SyntaxKind.InterfaceDeclaration && needsDeclare) {
write("declare ");
}
}
@@ -1155,7 +1155,7 @@ namespace ts {
diagnosticMessage: Diagnostics.extends_clause_of_exported_class_0_has_or_is_using_private_name_1,
errorNode: baseTypeNode,
typeName: node.name
});
}, !findAncestor(node, n => n.kind === SyntaxKind.ModuleDeclaration));
}
emitJsDocComments(node);

View File

@@ -0,0 +1,67 @@
//// [declarationEmitExpressionInExtends5.ts]
namespace Test
{
export interface IFace
{
}
export class SomeClass implements IFace
{
}
export class Derived extends getClass<IFace>()
{
}
export function getClass<T>() : new() => T
{
return SomeClass as (new() => T);
}
}
//// [declarationEmitExpressionInExtends5.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Test;
(function (Test) {
var SomeClass = (function () {
function SomeClass() {
}
return SomeClass;
}());
Test.SomeClass = SomeClass;
var Derived = (function (_super) {
__extends(Derived, _super);
function Derived() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Derived;
}(getClass()));
Test.Derived = Derived;
function getClass() {
return SomeClass;
}
Test.getClass = getClass;
})(Test || (Test = {}));
//// [declarationEmitExpressionInExtends5.d.ts]
declare namespace Test {
interface IFace {
}
class SomeClass implements IFace {
}
const Derived_base: new () => IFace;
class Derived extends Derived_base {
}
function getClass<T>(): new () => T;
}

View File

@@ -0,0 +1,33 @@
=== tests/cases/compiler/declarationEmitExpressionInExtends5.ts ===
namespace Test
>Test : Symbol(Test, Decl(declarationEmitExpressionInExtends5.ts, 0, 0))
{
export interface IFace
>IFace : Symbol(IFace, Decl(declarationEmitExpressionInExtends5.ts, 1, 1))
{
}
export class SomeClass implements IFace
>SomeClass : Symbol(SomeClass, Decl(declarationEmitExpressionInExtends5.ts, 4, 2))
>IFace : Symbol(IFace, Decl(declarationEmitExpressionInExtends5.ts, 1, 1))
{
}
export class Derived extends getClass<IFace>()
>Derived : Symbol(Derived, Decl(declarationEmitExpressionInExtends5.ts, 8, 2))
>getClass : Symbol(getClass, Decl(declarationEmitExpressionInExtends5.ts, 12, 2))
>IFace : Symbol(IFace, Decl(declarationEmitExpressionInExtends5.ts, 1, 1))
{
}
export function getClass<T>() : new() => T
>getClass : Symbol(getClass, Decl(declarationEmitExpressionInExtends5.ts, 12, 2))
>T : Symbol(T, Decl(declarationEmitExpressionInExtends5.ts, 14, 26))
>T : Symbol(T, Decl(declarationEmitExpressionInExtends5.ts, 14, 26))
{
return SomeClass as (new() => T);
>SomeClass : Symbol(SomeClass, Decl(declarationEmitExpressionInExtends5.ts, 4, 2))
>T : Symbol(T, Decl(declarationEmitExpressionInExtends5.ts, 14, 26))
}
}

View File

@@ -0,0 +1,35 @@
=== tests/cases/compiler/declarationEmitExpressionInExtends5.ts ===
namespace Test
>Test : typeof Test
{
export interface IFace
>IFace : IFace
{
}
export class SomeClass implements IFace
>SomeClass : SomeClass
>IFace : IFace
{
}
export class Derived extends getClass<IFace>()
>Derived : Derived
>getClass<IFace>() : IFace
>getClass : <T>() => new () => T
>IFace : IFace
{
}
export function getClass<T>() : new() => T
>getClass : <T>() => new () => T
>T : T
>T : T
{
return SomeClass as (new() => T);
>SomeClass as (new() => T) : new () => T
>SomeClass : typeof SomeClass
>T : T
}
}

View File

@@ -0,0 +1,20 @@
// @declaration: true
namespace Test
{
export interface IFace
{
}
export class SomeClass implements IFace
{
}
export class Derived extends getClass<IFace>()
{
}
export function getClass<T>() : new() => T
{
return SomeClass as (new() => T);
}
}