Skip to content

Commit 5637d02

Browse files
committed
it works more
1 parent 6df56ca commit 5637d02

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33862,7 +33862,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3386233862
return objectType;
3386333863
}
3386433864

33865-
if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression)) {
33865+
// TODO(jakebailey): write test case
33866+
if (isConstEnumObjectType(objectType) && !isStringLiteralLike(indexExpression) && !isPreservedConstEnum(objectType)) {
3386633867
error(indexExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
3386733868
return errorType;
3386833869
}
@@ -39793,26 +39794,29 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3979339794
(node.parent.kind === SyntaxKind.TypeQuery && (node.parent as TypeQueryNode).exprName === node)) ||
3979439795
(node.parent.kind === SyntaxKind.ExportSpecifier); // We allow reexporting const enums
3979539796

39796-
if (!ok) {
39797-
// TODO(jakebailey): dedupe
39798-
Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39799-
const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39800-
const otherFile = getSourceFileOfNode(constEnumDeclaration);
39801-
const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(otherFile.resolvedPath);
39802-
const isLegalValueAccess = !otherFile.isDeclarationFile || (redirect && shouldPreserveConstEnums(redirect.commandLine.options))
39803-
if (!isLegalValueAccess) {
39804-
error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
39797+
if (!ok || getIsolatedModules(compilerOptions)) {
39798+
if (!isPreservedConstEnum(type)) {
39799+
if (!ok) {
39800+
// TODO(jakebailey): write negative test case
39801+
// TODO(jakebailey): make error message mention const enum preservation
39802+
error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment_or_type_query);
39803+
} else if (type.symbol.valueDeclaration!.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node)) {
39804+
error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
39805+
}
3980539806
}
3980639807
}
39808+
}
3980739809

39808-
if (getIsolatedModules(compilerOptions)) {
39809-
Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39810-
const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39811-
const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(getSourceFileOfNode(constEnumDeclaration).resolvedPath);
39812-
if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node) && (!redirect || !shouldPreserveConstEnums(redirect.commandLine.options))) {
39813-
error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
39814-
}
39810+
function isPreservedConstEnum(type: Type) {
39811+
Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
39812+
const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
39813+
const otherFile = getSourceFileOfNode(constEnumDeclaration);
39814+
if (!otherFile.isDeclarationFile) {
39815+
// This file can only have come from the current project.
39816+
return shouldPreserveConstEnums(compilerOptions);
3981539817
}
39818+
const redirect = host.getRedirectReferenceForResolutionFromSourceOfProject(otherFile.resolvedPath);
39819+
return redirect && shouldPreserveConstEnums(redirect.commandLine.options)
3981639820
}
3981739821

3981839822
function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {

0 commit comments

Comments
 (0)