Skip to content

Reissue used before declaration error for const enums in isolatedModules #57174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3924,6 +3924,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else if (result.flags & SymbolFlags.RegularEnum) {
diagnosticMessage = error(errorLocation, Diagnostics.Enum_0_used_before_its_declaration, declarationName);
}
else {
Debug.assert(!!(result.flags & SymbolFlags.ConstEnum));
if (getIsolatedModules(compilerOptions)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (compilerOptions.isolatedModules) {
if (getIsolatedModules(compilerOptions)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would commit this suggestion for you and approve, but GitHub can't handle this on checker.ts 🥲

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫠

I pushed out the change + the relevant missing test case for verbatimModuleSyntax :p

diagnosticMessage = error(errorLocation, Diagnostics.Enum_0_used_before_its_declaration, declarationName);
}
}

if (diagnosticMessage) {
addRelatedInfo(diagnosticMessage, createDiagnosticForNode(declaration, Diagnostics._0_is_declared_here, declarationName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ blockScopedEnumVariablesUseBeforeDef.ts(2,12): error TS2450: Enum 'E' used befor
function foo2() {
return E.A
const enum E { A }
}
}

const config = {
a: AfterObject.A,
};

const enum AfterObject {
A = 2,
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ function foo1() {
function foo2() {
return E.A
const enum E { A }
}
}

const config = {
a: AfterObject.A,
};

const enum AfterObject {
A = 2,
}


//// [blockScopedEnumVariablesUseBeforeDef.js]
function foo1() {
Expand All @@ -22,3 +31,6 @@ function foo1() {
function foo2() {
return 0 /* E.A */;
}
var config = {
a: 2 /* AfterObject.A */,
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@ function foo2() {
>E : Symbol(E, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 6, 14))
>A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 7, 18))
}

const config = {
>config : Symbol(config, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 10, 5))

a: AfterObject.A,
>a : Symbol(a, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 10, 16))
>AfterObject.A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 14, 24))
>AfterObject : Symbol(AfterObject, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 12, 2))
>A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 14, 24))

};

const enum AfterObject {
>AfterObject : Symbol(AfterObject, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 12, 2))

A = 2,
>A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef.ts, 14, 24))
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@ function foo2() {
>E : E
>A : E.A
}

const config = {
>config : { a: AfterObject; }
>{ a: AfterObject.A,} : { a: AfterObject; }

a: AfterObject.A,
>a : AfterObject
>AfterObject.A : AfterObject
>AfterObject : typeof AfterObject
>A : AfterObject

};

const enum AfterObject {
>AfterObject : AfterObject

A = 2,
>A : AfterObject.A
>2 : 2
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts(2,12): error TS2450: Enum 'E' used before its declaration.
blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts(7,12): error TS2450: Enum 'E' used before its declaration.
blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts(12,8): error TS2450: Enum 'AfterObject' used before its declaration.


==== blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts (3 errors) ====
function foo1() {
return E.A
~
!!! error TS2450: Enum 'E' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts:3:10: 'E' is declared here.
enum E { A }
}

function foo2() {
return E.A
~
!!! error TS2450: Enum 'E' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts:8:16: 'E' is declared here.
const enum E { A }
}

const config = {
a: AfterObject.A,
~~~~~~~~~~~
!!! error TS2450: Enum 'AfterObject' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts:15:12: 'AfterObject' is declared here.
};

const enum AfterObject {
A = 2,
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts] ////

//// [blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts]
function foo1() {
return E.A
enum E { A }
}

function foo2() {
return E.A
const enum E { A }
}

const config = {
a: AfterObject.A,
};

const enum AfterObject {
A = 2,
}


//// [blockScopedEnumVariablesUseBeforeDef_isolatedModules.js]
function foo1() {
return E.A;
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}
function foo2() {
return E.A;
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}
var config = {
a: AfterObject.A,
};
var AfterObject;
(function (AfterObject) {
AfterObject[AfterObject["A"] = 2] = "A";
})(AfterObject || (AfterObject = {}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/compiler/blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts] ////

=== blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts ===
function foo1() {
>foo1 : Symbol(foo1, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 0, 0))

return E.A
>E.A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 2, 12))
>E : Symbol(E, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 1, 14))
>A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 2, 12))

enum E { A }
>E : Symbol(E, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 1, 14))
>A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 2, 12))
}

function foo2() {
>foo2 : Symbol(foo2, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 3, 1))

return E.A
>E.A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 7, 18))
>E : Symbol(E, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 6, 14))
>A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 7, 18))

const enum E { A }
>E : Symbol(E, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 6, 14))
>A : Symbol(E.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 7, 18))
}

const config = {
>config : Symbol(config, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 10, 5))

a: AfterObject.A,
>a : Symbol(a, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 10, 16))
>AfterObject.A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 14, 24))
>AfterObject : Symbol(AfterObject, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 12, 2))
>A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 14, 24))

};

const enum AfterObject {
>AfterObject : Symbol(AfterObject, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 12, 2))

A = 2,
>A : Symbol(AfterObject.A, Decl(blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts, 14, 24))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tests/cases/compiler/blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts] ////

=== blockScopedEnumVariablesUseBeforeDef_isolatedModules.ts ===
function foo1() {
>foo1 : () => E

return E.A
>E.A : E
>E : typeof E
>A : E

enum E { A }
>E : E
>A : E.A
}

function foo2() {
>foo2 : () => E

return E.A
>E.A : E
>E : typeof E
>A : E

const enum E { A }
>E : E
>A : E.A
}

const config = {
>config : { a: AfterObject; }
>{ a: AfterObject.A,} : { a: AfterObject; }

a: AfterObject.A,
>a : AfterObject
>AfterObject.A : AfterObject
>AfterObject : typeof AfterObject
>A : AfterObject

};

const enum AfterObject {
>AfterObject : AfterObject

A = 2,
>A : AfterObject.A
>2 : 2
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts(2,12): error TS2450: Enum 'E' used before its declaration.
blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts(7,12): error TS2450: Enum 'E' used before its declaration.
blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts(12,8): error TS2450: Enum 'AfterObject' used before its declaration.


==== blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts (3 errors) ====
function foo1() {
return E.A
~
!!! error TS2450: Enum 'E' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts:3:10: 'E' is declared here.
enum E { A }
}

function foo2() {
return E.A
~
!!! error TS2450: Enum 'E' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts:8:16: 'E' is declared here.
const enum E { A }
}

const config = {
a: AfterObject.A,
~~~~~~~~~~~
!!! error TS2450: Enum 'AfterObject' used before its declaration.
!!! related TS2728 blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts:15:12: 'AfterObject' is declared here.
};

const enum AfterObject {
A = 2,
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts] ////

//// [blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.ts]
function foo1() {
return E.A
enum E { A }
}

function foo2() {
return E.A
const enum E { A }
}

const config = {
a: AfterObject.A,
};

const enum AfterObject {
A = 2,
}


//// [blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.js]
function foo1() {
return E.A;
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}
function foo2() {
return E.A;
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}
var config = {
a: AfterObject.A,
};
var AfterObject;
(function (AfterObject) {
AfterObject[AfterObject["A"] = 2] = "A";
})(AfterObject || (AfterObject = {}));
Loading