Skip to content

Commit 9b17880

Browse files
committed
Order members of types implied by binding patterns by their elements
The members of a type implied by a binding pattern have no declarations, so types implied by two patterns with the same names, and their widened copies, could be told apart only by their member symbols, which were ordered by symbol id (seen in vscode). Each member now records the binding element declaring it, as TypeScript records it, and is ordered by that element. Types implied by different patterns that record them are also ordered by their patterns before their members.
1 parent cd8efe5 commit 9b17880

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ type Checker struct {
680680
objectContentTypes map[CacheHashKey]*Type
681681
deferredConditionalTypes map[deferredConditionalTypeKey]*Type
682682
emptyObjectTypesFromStringLiterals map[CacheHashKey]*Type
683+
bindingElementOfMember map[*ast.Symbol]*ast.Node // The binding element declaring a member of a type implied by a binding pattern
683684
spreadSymbols map[spreadSymbolKey]*ast.Symbol
684685
symbolsWithType map[symbolWithTypeKey]*ast.Symbol
685686
paddedProperties map[symbolWithTypeKey]*ast.Symbol
@@ -1029,6 +1030,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10291030
c.objectContentTypes = make(map[CacheHashKey]*Type)
10301031
c.deferredConditionalTypes = make(map[deferredConditionalTypeKey]*Type)
10311032
c.emptyObjectTypesFromStringLiterals = make(map[CacheHashKey]*Type)
1033+
c.bindingElementOfMember = make(map[*ast.Symbol]*ast.Node)
10321034
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10331035
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10341036
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)
@@ -18666,6 +18668,7 @@ func (c *Checker) getTypeFromObjectBindingPattern(pattern *ast.Node, includePatt
1866618668
flags := ast.SymbolFlagsProperty | core.IfElse(e.Initializer() != nil, ast.SymbolFlagsOptional, 0)
1866718669
symbol := c.newSymbol(flags, text)
1866818670
c.valueSymbolLinks.Get(symbol).resolvedType = c.getTypeFromBindingElement(e, includePatternInType, reportErrors)
18671+
c.bindingElementOfMember[symbol] = e
1866918672
members.Set(symbol.Name(), symbol)
1867018673
}
1867118674
var indexInfos []*IndexInfo

‎tsc/internal/checker/utilities.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ func (c *Checker) compareSymbolsWorker(s1, s2 *ast.Symbol) int {
394394
}
395395

396396
// compareSymbolOrigins orders transient symbols by the symbols and types they
397-
// are made from, which are recorded when they are created: the properties a
397+
// are made from, which are recorded when they are created: the binding element
398+
// declaring a member of a type implied by a binding pattern, the properties a
398399
// spread merges or copies, the mapped, union, or intersection type containing
399400
// a synthesized property and its key type, and the symbol an instantiated or
400401
// copied symbol is made from along with its mapper.
@@ -407,6 +408,9 @@ func (c *Checker) compareSymbolOrigins(s1, s2 *ast.Symbol) int {
407408
if s1.CheckFlags()&s2.CheckFlags()&ast.CheckFlagsUnresolved != 0 {
408409
return c.compareSymbols(s1.Parent(), s2.Parent())
409410
}
411+
if r := c.compareNodes(c.bindingElementOfMember[s1], c.bindingElementOfMember[s2]); r != 0 {
412+
return r
413+
}
410414
spread1 := c.spreadLinks.TryGet(s1)
411415
spread2 := c.spreadLinks.TryGet(s2)
412416
if (spread1 != nil) != (spread2 != nil) {
@@ -690,6 +694,10 @@ func CompareTypes(t1, t2 *Type) int {
690694
if c := int(t1.objectFlags&objectLiteralCreationFlags) - int(t2.objectFlags&objectLiteralCreationFlags); c != 0 {
691695
return c
692696
}
697+
// Types implied by different binding patterns differ only in their patterns.
698+
if c := t1.checker.compareNodes(t1.checker.patternForType[t1], t2.checker.patternForType[t2]); c != 0 {
699+
return c
700+
}
693701
if c := compareObjectMemberSymbols(t1, t2); c != 0 {
694702
return c
695703
}

0 commit comments

Comments
 (0)