If there is an export default x; alias declaration, disallow other default exports (#19872)

This commit is contained in:
Andy 2017-11-09 12:21:37 -08:00 committed by GitHub
parent a1014b2b13
commit 90ae9ffe6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 53 additions and 5 deletions

View file

@ -2205,15 +2205,14 @@ namespace ts {
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node));
}
else {
// An export default clause with an expression exports a value
// We want to exclude both class and function here, this is necessary to issue an error when there are both
// default export-assignment and default export function and class declaration.
const flags = node.kind === SyntaxKind.ExportAssignment && exportAssignmentIsAlias(<ExportAssignment>node)
const flags = node.kind === SyntaxKind.ExportAssignment && exportAssignmentIsAlias(node)
// An export default clause with an EntityNameExpression exports all meanings of that identifier
? SymbolFlags.Alias
// An export default clause with any other expression exports a value
: SymbolFlags.Property;
declareSymbol(container.symbol.exports, container.symbol, node, flags, SymbolFlags.Property | SymbolFlags.AliasExcludes | SymbolFlags.Class | SymbolFlags.Function);
// If there is an `export default x;` alias declaration, can't `export default` anything else.
// (In contrast, you can still have `export default function f() {}` and `export default interface I {}`.)
declareSymbol(container.symbol.exports, container.symbol, node, flags, SymbolFlags.All);
}
}

View file

@ -3021,6 +3021,10 @@ namespace ts {
Optional = 1 << 24, // Optional property
Transient = 1 << 25, // Transient symbol (created during type check)
/* @internal */
All = FunctionScopedVariable | BlockScopedVariable | Property | EnumMember | Function | Class | Interface | ConstEnum | RegularEnum | ValueModule | NamespaceModule | TypeLiteral
| ObjectLiteral | Method | Constructor | GetAccessor | SetAccessor | Signature | TypeParameter | TypeAlias | ExportValue | Alias | Prototype | ExportStar | Optional | Transient,
Enum = RegularEnum | ConstEnum,
Variable = FunctionScopedVariable | BlockScopedVariable,
Value = Variable | Property | EnumMember | Function | Class | Enum | ValueModule | Method | GetAccessor | SetAccessor,

View file

@ -0,0 +1,13 @@
tests/cases/compiler/exportDefaultAlias_excludesEverything.ts(1,26): error TS2528: A module cannot have multiple default exports.
tests/cases/compiler/exportDefaultAlias_excludesEverything.ts(3,16): error TS2528: A module cannot have multiple default exports.
==== tests/cases/compiler/exportDefaultAlias_excludesEverything.ts (2 errors) ====
export default interface A {}
~
!!! error TS2528: A module cannot have multiple default exports.
interface B {}
export default B;
~
!!! error TS2528: A module cannot have multiple default exports.

View file

@ -0,0 +1,9 @@
//// [exportDefaultAlias_excludesEverything.ts]
export default interface A {}
interface B {}
export default B;
//// [exportDefaultAlias_excludesEverything.js]
"use strict";
exports.__esModule = true;

View file

@ -0,0 +1,10 @@
=== tests/cases/compiler/exportDefaultAlias_excludesEverything.ts ===
export default interface A {}
>A : Symbol(A, Decl(exportDefaultAlias_excludesEverything.ts, 0, 0))
interface B {}
>B : Symbol(B, Decl(exportDefaultAlias_excludesEverything.ts, 0, 29))
export default B;
>B : Symbol(B, Decl(exportDefaultAlias_excludesEverything.ts, 0, 29))

View file

@ -0,0 +1,10 @@
=== tests/cases/compiler/exportDefaultAlias_excludesEverything.ts ===
export default interface A {}
>A : A
interface B {}
>B : B
export default B;
>B : B

View file

@ -0,0 +1,3 @@
export default interface A {}
interface B {}
export default B;