diff --git a/tsc/internal/checker/emitresolver.go b/tsc/internal/checker/emitresolver.go index e4fad5c3f5b1f..9ee8483aa4b19 100644 --- a/tsc/internal/checker/emitresolver.go +++ b/tsc/internal/checker/emitresolver.go @@ -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()) + } + } + 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) } diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index d25c14a5fff44..9bb748c0eb43d 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -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) } } else { diff --git a/tsc/internal/printer/emitresolver.go b/tsc/internal/printer/emitresolver.go index d86a0ca378ecc..25cafd43ec9be 100644 --- a/tsc/internal/printer/emitresolver.go +++ b/tsc/internal/printer/emitresolver.go @@ -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 diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 9538225ed249f..8184d1377997b 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -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 @@ -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) @@ -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`), @@ -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)) + }) + tx.expandoHostsWithNamespaceMembers[hostId] = result + return result +} diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.js b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.js new file mode 100644 index 0000000000000..07dfd8928e96c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.js @@ -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 {}; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.symbols new file mode 100644 index 0000000000000..f5aa11dafeee1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.symbols @@ -0,0 +1,184 @@ +//// [tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts] //// + +=== lib.ts === +export const handler = () => 0; +>handler : Symbol(handler, Decl(lib.ts, 0, 12)) + +handler.retries = 3; +>handler.retries : Symbol(handler.retries, Decl(lib.ts, 0, 31)) +>handler : Symbol(handler, Decl(lib.ts, 0, 12)) +>retries : Symbol(handler.retries, Decl(lib.ts, 0, 31)) + +handler["x-request-id"] = "abc"; +>handler : Symbol(handler, Decl(lib.ts, 0, 12)) +>"x-request-id" : Symbol(handler["x-request-id"], Decl(lib.ts, 1, 20)) + +export const numeric = () => {}; +>numeric : Symbol(numeric, Decl(lib.ts, 4, 12)) + +numeric.a = 1; +>numeric.a : Symbol(numeric.a, Decl(lib.ts, 4, 32)) +>numeric : Symbol(numeric, Decl(lib.ts, 4, 12)) +>a : Symbol(numeric.a, Decl(lib.ts, 4, 32)) + +numeric[3] = "n"; +>numeric : Symbol(numeric, Decl(lib.ts, 4, 12)) +>3 : Symbol(numeric[3], Decl(lib.ts, 5, 14)) + +export const spaced = function () {}; +>spaced : Symbol(spaced, Decl(lib.ts, 8, 12)) + +spaced.a = 1; +>spaced.a : Symbol(spaced.a, Decl(lib.ts, 8, 37)) +>spaced : Symbol(spaced, Decl(lib.ts, 8, 12)) +>a : Symbol(spaced.a, Decl(lib.ts, 8, 37)) + +spaced["a b"] = 2; +>spaced : Symbol(spaced, Decl(lib.ts, 8, 12)) +>"a b" : Symbol(spaced["a b"], Decl(lib.ts, 9, 13)) + +const sym = Symbol(); +>sym : Symbol(sym, Decl(lib.ts, 12, 5)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + +export const withSymbol = () => {}; +>withSymbol : Symbol(withSymbol, Decl(lib.ts, 13, 12)) + +withSymbol.a = 1; +>withSymbol.a : Symbol(withSymbol.a, Decl(lib.ts, 13, 35)) +>withSymbol : Symbol(withSymbol, Decl(lib.ts, 13, 12)) +>a : Symbol(withSymbol.a, Decl(lib.ts, 13, 35)) + +withSymbol[sym] = true; +>withSymbol : Symbol(withSymbol, Decl(lib.ts, 13, 12)) +>sym : Symbol(sym, Decl(lib.ts, 12, 5)) + +export const reservedAndNonIdentifier = () => {}; +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(lib.ts, 17, 12)) + +reservedAndNonIdentifier.default = 1; +>reservedAndNonIdentifier.default : Symbol(reservedAndNonIdentifier.default, Decl(lib.ts, 17, 49)) +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(lib.ts, 17, 12)) +>default : Symbol(reservedAndNonIdentifier.default, Decl(lib.ts, 17, 49)) + +reservedAndNonIdentifier["b-c"] = 2; +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(lib.ts, 17, 12)) +>"b-c" : Symbol(reservedAndNonIdentifier["b-c"], Decl(lib.ts, 18, 37)) + +const key = "d-e"; +>key : Symbol(key, Decl(lib.ts, 21, 5)) + +export const constKey = () => {}; +>constKey : Symbol(constKey, Decl(lib.ts, 22, 12)) + +constKey.a = 1; +>constKey.a : Symbol(constKey.a, Decl(lib.ts, 22, 33)) +>constKey : Symbol(constKey, Decl(lib.ts, 22, 12)) +>a : Symbol(constKey.a, Decl(lib.ts, 22, 33)) + +constKey[key] = 2; +>constKey : Symbol(constKey, Decl(lib.ts, 22, 12)) +>key : Symbol(key, Decl(lib.ts, 21, 5)) + +const local = () => {}; +>local : Symbol(local, Decl(lib.ts, 26, 5)) + +local.a = 1; +>local.a : Symbol(local.a, Decl(lib.ts, 26, 23)) +>local : Symbol(local, Decl(lib.ts, 26, 5)) +>a : Symbol(local.a, Decl(lib.ts, 26, 23)) + +local["f-g"] = 2; +>local : Symbol(local, Decl(lib.ts, 26, 5)) +>"f-g" : Symbol(local["f-g"], Decl(lib.ts, 27, 12)) + +export default local; +>local : Symbol(local, Decl(lib.ts, 26, 5)) + +export const identifiersOnly = () => {}; +>identifiersOnly : Symbol(identifiersOnly, Decl(lib.ts, 31, 12)) + +identifiersOnly.a = 1; +>identifiersOnly.a : Symbol(identifiersOnly.a, Decl(lib.ts, 31, 40)) +>identifiersOnly : Symbol(identifiersOnly, Decl(lib.ts, 31, 12)) +>a : Symbol(identifiersOnly.a, Decl(lib.ts, 31, 40)) + +identifiersOnly["b"] = 2; +>identifiersOnly : Symbol(identifiersOnly, Decl(lib.ts, 31, 12)) +>"b" : Symbol(identifiersOnly["b"], Decl(lib.ts, 32, 22)) + +=== lib2.js === +export const handler = () => 0; +>handler : Symbol(handler, Decl(lib2.js, 0, 12)) + +handler.retries = 3; +>handler.retries : Symbol(handler.retries, Decl(lib2.js, 0, 31)) +>handler : Symbol(handler, Decl(lib2.js, 0, 12)) +>retries : Symbol(handler.retries, Decl(lib2.js, 0, 31)) + +handler["x-request-id"] = "abc"; +>handler : Symbol(handler, Decl(lib2.js, 0, 12)) +>"x-request-id" : Symbol(handler["x-request-id"], Decl(lib2.js, 1, 20)) + +=== use.ts === +import local, { handler, numeric, spaced, withSymbol, reservedAndNonIdentifier, constKey, identifiersOnly } from "./lib"; +>local : Symbol(local, Decl(use.ts, 0, 6)) +>handler : Symbol(handler, Decl(use.ts, 0, 15)) +>numeric : Symbol(numeric, Decl(use.ts, 0, 24)) +>spaced : Symbol(spaced, Decl(use.ts, 0, 33)) +>withSymbol : Symbol(withSymbol, Decl(use.ts, 0, 41)) +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(use.ts, 0, 53)) +>constKey : Symbol(constKey, Decl(use.ts, 0, 79)) +>identifiersOnly : Symbol(identifiersOnly, Decl(use.ts, 0, 89)) + +const s1: string = handler["x-request-id"]; +>s1 : Symbol(s1, Decl(use.ts, 1, 5)) +>handler : Symbol(handler, Decl(use.ts, 0, 15)) +>"x-request-id" : Symbol(handler["x-request-id"], Decl(lib.ts, 1, 20)) + +const n1: number = handler.retries; +>n1 : Symbol(n1, Decl(use.ts, 2, 5)) +>handler.retries : Symbol(handler.retries, Decl(lib.ts, 0, 31)) +>handler : Symbol(handler, Decl(use.ts, 0, 15)) +>retries : Symbol(handler.retries, Decl(lib.ts, 0, 31)) + +const s2: string = numeric[3]; +>s2 : Symbol(s2, Decl(use.ts, 3, 5)) +>numeric : Symbol(numeric, Decl(use.ts, 0, 24)) +>3 : Symbol(numeric[3], Decl(lib.ts, 5, 14)) + +const n2: number = spaced["a b"]; +>n2 : Symbol(n2, Decl(use.ts, 4, 5)) +>spaced : Symbol(spaced, Decl(use.ts, 0, 33)) +>"a b" : Symbol(spaced["a b"], Decl(lib.ts, 9, 13)) + +const n3: number = reservedAndNonIdentifier["b-c"] + reservedAndNonIdentifier.default; +>n3 : Symbol(n3, Decl(use.ts, 5, 5)) +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(use.ts, 0, 53)) +>"b-c" : Symbol(reservedAndNonIdentifier["b-c"], Decl(lib.ts, 18, 37)) +>reservedAndNonIdentifier.default : Symbol(reservedAndNonIdentifier.default, Decl(lib.ts, 17, 49)) +>reservedAndNonIdentifier : Symbol(reservedAndNonIdentifier, Decl(use.ts, 0, 53)) +>default : Symbol(reservedAndNonIdentifier.default, Decl(lib.ts, 17, 49)) + +const n4: number = constKey["d-e"]; +>n4 : Symbol(n4, Decl(use.ts, 6, 5)) +>constKey : Symbol(constKey, Decl(use.ts, 0, 79)) +>"d-e" : Symbol(constKey[constKey[key]], Decl(lib.ts, 23, 15)) + +const n5: number = local["f-g"] + local.a; +>n5 : Symbol(n5, Decl(use.ts, 7, 5)) +>local : Symbol(local, Decl(use.ts, 0, 6)) +>"f-g" : Symbol(local["f-g"], Decl(lib.ts, 27, 12)) +>local.a : Symbol(local.a, Decl(lib.ts, 26, 23)) +>local : Symbol(local, Decl(use.ts, 0, 6)) +>a : Symbol(local.a, Decl(lib.ts, 26, 23)) + +const n6: number = identifiersOnly.a + identifiersOnly.b; +>n6 : Symbol(n6, Decl(use.ts, 8, 5)) +>identifiersOnly.a : Symbol(identifiersOnly.a, Decl(lib.ts, 31, 40)) +>identifiersOnly : Symbol(identifiersOnly, Decl(use.ts, 0, 89)) +>a : Symbol(identifiersOnly.a, Decl(lib.ts, 31, 40)) +>identifiersOnly.b : Symbol(identifiersOnly["b"], Decl(lib.ts, 32, 22)) +>identifiersOnly : Symbol(identifiersOnly, Decl(use.ts, 0, 89)) +>b : Symbol(identifiersOnly["b"], Decl(lib.ts, 32, 22)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.types b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.types new file mode 100644 index 0000000000000..ffc3e69397c8e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoNonIdentifierMembers.types @@ -0,0 +1,251 @@ +//// [tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts] //// + +=== lib.ts === +export const handler = () => 0; +>handler : { (): number; retries: number; "x-request-id": string; } +>() => 0 : { (): number; retries: number; "x-request-id": string; } +>0 : 0 + +handler.retries = 3; +>handler.retries = 3 : 3 +>handler.retries : number +>handler : { (): number; retries: number; "x-request-id": string; } +>retries : number +>3 : 3 + +handler["x-request-id"] = "abc"; +>handler["x-request-id"] = "abc" : "abc" +>handler["x-request-id"] : string +>handler : { (): number; retries: number; "x-request-id": string; } +>"x-request-id" : "x-request-id" +>"abc" : "abc" + +export const numeric = () => {}; +>numeric : { (): void; a: number; 3: string; } +>() => {} : { (): void; a: number; 3: string; } + +numeric.a = 1; +>numeric.a = 1 : 1 +>numeric.a : number +>numeric : { (): void; a: number; 3: string; } +>a : number +>1 : 1 + +numeric[3] = "n"; +>numeric[3] = "n" : "n" +>numeric[3] : string +>numeric : { (): void; a: number; 3: string; } +>3 : 3 +>"n" : "n" + +export const spaced = function () {}; +>spaced : { (): void; a: number; "a b": number; } +>function () {} : { (): void; a: number; "a b": number; } + +spaced.a = 1; +>spaced.a = 1 : 1 +>spaced.a : number +>spaced : { (): void; a: number; "a b": number; } +>a : number +>1 : 1 + +spaced["a b"] = 2; +>spaced["a b"] = 2 : 2 +>spaced["a b"] : number +>spaced : { (): void; a: number; "a b": number; } +>"a b" : "a b" +>2 : 2 + +const sym = Symbol(); +>sym : unique symbol +>Symbol() : unique symbol +>Symbol : SymbolConstructor + +export const withSymbol = () => {}; +>withSymbol : { (): void; a: number; [sym]: boolean; } +>() => {} : { (): void; a: number; [sym]: boolean; } + +withSymbol.a = 1; +>withSymbol.a = 1 : 1 +>withSymbol.a : number +>withSymbol : { (): void; a: number; [sym]: boolean; } +>a : number +>1 : 1 + +withSymbol[sym] = true; +>withSymbol[sym] = true : true +>withSymbol[sym] : boolean +>withSymbol : { (): void; a: number; [sym]: boolean; } +>sym : unique symbol +>true : true + +export const reservedAndNonIdentifier = () => {}; +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>() => {} : { (): void; default: number; "b-c": number; } + +reservedAndNonIdentifier.default = 1; +>reservedAndNonIdentifier.default = 1 : 1 +>reservedAndNonIdentifier.default : number +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>default : number +>1 : 1 + +reservedAndNonIdentifier["b-c"] = 2; +>reservedAndNonIdentifier["b-c"] = 2 : 2 +>reservedAndNonIdentifier["b-c"] : number +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>"b-c" : "b-c" +>2 : 2 + +const key = "d-e"; +>key : "d-e" +>"d-e" : "d-e" + +export const constKey = () => {}; +>constKey : { (): void; a: number; "d-e": number; } +>() => {} : { (): void; a: number; "d-e": number; } + +constKey.a = 1; +>constKey.a = 1 : 1 +>constKey.a : number +>constKey : { (): void; a: number; "d-e": number; } +>a : number +>1 : 1 + +constKey[key] = 2; +>constKey[key] = 2 : 2 +>constKey[key] : number +>constKey : { (): void; a: number; "d-e": number; } +>key : "d-e" +>2 : 2 + +const local = () => {}; +>local : { (): void; a: number; "f-g": number; } +>() => {} : { (): void; a: number; "f-g": number; } + +local.a = 1; +>local.a = 1 : 1 +>local.a : number +>local : { (): void; a: number; "f-g": number; } +>a : number +>1 : 1 + +local["f-g"] = 2; +>local["f-g"] = 2 : 2 +>local["f-g"] : number +>local : { (): void; a: number; "f-g": number; } +>"f-g" : "f-g" +>2 : 2 + +export default local; +>local : { (): void; a: number; "f-g": number; } + +export const identifiersOnly = () => {}; +>identifiersOnly : { (): void; a: number; b: number; } +>() => {} : { (): void; a: number; b: number; } + +identifiersOnly.a = 1; +>identifiersOnly.a = 1 : 1 +>identifiersOnly.a : number +>identifiersOnly : { (): void; a: number; b: number; } +>a : number +>1 : 1 + +identifiersOnly["b"] = 2; +>identifiersOnly["b"] = 2 : 2 +>identifiersOnly["b"] : number +>identifiersOnly : { (): void; a: number; b: number; } +>"b" : "b" +>2 : 2 + +=== lib2.js === +export const handler = () => 0; +>handler : { (): number; retries: number; "x-request-id": string; } +>() => 0 : { (): number; retries: number; "x-request-id": string; } +>0 : 0 + +handler.retries = 3; +>handler.retries = 3 : 3 +>handler.retries : number +>handler : { (): number; retries: number; "x-request-id": string; } +>retries : number +>3 : 3 + +handler["x-request-id"] = "abc"; +>handler["x-request-id"] = "abc" : "abc" +>handler["x-request-id"] : string +>handler : { (): number; retries: number; "x-request-id": string; } +>"x-request-id" : "x-request-id" +>"abc" : "abc" + +=== use.ts === +import local, { handler, numeric, spaced, withSymbol, reservedAndNonIdentifier, constKey, identifiersOnly } from "./lib"; +>local : { (): void; a: number; "f-g": number; } +>handler : { (): number; retries: number; "x-request-id": string; } +>numeric : { (): void; a: number; 3: string; } +>spaced : { (): void; a: number; "a b": number; } +>withSymbol : { (): void; a: number; [sym]: boolean; } +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>constKey : { (): void; a: number; "d-e": number; } +>identifiersOnly : { (): void; a: number; b: number; } + +const s1: string = handler["x-request-id"]; +>s1 : string +>handler["x-request-id"] : string +>handler : { (): number; retries: number; "x-request-id": string; } +>"x-request-id" : "x-request-id" + +const n1: number = handler.retries; +>n1 : number +>handler.retries : number +>handler : { (): number; retries: number; "x-request-id": string; } +>retries : number + +const s2: string = numeric[3]; +>s2 : string +>numeric[3] : string +>numeric : { (): void; a: number; 3: string; } +>3 : 3 + +const n2: number = spaced["a b"]; +>n2 : number +>spaced["a b"] : number +>spaced : { (): void; a: number; "a b": number; } +>"a b" : "a b" + +const n3: number = reservedAndNonIdentifier["b-c"] + reservedAndNonIdentifier.default; +>n3 : number +>reservedAndNonIdentifier["b-c"] + reservedAndNonIdentifier.default : number +>reservedAndNonIdentifier["b-c"] : number +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>"b-c" : "b-c" +>reservedAndNonIdentifier.default : number +>reservedAndNonIdentifier : { (): void; default: number; "b-c": number; } +>default : number + +const n4: number = constKey["d-e"]; +>n4 : number +>constKey["d-e"] : number +>constKey : { (): void; a: number; "d-e": number; } +>"d-e" : "d-e" + +const n5: number = local["f-g"] + local.a; +>n5 : number +>local["f-g"] + local.a : number +>local["f-g"] : number +>local : { (): void; a: number; "f-g": number; } +>"f-g" : "f-g" +>local.a : number +>local : { (): void; a: number; "f-g": number; } +>a : number + +const n6: number = identifiersOnly.a + identifiersOnly.b; +>n6 : number +>identifiersOnly.a + identifiersOnly.b : number +>identifiersOnly.a : number +>identifiersOnly : { (): void; a: number; b: number; } +>a : number +>identifiersOnly.b : number +>identifiersOnly : { (): void; a: number; b: number; } +>b : number + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts b/tsc/testdata/tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts new file mode 100644 index 0000000000000..6b4e502fb6a3b --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitExpandoNonIdentifierMembers.ts @@ -0,0 +1,57 @@ +// @declaration: true +// @strict: true +// @target: esnext +// @allowJs: true +// @outDir: out + +// @filename: 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; + +// @filename: lib2.js +export const handler = () => 0; +handler.retries = 3; +handler["x-request-id"] = "abc"; + +// @filename: 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;