diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index f1e5564580f3c..fd6ea3bedbbb7 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -31,6 +31,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/tsoptions" "github.com/microsoft/TypeScript/tsc/internal/tspath" "github.com/zeebo/xxh3" + "unicode/utf8" ) // CheckMode @@ -8785,7 +8786,9 @@ func (c *Checker) resolveCallExpression(node *ast.Node, candidatesOutArray *[]*S if len(node.Arguments()) == 1 { text := ast.GetSourceFileOfNode(node).Text() options := scanner.SkipTriviaOptions{StopAfterLineBreak: true} - if stringutil.IsLineBreak(rune(text[scanner.SkipTriviaEx(text, node.Expression().End(), &options)-1])) { + pos := scanner.SkipTriviaEx(text, node.Expression().End(), &options) + lineBreak, _ := utf8.DecodeLastRuneInString(text[:pos]) + if stringutil.IsLineBreak(lineBreak) { relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon) } } diff --git a/tsc/internal/ls/change/delete.go b/tsc/internal/ls/change/delete.go index 8156e36e4d0be..d54590c151967 100644 --- a/tsc/internal/ls/change/delete.go +++ b/tsc/internal/ls/change/delete.go @@ -2,6 +2,7 @@ package change import ( "slices" + "unicode/utf8" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/astnav" @@ -234,12 +235,14 @@ func (t *Tracker) endPositionToDeleteNodeInList(sourceFile *ast.SourceFile, node if isSeparator(prevNode, prevToken) { pos := scanner.SkipTriviaEx(sourceFile.Text(), token.End(), &scanner.SkipTriviaOptions{StopAfterLineBreak: true, StopAtComments: true}) if positionsAreOnSameLine(astnav.GetStartOfNode(prevToken, sourceFile, false), astnav.GetStartOfNode(token, sourceFile, false), sourceFile) { - if pos > 0 && stringutil.IsLineBreak(rune(sourceFile.Text()[pos-1])) { - return pos - 1 + lastRune, size := utf8.DecodeLastRuneInString(sourceFile.Text()[:pos]) + if pos > 0 && stringutil.IsLineBreak(lastRune) { + return pos - size } return pos } - if stringutil.IsLineBreak(rune(sourceFile.Text()[pos])) { + lineBreak, _ := utf8.DecodeRuneInString(sourceFile.Text()[pos:]) + if stringutil.IsLineBreak(lineBreak) { return pos } } diff --git a/tsc/internal/ls/change/tracker.go b/tsc/internal/ls/change/tracker.go index d797d2cc821f4..db9ba698f8a40 100644 --- a/tsc/internal/ls/change/tracker.go +++ b/tsc/internal/ls/change/tracker.go @@ -3,6 +3,7 @@ package change import ( "context" "slices" + "unicode/utf8" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/astnav" @@ -498,9 +499,13 @@ func (t *Tracker) InsertNodeInListAfter(sourceFile *ast.SourceFile, after *ast.N indentation := format.FindFirstNonWhitespaceColumn(afterStartLinePosition, afterStart, sourceFile, t.formatSettings) // insert element before the line break on the line that contains 'after' element insertPos := scanner.SkipTriviaEx(sourceFile.Text(), end, &scanner.SkipTriviaOptions{StopAfterLineBreak: true, StopAtComments: false}) - // find position before "\n" or "\r\n" - for insertPos != end && stringutil.IsLineBreak(rune(sourceFile.Text()[insertPos-1])) { - insertPos-- + // find position before the line break + for insertPos != end { + lastRune, size := utf8.DecodeLastRuneInString(sourceFile.Text()[end:insertPos]) + if !stringutil.IsLineBreak(lastRune) { + break + } + insertPos -= size } insertLSPos := core.TextPos(insertPos) t.ReplaceRange( diff --git a/tsc/internal/ls/change/trackerimpl.go b/tsc/internal/ls/change/trackerimpl.go index 548bcc4d1d1eb..aa8f6f74e1bb6 100644 --- a/tsc/internal/ls/change/trackerimpl.go +++ b/tsc/internal/ls/change/trackerimpl.go @@ -5,6 +5,7 @@ import ( "slices" "strings" "unicode" + "unicode/utf8" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/astnav" @@ -394,7 +395,8 @@ func (t *Tracker) getAdjustedEndPosition(sourceFile *ast.SourceFile, node *ast.N newEnd := scanner.SkipTriviaEx(sourceFile.Text(), node.End(), &scanner.SkipTriviaOptions{StopAfterLineBreak: true}) - if newEnd != node.End() && (TrailingTriviaOption == TrailingTriviaOptionInclude || stringutil.IsLineBreak(rune(sourceFile.Text()[newEnd-1]))) { + lastRune, _ := utf8.DecodeLastRuneInString(sourceFile.Text()[:newEnd]) + if newEnd != node.End() && (TrailingTriviaOption == TrailingTriviaOptionInclude || stringutil.IsLineBreak(lastRune)) { return newEnd } return node.End() diff --git a/tsc/internal/ls/change/trackerimpl_test.go b/tsc/internal/ls/change/trackerimpl_test.go index 21303eaf46150..109d9e410d739 100644 --- a/tsc/internal/ls/change/trackerimpl_test.go +++ b/tsc/internal/ls/change/trackerimpl_test.go @@ -1,9 +1,14 @@ package change import ( - "testing" - + "context" + "github.com/microsoft/TypeScript/tsc/internal/ast" + "github.com/microsoft/TypeScript/tsc/internal/core" + "github.com/microsoft/TypeScript/tsc/internal/ls/lsutil" "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" + "github.com/microsoft/TypeScript/tsc/internal/parser" + "strings" + "testing" ) func TestTextEditsConflictAtSameInsertionPointAcrossProjections(t *testing.T) { @@ -20,3 +25,76 @@ func TestTextEditsConflictAtSameInsertionPointAcrossProjections(t *testing.T) { t.Fatal("insertions at the same position within one projection should retain their existing ordering") } } +func TestInsertNodeInListAfterUnicodeLineSeparators(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + separator string + }{ + {name: "U+2028", separator: "\u2028"}, + {name: "U+2029", separator: "\u2029"}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + text := "const values = [\n 1,\n 2 " + test.separator + "];" + + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ + FileName: "/index.ts", + PathKey: "/index.ts", + }, text, core.ScriptKindTS) + + declaration := sourceFile.Statements.Nodes[0]. + AsVariableStatement(). + DeclarationList. + AsVariableDeclarationList(). + Declarations.Nodes[0] + + array := declaration.AsVariableDeclaration().Initializer + elements := array.AsArrayLiteralExpression().Elements.Nodes + after := elements[1] + + tracker := NewTracker( + context.Background(), + core.EmptyCompilerOptions, + lsutil.GetDefaultFormatCodeSettings(), + nil, + ) + + newNode := tracker.NewNumericLiteral("3", ast.TokenFlagsNone) + + tracker.InsertNodeInListAfter( + sourceFile, + after, + newNode, + array.AsArrayLiteralExpression().Elements, + ) + + expectedPosition := strings.Index(text, test.separator) + found := false + + for _, edit := range tracker.changes.Get(sourceFile) { + if edit.Node == newNode { + found = true + actualPosition := edit.TextRange.Pos() + + if actualPosition != expectedPosition { + t.Fatalf( + "insertion position = %d, want %d", + actualPosition, + expectedPosition, + ) + } + break + } + } + + if !found { + t.Fatal("could not find the edit inserting the new node") + } + }) + } +} diff --git a/tsc/internal/scanner/scanner.go b/tsc/internal/scanner/scanner.go index 7dc5199678d93..2c38b33575821 100644 --- a/tsc/internal/scanner/scanner.go +++ b/tsc/internal/scanner/scanner.go @@ -2388,6 +2388,13 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int { } canConsumeStar = options.InJSDoc continue + case '\u2028', '\u2029': + pos += size + if options.StopAfterLineBreak { + return pos + } + canConsumeStar = options.InJSDoc + continue case '\t', '\v', '\f', ' ': pos++ continue diff --git a/tsc/internal/scanner/scanner_test.go b/tsc/internal/scanner/scanner_test.go index a2e666214c561..7c1aef7e5add5 100644 --- a/tsc/internal/scanner/scanner_test.go +++ b/tsc/internal/scanner/scanner_test.go @@ -173,3 +173,73 @@ func TestRegExpLiteralReplacementCharacterInClassRange(t *testing.T) { }) } } + +func TestSkipTriviaExHandlesUnicodeLineSeparators(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + text string + stopAfterLineBreak bool + expected int + }{ + { + name: "U+2028 stops after line separator", + text: " \u2028 next", + stopAfterLineBreak: true, + expected: len(" \u2028"), + }, + { + name: "U+2029 stops after paragraph separator", + text: " \u2029 next", + stopAfterLineBreak: true, + expected: len(" \u2029"), + }, + { + name: "U+2028 continues when not stopping", + text: " \u2028 next", + expected: len(" \u2028 "), + }, + { + name: "U+2029 continues when not stopping", + text: " \u2029 next", + expected: len(" \u2029 "), + }, + { + name: "LF preserves existing behavior", + text: " \n next", + stopAfterLineBreak: true, + expected: len(" \n"), + }, + { + name: "CRLF preserves existing behavior", + text: " \r\n next", + stopAfterLineBreak: true, + expected: len(" \r\n"), + }, + { + name: "line comment stops at U+2028", + text: "// comment\u2028 next", + stopAfterLineBreak: true, + expected: len("// comment\u2028"), + }, + { + name: "line comment continues after U+2029", + text: "// comment\u2029 next", + expected: len("// comment\u2029 "), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + options := &SkipTriviaOptions{ + StopAfterLineBreak: test.stopAfterLineBreak, + } + + actual := SkipTriviaEx(test.text, 0, options) + assert.Equal(t, actual, test.expected) + }) + } +} diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.errors.txt b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.errors.txt index f5dd0414ebbb2..97f0646da1d14 100644 --- a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.errors.txt @@ -8,9 +8,13 @@ betterErrorForAccidentalCall.ts(10,1): error TS2349: This expression is not call Type 'String' has no call signatures. betterErrorForAccidentalCall.ts(13,1): error TS2349: This expression is not callable. Type 'String' has no call signatures. +betterErrorForAccidentalCall.ts(16,1): error TS2349: This expression is not callable. + Type 'String' has no call signatures. +betterErrorForAccidentalCall.ts(19,1): error TS2349: This expression is not callable. + Type 'String' has no call signatures. -==== betterErrorForAccidentalCall.ts (5 errors) ==== +==== betterErrorForAccidentalCall.ts (7 errors) ==== declare function foo(): string; foo()(1 as number).toString(); @@ -43,4 +47,16 @@ betterErrorForAccidentalCall.ts(13,1): error TS2349: This expression is not call !!! error TS2349: Type 'String' has no call signatures. !!! related TS2734 betterErrorForAccidentalCall.ts:13:1: Are you missing a semicolon? (1).toString(); - \ No newline at end of file + + foo()
(1 as number).toString(); + ~~~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'String' has no call signatures. +!!! related TS2734 betterErrorForAccidentalCall.ts:16:1: Are you missing a semicolon? + + foo()
(1 as number).toString(); + + +!!! error TS2349: This expression is not callable. +!!! error TS2349: Type 'String' has no call signatures. +!!! related TS2734 betterErrorForAccidentalCall.ts:19:1: Are you missing a semicolon? \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.js b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.js index 492580b95be5b..d5064f3cc0b59 100644 --- a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.js +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.js @@ -15,6 +15,10 @@ foo() foo() (1).toString(); + +foo()
(1 as number).toString(); + +foo()
(1 as number).toString(); //// [betterErrorForAccidentalCall.js] @@ -24,3 +28,5 @@ foo()(1).toString(); foo()(1).toString(); foo()(1 + 2).toString(); foo()(1).toString(); +foo()(1).toString(); +foo()(1).toString(); diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.symbols b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.symbols index 1da1e8c67f8ef..fbeeab9940a2e 100644 --- a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.symbols +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.symbols @@ -25,3 +25,13 @@ foo() (1).toString(); +foo() +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +(1 as number).toString(); + +foo() +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +(1 as number).toString(); + diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.types b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.types index 8dfd9272bb83f..797f2f87f39bd 100644 --- a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.types +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCall.types @@ -61,3 +61,27 @@ foo() >1 : 1 >toString : any +foo() +>foo()
(1 as number).toString() : any +>foo()
(1 as number).toString : any +>foo()
(1 as number) : any +>foo() : string +>foo : () => string + +(1 as number).toString(); +>1 as number : number +>1 : 1 +>toString : any + +foo() +>foo()
(1 as number).toString() : any +>foo()
(1 as number).toString : any +>foo()
(1 as number) : any +>foo() : string +>foo : () => string + +(1 as number).toString(); +>1 as number : number +>1 : 1 +>toString : any + diff --git a/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCall.ts b/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCall.ts index 613cc9e9bc813..774512f0968b3 100644 --- a/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCall.ts +++ b/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCall.ts @@ -13,3 +13,7 @@ foo() foo() (1).toString(); + +foo()
(1 as number).toString(); + +foo()
(1 as number).toString();