Skip to content

Commit f4eb8d7

Browse files
authored
Fix various LS text insertion / quoting bugs (#65608)
1 parent eb745b1 commit f4eb8d7

13 files changed

Lines changed: 700 additions & 66 deletions

‎tsc/internal/checker/checker.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19697,7 +19697,7 @@ func (c *Checker) isApplicableIndexType(source *Type, target *Type) bool {
1969719697
// signature applies to types assignable to 'number', `${number}` and numeric string literal types.
1969819698
return c.isTypeAssignableTo(source, target) ||
1969919699
target == c.stringType && c.isTypeAssignableTo(source, c.numberType) ||
19700-
target == c.numberType && (source == c.numericStringType || source.flags&TypeFlagsStringLiteral != 0 && isNumericLiteralName(getStringLiteralValue(source)))
19700+
target == c.numberType && (source == c.numericStringType || source.flags&TypeFlagsStringLiteral != 0 && jsnum.IsNumericLiteralName(getStringLiteralValue(source)))
1970119701
}
1970219702

1970319703
func (c *Checker) resolveStructuredTypeMembers(t *Type) *StructuredType {
@@ -20499,7 +20499,7 @@ func (c *Checker) isSymbolWithSymbolName(symbol *ast.Symbol) bool {
2049920499
}
2050020500

2050120501
func (c *Checker) isSymbolWithNumericName(symbol *ast.Symbol) bool {
20502-
if isNumericLiteralName(symbol.Name()) {
20502+
if jsnum.IsNumericLiteralName(symbol.Name()) {
2050320503
return true
2050420504
}
2050520505
if len(symbol.Declarations()) != 0 {
@@ -20522,7 +20522,7 @@ func (c *Checker) isNumericName(name *ast.Node) bool {
2052220522
case ast.KindComputedPropertyName:
2052320523
return c.isNumericComputedName(name)
2052420524
case ast.KindIdentifier, ast.KindNumericLiteral, ast.KindStringLiteral:
20525-
return isNumericLiteralName(name.Text())
20525+
return jsnum.IsNumericLiteralName(name.Text())
2052620526
}
2052720527
return false
2052820528
}
@@ -24894,7 +24894,7 @@ func (c *Checker) computeEnumMemberValue(member *ast.Node, autoValue *jsnum.Numb
2489424894
c.error(member.Name(), diagnostics.An_enum_member_cannot_have_a_numeric_name)
2489524895
} else {
2489624896
text := ast.GetTextOfPropertyName(member.Name())
24897-
if isNumericLiteralName(text) && !ast.IsInfinityOrNaNString(text) {
24897+
if jsnum.IsNumericLiteralName(text) && !ast.IsInfinityOrNaNString(text) {
2489824898
c.error(member.Name(), diagnostics.An_enum_member_cannot_have_a_numeric_name)
2489924899
}
2490024900
}
@@ -28053,7 +28053,7 @@ func (c *Checker) getPropertyTypeForIndexType(originalObjectType *Type, objectTy
2805328053
return propType
2805428054
}
2805528055
}
28056-
if everyType(objectType, isTupleType) && isNumericLiteralName(propName) {
28056+
if everyType(objectType, isTupleType) && jsnum.IsNumericLiteralName(propName) {
2805728057
index := jsnum.FromString(propName)
2805828058
if accessNode != nil && everyType(objectType, func(t *Type) bool {
2805928059
return t.TargetTupleType().combinedFlags&ElementFlagsVariable == 0
@@ -28377,7 +28377,7 @@ func indexTypeLessThan(indexType *Type, limit int) bool {
2837728377
return everyType(indexType, func(t *Type) bool {
2837828378
if t.flags&TypeFlagsStringOrNumberLiteral != 0 {
2837928379
propName := getPropertyNameFromType(t)
28380-
if isNumericLiteralName(propName) {
28380+
if jsnum.IsNumericLiteralName(propName) {
2838128381
index := jsnum.FromString(propName)
2838228382
return index >= 0 && index < jsnum.Number(limit)
2838328383
}
@@ -31689,7 +31689,7 @@ func (c *Checker) getTypeOfConcretePropertyOfContextualType(t *Type, name string
3168931689
}
3169031690

3169131691
func (c *Checker) getTypeFromIndexInfosOfContextualType(t *Type, name string, nameType *Type) *Type {
31692-
if isTupleType(t) && isNumericLiteralName(name) && jsnum.FromString(name) >= 0 {
31692+
if isTupleType(t) && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0 {
3169331693
restType := c.getElementTypeOfSliceOfTupleType(t, t.TargetTupleType().fixedLength, 0 /*endSkipCount*/, false /*writing*/, true /*noReductions*/)
3169431694
if restType != nil {
3169531695
return restType

‎tsc/internal/checker/nodebuilderimpl.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ func (b *NodeBuilderImpl) createExpressionFromSymbolChain(chain []*ast.Symbol, i
893893
literalText := stringutil.UnquoteString(symbolName)
894894
b.ctx.approximateLength += len(literalText) + 2
895895
expression = b.newStringLiteralEx(literalText, symbolName[0] == '\'')
896-
} else if jsnum.FromString(symbolName).String() == symbolName {
896+
} else if jsnum.IsNumericLiteralName(symbolName) {
897897
// TODO: the follwing in strada would assert if the number is negative, but no such assertion exists here
898898
// Moreover, what's even guaranteeing the name *isn't* -1 here anyway? Needs double-checking.
899899
b.ctx.approximateLength += len(symbolName)
@@ -947,10 +947,10 @@ func (b *NodeBuilderImpl) getNameOfSymbolFromNameType(symbol *ast.Symbol) string
947947
case jsnum.Number:
948948
name = v.String()
949949
}
950-
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && !isNumericLiteralName(name) {
950+
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && !jsnum.IsNumericLiteralName(name) {
951951
return b.ch.valueToString(nameType.AsLiteralType().value)
952952
}
953-
if isNumericLiteralName(name) && strings.HasPrefix(name, "-") {
953+
if jsnum.IsNumericLiteralName(name) && strings.HasPrefix(name, "-") {
954954
return fmt.Sprintf("[%s]", name)
955955
}
956956
return name
@@ -2479,7 +2479,7 @@ func classifyPropertyName(name string, stringNamed bool, isMethod bool) property
24792479
if scanner.IsIdentifierText(name, core.LanguageVariantStandard) {
24802480
return propertyNameNodeKindIdentifier
24812481
}
2482-
return core.IfElse(!stringNamed && isNumericLiteralName(name) && jsnum.FromString(name) >= 0, propertyNameNodeKindNumericLiteral, propertyNameNodeKindStringLiteral)
2482+
return core.IfElse(!stringNamed && jsnum.IsNumericLiteralName(name) && jsnum.FromString(name) >= 0, propertyNameNodeKindNumericLiteral, propertyNameNodeKindStringLiteral)
24832483
}
24842484

24852485
func (b *NodeBuilderImpl) createPropertyNameNodeForIdentifierOrLiteral(name string, singleQuote bool, stringNamed bool, isMethod bool, symbol *ast.Symbol) *ast.Node {
@@ -2577,11 +2577,11 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S
25772577
case string:
25782578
name = nameType.AsLiteralType().value.(string)
25792579
}
2580-
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && (stringNamed || !isNumericLiteralName(name)) {
2580+
if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) && (stringNamed || !jsnum.IsNumericLiteralName(name)) {
25812581
node := b.f.NewStringLiteral(name, core.IfElse(singleQuote, ast.TokenFlagsSingleQuote, ast.TokenFlagsNone))
25822582
return node
25832583
}
2584-
if isNumericLiteralName(name) && name[0] == '-' {
2584+
if jsnum.IsNumericLiteralName(name) && name[0] == '-' {
25852585
return b.f.NewComputedPropertyName(b.f.NewPrefixUnaryExpression(ast.KindMinusToken, b.f.NewNumericLiteral(name[1:], ast.TokenFlagsNone)))
25862586
}
25872587
return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol)

‎tsc/internal/checker/relater.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4314,7 +4314,7 @@ func (r *Relater) propertiesRelatedTo(source *Type, target *Type, reportErrors b
43144314
numericNamesOnly := isTupleType(source) && isTupleType(target)
43154315
for _, targetProp := range excludeProperties(properties, excludedProperties) {
43164316
name := targetProp.Name()
4317-
if targetProp.Flags()&ast.SymbolFlagsPrototype == 0 && (!numericNamesOnly || isNumericLiteralName(name) || name == "length") && (!optionalsOnly || targetProp.Flags()&ast.SymbolFlagsOptional != 0) {
4317+
if targetProp.Flags()&ast.SymbolFlagsPrototype == 0 && (!numericNamesOnly || jsnum.IsNumericLiteralName(name) || name == "length") && (!optionalsOnly || targetProp.Flags()&ast.SymbolFlagsOptional != 0) {
43184318
sourceProp := r.c.getPropertyOfType(source, name)
43194319
if sourceProp != nil && sourceProp != targetProp {
43204320
related := r.propertyRelatedTo(source, target, sourceProp, targetProp, r.c.getNonMissingTypeOfSymbol, reportErrors, intersectionState, r.relation == r.c.comparableRelation)

‎tsc/internal/checker/utilities.go‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,31 +1057,6 @@ func getPropertyNameFromType(t *Type) string {
10571057
panic("Unhandled case in getPropertyNameFromType")
10581058
}
10591059

1060-
func isNumericLiteralName(name string) bool {
1061-
// The intent of numeric names is that
1062-
// - they are names with text in a numeric form, and that
1063-
// - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit',
1064-
// acquired by applying the abstract 'ToNumber' operation on the name's text.
1065-
//
1066-
// The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name.
1067-
// In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold.
1068-
//
1069-
// Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)'
1070-
// according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'.
1071-
// Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names
1072-
// because their 'ToString' representation is not equal to their original text.
1073-
// This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1.
1074-
//
1075-
// Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'.
1076-
// The '+' prefix operator is equivalent here to applying the abstract ToNumber operation.
1077-
// Applying the 'toString()' method on a number gives us the abstract ToString operation on a number.
1078-
//
1079-
// Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional.
1080-
// This is desired behavior, because when indexing with them as numeric entities, you are indexing
1081-
// with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively.
1082-
return jsnum.FromString(name).String() == name
1083-
}
1084-
10851060
func isThisProperty(node *ast.Node) bool {
10861061
return (ast.IsPropertyAccessExpression(node) || ast.IsElementAccessExpression(node)) && node.Expression().Kind == ast.KindThisKeyword
10871062
}

‎tsc/internal/fourslash/tests/completionForStringLiteralRelativeImport4_test.go‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/microsoft/TypeScript/tsc/internal/fourslash"
77
. "github.com/microsoft/TypeScript/tsc/internal/fourslash/tests/util"
8+
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
89
"github.com/microsoft/TypeScript/tsc/internal/testutil"
910
)
1011

@@ -73,3 +74,155 @@ export const x = 0;
7374
},
7475
})
7576
}
77+
78+
func TestPathCompletionEscaping(t *testing.T) {
79+
t.Parallel()
80+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
81+
const content = `// @Filename: /src/main.ts
82+
import "./[|a/*double*/|]";
83+
import './[|a/*single*/|]';
84+
// @Filename: /src/a"b.ts
85+
export {};
86+
// @Filename: /src/a'b.ts
87+
export {};
88+
`
89+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
90+
defer done()
91+
for i, test := range []struct {
92+
marker string
93+
label string
94+
insertText string
95+
newText string
96+
}{
97+
{"double", `a"b`, `a\"b`, `a\"b`},
98+
{"single", `a'b`, `a\'b`, `a\'b`},
99+
} {
100+
item := &lsproto.CompletionItem{
101+
Label: test.label,
102+
Detail: new(test.label + ".ts"),
103+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
104+
TextEdit: &lsproto.TextEdit{NewText: test.newText, Range: f.Ranges()[i].LSRange},
105+
},
106+
}
107+
if test.insertText != "" {
108+
item.InsertText = &test.insertText
109+
}
110+
f.VerifyCompletions(t, test.marker, &fourslash.CompletionsExpectedList{
111+
IsIncomplete: false,
112+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
113+
CommitCharacters: &[]string{},
114+
EditRange: Ignored,
115+
},
116+
Items: &fourslash.CompletionsExpectedItems{Includes: []fourslash.CompletionsExpectedItem{item}},
117+
})
118+
}
119+
}
120+
121+
func TestPathCompletionEscapedDirectory(t *testing.T) {
122+
t.Parallel()
123+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
124+
const content = `// @Filename: /src/main.ts
125+
import "./a\"b/c/*escaped*/";
126+
import "./\u0061\x2fc/*escapedSeparator*/";
127+
// @Filename: /src/a"b/cd.ts
128+
export {};
129+
// @Filename: /src/a/cd.ts
130+
export {};
131+
`
132+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
133+
defer done()
134+
for _, test := range []struct {
135+
marker string
136+
// Character offset of the replaced text; the "c" is at 15 and 20 respectively.
137+
character uint32
138+
}{
139+
{"escaped", 15},
140+
{"escapedSeparator", 20},
141+
} {
142+
line := f.MarkerByName(t, test.marker).LSPos().Line
143+
f.VerifyCompletions(t, test.marker, &fourslash.CompletionsExpectedList{
144+
IsIncomplete: false,
145+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
146+
CommitCharacters: &[]string{},
147+
EditRange: Ignored,
148+
},
149+
Items: &fourslash.CompletionsExpectedItems{
150+
Includes: []fourslash.CompletionsExpectedItem{
151+
&lsproto.CompletionItem{
152+
Label: "cd",
153+
Detail: new("cd.ts"),
154+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
155+
TextEdit: &lsproto.TextEdit{
156+
NewText: "cd",
157+
Range: lsproto.Range{
158+
Start: lsproto.Position{Line: line, Character: test.character},
159+
End: lsproto.Position{Line: line, Character: test.character + 1},
160+
},
161+
},
162+
},
163+
},
164+
},
165+
},
166+
})
167+
}
168+
}
169+
170+
func TestTripleSlashReferenceCompletionQuotes(t *testing.T) {
171+
t.Parallel()
172+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
173+
const content = `// @Filename: /src/main.ts
174+
/// <reference path="./a/*reference*/" />
175+
// @Filename: /src/a"b.ts
176+
export {};
177+
// @Filename: /src/a'b.ts
178+
export {};
179+
`
180+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
181+
defer done()
182+
f.VerifyCompletions(t, "reference", &fourslash.CompletionsExpectedList{
183+
IsIncomplete: false,
184+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
185+
CommitCharacters: &[]string{},
186+
EditRange: Ignored,
187+
},
188+
Items: &fourslash.CompletionsExpectedItems{
189+
Includes: []fourslash.CompletionsExpectedItem{"a'b.ts"},
190+
Excludes: []string{`a"b.ts`},
191+
},
192+
})
193+
}
194+
195+
func TestPathCompletionEscapedTemplate(t *testing.T) {
196+
t.Parallel()
197+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
198+
const content = "// @Filename: /src/main.ts\n" +
199+
"import(`./\\x61/c/*template*/`);\n" +
200+
"// @Filename: /src/a/cd.ts\n" +
201+
"export {};\n"
202+
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
203+
defer done()
204+
f.VerifyCompletions(t, "template", &fourslash.CompletionsExpectedList{
205+
IsIncomplete: false,
206+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
207+
CommitCharacters: &[]string{},
208+
EditRange: Ignored,
209+
},
210+
Items: &fourslash.CompletionsExpectedItems{
211+
Includes: []fourslash.CompletionsExpectedItem{
212+
&lsproto.CompletionItem{
213+
Label: "cd",
214+
Detail: new("cd.ts"),
215+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
216+
TextEdit: &lsproto.TextEdit{
217+
NewText: "cd",
218+
Range: lsproto.Range{
219+
Start: lsproto.Position{Line: 0, Character: 15},
220+
End: lsproto.Position{Line: 0, Character: 16},
221+
},
222+
},
223+
},
224+
},
225+
},
226+
},
227+
})
228+
}

0 commit comments

Comments
 (0)