Deprecate literal computed property names in enums - #64358
Open
magic-akari wants to merge 2 commits into
Open
magic-akari wants to merge 2 commits into
magic-akari wants to merge 2 commits into
Conversation
Report a deprecation suggestion for enum members named with computed string literals or template literals without substitutions. Preserve existing compiler behavior and avoid adding suggestions to names that already produce errors. Add a quick fix to replace the computed name with a plain string literal, with support for fix-all quick fixes and source.fixAll. Preserve the decoded member name, including escapes and multiline template contents. Add regression coverage for diagnostics, individual and batch fixes, and invalid dynamic or numeric names. Refs microsoft#42468
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The code fix can silently delete comments contained inside computed property brackets.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds a gradual deprecation path for literal computed enum member names and an automated conversion fix.
Changes:
- Reports deprecated computed string/template enum names.
- Adds individual, Fix All, and
source.fixAllconversions. - Adds diagnostic and code-fix coverage.
| File | Description |
|---|---|
tsc/internal/ls/codeactions.go |
Registers the new code-fix provider. |
tsc/internal/ls/codeactions_convertcomputedenummembername.go |
Implements computed-name conversions. |
tsc/internal/fourslash/tests/enumComputedPropertyNameError_test.go |
Verifies existing errors remain unchanged. |
tsc/internal/fourslash/tests/enumComputedPropertyNameDeprecated_test.go |
Verifies deprecation suggestions. |
tsc/internal/fourslash/tests/codeFixEnumComputedPropertyNameAll_test.go |
Tests batch fixes. |
tsc/internal/fourslash/tests/codeFixEnumComputedPropertyName_test.go |
Tests individual fixes and escaping. |
tsc/internal/diagnostics/diagnostics_generated.go |
Adds generated diagnostic definitions. |
tsc/internal/diagnostics/diagnosticMessages.json |
Defines new diagnostic messages. |
tsc/internal/checker/checker.go |
Emits the deprecation suggestion. |
Files not reviewed (1)
- tsc/internal/diagnostics/diagnostics_generated.go: Generated file
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The implementation matches the stated migration behavior and includes comprehensive focused coverage.
Review effort: Balanced
Findings: None
Resolved since last review (1)
Files not reviewed (1)
- tsc/internal/diagnostics/diagnostics_generated.go: Generated file
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

TypeScript already reports “Computed property names are not allowed in enums.” However, it makes an exception for string literals and template literals without substitutions. As Anders noted in the issue, disallowing computed names altogether would match the intent of the existing diagnostic, but compatibility is a concern.
The accepted forms do not add expressive power: single-quoted strings, double-quoted strings, and template literals without substitutions can all be written as ordinary string literal member names. References to variables and expressions such as
["a" + "b"]are already rejected.For example:
This PR provides a gradual migration path without introducing new compilation errors. It adds a deprecation suggestion for the accepted computed names and a quick fix to convert them to ordinary string literals. Individual fixes, Fix All, and
source.fixAllare supported, with decoded member names preserved.Names that already produce errors retain their existing diagnostics without an additional deprecation suggestion. Tests cover these cases, individual and batch fixes, and escaped and multiline literal contents.