Skip to content

Commit

Permalink
Declaration emit should retain (instead of elide) unresolved computed…
Browse files Browse the repository at this point in the history
… names (#58428)

Co-authored-by: TypeScript Bot <[email protected]>
  • Loading branch information
weswigham and typescript-bot committed May 7, 2024
1 parent 16beff1 commit 0d3c481
Show file tree
Hide file tree
Showing 12 changed files with 970 additions and 936 deletions.
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8459,7 +8459,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
);
}
if (isNamedDeclaration(node) && node.name.kind === SyntaxKind.ComputedPropertyName && !isLateBindableName(node.name)) {
return undefined;
if (!(context.flags & NodeBuilderFlags.AllowUnresolvedComputedNames && hasDynamicName(node) && isEntityNameExpression(node.name.expression) && checkComputedPropertyName(node.name).flags & TypeFlags.Any)) {
return undefined;
}
}
if (
(isFunctionLike(node) && !node.type)
Expand Down
10 changes: 8 additions & 2 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals
NodeBuilderFlags.UseTypeOfFunction |
NodeBuilderFlags.UseStructuralFallback |
NodeBuilderFlags.AllowEmptyTuple |
NodeBuilderFlags.AllowUnresolvedComputedNames |
NodeBuilderFlags.GenerateNamesForShadowedTypeParams |
NodeBuilderFlags.NoTruncation;

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

Expand Down Expand Up @@ -1778,7 +1784,7 @@ export function transformDeclarations(context: TransformationContext) {
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNodeName(node);
}
errorNameNode = (node as NamedDeclaration).name;
Debug.assert(resolver.isLateBound(getParseTreeNode(node) as Declaration)); // Should only be called with dynamic names
Debug.assert(hasDynamicName(node as NamedDeclaration)); // Should only be called with dynamic names
const decl = node as NamedDeclaration as LateBoundDeclaration;
const entityName = decl.name.expression;
checkEntityNameVisibility(entityName, enclosingDeclaration);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5431,6 +5431,7 @@ export const enum NodeBuilderFlags {
// Errors (cont.)
AllowNodeModulesRelativePaths = 1 << 26,
/** @internal */ DoNotIncludeSymbolChain = 1 << 27, // Skip looking up and printing an accessible symbol chain
/** @internal */ AllowUnresolvedComputedNames = 1 << 32,

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

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/complicatedPrivacy.types
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module m1 {
}

export function f4(arg1:
>f4 : (arg1: {}) => void
> : ^ ^^ ^^^^^^^^^
>f4 : (arg1: { [number]: C1; }) => void
> : ^ ^^ ^^^^^^^^^
>arg1 : {}
> : ^^
{
Expand Down
501 changes: 227 additions & 274 deletions tests/baselines/reference/giant.errors.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/baselines/reference/giant.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MAX DEPTH 3 LEVELS
*/
const p = "propName";
var V;
function F() { };
class C {
Expand Down Expand Up @@ -701,6 +702,7 @@ define(["require", "exports"], function (require, exports) {
MAX DEPTH 3 LEVELS
*/
var p = "propName";
var V;
function F() { }
;
Expand Down Expand Up @@ -1114,6 +1116,7 @@ define(["require", "exports"], function (require, exports) {


//// [giant.d.ts]
declare const p = "propName";
export declare var eV: any;
export declare function eF(): void;
export declare class eC {
Expand Down Expand Up @@ -1153,6 +1156,7 @@ export interface eI {
new (...p3: any[]): any;
new (p4: string, p5?: string): any;
new (p6: string, ...p7: any[]): any;
[p]: any;
[p1: string]: any;
[p2: string, p3: number]: any;
p: any;
Expand Down Expand Up @@ -1205,6 +1209,7 @@ export declare namespace eM {
new (...p3: any[]): any;
new (p4: string, p5?: string): any;
new (p6: string, ...p7: any[]): any;
[p]: any;
[p1: string]: any;
[p2: string, p3: number]: any;
p: any;
Expand Down Expand Up @@ -1320,6 +1325,7 @@ export declare namespace eaM {
new (...p3: any[]): any;
new (p4: string, p5?: string): any;
new (p6: string, ...p7: any[]): any;
[p]: any;
[p1: string]: any;
[p2: string, p3: number]: any;
p: any;
Expand Down Expand Up @@ -1379,6 +1385,7 @@ export declare namespace eaM {
new (...p3: any[]): any;
new (p4: string, p5?: string): any;
new (p6: string, ...p7: any[]): any;
[p]: any;
[p1: string]: any;
[p2: string, p3: number]: any;
p: any;
Expand Down Expand Up @@ -1406,3 +1413,4 @@ export declare namespace eaM {
namespace eM { }
}
}
export {};

0 comments on commit 0d3c481

Please sign in to comment.