diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 694fc155b8e16..f0ddce515b8ae 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -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 { @@ -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 { @@ -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 } @@ -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) } } @@ -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 @@ -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) } @@ -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 diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 5e8d3205ca031..a127c1762f75f 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -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) @@ -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 @@ -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 { @@ -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) diff --git a/tsc/internal/checker/relater.go b/tsc/internal/checker/relater.go index bbaca3eba478e..ccd1e92e8140d 100644 --- a/tsc/internal/checker/relater.go +++ b/tsc/internal/checker/relater.go @@ -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) diff --git a/tsc/internal/checker/utilities.go b/tsc/internal/checker/utilities.go index c4cc97fa29a1b..01abc2278b039 100644 --- a/tsc/internal/checker/utilities.go +++ b/tsc/internal/checker/utilities.go @@ -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 } diff --git a/tsc/internal/fourslash/tests/completionForStringLiteralRelativeImport4_test.go b/tsc/internal/fourslash/tests/completionForStringLiteralRelativeImport4_test.go index 183514f45f275..057e060a72567 100644 --- a/tsc/internal/fourslash/tests/completionForStringLiteralRelativeImport4_test.go +++ b/tsc/internal/fourslash/tests/completionForStringLiteralRelativeImport4_test.go @@ -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" ) @@ -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 +/// +// @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}, + }, + }, + }, + }, + }, + }, + }) +} diff --git a/tsc/internal/fourslash/tests/completionListInvalidMemberNames_escapeQuote_test.go b/tsc/internal/fourslash/tests/completionListInvalidMemberNames_escapeQuote_test.go index 63510a2b85ed2..db38662ac299d 100644 --- a/tsc/internal/fourslash/tests/completionListInvalidMemberNames_escapeQuote_test.go +++ b/tsc/internal/fourslash/tests/completionListInvalidMemberNames_escapeQuote_test.go @@ -61,3 +61,179 @@ x[|./**/|];` UserPreferences: &lsutil.UserPreferences{QuotePreference: lsutil.QuotePreference("single")}, }) } + +func TestStringCompletionEscaping(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `interface Properties { + "a'b\"c${x}\\z": number; +} +declare const value: Properties; +value["[|/*double*/|]"]; +value['[|/*single*/|]']; +value[` + "`[|/*template*/|]`" + `]; +type Indexed = Properties['[|/*indexed*/|]']; +"[|/*in*/|]" in value; +const object: Properties = { '[|/*object*/|]': 1 }; +declare function f(value: "a'b\"c${x}\\z"): void; +f('[|/*typeSingle*/|]'); +f(` + "`[|/*typeTemplate*/|]`" + `); +` + 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"c${x}\z`, `a'b\"c${x}\\z`, `a'b\"c${x}\\z`}, + {"single", `a'b"c${x}\z`, `a\'b"c${x}\\z`, `a\'b"c${x}\\z`}, + {"template", `a'b"c${x}\z`, `a'b"c\${x}\\z`, `a'b"c\${x}\\z`}, + {"indexed", `a'b"c${x}\z`, `a\'b"c${x}\\z`, `a\'b"c${x}\\z`}, + {"in", `a'b"c${x}\z`, `a'b\"c${x}\\z`, `a'b\"c${x}\\z`}, + {"object", `a'b"c${x}\z`, `a\'b"c${x}\\z`, `a\'b"c${x}\\z`}, + {"typeSingle", `a\'b"c${x}\\z`, "", `a\'b"c${x}\\z`}, + {"typeTemplate", `a'b"c\${x}\\z`, "", `a'b"c\${x}\\z`}, + } { + item := &lsproto.CompletionItem{ + Label: test.label, + 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: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{Includes: []fourslash.CompletionsExpectedItem{item}}, + }) + } +} + +func TestStringCompletionEscapingUnterminated(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `declare const value: { "a'b": number }; +value['/*completion*/` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCompletions(t, "completion", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + }, + Items: &fourslash.CompletionsExpectedItems{ + Includes: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{Label: "a'b", InsertText: new(`a\'b`)}, + }, + }, + }) +} + +func TestCompletionListDigitPrefixedMemberNames(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `declare const x: { "1+sideEffect()": 0; "01": 0; 1: 0 }; +x[|./*member*/|];` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + var items []fourslash.CompletionsExpectedItem + for _, test := range []struct { + label string + newText string + }{ + {"1+sideEffect()", `["1+sideEffect()"]`}, + {"01", `["01"]`}, + {"1", "[1]"}, + } { + items = append(items, &lsproto.CompletionItem{ + Label: test.label, + InsertText: new(test.newText), + TextEdit: &lsproto.TextEditOrInsertReplaceEdit{ + TextEdit: &lsproto.TextEdit{NewText: test.newText, Range: f.Ranges()[0].LSRange}, + }, + }) + } + f.VerifyCompletions(t, "member", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{Includes: items}, + }) +} + +func TestCompletionForQuotedImportExportNames(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @Filename: /lib.ts +const value = 0; +export { value as "a'b", value as "1" }; +// @Filename: /main.ts +import { '[|/*import*/|]' as a } from "./lib"; +export { '[|/*export*/|]' } from "./lib"; +export { /*plain*/ } from "./lib"; +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCompletions(t, "plain", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{ + Includes: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{Label: "1", InsertText: new(`"1"`)}, + }, + }, + }) + for i, marker := range []string{"import", "export"} { + f.VerifyCompletions(t, marker, &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{ + Includes: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{ + Label: "a'b", + InsertText: new(`a\'b`), + TextEdit: &lsproto.TextEditOrInsertReplaceEdit{ + TextEdit: &lsproto.TextEdit{NewText: `a\'b`, Range: f.Ranges()[i].LSRange}, + }, + }, + }, + }, + }) + } +} + +func TestLiteralCompletionLoneSurrogate(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `declare function f(x: "\uD800"): void; +f(/*argument*/);` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCompletions(t, "argument", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{ + Includes: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{Label: `"\ud800"`, CommitCharacters: &[]string{}}, + }, + }, + }) +} diff --git a/tsc/internal/fourslash/tests/completionsJsxAttributeInitializer2_test.go b/tsc/internal/fourslash/tests/completionsJsxAttributeInitializer2_test.go index 159e49f6521fc..1f231a25d2884 100644 --- a/tsc/internal/fourslash/tests/completionsJsxAttributeInitializer2_test.go +++ b/tsc/internal/fourslash/tests/completionsJsxAttributeInitializer2_test.go @@ -10,7 +10,6 @@ import ( ) func TestCompletionsJsxAttributeInitializer2(t *testing.T) { - t.Skip("Known failing fourslash test") t.Parallel() defer testutil.RecoverAndFail(t, "Panic on fourslash test") const content = `// @Filename: /a.tsx diff --git a/tsc/internal/fourslash/tests/stringLiteralCompletionsInJsxAttributeInitializer_test.go b/tsc/internal/fourslash/tests/stringLiteralCompletionsInJsxAttributeInitializer_test.go index 3255d475ba49d..60121e38d1f0e 100644 --- a/tsc/internal/fourslash/tests/stringLiteralCompletionsInJsxAttributeInitializer_test.go +++ b/tsc/internal/fourslash/tests/stringLiteralCompletionsInJsxAttributeInitializer_test.go @@ -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" ) @@ -61,3 +62,133 @@ const a5 = ` }, }) } + +func TestStringLiteralCompletionsInJsxAttributeEscaping(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @jsx: preserve +// @filename: /a.tsx +declare function Foo(args: { value: "a'b&c\\z" | "a\"b" | "&" }): any; +const a = ; +const b = ; +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + textEdit := func(lsRange lsproto.Range, text string) *lsproto.TextEditOrInsertReplaceEdit { + return &lsproto.TextEditOrInsertReplaceEdit{ + TextEdit: &lsproto.TextEdit{NewText: text, Range: lsRange}, + } + } + verify := func(marker string, items ...fourslash.CompletionsExpectedItem) { + f.VerifyCompletions(t, marker, &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{Includes: items}, + }) + } + verify("raw", &lsproto.CompletionItem{ + Label: `a'b&c\z`, + TextEdit: textEdit(f.Ranges()[0].LSRange, `a'b&c\z`), + }) + verify("container", + &lsproto.CompletionItem{ + Label: `a"b`, + InsertText: new(`{"a\"b"}`), + FilterText: new(`"a"b`), + TextEdit: textEdit(f.Ranges()[1].LSRange, `{"a\"b"}`), + }, + &lsproto.CompletionItem{ + Label: `&`, + InsertText: new(`{"&"}`), + FilterText: new(`"&`), + TextEdit: textEdit(f.Ranges()[1].LSRange, `{"&"}`), + }, + ) +} + +func TestJsxAttributeInitializerLiteralCompletions(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @jsx: preserve +// @filename: /a.tsx +declare function Foo(args: { value: "a'b&c\\z" | "a\"b" | "&"; count: 1 }): any; +const a = ; +const b = ; +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + for _, test := range []struct { + marker string + label string + insertText string + }{ + {"value", `"a'b&c\\z"`, `"a'b&c\z"`}, + {"value", `"a\"b"`, `'a"b'`}, + {"value", `"&"`, `{"&"}`}, + {"count", "1", "{1}"}, + } { + item := &lsproto.CompletionItem{Label: test.label, CommitCharacters: &[]string{}} + if test.insertText != "" { + item.InsertText = &test.insertText + } + f.VerifyCompletions(t, test.marker, &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{Includes: []fourslash.CompletionsExpectedItem{item}}, + }) + } +} + +func TestJsxAttributeCompletionLoneSurrogate(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @jsx: preserve +// @filename: /a.tsx +declare function Foo(args: { value: "\uD800" }): any; +const a = ; +const b = ; +` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCompletions(t, "quoted", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{ + Exact: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{ + Label: `\ud800`, + InsertText: new(`{"\uD800"}`), + FilterText: new(`"\ud800`), + TextEdit: &lsproto.TextEditOrInsertReplaceEdit{ + TextEdit: &lsproto.TextEdit{NewText: `{"\uD800"}`, Range: f.Ranges()[0].LSRange}, + }, + }, + }, + }, + }) + f.VerifyCompletions(t, "initializer", &fourslash.CompletionsExpectedList{ + IsIncomplete: false, + ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{ + CommitCharacters: &DefaultCommitCharacters, + EditRange: Ignored, + }, + Items: &fourslash.CompletionsExpectedItems{ + Includes: []fourslash.CompletionsExpectedItem{ + &lsproto.CompletionItem{ + Label: `"\ud800"`, + InsertText: new(`{"\uD800"}`), + CommitCharacters: &[]string{}, + }, + }, + }, + }) +} diff --git a/tsc/internal/jsnum/string.go b/tsc/internal/jsnum/string.go index 639dd7f59380a..919af42e4a7f4 100644 --- a/tsc/internal/jsnum/string.go +++ b/tsc/internal/jsnum/string.go @@ -96,6 +96,32 @@ func FromString(s string) Number { return Number(math.Copysign(f, sign)) } +// IsNumericLiteralName reports whether name is the canonical string form of a number. +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 FromString(name).String() == name +} + func isStrWhiteSpace(r rune) bool { // This is different than stringutil.IsWhiteSpaceLike. diff --git a/tsc/internal/ls/completions.go b/tsc/internal/ls/completions.go index eea053c35a173..9affd8602168a 100644 --- a/tsc/internal/ls/completions.go +++ b/tsc/internal/ls/completions.go @@ -769,6 +769,7 @@ func (l *LanguageService) getCompletionData( if parent != previousToken.Parent && parent.Initializer() == nil && astnav.FindChildOfKind(parent, ast.KindEqualsToken, file) != nil { + jsxInitializer.isInitializer = true jsxInitializer.initializer = previousToken } } @@ -1901,7 +1902,7 @@ func (l *LanguageService) completionInfoFromData( } for _, literal := range literals { - literalEntry := createCompletionItemForLiteral(file, preferences, literal) + literalEntry := createCompletionItemForLiteral(file, preferences, literal, data.jsxInitializer.isInitializer) uniqueNames.Add(literalEntry.Label) sortedEntries = append(sortedEntries, &CompletionItem{CompletionItem: literalEntry}) } @@ -2168,13 +2169,24 @@ func createCompletionItemForLiteral( file *ast.SourceFile, preferences lsutil.UserPreferences, literal literalValue, + isJsxInitializer bool, ) *lsproto.CompletionItem { - return &lsproto.CompletionItem{ + item := &lsproto.CompletionItem{ Label: completionNameForLiteral(file, preferences, literal), Kind: new(lsproto.CompletionItemKindConstant), SortText: new(string(SortTextLocationPriority)), CommitCharacters: new([]string{}), } + if isJsxInitializer { + insertText := "{" + item.Label + "}" + if value, ok := literal.(string); ok { + insertText = jsxAttributeInitializerText(file, preferences, value) + } + if insertText != item.Label { + item.InsertText = &insertText + } + } + return item } func (l *LanguageService) createCompletionItem( @@ -2201,6 +2213,12 @@ func (l *LanguageService) createCompletionItem( source := getSourceFromOrigin(origin) var labelDetails *lsproto.CompletionItemLabelDetails preferences := l.UserPreferences() + if data.completionKind == CompletionKindString { + escaped := printer.EscapeString(name, stringCompletionQuoteChar(contextToken)) + if escaped != name { + insertText = escaped + } + } insertQuestionDot := originIsNullableMember(origin) useBraces := originIsSymbolMember(origin) || needsConvertPropertyAccess if originIsThisTypeNode(origin) { @@ -2405,7 +2423,7 @@ func (l *LanguageService) createCompletionItem( parentNamedImportOrExport := ast.FindAncestor(data.location, isNamedImportsOrExports) if parentNamedImportOrExport != nil { if !scanner.IsIdentifierText(name, core.LanguageVariantStandard) { - insertText = quotePropertyName(file, preferences, name) + insertText = quote(file, preferences, name) if parentNamedImportOrExport.Kind == ast.KindNamedImports { // Check if it is `import { ^here as name } from '...'``. @@ -3709,8 +3727,7 @@ func (l *LanguageService) createRangeFromStringLiteralLikeContent(file *ast.Sour } func quotePropertyName(file *ast.SourceFile, preferences lsutil.UserPreferences, name string) string { - r, _ := utf8.DecodeRuneInString(name) - if unicode.IsDigit(r) { + if len(name) > 0 && name[0] >= '0' && name[0] <= '9' && jsnum.IsNumericLiteralName(name) { return name } return quote(file, preferences, name) diff --git a/tsc/internal/ls/string_completions.go b/tsc/internal/ls/string_completions.go index b3f0f4c01ed46..aa9b9a8b193c7 100644 --- a/tsc/internal/ls/string_completions.go +++ b/tsc/internal/ls/string_completions.go @@ -8,6 +8,7 @@ import ( "maps" "slices" "strings" + "unicode/utf8" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/astnav" @@ -65,7 +66,7 @@ func (l *LanguageService) getStringLiteralCompletions( ) *CompletionList { if isInReferenceComment(file, position) { completion := l.getTripleSlashReferenceCompletions(file, position, l.GetProgram(), checker) - return l.convertPathCompletions(ctx, completion, file, position) + return l.convertPathCompletions(ctx, completion, nil, file, position) } if IsInString(file, position, contextToken) { if contextToken == nil || !ast.IsStringLiteralLike(contextToken) { @@ -109,7 +110,7 @@ func (l *LanguageService) convertStringLiteralCompletions( optionalReplacementRange := l.createRangeFromStringLiteralLikeContent(file, contextToken, position) switch { case completion.fromPaths != nil: - return l.convertPathCompletions(ctx, completion.fromPaths, file, position) + return l.convertPathCompletions(ctx, completion.fromPaths, contextToken, file, position) case completion.fromProperties != nil: completion := completion.fromProperties data := &completionDataData{ @@ -148,25 +149,39 @@ func (l *LanguageService) convertStringLiteralCompletions( } case completion.fromTypes != nil: completion := completion.fromTypes - var quoteChar printer.QuoteChar - if contextToken.Kind == ast.KindNoSubstitutionTemplateLiteral { - quoteChar = printer.QuoteCharBacktick - } else if strings.HasPrefix(contextToken.Text(), "'") { - quoteChar = printer.QuoteCharSingleQuote - } else { - quoteChar = printer.QuoteCharDoubleQuote - } - items := core.Map(completion.types, func(t *checker.StringLiteralType) *CompletionItem { - name := printer.EscapeString(t.AsLiteralType().Value().(string), quoteChar) + quoteChar := stringCompletionQuoteChar(contextToken) + isJsxAttribute := ast.IsJsxAttribute(contextToken.Parent) + items := core.MapNonNil(completion.types, func(t *checker.StringLiteralType) *CompletionItem { + value := t.AsLiteralType().Value().(string) + name := mapLoneSurrogates(value, func(s string) string { return s }) + insertText := "" + filterText := "" + replacementSpan := l.getReplacementRangeForContextToken(file, contextToken, position) + if !isJsxAttribute { + name = printer.EscapeString(value, quoteChar) + } else if !jsxAttributeStringCanContain(value, byte(quoteChar)) { + // JSX attribute strings have no escape sequences, so replace the + // string with an expression container holding the value. + if ast.IsUnterminatedLiteral(contextToken) { + return nil + } + lspRange, fidelity := l.createLspRangeFromNode(contextToken, file) + if !fidelity.IsExact() { + return nil + } + replacementSpan = &lspRange + insertText = jsxExpressionContainerText(file, l.UserPreferences(), value) + filterText = string(quoteChar) + name + } lspItem := l.createLSPCompletionItem( ctx, name, - "", /*insertText*/ - "", /*filterText*/ + insertText, + filterText, SortTextLocationPriority, lsutil.ScriptElementKindString, lsutil.ScriptElementKindModifierNone, - l.getReplacementRangeForContextToken(file, contextToken, position), + replacementSpan, nil, /*commitCharacters*/ nil, /*labelDetails*/ file, @@ -203,9 +218,62 @@ func (l *LanguageService) convertStringLiteralCompletions( } } +// JSX attribute strings have no escape sequences and their character references +// are decoded only during emit, so values containing the quote character, a +// possible character reference, or a lone surrogate must be written as an +// expression instead. +func jsxAttributeStringCanContain(value string, quoteChar byte) bool { + if strings.IndexByte(value, quoteChar) >= 0 { + return false + } + if amp := strings.IndexByte(value, '&'); amp >= 0 && strings.IndexByte(value[amp:], ';') >= 0 { + return false + } + for i := 0; i < len(value); { + ch, size := stringutil.DecodeJSStringRune(value[i:]) + if stringutil.IsSurrogate(ch) || ch == utf8.RuneError && size == 1 { + return false + } + i += size + } + return true +} + +func jsxExpressionContainerText(file *ast.SourceFile, preferences lsutil.UserPreferences, value string) string { + quoteChar := printer.QuoteCharDoubleQuote + if lsutil.GetQuotePreference(file, preferences) == lsutil.QuotePreferenceSingle { + quoteChar = printer.QuoteCharSingleQuote + } + return "{" + string(quoteChar) + printer.EscapeString(value, quoteChar) + string(quoteChar) + "}" +} + +func jsxAttributeInitializerText(file *ast.SourceFile, preferences lsutil.UserPreferences, value string) string { + quoteChar, otherQuoteChar := byte('"'), byte('\'') + if lsutil.GetQuotePreference(file, preferences) == lsutil.QuotePreferenceSingle { + quoteChar, otherQuoteChar = otherQuoteChar, quoteChar + } + for _, q := range []byte{quoteChar, otherQuoteChar} { + if jsxAttributeStringCanContain(value, q) { + return string(q) + value + string(q) + } + } + return jsxExpressionContainerText(file, preferences, value) +} + +func stringCompletionQuoteChar(contextToken *ast.StringLiteralLike) printer.QuoteChar { + if contextToken.Kind == ast.KindNoSubstitutionTemplateLiteral { + return printer.QuoteCharBacktick + } + if contextToken.AsStringLiteral().TokenFlags&ast.TokenFlagsSingleQuote != 0 { + return printer.QuoteCharSingleQuote + } + return printer.QuoteCharDoubleQuote +} + func (l *LanguageService) convertPathCompletions( ctx context.Context, completion *pathCompletions, + contextToken *ast.StringLiteralLike, file *ast.SourceFile, position int, ) *CompletionList { @@ -219,10 +287,17 @@ func (l *LanguageService) convertPathCompletions( if !strings.HasSuffix(pathCompletion.name, pathCompletion.extension) { detail += pathCompletion.extension } + insertText := "" + if contextToken != nil { + escaped := printer.EscapeString(pathCompletion.name, stringCompletionQuoteChar(contextToken)) + if escaped != pathCompletion.name { + insertText = escaped + } + } lspItem := l.createLSPCompletionItem( ctx, pathCompletion.name, - "", /*insertText*/ + insertText, "", /*filterText*/ SortTextLocationPriority, pathCompletion.kind, @@ -384,9 +459,9 @@ func (l *LanguageService) getStringLiteralCompletionEntries( namedImportsOrExports := specifier.Parent var moduleSpecifier *ast.Node if namedImportsOrExports.Kind == ast.KindNamedImports { - moduleSpecifier = namedImportsOrExports.Parent.Parent + moduleSpecifier = namedImportsOrExports.Parent.Parent.ModuleSpecifier() } else { - moduleSpecifier = namedImportsOrExports.Parent + moduleSpecifier = namedImportsOrExports.Parent.ModuleSpecifier() } if moduleSpecifier == nil { return nil @@ -590,8 +665,7 @@ func (l *LanguageService) getStringLiteralCompletionsFromModuleNames( program *compiler.Program, checker *checker.Checker, ) *stringLiteralCompletions { - textStart := astnav.GetStartOfNode(node, file, false /*includeJSDoc*/) + 1 - replacementSpan, ok := l.pathCompletionReplacementSpan(file, getDirectoryFragmentRange(node.Text(), textStart)) + replacementSpan, ok := l.pathCompletionReplacementSpan(file, getDirectoryFragmentRangeOfLiteral(file, node)) if !ok { return nil } @@ -660,6 +734,33 @@ func getDirectoryFragmentRange(text string, textStart int) *core.TextRange { return new(core.NewTextRange(textStart+offset, textStart+offset+length)) } +// Like getDirectoryFragmentRange, but measures the fragment in the source text +// of a string literal, whose escape sequences may differ in length from their values. +func getDirectoryFragmentRangeOfLiteral(file *ast.SourceFile, node *ast.LiteralExpression) *core.TextRange { + start := astnav.GetStartOfNode(node, file, false /*includeJSDoc*/) + end := node.End() + if !ast.IsUnterminatedLiteral(node) { + end-- + } + rawText := file.Text()[start+1 : max(start+1, end)] + text := node.Text() + if rawText == text { + return getDirectoryFragmentRange(text, start+1) + } + index := strings.LastIndexFunc(text, isAnyDirectorySeparator) + offset := 0 + if index != -1 { + offset = scanner.GetStringLiteralSourceOffset(rawText, index+1, node.Kind == ast.KindNoSubstitutionTemplateLiteral) + if offset == -1 { + return nil + } + } + if offset == len(rawText) { + return nil + } + return new(core.NewTextRange(start+1+offset, start+1+len(rawText))) +} + func (l *LanguageService) getStringLiteralCompletionsFromModuleNamesWorker( file *ast.SourceFile, node *ast.LiteralExpression, @@ -2261,6 +2362,11 @@ func (l *LanguageService) getTripleSlashReferenceCompletions( l.getCompletionEntriesFromTypings(program, scriptDirectory, getFragmentDirectory(toComplete), extensionOptions, result) names = slices.Collect(maps.Values(result.names)) } + // Triple-slash directive values cannot contain their own quote character. + quoteChar := prefix[len(prefix)-1] + names = slices.DeleteFunc(names, func(name moduleCompletionNameAndKind) bool { + return strings.IndexByte(name.name, quoteChar) >= 0 + }) return &pathCompletions{ entries: toPathCompletions(names), diff --git a/tsc/internal/ls/utilities.go b/tsc/internal/ls/utilities.go index f8a73f0f1eabb..c05dc839784d3 100644 --- a/tsc/internal/ls/utilities.go +++ b/tsc/internal/ls/utilities.go @@ -312,13 +312,35 @@ func (l *LanguageService) createLspPosition(position int, file *ast.SourceFile) func quote(file *ast.SourceFile, preferences lsutil.UserPreferences, text string) string { // Editors can pass in undefined or empty string - we want to infer the preference in those cases. quotePreference := lsutil.GetQuotePreference(file, preferences) - quoted, _ := core.StringifyJson(text, "" /*prefix*/, "" /*indent*/) + quoted := `"` + mapLoneSurrogates(text, func(s string) string { + quoted, _ := core.StringifyJson(s, "" /*prefix*/, "" /*indent*/) + return stringutil.StripQuotes(quoted) + }) + `"` if quotePreference == lsutil.QuotePreferenceSingle { quoted = "'" + quoteReplacer.Replace(stringutil.StripQuotes(quoted)) + "'" } return quoted } +// mapLoneSurrogates applies f to the text between the lone surrogates in s and +// writes each lone surrogate as a \uXXXX escape, as JSON.stringify does, since +// neither JSON nor LSP can carry one as raw text. +func mapLoneSurrogates(s string, f func(string) string) string { + var b strings.Builder + start := 0 + for i := 0; i < len(s); { + ch, size := stringutil.DecodeJSStringRune(s[i:]) + if stringutil.IsSurrogate(ch) { + b.WriteString(f(s[start:i])) + fmt.Fprintf(&b, `\u%04x`, ch) + start = i + size + } + i += size + } + b.WriteString(f(s[start:])) + return b.String() +} + var typeKeywords *collections.Set[ast.Kind] = collections.NewSetFromItems( ast.KindAnyKeyword, ast.KindAssertsKeyword, diff --git a/tsc/internal/scanner/scanner.go b/tsc/internal/scanner/scanner.go index a59637584e2c5..273cbae5b02a3 100644 --- a/tsc/internal/scanner/scanner.go +++ b/tsc/internal/scanner/scanner.go @@ -2555,6 +2555,35 @@ func GetShebang(text string) string { return text[:end] } +// GetStringLiteralSourceOffset returns the offset in contents, the source text +// between the delimiters of a string or no-substitution template literal, at +// which the first valueOffset bytes of the literal's value end. It returns -1 +// if that point is inside an escape sequence or past the end of the value. +func GetStringLiteralSourceOffset(contents string, valueOffset int, isTemplate bool) int { + s := NewScanner() + s.SetText(contents) + value := 0 + for value < valueOffset && s.pos < len(contents) { + if contents[s.pos] == '\\' { + value += len(s.scanEscapeSequence(EscapeSequenceScanningFlagsString | EscapeSequenceScanningFlagsReportErrors)) + } else if isTemplate && contents[s.pos] == '\r' { + // Template values normalize CR and CRLF to LF. + s.pos++ + if s.char() == '\n' { + s.pos++ + } + value++ + } else { + s.pos++ + value++ + } + } + if value != valueOffset { + return -1 + } + return s.pos +} + func GetScannerForSourceFile(sourceFile *ast.SourceFile, pos int) *Scanner { s := NewScanner() s.text = sourceFile.Text()