Skip to content

Commit 0d3c481

Browse files
Declaration emit should retain (instead of elide) unresolved computed names (#58428)
Co-authored-by: TypeScript Bot <[email protected]>
1 parent 16beff1 commit 0d3c481

12 files changed

+970
-936
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8459,7 +8459,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84598459
);
84608460
}
84618461
if (isNamedDeclaration(node) && node.name.kind === SyntaxKind.ComputedPropertyName && !isLateBindableName(node.name)) {
8462-
return undefined;
8462+
if (!(context.flags & NodeBuilderFlags.AllowUnresolvedComputedNames && hasDynamicName(node) && isEntityNameExpression(node.name.expression) && checkComputedPropertyName(node.name).flags & TypeFlags.Any)) {
8463+
return undefined;
8464+
}
84638465
}
84648466
if (
84658467
(isFunctionLike(node) && !node.type)

src/compiler/transformers/declarations.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals
233233
NodeBuilderFlags.UseTypeOfFunction |
234234
NodeBuilderFlags.UseStructuralFallback |
235235
NodeBuilderFlags.AllowEmptyTuple |
236+
NodeBuilderFlags.AllowUnresolvedComputedNames |
236237
NodeBuilderFlags.GenerateNamesForShadowedTypeParams |
237238
NodeBuilderFlags.NoTruncation;
238239

@@ -1006,7 +1007,12 @@ export function transformDeclarations(context: TransformationContext) {
10061007
) {
10071008
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
10081009
}
1009-
return;
1010+
if (!isEntityNameExpression(input.name.expression)) {
1011+
return;
1012+
}
1013+
// A.B.C that is not late bound - usually this means the expression did not resolve.
1014+
// Check the entity name, and copy the declaration, rather than elide it (there's
1015+
// probably a checker error in the input, but this is most likely the desired output).
10101016
}
10111017
}
10121018

@@ -1778,7 +1784,7 @@ export function transformDeclarations(context: TransformationContext) {
17781784
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNodeName(node);
17791785
}
17801786
errorNameNode = (node as NamedDeclaration).name;
1781-
Debug.assert(resolver.isLateBound(getParseTreeNode(node) as Declaration)); // Should only be called with dynamic names
1787+
Debug.assert(hasDynamicName(node as NamedDeclaration)); // Should only be called with dynamic names
17821788
const decl = node as NamedDeclaration as LateBoundDeclaration;
17831789
const entityName = decl.name.expression;
17841790
checkEntityNameVisibility(entityName, enclosingDeclaration);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5431,6 +5431,7 @@ export const enum NodeBuilderFlags {
54315431
// Errors (cont.)
54325432
AllowNodeModulesRelativePaths = 1 << 26,
54335433
/** @internal */ DoNotIncludeSymbolChain = 1 << 27, // Skip looking up and printing an accessible symbol chain
5434+
/** @internal */ AllowUnresolvedComputedNames = 1 << 32,
54345435

54355436
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifiedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection | AllowEmptyTuple | AllowEmptyIndexInfoType | AllowNodeModulesRelativePaths,
54365437

tests/baselines/reference/complicatedPrivacy.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ module m1 {
8484
}
8585

8686
export function f4(arg1:
87-
>f4 : (arg1: {}) => void
88-
> : ^ ^^ ^^^^^^^^^
87+
>f4 : (arg1: { [number]: C1; }) => void
88+
> : ^ ^^ ^^^^^^^^^
8989
>arg1 : {}
9090
> : ^^
9191
{

tests/baselines/reference/giant.errors.txt

Lines changed: 227 additions & 274 deletions
Large diffs are not rendered by default.

tests/baselines/reference/giant.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
MAX DEPTH 3 LEVELS
1616
*/
17+
const p = "propName";
1718
var V;
1819
function F() { };
1920
class C {
@@ -701,6 +702,7 @@ define(["require", "exports"], function (require, exports) {
701702
702703
MAX DEPTH 3 LEVELS
703704
*/
705+
var p = "propName";
704706
var V;
705707
function F() { }
706708
;
@@ -1114,6 +1116,7 @@ define(["require", "exports"], function (require, exports) {
11141116

11151117

11161118
//// [giant.d.ts]
1119+
declare const p = "propName";
11171120
export declare var eV: any;
11181121
export declare function eF(): void;
11191122
export declare class eC {
@@ -1153,6 +1156,7 @@ export interface eI {
11531156
new (...p3: any[]): any;
11541157
new (p4: string, p5?: string): any;
11551158
new (p6: string, ...p7: any[]): any;
1159+
[p]: any;
11561160
[p1: string]: any;
11571161
[p2: string, p3: number]: any;
11581162
p: any;
@@ -1205,6 +1209,7 @@ export declare namespace eM {
12051209
new (...p3: any[]): any;
12061210
new (p4: string, p5?: string): any;
12071211
new (p6: string, ...p7: any[]): any;
1212+
[p]: any;
12081213
[p1: string]: any;
12091214
[p2: string, p3: number]: any;
12101215
p: any;
@@ -1320,6 +1325,7 @@ export declare namespace eaM {
13201325
new (...p3: any[]): any;
13211326
new (p4: string, p5?: string): any;
13221327
new (p6: string, ...p7: any[]): any;
1328+
[p]: any;
13231329
[p1: string]: any;
13241330
[p2: string, p3: number]: any;
13251331
p: any;
@@ -1379,6 +1385,7 @@ export declare namespace eaM {
13791385
new (...p3: any[]): any;
13801386
new (p4: string, p5?: string): any;
13811387
new (p6: string, ...p7: any[]): any;
1388+
[p]: any;
13821389
[p1: string]: any;
13831390
[p2: string, p3: number]: any;
13841391
p: any;
@@ -1406,3 +1413,4 @@ export declare namespace eaM {
14061413
namespace eM { }
14071414
}
14081415
}
1416+
export {};

0 commit comments

Comments
 (0)