-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Change isolatedModules to allow const enum declaration and disallow access #28465
Merged
RyanCavanaugh
merged 4 commits into
microsoft:master
from
alangpierce:enforce-const-enum-access-for-isolatedmodules
Feb 6, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
293eba6
Change isolatedModules to allow const enum declaration and disallow a…
alangpierce a60795f
Merge remote-tracking branch 'origin/master' into enforce-const-enum-…
alangpierce beae9d7
Accept whitespace change in baseline after merge
alangpierce 942b020
Merge remote-tracking branch 'origin/master' into enforce-const-enum-…
alangpierce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
tests/baselines/reference/isolatedModulesAmbientConstEnum.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
tests/cases/compiler/file1.ts(1,20): error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided. | ||
tests/cases/compiler/file1.ts(2,16): error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided. | ||
|
||
|
||
==== tests/cases/compiler/file1.ts (1 errors) ==== | ||
declare const enum E { X = 1} | ||
~ | ||
!!! error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided. | ||
export var y; | ||
export var y = E.X; | ||
~ | ||
!!! error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//// [file1.ts] | ||
declare const enum E { X = 1} | ||
export var y; | ||
export var y = E.X; | ||
|
||
|
||
//// [file1.js] | ||
export var y; | ||
export var y = E.X; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
// @filename: file1.ts | ||
|
||
declare const enum E { X = 1} | ||
export var y; | ||
export var y = E.X; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this error be any other file than the declaration of const enum file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention with this change is for the error to be on the access, not on the declaration, so in practice it could be in a completely different file than the const enum declaration. In a typical case, I might import the
chalk
library, which shouldn't error, but any attempt to use theLevel
enum should error.I could see an argument for putting an additional error on the const enum (like before), but the point of this change is that simply declaring a const enum is not an error anymore; the error is if you try to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the error is on access but declaring const enum a {} and using a in module1 shouldn't cause error. It should cause when accessing in module2...n instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I just tested it a bit more, and I believe the error still makes sense even within the same file. To be clear, this only happens with
isolatedModules
enabled and only happens for ambient const enums (thedeclare
is necessary in the code above example).From my testing, it looks like with
isolatedModules
enabled, const enums are always compiled as plain enums and const enum accesses are always compiled as plain enum accesses, even when the const enum is in the same file. That means that in this code snippet, the first line is erased (since it's adeclare
) and the second line is compiled as-is asexport var y = E.X
. WithisolatedModules
, TypeScript will not inline the constant even within the same file, so this code would crash at runtime (becauseE
doesn't exist) even though it would work at runtime withoutisolatedModules
. So I think it's fair for TS to error here.It certainly seems like a reasonable future improvement to implement inlining within the same file even when
isolatedModules
is on. In that case, this error could be limited to cross-file accesses, but for now, even same-file ambient const enum accesses need to be errors. This would also require the Babel implementation to implement same-file constant inlining.