Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1be0595
Share numeric literal name check through jsnum
jakebailey Nov 20, 2026
edc7fd2
Add tests for escaping in string completions
jakebailey Nov 20, 2026
d1d66f9
Escape string completions for the surrounding quote
jakebailey Nov 20, 2026
d2a06bc
Add tests for escaping in module path completions
jakebailey Nov 20, 2026
bac6db7
Escape module path completions for the surrounding quote
jakebailey Nov 20, 2026
2b5d4aa
Add tests for digit-prefixed member name completions
jakebailey Nov 20, 2026
73ac3a1
Quote digit-prefixed member names unless they are numbers
jakebailey Nov 20, 2026
244ba96
Add tests for quoted import and export name completions
jakebailey Nov 20, 2026
851f10b
Fix completions in quoted import and export names
jakebailey Nov 20, 2026
a070103
Add tests for JSX attribute string completions
jakebailey Nov 20, 2026
4969dab
Insert JSX attribute string completions as they are written
jakebailey Nov 20, 2026
d356ea8
Add tests for literal completions in JSX attribute initializers
jakebailey Nov 20, 2026
9b6ad34
Insert JSX-valid literals in JSX attribute initializers
jakebailey Nov 20, 2026
4eb2493
Enable test for partially typed JSX attribute values
jakebailey Nov 20, 2026
795d6e2
Treat partially typed JSX attribute values as initializers
jakebailey Nov 20, 2026
3815cbe
Add tests for path completions after escaped directory names
jakebailey Nov 20, 2026
f3336b8
Measure path completion ranges in source text
jakebailey Nov 20, 2026
9ddba5a
Add tests for quotes in triple-slash reference completions
jakebailey Nov 20, 2026
4d30193
Omit unrepresentable triple-slash reference completions
jakebailey Nov 20, 2026
1734c6a
Add tests for lone surrogates in JSX attribute completions
jakebailey Nov 20, 2026
bb07fa3
Escape lone surrogates in JSX attribute completions
jakebailey Nov 20, 2026
66e945b
Find path completion source offsets in a single pass
jakebailey Nov 20, 2026
df8ac04
Add tests for lone surrogates in literal completion labels
jakebailey Nov 20, 2026
c1a4505
Escape lone surrogates when quoting completion text
jakebailey Nov 20, 2026
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
14 changes: 7 additions & 7 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19672,7 +19672,7 @@ func (c *Checker) isApplicableIndexType(source *Type, target *Type) bool {
// signature applies to types assignable to 'number', `${number}` and numeric string literal types.
return c.isTypeAssignableTo(source, target) ||
target == c.stringType && c.isTypeAssignableTo(source, c.numberType) ||
target == c.numberType && (source == c.numericStringType || source.flags&TypeFlagsStringLiteral != 0 && isNumericLiteralName(getStringLiteralValue(source)))
target == c.numberType && (source == c.numericStringType || source.flags&TypeFlagsStringLiteral != 0 && jsnum.IsNumericLiteralName(getStringLiteralValue(source)))
}

