Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tsc/internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ func (r *EmitResolver) IsExpandoFunctionDeclaration(node *ast.Node) bool {
return r.IsExpandoFunctionDeclarationUnsafe(node)
}

// GetExpandoPropertyAssignments returns the assignment that declares each expando property of a function-like declaration
func (r *EmitResolver) GetExpandoPropertyAssignments(node *ast.Node) []*ast.Node {
if !ast.IsParseTreeNode(node) {
return nil
}
r.checkerMu.Lock()
defer r.checkerMu.Unlock()
var assignments []*ast.Node
for _, p := range r.GetPropertiesOfContainerFunction(node) {
if ast.IsExpandoPropertyDeclaration(p.ValueDeclaration()) {
assignments = append(assignments, p.ValueDeclaration())
Comment on lines +379 to +380
}
}
return assignments
}

func (r *EmitResolver) isSymbolAccessible(symbol *ast.Symbol, enclosingDeclaration *ast.Node, meaning ast.SymbolFlags, shouldComputeAliasToMarkVisible bool) printer.SymbolAccessibilityResult {
return r.checker.IsSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasToMarkVisible)
}
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,7 @@ func (b *NodeBuilderImpl) addPropertyToElementList(propertySymbol *ast.Symbol, t
if b.ch.hasLateBindableName(decl) {
if ast.IsBinaryExpression(decl) {
name := ast.GetNameOfDeclaration(decl)
if name != nil && ast.IsElementAccessExpression(name) && ast.IsPropertyAccessEntityNameExpression(name.AsElementAccessExpression().ArgumentExpression, false /*allowJs*/) {
if name != nil && ast.IsElementAccessExpression(name) && ast.IsEntityNameExpression(name.AsElementAccessExpression().ArgumentExpression) {
b.trackComputedName(name.AsElementAccessExpression().ArgumentExpression, saveEnclosingDeclaration)
Comment on lines +2613 to 2614
}
} else {
Expand Down
1 change: 1 addition & 0 deletions tsc/internal/printer/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type EmitResolver interface {
IsEntityNameVisible(entityName *ast.Node, enclosingDeclaration *ast.Node) SymbolAccessibilityResult // previously SymbolVisibilityResult in strada - ErrorModuleName never set
IsExpandoFunctionDeclaration(node *ast.Node) bool
IsExpandoFunctionDeclarationUnsafe(node *ast.Node) bool
GetExpandoPropertyAssignments(node *ast.Node) []*ast.Node
IsLiteralConstDeclaration(node *ast.Node) bool
RequiresAddingImplicitUndefined(node *ast.Node, symbol *ast.Symbol, enclosingDeclaration *ast.Node) bool
IsDeclarationVisible(node *ast.Node) bool
Expand Down
31 changes: 28 additions & 3 deletions tsc/internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type DeclarationTransformer struct {
expandoHosts map[ast.NodeId]*ast.Node // store the result of transforming expando hosts so they can be inserted later if the host is actually referenced
expandoMembers map[ast.NodeId][]*ast.Node // store any found expando _members_ after transforming them so *if* the host is referenced, they can be emitted alongside it
deferredExpandoAssignments map[ast.NodeId][]*ast.BinaryExpression // expando assignments whose host wasn't visible when collected, processed if the host is late-marked visible
expandoHostsWithNamespaceMembers map[ast.NodeId]bool // whether each variable expando host has only members that a namespace can declare
seenProperties collections.Set[thisPropertyAssignmentKey]
thisPropertyAssignmentsCollected []*ast.Node
rawReferencedFiles []ReferencedFilePair
Expand Down Expand Up @@ -295,6 +296,7 @@ func (tx *DeclarationTransformer) visitSourceFile(node *ast.SourceFile) *ast.Nod
tx.expandoHosts = make(map[ast.NodeId]*ast.Node)
tx.expandoMembers = make(map[ast.NodeId][]*ast.Node)
tx.deferredExpandoAssignments = make(map[ast.NodeId][]*ast.BinaryExpression)
tx.expandoHostsWithNamespaceMembers = make(map[ast.NodeId]bool)
tx.rawReferencedFiles = make([]ReferencedFilePair, 0)
tx.rawTypeReferenceDirectives = make([]*ast.FileReference, 0)
tx.rawLibReferenceDirectives = make([]*ast.FileReference, 0)
Expand Down Expand Up @@ -2870,14 +2872,18 @@ func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExp
return
}

hostId := tx.getExpandoHostId(declaration)

if ast.IsVariableDeclaration(declaration) && !tx.canDeclareExpandoMembersInNamespace(hostId, declaration) {
return // The variable keeps its type, which lists every member, so no namespace is needed
}

name := tx.Factory().NewIdentifier(ns.Text())
property := tx.tryGetPropertyName(left)
if property == "" || !scanner.IsIdentifierText(property, core.LanguageVariantStandard) {
if !isNamespaceMemberName(property) {
return
}

hostId := tx.getExpandoHostId(declaration)

if ast.IsDeclaration(declaration) && isDeclarationAndNotVisible(tx.EmitContext(), tx.resolver, declaration) {
// The host isn't visible (yet) - printing the type of a visible declaration may still
// late-mark it as visible (e.g. an exported variable whose type prints as `typeof host`),
Expand Down Expand Up @@ -3110,3 +3116,22 @@ func (tx *DeclarationTransformer) tryGetPropertyName(node *ast.Node) string {
}
return ""
}

// isNamespaceMemberName reports whether an expando property can be declared as a member of a namespace
func isNamespaceMemberName(property string) bool {
return property != "" && scanner.IsIdentifierText(property, core.LanguageVariantStandard)
}

// canDeclareExpandoMembersInNamespace reports whether every expando property of a variable host can be
// declared in a namespace. When one can't (a string, numeric or unique symbol key that isn't an identifier),
// the host stays a variable, whose type keeps every member, instead of a function and a namespace without it.
func (tx *DeclarationTransformer) canDeclareExpandoMembersInNamespace(hostId ast.NodeId, declaration *ast.Declaration) bool {
if result, ok := tx.expandoHostsWithNamespaceMembers[hostId]; ok {
return result
}
result := core.Every(tx.resolver.GetExpandoPropertyAssignments(declaration), func(assignment *ast.Node) bool {
return isNamespaceMemberName(tx.tryGetPropertyName(assignment.AsBinaryExpression().Left))
})
Comment on lines +3132 to +3134
tx.expandoHostsWithNamespaceMembers[hostId] = result
return result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//// [tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts] ////

//// [lib.ts]
export const handler = () => 0;
handler.retries = 3;
handler["x-request-id"] = "abc";

export const numeric = () => {};
numeric.a = 1;
numeric[3] = "n";

export const spaced = function () {};
spaced.a = 1;
spaced["a b"] = 2;

const sym = Symbol();
export const withSymbol = () => {};
withSymbol.a = 1;
withSymbol[sym] = true;

export const reservedAndNonIdentifier = () => {};
reservedAndNonIdentifier.default = 1;
reservedAndNonIdentifier["b-c"] = 2;

const key = "d-e";
export const constKey = () => {};
constKey.a = 1;
constKey[key] = 2;

const local = () => {};
local.a = 1;
local["f-g"] = 2;
export default local;

export const identifiersOnly = () => {};
identifiersOnly.a = 1;
identifiersOnly["b"] = 2;

//// [lib2.js]
export const handler = () => 0;
handler.retries = 3;
handler["x-request-id"] = "abc";

//// [use.ts]
import local, { handler, numeric, spaced, withSymbol, reservedAndNonIdentifier, constKey, identifiersOnly } from "./lib";
const s1: string = handler["x-request-id"];
const n1: number = handler.retries;
const s2: string = numeric[3];
const n2: number = spaced["a b"];
const n3: number = reservedAndNonIdentifier["b-c"] + reservedAndNonIdentifier.default;
const n4: number = constKey["d-e"];
const n5: number = local["f-g"] + local.a;
const n6: number = identifiersOnly.a + identifiersOnly.b;


//// [lib.js]
export const handler = () => 0;
handler.retries = 3;
handler["x-request-id"] = "abc";
export const numeric = () => { };
numeric.a = 1;
numeric[3] = "n";
export const spaced = function () { };
spaced.a = 1;
spaced["a b"] = 2;
const sym = Symbol();
export const withSymbol = () => { };
withSymbol.a = 1;
withSymbol[sym] = true;
export const reservedAndNonIdentifier = () => { };
reservedAndNonIdentifier.default = 1;
reservedAndNonIdentifier["b-c"] = 2;
const key = "d-e";
export const constKey = () => { };
constKey.a = 1;
constKey[key] = 2;
const local = () => { };
local.a = 1;
local["f-g"] = 2;
export default local;
export const identifiersOnly = () => { };
identifiersOnly.a = 1;
identifiersOnly["b"] = 2;
//// [lib2.js]
export const handler = () => 0;
handler.retries = 3;
handler["x-request-id"] = "abc";
//// [use.js]
import local, { handler, numeric, spaced, reservedAndNonIdentifier, constKey, identifiersOnly } from "./lib";
const s1 = handler["x-request-id"];
const n1 = handler.retries;
const s2 = numeric[3];
const n2 = spaced["a b"];
const n3 = reservedAndNonIdentifier["b-c"] + reservedAndNonIdentifier.default;
const n4 = constKey["d-e"];
const n5 = local["f-g"] + local.a;
const n6 = identifiersOnly.a + identifiersOnly.b;


//// [lib.d.ts]
export declare const handler: {
(): number;
retries: number;
"x-request-id": string;
};
export declare const numeric: {
(): void;
a: number;
3: string;
};
export declare const spaced: {
(): void;
a: number;
"a b": number;
};
declare const sym: unique symbol;
export declare const withSymbol: {
(): void;
a: number;
[sym]: boolean;
};
export declare const reservedAndNonIdentifier: {
(): void;
default: number;
"b-c": number;
};
export declare const constKey: {
(): void;
a: number;
"d-e": number;
};
declare const local: {
(): void;
a: number;
"f-g": number;
};
export default local;
export declare function identifiersOnly(): void;
export declare namespace identifiersOnly {
var a: number;
var b: number;
}
//// [lib2.d.ts]
export declare const handler: {
(): number;
retries: number;
"x-request-id": string;
};
//// [use.d.ts]
export {};
Loading