Skip to content
Open
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
75 changes: 42 additions & 33 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ type Checker struct {
SignatureCount uint32
TotalInstantiationCount uint32
instantiationCount uint32
templateBuf []byte
memberLayoutCount uint32
instantiationStack []*Type
conditionalConstraintDepth uint32
Expand Down Expand Up @@ -30259,50 +30260,29 @@ func (c *Checker) getTemplateLiteralType(texts []string, types []*Type) *Type {
}
var newTypes []*Type
var newTexts []string
var sb strings.Builder
sb.WriteString(texts[0])
const maxTemplateBufRetainCap = 4096
out := append(c.templateBuf[:0], texts[0]...)
c.templateBuf = nil
textLength := 0 // combined length of the segments already moved into newTexts
tooLarge := false
var addSpans func([]string, []*Type) bool
addSpans = func(texts []string, types []*Type) bool {
for i, t := range types {
switch {
case t.flags&(TypeFlagsLiteral|TypeFlagsNull|TypeFlagsUndefined) != 0:
sb.WriteString(c.getTemplateStringForType(t))
sb.WriteString(texts[i+1])
case t.flags&TypeFlagsTemplateLiteral != 0:
sb.WriteString(t.AsTemplateLiteralType().texts[0])
if !addSpans(t.AsTemplateLiteralType().texts, t.AsTemplateLiteralType().types) {
return false
}
sb.WriteString(texts[i+1])
case c.isGenericIndexType(t) || c.isPatternLiteralPlaceholderType(t):
newTypes = append(newTypes, t)
newTexts = append(newTexts, stringutil.CombineSurrogatePairs(sb.String()))
textLength += sb.Len()
sb.Reset()
sb.WriteString(texts[i+1])
default:
return false
}
if textLength+sb.Len() > maxTemplateLiteralTypeLength || len(newTypes) > maxTemplateLiteralTypeSpans {
tooLarge = true
return false
}
}
return true
ok := c.addTemplateLiteralSpans(texts, types, &out, &newTypes, &newTexts, &textLength, &tooLarge)
if cap(out) <= maxTemplateBufRetainCap {
c.templateBuf = out[:0]
}
if !addSpans(texts, types) {
if !ok {
if tooLarge {
c.error(c.currentNode, diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite)
return c.errorType
}
return c.stringType
}
if len(newTypes) == 0 {
return c.getStringLiteralType(stringutil.CombineSurrogatePairs(sb.String()))
if len(out) == 0 {
return c.getStringLiteralType("")
}
return c.getStringLiteralType(stringutil.CombineSurrogatePairs(string(out)))
}
newTexts = append(newTexts, stringutil.CombineSurrogatePairs(sb.String()))
newTexts = append(newTexts, stringutil.CombineSurrogatePairs(string(out)))
if core.Every(newTexts, func(t string) bool { return t == "" }) {
if core.Every(newTypes, func(t *Type) bool { return t.flags&TypeFlagsString != 0 }) {
return c.stringType
Expand All @@ -30321,6 +30301,35 @@ func (c *Checker) getTemplateLiteralType(texts []string, types []*Type) *Type {
return t
}

func (c *Checker) addTemplateLiteralSpans(texts []string, types []*Type, out *[]byte, newTypes *[]*Type, newTexts *[]string, textLength *int, tooLarge *bool) bool {
for i, t := range types {
switch {
case t.flags&(TypeFlagsLiteral|TypeFlagsNull|TypeFlagsUndefined) != 0:
*out = append(*out, c.getTemplateStringForType(t)...)
*out = append(*out, texts[i+1]...)
case t.flags&TypeFlagsTemplateLiteral != 0:
*out = append(*out, t.AsTemplateLiteralType().texts[0]...)
if !c.addTemplateLiteralSpans(t.AsTemplateLiteralType().texts, t.AsTemplateLiteralType().types, out, newTypes, newTexts, textLength, tooLarge) {
return false
}
*out = append(*out, texts[i+1]...)
case c.isGenericIndexType(t) || c.isPatternLiteralPlaceholderType(t):
*newTypes = append(*newTypes, t)
*newTexts = append(*newTexts, stringutil.CombineSurrogatePairs(string(*out)))
*textLength += len(*out)
*out = (*out)[:0]
*out = append(*out, texts[i+1]...)
default:
return false
}
if *textLength+len(*out) > maxTemplateLiteralTypeLength || len(*newTypes) > maxTemplateLiteralTypeSpans {
*tooLarge = true
return false
}
}
return true
}

func (c *Checker) getTemplateStringForType(t *Type) string {
switch {
case t.flags&(TypeFlagsStringLiteral|TypeFlagsNumberLiteral|TypeFlagsBooleanLiteral|TypeFlagsBigIntLiteral) != 0:
Expand Down
Loading