-
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
isolatedModules errors for non-literal enum initializers #56736
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c51645a
isolatedModules errors for non-literal enum initializers
frigus02 93b0b89
use getIsolatedModules() & isSyntacticallyNumeric/String
frigus02 af97344
update error message
frigus02 6325c1d
Remove @noEmit from tests
frigus02 e112f4d
No reverse mapping for syntactically known strings
frigus02 dd647aa
Revert "No reverse mapping for syntactically known strings"
frigus02 baacbe8
Merge remote-tracking branch 'upstream/main' into enum-initializers
frigus02 42eb9b1
Update based on #57686
frigus02 dafdaec
Rename to isSyntacticallyNumericConstant
frigus02 bd28afa
Update string error message
frigus02 8b926c9
Update isolatedModulesGlobalNamespacesAndEnums baseline
frigus02 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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/enumNoInitializerFollowsNonLiteralInitializer.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
bad.ts(4,5): error TS18056: Enum member following a non-literal numeric member must have an initializer when 'isolatedModules' is enabled. | ||
|
||
|
||
==== ./helpers.ts (0 errors) ==== | ||
export const foo = 2; | ||
|
||
==== ./bad.ts (1 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b, | ||
~ | ||
!!! error TS18056: Enum member following a non-literal numeric member must have an initializer when 'isolatedModules' is enabled. | ||
} | ||
|
||
==== ./good.ts (0 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b = 3, | ||
} | ||
enum B { | ||
a = 1 + 1, | ||
b, | ||
} | ||
enum C { | ||
a = +2, | ||
b, | ||
} | ||
enum D { | ||
a = (2), | ||
b, | ||
} | ||
|
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/enumNoInitializerFollowsNonLiteralInitializer.js
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//// [tests/cases/compiler/enumNoInitializerFollowsNonLiteralInitializer.ts] //// | ||
|
||
//// [helpers.ts] | ||
export const foo = 2; | ||
|
||
//// [bad.ts] | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b, | ||
} | ||
|
||
//// [good.ts] | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b = 3, | ||
} | ||
enum B { | ||
a = 1 + 1, | ||
b, | ||
} | ||
enum C { | ||
a = +2, | ||
b, | ||
} | ||
enum D { | ||
a = (2), | ||
b, | ||
} | ||
|
||
|
||
//// [helpers.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.foo = void 0; | ||
exports.foo = 2; | ||
//// [bad.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("./helpers"); | ||
var A; | ||
(function (A) { | ||
A[A["a"] = 2] = "a"; | ||
A[A["b"] = 3] = "b"; | ||
})(A || (A = {})); | ||
//// [good.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("./helpers"); | ||
var A; | ||
(function (A) { | ||
A[A["a"] = 2] = "a"; | ||
A[A["b"] = 3] = "b"; | ||
})(A || (A = {})); | ||
var B; | ||
(function (B) { | ||
B[B["a"] = 2] = "a"; | ||
B[B["b"] = 3] = "b"; | ||
})(B || (B = {})); | ||
var C; | ||
(function (C) { | ||
C[C["a"] = 2] = "a"; | ||
C[C["b"] = 3] = "b"; | ||
})(C || (C = {})); | ||
var D; | ||
(function (D) { | ||
D[D["a"] = 2] = "a"; | ||
D[D["b"] = 3] = "b"; | ||
})(D || (D = {})); |
24 changes: 24 additions & 0 deletions
24
tests/baselines/reference/enumWithNonLiteralStringInitializer.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
bad.ts(3,8): error TS18055: A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled. | ||
|
||
|
||
==== ./helpers.ts (0 errors) ==== | ||
export const foo = 2; | ||
export const bar = "bar"; | ||
|
||
==== ./bad.ts (1 errors) ==== | ||
import { bar } from "./helpers"; | ||
enum A { | ||
a = bar, | ||
~~~ | ||
!!! error TS18055: A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled. | ||
} | ||
|
||
==== ./good.ts (0 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = `${foo}`, | ||
b = "" + 2, | ||
c = 2 + "", | ||
d = ("foo"), | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
tests/baselines/reference/enumWithNonLiteralStringInitializer.js
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//// [tests/cases/compiler/enumWithNonLiteralStringInitializer.ts] //// | ||
|
||
//// [helpers.ts] | ||
export const foo = 2; | ||
export const bar = "bar"; | ||
|
||
//// [bad.ts] | ||
import { bar } from "./helpers"; | ||
enum A { | ||
a = bar, | ||
} | ||
|
||
//// [good.ts] | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = `${foo}`, | ||
b = "" + 2, | ||
c = 2 + "", | ||
d = ("foo"), | ||
} | ||
|
||
|
||
//// [helpers.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bar = exports.foo = void 0; | ||
exports.foo = 2; | ||
exports.bar = "bar"; | ||
//// [bad.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("./helpers"); | ||
var A; | ||
(function (A) { | ||
A["a"] = "bar"; | ||
})(A || (A = {})); | ||
//// [good.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var helpers_1 = require("./helpers"); | ||
var A; | ||
(function (A) { | ||
A["a"] = "2"; | ||
A["b"] = "2"; | ||
A["c"] = "2"; | ||
A["d"] = "foo"; | ||
})(A || (A = {})); |
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
31 changes: 31 additions & 0 deletions
31
tests/cases/compiler/enumNoInitializerFollowsNonLiteralInitializer.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @isolatedModules: true | ||
// @noTypesAndSymbols: true | ||
|
||
// @filename: ./helpers.ts | ||
export const foo = 2; | ||
|
||
// @filename: ./bad.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b, | ||
} | ||
|
||
// @filename: ./good.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b = 3, | ||
} | ||
enum B { | ||
a = 1 + 1, | ||
b, | ||
} | ||
enum C { | ||
a = +2, | ||
b, | ||
} | ||
enum D { | ||
a = (2), | ||
b, | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/cases/compiler/enumWithNonLiteralStringInitializer.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @isolatedModules: true | ||
// @noTypesAndSymbols: true | ||
|
||
// @filename: ./helpers.ts | ||
export const foo = 2; | ||
export const bar = "bar"; | ||
|
||
// @filename: ./bad.ts | ||
import { bar } from "./helpers"; | ||
enum A { | ||
a = bar, | ||
} | ||
|
||
// @filename: ./good.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = `${foo}`, | ||
b = "" + 2, | ||
c = 2 + "", | ||
d = ("foo"), | ||
} |
Oops, something went wrong.
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.
Paraphrasing a suggestion from @DanielRosenwasser:
We could totally do a quick fix to wrap it in a template literal but I think this error is going to be so niche that I’m not sure it’s worth 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.
I like the suggested error message. But what do I substitute
{0}
with? The initializer can be a relatively complex expression. Is there a utility function to stringify that? Or should I use the enum member name or even the computed value?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.
I forgot to include that; Daniel’s suggestion was
'EnumName.MemberName'
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.
Sounds good. Do we want to handle member names that are not valid identifier? E.g.:
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.
I updated the error message and ignored the non-identifier case for now, because it seems like an extremely rare case. That means it would print
'Foo.not an identifier'
in the error message.