diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 0c1c0acd474..7c842de277f 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -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); diff --git a/tests/baselines/reference/declarationEmitExpressionInExtends5.js b/tests/baselines/reference/declarationEmitExpressionInExtends5.js new file mode 100644 index 00000000000..77f8fc0e12c --- /dev/null +++ b/tests/baselines/reference/declarationEmitExpressionInExtends5.js @@ -0,0 +1,67 @@ +//// [declarationEmitExpressionInExtends5.ts] +namespace Test +{ + export interface IFace + { + } + + export class SomeClass implements IFace + { + } + + export class Derived extends getClass() + { + } + + export function getClass() : 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(): new () => T; +} diff --git a/tests/baselines/reference/declarationEmitExpressionInExtends5.symbols b/tests/baselines/reference/declarationEmitExpressionInExtends5.symbols new file mode 100644 index 00000000000..42bf5892211 --- /dev/null +++ b/tests/baselines/reference/declarationEmitExpressionInExtends5.symbols @@ -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() +>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() : 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)) + } +} + diff --git a/tests/baselines/reference/declarationEmitExpressionInExtends5.types b/tests/baselines/reference/declarationEmitExpressionInExtends5.types new file mode 100644 index 00000000000..22aafb85e58 --- /dev/null +++ b/tests/baselines/reference/declarationEmitExpressionInExtends5.types @@ -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() +>Derived : Derived +>getClass() : IFace +>getClass : () => new () => T +>IFace : IFace + { + } + + export function getClass() : new() => T +>getClass : () => new () => T +>T : T +>T : T + { + return SomeClass as (new() => T); +>SomeClass as (new() => T) : new () => T +>SomeClass : typeof SomeClass +>T : T + } +} + diff --git a/tests/cases/compiler/declarationEmitExpressionInExtends5.ts b/tests/cases/compiler/declarationEmitExpressionInExtends5.ts new file mode 100644 index 00000000000..5057ade7570 --- /dev/null +++ b/tests/cases/compiler/declarationEmitExpressionInExtends5.ts @@ -0,0 +1,20 @@ +// @declaration: true +namespace Test +{ + export interface IFace + { + } + + export class SomeClass implements IFace + { + } + + export class Derived extends getClass() + { + } + + export function getClass() : new() => T + { + return SomeClass as (new() => T); + } +}