func (c *Checker) resolveStructuredTypeMembers(t *Type) *StructuredType {
Expand Down Expand Up @@ -20467,7 +20467,7 @@ func (c *Checker) isSymbolWithSymbolName(symbol *ast.Symbol) bool {
}

func (c *Checker) isSymbolWithNumericName(symbol *ast.Symbol) bool {
if isNumericLiteralName(symbol.Name()) {
if jsnum.IsNumericLiteralName(symbol.Name()) {
return true
}
if len(symbol.Declarations()) != 0 {
Expand All @@ -20490,7 +20490,7 @@ func (c *Checker) isNumericName(name *ast.Node) bool {
case ast.KindComputedPropertyName:
return c.isNumericComputedName(name)
case ast.KindIdentifier, ast.KindNumericLiteral, ast.KindStringLiteral:
return isNumericLiteralName(name.Text())
return jsnum.IsNumericLiteralName(name.Text())
}
return false
}
Expand Down Expand Up @@ -24806,7 +24806,7 @@ func (c *Checker) computeEnumMemberValue(member *ast.Node, autoValue *jsnum.Numb
c.error(member.Name(), diagnostics.An_enum_member_cannot_have_a_numeric_name)
} else {
text := ast.GetTextOfPropertyName(member.Name())
if isNumericLiteralName(text) && !ast.IsInfinityOrNaNString(text) {
if jsnum.IsNumericLiteralName(text) && !ast.IsInfinityOrNaNString(text) {
c.error(member.Name(), diagnostics.An_enum_member_cannot_have_a_numeric_name)
}
}
Expand Down Expand Up @@ -27965,7 +27965,7 @@ func (c *Checker) getPropertyTypeForIndexType(originalObjectType *Type, objectTy
return propType
}
}
if everyType(objectType, isTupleType) && isNumericLiteralName(propName) {
if everyType(objectType, isTupleType) && jsnum.IsNumericLiteralName(propName) {
index := jsnum.FromString(propName)
if accessNode != nil && everyType(objectType, func(t *Type) bool {
return t.TargetTupleType().combinedFlags&ElementFlagsVariable == 0
Expand Down Expand Up @@ -28289,7 +28289,7 @@ func indexTypeLessThan(indexType *Type, limit int) bool {
return everyType(indexType, func(t *Type) bool {
if t.flags&TypeFlagsStringOrNumberLiteral != 0 {
propName := getPropertyNameFromType(t)
if isNumericLiteralName(propName) {
if jsnum.IsNumericLiteralName(propName) {
index := jsnum.FromString(propName)
return index >= 0 && index < jsnum.Number(limit)
}
Expand Down Expand Up @@ -31590,7 +31590,7 @@ func (c *Checker) getTypeOfConcretePropertyOfContextualType(t *Type, name string
}

func (c *Checker) getTypeFromIndexInfosOfContextualType(t *Type, name string, nameType *Type) *Type {
if isTupleType(t) && isNumericLiteralName(name) && jsnum.FromString(name) >= 0 {
if isTupleType(t) && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0 {
restType := c.getElementTypeOfSliceOfTupleType(t, t.TargetTupleType().fixedLength, 0 /*endSkipCount*/, false /*writing*/, true /*noReductions*/)
if restType != nil {
return restType
Expand Down
12 changes: 6 additions & 6 deletions tsc/internal/checker/nodebuilderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ func (b *NodeBuilderImpl) createExpressionFromSymbolChain(chain []*ast.Symbol, i
literalText := stringutil.UnquoteString(symbolName)
b.ctx.approximateLength += len(literalText) + 2
expression = b.newStringLiteralEx(literalText, symbolName[0] == '\'')
} else if jsnum.FromString(symbolName).String() == symbolName {
} else if jsnum.IsNumericLiteralName(symbolName) {
// TODO: the follwing in strada would assert if the number is negative, but no such assertion exists here
// Moreover, what's even guaranteeing the name *isn't* -1 here anyway? Needs double-checking.
b.ctx.approximateLength += len(symbolName)
Expand Down Expand Up @@ -979,10 +979,10 @@ func (b *NodeBuilderImpl) getNameOfSymbolFromNameType(symbol *ast.Symbol) string
case jsnum.Number:
name = v.String()
}
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && !isNumericLiteralName(name) {
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && !jsnum.IsNumericLiteralName(name) {
return b.ch.valueToString(nameType.AsLiteralType().value)
}
if isNumericLiteralName(name) && strings.HasPrefix(name, "-") {
if jsnum.IsNumericLiteralName(name) && strings.HasPrefix(name, "-") {
return fmt.Sprintf("[%s]", name)
}
return name
Expand Down Expand Up @@ -2510,7 +2510,7 @@ func classifyPropertyName(name string, stringNamed bool, isMethod bool) property
if scanner.IsIdentifierText(name, core.LanguageVariantStandard) {
return propertyNameNodeKindIdentifier
}
return core.IfElse(!stringNamed && isNumericLiteralName(name) && jsnum.FromString(name) >= 0, propertyNameNodeKindNumericLiteral, propertyNameNodeKindStringLiteral)
return core.IfElse(!stringNamed && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0, propertyNameNodeKindNumericLiteral, propertyNameNodeKindStringLiteral)
}

func (b *NodeBuilderImpl) createPropertyNameNodeForIdentifierOrLiteral(name string, singleQuote bool, stringNamed bool, isMethod bool, symbol *ast.Symbol) *ast.Node {
Expand Down Expand Up @@ -2608,11 +2608,11 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S
case string:
name = nameType.AsLiteralType().value.(string)
}
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && (stringNamed || !isNumericLiteralName(name)) {
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && (stringNamed || !jsnum.IsNumericLiteralName(name)) {
node := b.f.NewStringLiteral(name, core.IfElse(singleQuote, ast.TokenFlagsSingleQuote, ast.TokenFlagsNone))
return node
}
if isNumericLiteralName(name) && name[0] == '-' {
if jsnum.IsNumericLiteralName(name) && name[0] == '-' {
return b.f.NewComputedPropertyName(b.f.NewPrefixUnaryExpression(ast.KindMinusToken, b.f.NewNumericLiteral(name[1:], ast.TokenFlagsNone)))
}
return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol)
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4314,7 +4314,7 @@ func (r *Relater) propertiesRelatedTo(source *Type, target *Type, reportErrors b
numericNamesOnly := isTupleType(source) && isTupleType(target)
for _, targetProp := range excludeProperties(properties, excludedProperties) {
name := targetProp.Name()
if targetProp.Flags()&ast.SymbolFlagsPrototype == 0 && (!numericNamesOnly || isNumericLiteralName(name) || name == "length") && (!optionalsOnly || targetProp.Flags()&ast.SymbolFlagsOptional != 0) {
if targetProp.Flags()&ast.SymbolFlagsPrototype == 0 && (!numericNamesOnly || jsnum.IsNumericLiteralName(name) || name == "length") && (!optionalsOnly || targetProp.Flags()&ast.SymbolFlagsOptional != 0) {
sourceProp := r.c.getPropertyOfType(source, name)
if sourceProp != nil && sourceProp != targetProp {
related := r.propertyRelatedTo(source, target, sourceProp, targetProp, r.c.getNonMissingTypeOfSymbol, reportErrors, intersectionState, r.relation == r.c.comparableRelation)
Expand Down
25 changes: 0 additions & 25 deletions tsc/internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,31 +1057,6 @@ func getPropertyNameFromType(t *Type) string {
panic("Unhandled case in getPropertyNameFromType")
}

func isNumericLiteralName(name string) bool {
// The intent of numeric names is that
// - they are names with text in a numeric form, and that
// - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit',
// acquired by applying the abstract 'ToNumber' operation on the name's text.
//
// The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name.
// In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold.
//
// Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)'
// according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'.
// Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names
// because their 'ToString' representation is not equal to their original text.
// This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1.
//
// Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'.
// The '+' prefix operator is equivalent here to applying the abstract ToNumber operation.
// Applying the 'toString()' method on a number gives us the abstract ToString operation on a number.
//
// Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional.
// This is desired behavior, because when indexing with them as numeric entities, you are indexing
// with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively.
return jsnum.FromString(name).String() == name
}

func isThisProperty(node *ast.Node) bool {
return (ast.IsPropertyAccessExpression(node) || ast.IsElementAccessExpression(node)) && node.Expression().Kind == ast.KindThisKeyword
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
. "github.com/microsoft/TypeScript/tsc/internal/fourslash/tests/util"
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

Expand Down Expand Up @@ -73,3 +74,155 @@ export const x = 0;
},
})
}

func TestPathCompletionEscaping(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/main.ts
import "./[|a/*double*/|]";
import './[|a/*single*/|]';
// @Filename: /src/a"b.ts
export {};
// @Filename: /src/a'b.ts
export {};
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
for i, test := range []struct {
marker string
label string
insertText string
newText string
}{
{"double", `a"b`, `a\"b`, `a\"b`},
{"single", `a'b`, `a\'b`, `a\'b`},
} {
item := &lsproto.CompletionItem{
Label: test.label,
Detail: new(test.label + ".ts"),
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
TextEdit: &lsproto.TextEdit{NewText: test.newText, Range: f.Ranges()[i].LSRange},
},
}
if test.insertText != "" {
item.InsertText = &test.insertText
}
f.VerifyCompletions(t, test.marker, &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{},
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{Includes: []fourslash.CompletionsExpectedItem{item}},
})
}
}

func TestPathCompletionEscapedDirectory(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/main.ts
import "./a\"b/c/*escaped*/";
import "./\u0061\x2fc/*escapedSeparator*/";
// @Filename: /src/a"b/cd.ts
export {};
// @Filename: /src/a/cd.ts
export {};
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
for _, test := range []struct {
marker string
// Character offset of the replaced text; the "c" is at 15 and 20 respectively.
character uint32
}{
{"escaped", 15},
{"escapedSeparator", 20},
} {
line := f.MarkerByName(t, test.marker).LSPos().Line
f.VerifyCompletions(t, test.marker, &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{},
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "cd",
Detail: new("cd.ts"),
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
TextEdit: &lsproto.TextEdit{
NewText: "cd",
Range: lsproto.Range{
Start: lsproto.Position{Line: line, Character: test.character},
End: lsproto.Position{Line: line, Character: test.character + 1},
},
},
},
},
},
},
})
}
}

func TestTripleSlashReferenceCompletionQuotes(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /src/main.ts
/// <reference path="./a/*reference*/" />
// @Filename: /src/a"b.ts
export {};
// @Filename: /src/a'b.ts
export {};
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyCompletions(t, "reference", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{},
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{"a'b.ts"},
Excludes: []string{`a"b.ts`},
},
})
}

func TestPathCompletionEscapedTemplate(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = "// @Filename: /src/main.ts\n" +
"import(`./\\x61/c/*template*/`);\n" +
"// @Filename: /src/a/cd.ts\n" +
"export {};\n"
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyCompletions(t, "template", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &[]string{},
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Includes: []fourslash.CompletionsExpectedItem{
&lsproto.CompletionItem{
Label: "cd",
Detail: new("cd.ts"),
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
TextEdit: &lsproto.TextEdit{
NewText: "cd",
Range: lsproto.Range{
Start: lsproto.Position{Line: 0, Character: 15},
End: lsproto.Position{Line: 0, Character: 16},
},
},
},
},
},
},
})
}
Loading
Loading