Fix error when exporting const enums (#33060) (#34721)

* Allow export declaration to reference const enums

* Update baselines

* Add test to verify reexported const enums are elided
This commit is contained in:
Evan Cahill 2019-10-25 16:04:44 -07:00 committed by Wesley Wigham
parent f12eee2e4b
commit 88f3593742
11 changed files with 224 additions and 241 deletions

View File

@ -27465,7 +27465,7 @@ namespace ts {
(node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).expression === node) ||
((node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName) && isInRightSideOfImportOrExportAssignment(<Identifier>node) ||
(node.parent.kind === SyntaxKind.TypeQuery && (<TypeQueryNode>node.parent).exprName === node)) ||
(node.parent.kind === SyntaxKind.ExportSpecifier && (compilerOptions.preserveConstEnums || node.flags & NodeFlags.Ambient)); // We allow reexporting const enums
(node.parent.kind === SyntaxKind.ExportSpecifier); // We allow reexporting const enums
if (!ok) {
error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);

View File

@ -0,0 +1,59 @@
//// [tests/cases/compiler/constEnumNoEmitReexport.ts] ////
//// [ConstEnum.ts]
export const enum MyConstEnum {
Foo,
Bar
};
//// [ImportExport.ts]
import { MyConstEnum } from './ConstEnum';
export { MyConstEnum };
//// [ImportExportDefault.ts]
import { MyConstEnum } from './ConstEnum';
export default MyConstEnum;
//// [ReExportDefault.ts]
export { MyConstEnum as default } from './ConstEnum';
//// [ReExport.ts]
export { MyConstEnum } from './ConstEnum';
//// [Usage1.ts]
import MyConstEnum1 from './ImportExportDefault';
import MyConstEnum2 from './ReExportDefault';
MyConstEnum1.Foo;
MyConstEnum2.Foo;
//// [Usage2.ts]
import { MyConstEnum } from './ImportExport';
MyConstEnum.Foo;
//// [Usage3.ts]
import { MyConstEnum } from './ReExport';
MyConstEnum.Foo;
//// [ConstEnum.js]
"use strict";
exports.__esModule = true;
;
//// [ImportExport.js]
"use strict";
exports.__esModule = true;
//// [ImportExportDefault.js]
"use strict";
exports.__esModule = true;
//// [ReExportDefault.js]
"use strict";
exports.__esModule = true;
//// [ReExport.js]
"use strict";
exports.__esModule = true;
//// [Usage1.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
0 /* Foo */;
//// [Usage2.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
//// [Usage3.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;

View File

@ -0,0 +1,69 @@
=== tests/cases/compiler/ConstEnum.ts ===
export const enum MyConstEnum {
>MyConstEnum : Symbol(MyConstEnum, Decl(ConstEnum.ts, 0, 0))
Foo,
>Foo : Symbol(MyConstEnum.Foo, Decl(ConstEnum.ts, 0, 31))
Bar
>Bar : Symbol(MyConstEnum.Bar, Decl(ConstEnum.ts, 1, 8))
};
=== tests/cases/compiler/ImportExport.ts ===
import { MyConstEnum } from './ConstEnum';
>MyConstEnum : Symbol(MyConstEnum, Decl(ImportExport.ts, 0, 8))
export { MyConstEnum };
>MyConstEnum : Symbol(MyConstEnum, Decl(ImportExport.ts, 1, 8))
=== tests/cases/compiler/ImportExportDefault.ts ===
import { MyConstEnum } from './ConstEnum';
>MyConstEnum : Symbol(MyConstEnum, Decl(ImportExportDefault.ts, 0, 8))
export default MyConstEnum;
>MyConstEnum : Symbol(MyConstEnum, Decl(ImportExportDefault.ts, 0, 8))
=== tests/cases/compiler/ReExportDefault.ts ===
export { MyConstEnum as default } from './ConstEnum';
>MyConstEnum : Symbol(MyConstEnum, Decl(ConstEnum.ts, 0, 0))
>default : Symbol(default, Decl(ReExportDefault.ts, 0, 8))
=== tests/cases/compiler/ReExport.ts ===
export { MyConstEnum } from './ConstEnum';
>MyConstEnum : Symbol(MyConstEnum, Decl(ReExport.ts, 0, 8))
=== tests/cases/compiler/Usage1.ts ===
import MyConstEnum1 from './ImportExportDefault';
>MyConstEnum1 : Symbol(MyConstEnum1, Decl(Usage1.ts, 0, 6))
import MyConstEnum2 from './ReExportDefault';
>MyConstEnum2 : Symbol(MyConstEnum2, Decl(Usage1.ts, 1, 6))
MyConstEnum1.Foo;
>MyConstEnum1.Foo : Symbol(MyConstEnum1.Foo, Decl(ConstEnum.ts, 0, 31))
>MyConstEnum1 : Symbol(MyConstEnum1, Decl(Usage1.ts, 0, 6))
>Foo : Symbol(MyConstEnum1.Foo, Decl(ConstEnum.ts, 0, 31))
MyConstEnum2.Foo;
>MyConstEnum2.Foo : Symbol(MyConstEnum1.Foo, Decl(ConstEnum.ts, 0, 31))
>MyConstEnum2 : Symbol(MyConstEnum2, Decl(Usage1.ts, 1, 6))
>Foo : Symbol(MyConstEnum1.Foo, Decl(ConstEnum.ts, 0, 31))
=== tests/cases/compiler/Usage2.ts ===
import { MyConstEnum } from './ImportExport';
>MyConstEnum : Symbol(MyConstEnum, Decl(Usage2.ts, 0, 8))
MyConstEnum.Foo;
>MyConstEnum.Foo : Symbol(MyConstEnum.Foo, Decl(ConstEnum.ts, 0, 31))
>MyConstEnum : Symbol(MyConstEnum, Decl(Usage2.ts, 0, 8))
>Foo : Symbol(MyConstEnum.Foo, Decl(ConstEnum.ts, 0, 31))
=== tests/cases/compiler/Usage3.ts ===
import { MyConstEnum } from './ReExport';
>MyConstEnum : Symbol(MyConstEnum, Decl(Usage3.ts, 0, 8))
MyConstEnum.Foo;
>MyConstEnum.Foo : Symbol(MyConstEnum.Foo, Decl(ConstEnum.ts, 0, 31))
>MyConstEnum : Symbol(MyConstEnum, Decl(Usage3.ts, 0, 8))
>Foo : Symbol(MyConstEnum.Foo, Decl(ConstEnum.ts, 0, 31))

View File

@ -0,0 +1,69 @@
=== tests/cases/compiler/ConstEnum.ts ===
export const enum MyConstEnum {
>MyConstEnum : MyConstEnum
Foo,
>Foo : MyConstEnum.Foo
Bar
>Bar : MyConstEnum.Bar
};
=== tests/cases/compiler/ImportExport.ts ===
import { MyConstEnum } from './ConstEnum';
>MyConstEnum : typeof MyConstEnum
export { MyConstEnum };
>MyConstEnum : typeof MyConstEnum
=== tests/cases/compiler/ImportExportDefault.ts ===
import { MyConstEnum } from './ConstEnum';
>MyConstEnum : typeof MyConstEnum
export default MyConstEnum;
>MyConstEnum : MyConstEnum
=== tests/cases/compiler/ReExportDefault.ts ===
export { MyConstEnum as default } from './ConstEnum';
>MyConstEnum : typeof import("tests/cases/compiler/ConstEnum").MyConstEnum
>default : typeof import("tests/cases/compiler/ConstEnum").MyConstEnum
=== tests/cases/compiler/ReExport.ts ===
export { MyConstEnum } from './ConstEnum';
>MyConstEnum : typeof import("tests/cases/compiler/ConstEnum").MyConstEnum
=== tests/cases/compiler/Usage1.ts ===
import MyConstEnum1 from './ImportExportDefault';
>MyConstEnum1 : typeof MyConstEnum1
import MyConstEnum2 from './ReExportDefault';
>MyConstEnum2 : typeof MyConstEnum1
MyConstEnum1.Foo;
>MyConstEnum1.Foo : MyConstEnum1.Foo
>MyConstEnum1 : typeof MyConstEnum1
>Foo : MyConstEnum1.Foo
MyConstEnum2.Foo;
>MyConstEnum2.Foo : MyConstEnum1.Foo
>MyConstEnum2 : typeof MyConstEnum1
>Foo : MyConstEnum1.Foo
=== tests/cases/compiler/Usage2.ts ===
import { MyConstEnum } from './ImportExport';
>MyConstEnum : typeof MyConstEnum
MyConstEnum.Foo;
>MyConstEnum.Foo : MyConstEnum.Foo
>MyConstEnum : typeof MyConstEnum
>Foo : MyConstEnum.Foo
=== tests/cases/compiler/Usage3.ts ===
import { MyConstEnum } from './ReExport';
>MyConstEnum : typeof MyConstEnum
MyConstEnum.Foo;
>MyConstEnum.Foo : MyConstEnum.Foo
>MyConstEnum : typeof MyConstEnum
>Foo : MyConstEnum.Foo

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
var v = 1;
function f() { }
class C {
}
interface I {
}
enum E {
A, B, C
}
const enum D {
A, B, C
}
module M {
export var x;
}
module N {
export interface I {
}
}
type T = number;
import a = M.x;
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v, f, C, I, E, D, M, N, T, a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
var v = 1;
function f() { }
class C {
}
interface I {
}
enum E {
A, B, C
}
const enum D {
A, B, C
}
module M {
export var x;
}
module N {
export interface I {
}
}
type T = number;
import a = M.x;
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v, f, C, I, E, D, M, N, T, a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
var v = 1;
function f() { }
class C {
}
interface I {
}
enum E {
A, B, C
}
const enum D {
A, B, C
}
module M {
export var x;
}
module N {
export interface I {
}
}
type T = number;
import a = M.x;
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v, f, C, I, E, D, M, N, T, a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,55): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
export var v = 1;
export function f() { }
export class C {
}
export interface I {
}
export enum E {
A, B, C
}
export const enum D {
A, B, C
}
export module M {
export var x;
}
export module N {
export interface I {
}
}
export type T = number;
export import a = M.x;
export { v as v1, f as f1, C as C1, I as I1, E as E1, D as D1, M as M1, N as N1, T as T1, a as a1 };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,55): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
export var v = 1;
export function f() { }
export class C {
}
export interface I {
}
export enum E {
A, B, C
}
export const enum D {
A, B, C
}
export module M {
export var x;
}
export module N {
export interface I {
}
}
export type T = number;
export import a = M.x;
export { v as v1, f as f1, C as C1, I as I1, E as E1, D as D1, M as M1, N as N1, T as T1, a as a1 };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -1,40 +0,0 @@
tests/cases/conformance/es6/modules/t1.ts(23,55): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
tests/cases/conformance/es6/modules/t3.ts(2,25): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t1.ts (1 errors) ====
export var v = 1;
export function f() { }
export class C {
}
export interface I {
}
export enum E {
A, B, C
}
export const enum D {
A, B, C
}
export module M {
export var x;
}
export module N {
export interface I {
}
}
export type T = number;
export import a = M.x;
export { v as v1, f as f1, C as C1, I as I1, E as E1, D as D1, M as M1, N as N1, T as T1, a as a1 };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
==== tests/cases/conformance/es6/modules/t2.ts (0 errors) ====
export { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
==== tests/cases/conformance/es6/modules/t3.ts (1 errors) ====
import { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N, T1 as T, a1 as a } from "./t1";
export { v, f, C, I, E, D, M, N, T, a };
~
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

View File

@ -0,0 +1,26 @@
// @filename: ConstEnum.ts
export const enum MyConstEnum {
Foo,
Bar
};
// @filename: ImportExport.ts
import { MyConstEnum } from './ConstEnum';
export { MyConstEnum };
// @filename: ImportExportDefault.ts
import { MyConstEnum } from './ConstEnum';
export default MyConstEnum;
// @filename: ReExportDefault.ts
export { MyConstEnum as default } from './ConstEnum';
// @filename: ReExport.ts
export { MyConstEnum } from './ConstEnum';
// @filename: Usage1.ts
import MyConstEnum1 from './ImportExportDefault';
import MyConstEnum2 from './ReExportDefault';
MyConstEnum1.Foo;
MyConstEnum2.Foo;
// @filename: Usage2.ts
import { MyConstEnum } from './ImportExport';
MyConstEnum.Foo;
// @filename: Usage3.ts
import { MyConstEnum } from './ReExport';
MyConstEnum.Foo;