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
61 changes: 61 additions & 0 deletions tsc/internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2587,13 +2587,74 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S
return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol)
}
if nameType.flags&TypeFlagsUniqueESSymbol != 0 {
if name := b.getReusableComputedName(symbol, nameType, enclosingDeclaration); name != nil {
return b.f.NewComputedPropertyName(name)
}
// The reference was tracked in the destination scope by trackComputedName.
// Reconstructing its spelling in the source scope must not paint that scope's declarations visible.
return b.f.NewComputedPropertyName(b.symbolToExpressionWorker(nameType.AsUniqueESSymbolType().symbol, ast.SymbolFlagsValue))
}
return nil
}

// getReusableComputedName rebuilds the name a late-bound property was written with, when its first identifier
// resolves to the same symbol in the destination scope. That identifier is the one trackComputedName tracked
// there, so the printed key agrees with what was made visible. Reconstructing the name in the source scope
// instead can pick a different spelling, such as a local `const S = Symbol` that is not emitted.
func (b *NodeBuilderImpl) getReusableComputedName(symbol *ast.Symbol, nameType *Type, enclosingDeclaration *ast.Node) *ast.Node {
if enclosingDeclaration == nil || len(symbol.Declarations()) == 0 {
return nil
}
decl := symbol.Declarations()[0]
if !b.ch.hasLateBindableName(decl) {
return nil
}
var expression *ast.Node
if ast.IsBinaryExpression(decl) {
if name := ast.GetNameOfDeclaration(decl); name != nil && ast.IsElementAccessExpression(name) && ast.IsPropertyAccessEntityNameExpression(name.AsElementAccessExpression().ArgumentExpression, false /*allowJs*/) {
expression = name.AsElementAccessExpression().ArgumentExpression
}
} else if ast.IsEntityNameExpression(decl.Name().Expression()) {
expression = decl.Name().Expression()
}
if expression == nil {
return nil
}
firstIdentifier := ast.GetFirstIdentifier(expression)
meaning := ast.SymbolFlagsValue | ast.SymbolFlagsExportValue
target := b.ch.resolveName(enclosingDeclaration, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false)
if target == nil || target != b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false) {
Comment on lines +2625 to +2626
return nil
}
name, nameDeclaredType := b.rebuildEntityNameExpression(expression, target)
// Declared types, not flow-narrowed ones: the declaration file only has the declared type of each name.
if name == nil || nameDeclaredType != nameType {
return nil
}
return name
}

// rebuildEntityNameExpression creates a copy of an entity name expression whose identifiers keep their symbols,
// and returns the declared type the copy denotes, following property declarations from the root symbol.
func (b *NodeBuilderImpl) rebuildEntityNameExpression(node *ast.Node, root *ast.Symbol) (*ast.Node, *Type) {
if ast.IsIdentifier(node) {
// An exported variable resolves to its local export-value symbol, which does not carry the type.
root = b.ch.getExportSymbolOfValueSymbolIfExported(root)
return b.newIdentifier(node.Text(), root), b.ch.getTypeOfSymbol(root)
}
access := node.AsPropertyAccessExpression()
left, leftType := b.rebuildEntityNameExpression(access.Expression, root)
if left == nil {
return nil, nil
}
memberName := access.Name().Text()
member := b.ch.getPropertyOfType(b.ch.getApparentType(leftType), memberName)
if member == nil {
return nil, nil
}
return b.f.NewPropertyAccessExpression(left, nil, b.newIdentifier(memberName, member), ast.NodeFlagsNone), b.ch.getTypeOfSymbol(member)
}

func (b *NodeBuilderImpl) addPropertyToElementList(propertySymbol *ast.Symbol, typeElements []*ast.TypeElement) []*ast.TypeElement {
propertyIsReverseMapped := propertySymbol.CheckFlags()&ast.CheckFlagsReverseMapped != 0
var propertyType *Type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] ////

//// [variable.ts]
const S = Symbol;
export const range = { *[Symbol.iterator]() { yield 1; } };

//// [parameter.ts]
export function make(s: SymbolConstructor) {
return { *[Symbol.iterator]() { yield 1; } };
}

//// [others.ts]
const S = Symbol;
export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } };

//// [nested.ts]
export function inner() {
const T = Symbol;
return { *[Symbol.iterator]() { yield 1; } };
}

