-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Implement erasable Enum Annotations #61414
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
Open
caitp
wants to merge
14
commits into
microsoft:main
Choose a base branch
from
caitp:AsEnumAlternate2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e6bd858
WIP -- Add Tests
caitp 8e3cd90
update tests and baselines
caitp 9cb6f7a
implement enum type annotation and enum literal syntax
caitp 335334c
Fix issues reporting first enum member not having an initializer
caitp 2a72167
nit: remove reference to syntax (TODO: rename the test files to refe…
caitp 52aec09
rename asEnum* to enumLiteral*
caitp 3650dd4
fix the comments being emitted (removing all comments from each member)
caitp 85401e6
Remove commented out lines, fix warnings and update typescript.d.ts t…
caitp d002e9e
run hereby format
caitp 303bf25
Fix conformance tests, add additional test cases to enumLiteralBaiscs…
caitp d40b1d0
EnumLiteralExpression name is no longer an AST node, bunch of linter …
caitp 380c082
Remove empty branch
caitp 34312af
attempt to make getTypeFromTypeReference changes easier to read
caitp 7a397ea
Refactor isEnumLiteralDeclaration() to allow assumptions about members
caitp 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,7 @@ import { | |
EntityNameOrEntityNameExpression, | ||
entityNameToString, | ||
EnumDeclaration, | ||
EnumLiteralExpression, | ||
EnumMember, | ||
EnumType, | ||
equateValues, | ||
|
@@ -536,7 +537,10 @@ import { | |
isEntityNameExpression, | ||
isEnumConst, | ||
isEnumDeclaration, | ||
isEnumLiteralDeclaration, | ||
isEnumLiteralExpression, | ||
isEnumMember, | ||
isEnumTypeAnnotation, | ||
isExclusivelyTypeOnlyImportOrExport, | ||
isExpandoPropertyDeclaration, | ||
isExportAssignment, | ||
|
@@ -11984,7 +11988,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
// getTypeOfSymbol dispatches some JS merges incorrectly because their symbol flags are not mutually exclusive. | ||
// Re-dispatch based on valueDeclaration.kind instead. | ||
else if (isEnumDeclaration(declaration)) { | ||
else if (isEnumDeclaration(declaration) || isEnumLiteralExpression(declaration)) { | ||
type = getTypeOfFuncClassEnumModule(symbol); | ||
} | ||
else if (isEnumMember(declaration)) { | ||
|
@@ -12876,7 +12880,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
const memberTypeList: Type[] = []; | ||
if (symbol.declarations) { | ||
for (const declaration of symbol.declarations) { | ||
if (declaration.kind === SyntaxKind.EnumDeclaration) { | ||
if (declaration.kind === SyntaxKind.EnumDeclaration || declaration.kind === SyntaxKind.EnumLiteralExpression) { | ||
for (const member of (declaration as EnumDeclaration).members) { | ||
if (hasBindableName(member)) { | ||
const memberSymbol = getSymbolOfDeclaration(member); | ||
|
@@ -16706,6 +16710,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
links.resolvedSymbol = unknownSymbol; | ||
return links.resolvedType = checkExpressionCached(node.parent.expression); | ||
} | ||
// `var MyEnum: enum = { FirstValue: 1, SecondValue: 2 }` should resolve to a union of the enum values. | ||
if (node.parent && isEnumLiteralDeclaration(node.parent)) { | ||
return links.resolvedType = checkExpressionCached(node.parent.initializer); | ||
} | ||
let symbol: Symbol | undefined; | ||
let type: Type | undefined; | ||
const meaning = SymbolFlags.Type; | ||
|
@@ -16760,6 +16768,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.ClassDeclaration: | ||
case SyntaxKind.InterfaceDeclaration: | ||
case SyntaxKind.EnumDeclaration: | ||
case SyntaxKind.EnumLiteralExpression: | ||
return declaration; | ||
} | ||
} | ||
|
@@ -41109,6 +41118,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return checkArrayLiteral(node as ArrayLiteralExpression, checkMode, forceTuple); | ||
case SyntaxKind.ObjectLiteralExpression: | ||
return checkObjectLiteral(node as ObjectLiteralExpression, checkMode); | ||
case SyntaxKind.EnumLiteralExpression: | ||
return checkEnumLiteralExpression(node as EnumLiteralExpression); | ||
case SyntaxKind.PropertyAccessExpression: | ||
return checkPropertyAccessExpression(node as PropertyAccessExpression, checkMode); | ||
case SyntaxKind.QualifiedName: | ||
|
@@ -44128,7 +44139,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
checkClassNameCollisionWithObject(name); | ||
} | ||
} | ||
else if (isEnumDeclaration(node)) { | ||
else if (isEnumDeclaration(node) || isEnumLiteralDeclaration(node)) { | ||
checkTypeNameIsReserved(name, Diagnostics.Enum_name_cannot_be_0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too |
||
} | ||
} | ||
|
@@ -47033,16 +47044,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
|
||
function computeEnumMemberValues(node: EnumDeclaration) { | ||
function computeEnumMemberValues(node: EnumDeclaration | EnumLiteralExpression) { | ||
const nodeLinks = getNodeLinks(node); | ||
if (!(nodeLinks.flags & NodeCheckFlags.EnumValuesComputed)) { | ||
nodeLinks.flags |= NodeCheckFlags.EnumValuesComputed; | ||
let autoValue: number | undefined = 0; | ||
// EnumLiteralExpressions are essentially plain ObjectLiteralExpressions and can not have computed values. | ||
const hasComputedValues = !isEnumLiteralExpression(node); | ||
let autoValue: number | undefined = hasComputedValues ? 0 : undefined; | ||
let previous: EnumMember | undefined; | ||
for (const member of node.members) { | ||
const result = computeEnumMemberValue(member, autoValue, previous); | ||
getNodeLinks(member).enumMemberValue = result; | ||
autoValue = typeof result.value === "number" ? result.value + 1 : undefined; | ||
autoValue = (hasComputedValues && typeof result.value === "number") ? result.value + 1 : undefined; | ||
previous = member; | ||
} | ||
} | ||
|
@@ -47090,6 +47103,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
const isConstEnum = isEnumConst(member.parent); | ||
const initializer = member.initializer!; | ||
const result = evaluate(initializer, member); | ||
const isDecl = isEnumDeclaration(member.parent); | ||
if (result.value !== undefined) { | ||
if (isConstEnum && typeof result.value === "number" && !isFinite(result.value)) { | ||
error( | ||
|
@@ -47103,7 +47117,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
error( | ||
initializer, | ||
Diagnostics._0_has_a_string_type_but_must_have_syntactically_recognizable_string_syntax_when_isolatedModules_is_enabled, | ||
`${idText(member.parent.name)}.${getTextOfPropertyName(member.name)}`, | ||
isDecl ? `${idText(member.parent.name)}.${getTextOfPropertyName(member.name)}` : `${member.parent.name}.${getTextOfPropertyName(member.name)}`, | ||
); | ||
} | ||
} | ||
|
@@ -47191,6 +47205,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
addLazyDiagnostic(() => checkEnumDeclarationWorker(node)); | ||
} | ||
|
||
function checkEnumLiteralExpression(node: EnumLiteralExpression) { | ||
addLazyDiagnostic(() => checkEnumDeclarationWorker(node as any)); | ||
return getTypeOfSymbol(getSymbolOfDeclaration(node)); | ||
} | ||
|
||
function checkEnumDeclarationWorker(node: EnumDeclaration) { | ||
// Grammar checking | ||
checkGrammarModifiers(node); | ||
|
@@ -47218,7 +47237,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
const enumIsConst = isEnumConst(node); | ||
// check that const is placed\omitted on all enum declarations | ||
forEach(enumSymbol.declarations, decl => { | ||
if (isEnumDeclaration(decl) && isEnumConst(decl) !== enumIsConst) { | ||
if ((isEnumDeclaration(decl) || isEnumLiteralExpression(decl)) && isEnumConst(decl) !== enumIsConst) { | ||
error(getNameOfDeclaration(decl), Diagnostics.Enum_declarations_must_all_be_const_or_non_const); | ||
} | ||
}); | ||
|
@@ -48854,6 +48873,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.ModuleDeclaration: | ||
copyLocallyVisibleExportSymbols(getSymbolOfDeclaration(location as ModuleDeclaration | SourceFile).exports!, meaning & SymbolFlags.ModuleMember); | ||
break; | ||
case SyntaxKind.EnumLiteralExpression: | ||
case SyntaxKind.EnumDeclaration: | ||
copySymbols(getSymbolOfDeclaration(location as EnumDeclaration).exports!, meaning & SymbolFlags.EnumMember); | ||
break; | ||
|
@@ -49304,6 +49324,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// no other meta properties are valid syntax, thus no others should have symbols | ||
return undefined; | ||
} | ||
else if (isEnumTypeAnnotation(node)) { | ||
// Avoid symbolizing "enum" keywords in type annotations. | ||
return undefined; | ||
} | ||
} | ||
|
||
switch (node.kind) { | ||
|
@@ -49490,6 +49514,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
if (isDeclarationNameOrImportPropertyName(node)) { | ||
const symbol = getSymbolAtLocation(node); | ||
if (symbol) { | ||
if (symbol.valueDeclaration && isEnumLiteralDeclaration(symbol.valueDeclaration)) { | ||
return getDeclaredTypeOfEnum(symbol); | ||
} | ||
return getTypeOfSymbol(symbol); | ||
} | ||
return errorType; | ||
|
@@ -50352,6 +50379,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.PropertyAssignment: | ||
case SyntaxKind.ShorthandPropertyAssignment: | ||
case SyntaxKind.EnumMember: | ||
case SyntaxKind.EnumLiteralExpression: | ||
case SyntaxKind.ObjectLiteralExpression: | ||
case SyntaxKind.FunctionDeclaration: | ||
case SyntaxKind.FunctionExpression: | ||
|
@@ -51329,6 +51357,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
findFirstModifierExcept(node, SyntaxKind.AwaitKeyword) : | ||
find(node.modifiers, isModifier); | ||
case SyntaxKind.EnumDeclaration: | ||
case SyntaxKind.EnumLiteralExpression: | ||
return findFirstModifierExcept(node, SyntaxKind.ConstKeyword); | ||
default: | ||
Debug.assertNever(node); | ||
|
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.