correctly check exported type aliases merged with overloads

This commit is contained in:
Vladimir Matveev 2016-02-17 10:19:20 -08:00
parent d92f78d7e2
commit 5e770bda2e
5 changed files with 47 additions and 5 deletions

View File

@ -15055,11 +15055,20 @@ namespace ts {
continue;
}
const { declarations, flags } = exports[id];
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. (TS Exceptions: namespaces, function overloads, enums, and interfaces)
if (!(flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) && (flags & SymbolFlags.TypeAlias ? declarations.length - 1 : declarations.length) > 1) {
const exportedDeclarations: Declaration[] = filter(declarations, isNotOverload);
if (exportedDeclarations.length > 1) {
for (const declaration of exportedDeclarations) {
// ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries.
// (TS Exceptions: namespaces, function overloads, enums, and interfaces)
if (flags & (SymbolFlags.Namespace | SymbolFlags.Interface | SymbolFlags.Enum)) {
continue;
}
const exportedDeclarationsCount = countWhere(declarations, isNotOverload);
if (flags & SymbolFlags.TypeAlias && exportedDeclarationsCount <= 2) {
// it is legal to merge type alias with other values
// so count should be either 1 (just type alias) or 2 (type alias + merged value)
continue;
}
if (exportedDeclarationsCount > 1) {
for (const declaration of declarations) {
if (isNotOverload(declaration)) {
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Cannot_redeclare_exported_variable_0, id));
}
}

View File

@ -0,0 +1,9 @@
//// [exportRedeclarationTypeAliases.ts]
export type Foo = number;
export function Foo(): number;
export function Foo(): any {}
//// [exportRedeclarationTypeAliases.js]
"use strict";
function Foo() { }
exports.Foo = Foo;

View File

@ -0,0 +1,10 @@
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
export type Foo = number;
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
export function Foo(): number;
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))
export function Foo(): any {}
>Foo : Symbol(Foo, Decl(exportRedeclarationTypeAliases.ts, 0, 0), Decl(exportRedeclarationTypeAliases.ts, 0, 25), Decl(exportRedeclarationTypeAliases.ts, 1, 30))

View File

@ -0,0 +1,10 @@
=== tests/cases/compiler/exportRedeclarationTypeAliases.ts ===
export type Foo = number;
>Foo : number
export function Foo(): number;
>Foo : () => number
export function Foo(): any {}
>Foo : () => number

View File

@ -0,0 +1,4 @@
// @module: commonjs
export type Foo = number;
export function Foo(): number;
export function Foo(): any {}