//// [written.ts]
const S = Symbol;
export const written = { *[S.iterator]() { yield 1; } };

//// [reexport.ts]
import { range } from "./variable";
export const again = range;


//// [narrowed.ts]
declare const k: typeof Symbol.iterator | undefined;
export function make() {
if (k === undefined) throw 0;
return { [k]: 1 };
}




//// [variable.d.ts]
export declare const range: {
[Symbol.iterator](): Generator<number, void, unknown>;
};
//// [parameter.d.ts]
export declare function make(s: SymbolConstructor): {
[Symbol.iterator](): Generator<number, void, unknown>;
};
//// [others.d.ts]
export declare const tagged: {
[Symbol.toStringTag]: string;
[Symbol.asyncIterator](): AsyncGenerator<number, void, unknown>;
};
//// [nested.d.ts]
export declare function inner(): {
[Symbol.iterator](): Generator<number, void, unknown>;
};
//// [written.d.ts]
declare const S: SymbolConstructor;
export declare const written: {
[S.iterator](): Generator<number, void, unknown>;
};
export {};
//// [reexport.d.ts]
export declare const again: {
[Symbol.iterator](): Generator<number, void, unknown>;
};
//// [narrowed.d.ts]
declare const k: typeof Symbol.iterator | undefined;
export declare function make(): {
[Symbol.iterator]: number;
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] ////

=== variable.ts ===
const S = Symbol;
>S : Symbol(S, Decl(variable.ts, 0, 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 range = { *[Symbol.iterator]() { yield 1; } };
>range : Symbol(range, Decl(variable.ts, 1, 12))
>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(variable.ts, 1, 22))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>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, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

=== parameter.ts ===
export function make(s: SymbolConstructor) {
>make : Symbol(make, Decl(parameter.ts, 0, 0))
>s : Symbol(s, Decl(parameter.ts, 0, 21))
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.asynciterable.d.ts, --, --), Decl(lib.es2020.symbol.wellknown.d.ts, --, --) ... and 2 more)

return { *[Symbol.iterator]() { yield 1; } };
>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(parameter.ts, 1, 12))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>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, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
}

=== others.ts ===
const S = Symbol;
>S : Symbol(S, Decl(others.ts, 0, 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 tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } };
>tagged : Symbol(tagged, Decl(others.ts, 1, 12))
>[Symbol.toStringTag] : Symbol([Symbol.toStringTag], Decl(others.ts, 1, 23))
>Symbol.toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>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, --, --))
>toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>[Symbol.asyncIterator] : Symbol([Symbol.asyncIterator], Decl(others.ts, 1, 50))
>Symbol.asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --))
>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, --, --))
>asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --))

=== nested.ts ===
export function inner() {
>inner : Symbol(inner, Decl(nested.ts, 0, 0))

const T = Symbol;
>T : Symbol(T, Decl(nested.ts, 1, 9))
>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, --, --))

return { *[Symbol.iterator]() { yield 1; } };
>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(nested.ts, 2, 12))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>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, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
}

=== written.ts ===
const S = Symbol;
>S : Symbol(S, Decl(written.ts, 0, 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 written = { *[S.iterator]() { yield 1; } };
>written : Symbol(written, Decl(written.ts, 1, 12))
>[S.iterator] : Symbol([S.iterator], Decl(written.ts, 1, 24))
>S.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>S : Symbol(S, Decl(written.ts, 0, 5))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

=== reexport.ts ===
import { range } from "./variable";
>range : Symbol(range, Decl(reexport.ts, 0, 8))

export const again = range;
>again : Symbol(again, Decl(reexport.ts, 1, 12))
>range : Symbol(range, Decl(reexport.ts, 0, 8))


=== narrowed.ts ===
declare const k: typeof Symbol.iterator | undefined;
>k : Symbol(k, Decl(narrowed.ts, 0, 13))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>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, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

export function make() {
>make : Symbol(make, Decl(narrowed.ts, 0, 52))

if (k === undefined) throw 0;
>k : Symbol(k, Decl(narrowed.ts, 0, 13))
>undefined : Symbol(undefined)

return { [k]: 1 };
>[k] : Symbol([k], Decl(narrowed.ts, 3, 12))
>k : Symbol(k, Decl(narrowed.ts, 0, 13))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] ////

=== variable.ts ===
const S = Symbol;
>S : SymbolConstructor
>Symbol : SymbolConstructor

export const range = { *[Symbol.iterator]() { yield 1; } };
>range : { [Symbol.iterator](): Generator<number, void, unknown>; }
>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator<number, void, unknown>; }
>[Symbol.iterator] : () => Generator<number, void, unknown>
>Symbol.iterator : unique symbol
>Symbol : SymbolConstructor
>iterator : unique symbol
>yield 1 : any
>1 : 1

=== parameter.ts ===
export function make(s: SymbolConstructor) {
>make : (s: SymbolConstructor) => { [Symbol.iterator](): Generator<number, void, unknown>; }
>s : SymbolConstructor

return { *[Symbol.iterator]() { yield 1; } };
>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator<number, void, unknown>; }
>[Symbol.iterator] : () => Generator<number, void, unknown>
>Symbol.iterator : unique symbol
>Symbol : SymbolConstructor
>iterator : unique symbol
>yield 1 : any
>1 : 1
}

=== others.ts ===
const S = Symbol;
>S : SymbolConstructor
>Symbol : SymbolConstructor

export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } };
>tagged : { [Symbol.toStringTag]: string; [Symbol.asyncIterator](): AsyncGenerator<number, void, unknown>; }
>{ [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } } : { [Symbol.toStringTag]: string; [Symbol.asyncIterator](): AsyncGenerator<number, void, unknown>; }
>[Symbol.toStringTag] : string
>Symbol.toStringTag : unique symbol
>Symbol : SymbolConstructor
>toStringTag : unique symbol
>"x" : "x"
>[Symbol.asyncIterator] : () => AsyncGenerator<number, void, unknown>
>Symbol.asyncIterator : unique symbol
>Symbol : SymbolConstructor
>asyncIterator : unique symbol
>yield 1 : any
>1 : 1

=== nested.ts ===
export function inner() {
>inner : () => { [Symbol.iterator](): Generator<number, void, unknown>; }

const T = Symbol;
>T : SymbolConstructor
>Symbol : SymbolConstructor

return { *[Symbol.iterator]() { yield 1; } };
>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator<number, void, unknown>; }
>[Symbol.iterator] : () => Generator<number, void, unknown>
>Symbol.iterator : unique symbol
>Symbol : SymbolConstructor
>iterator : unique symbol
>yield 1 : any
>1 : 1
}

=== written.ts ===
const S = Symbol;
>S : SymbolConstructor
>Symbol : SymbolConstructor

export const written = { *[S.iterator]() { yield 1; } };
>written : { [S.iterator](): Generator<number, void, unknown>; }
>{ *[S.iterator]() { yield 1; } } : { [S.iterator](): Generator<number, void, unknown>; }
>[S.iterator] : () => Generator<number, void, unknown>
>S.iterator : unique symbol
>S : SymbolConstructor
>iterator : unique symbol
>yield 1 : any
>1 : 1

=== reexport.ts ===
import { range } from "./variable";
>range : { [Symbol.iterator](): Generator<number, void, unknown>; }

export const again = range;
>again : { [Symbol.iterator](): Generator<number, void, unknown>; }
>range : { [Symbol.iterator](): Generator<number, void, unknown>; }


=== narrowed.ts ===
declare const k: typeof Symbol.iterator | undefined;
>k : unique symbol | undefined
>Symbol.iterator : unique symbol
>Symbol : SymbolConstructor
>iterator : unique symbol

export function make() {
>make : () => { [Symbol.iterator]: number; }

if (k === undefined) throw 0;
>k === undefined : boolean
>k : unique symbol | undefined
>undefined : undefined
>0 : 0

return { [k]: 1 };
>{ [k]: 1 } : { [Symbol.iterator]: number; }
>[k] : number
>k : unique symbol
>1 : 1
}

2 changes: 1 addition & 1 deletion tsc/testdata/baselines/reference/compiler/dynamicNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export declare const s2: typeof s0;
export declare const o1: {
a: number;
1: string;
[s0]: boolean;
[s2]: boolean;
};
export declare const o1_c4: number;
export declare const o1_c5: string;
Expand Down
Loading