Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"sync"
"sync/atomic"
"unicode/utf8"

"github.com/microsoft/TypeScript/tsc/internal/ast"
"github.com/microsoft/TypeScript/tsc/internal/binder"
Expand Down Expand Up @@ -8790,8 +8791,14 @@ 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])) {
relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon)
afterTrivia := scanner.SkipTriviaEx(text, node.Expression().End(), &options)
// Decode the previous UTF-8 rune; indexing text[afterTrivia-1] only works
// for ASCII line breaks (LF/CR), not U+2028 / U+2029 (issue #65433).
if afterTrivia > 0 {
prev, _ := utf8.DecodeLastRuneInString(text[:afterTrivia])
if stringutil.IsLineBreak(prev) {
relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon)
}
}
}
c.invocationError(node.Expression(), apparentType, SignatureKindCall, relatedInformation)
Expand Down
6 changes: 6 additions & 0 deletions tsc/internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,12 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
default:
if ch > rune(maxAsciiCharacter) && stringutil.IsWhiteSpaceLike(ch) {
pos += size
// U+2028 / U+2029 are line terminators (ECMAScript 7.3). When asked to
// stop after a line break, treat them like LF/CR rather than continuing
// past them as ordinary whitespace (issue #65433).
if options.StopAfterLineBreak && stringutil.IsLineBreak(ch) {
return pos
}
continue
}
}
Expand Down
30 changes: 30 additions & 0 deletions tsc/internal/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,33 @@ func TestRegExpLiteralReplacementCharacterInClassRange(t *testing.T) {
})
}
}

func TestSkipTriviaStopsAfterUnicodeLineBreaks(t *testing.T) {
t.Parallel()

options := &SkipTriviaOptions{StopAfterLineBreak: true}
// Trailing space after each terminator matters: without StopAfterLineBreak for
// U+2028/U+2029, SkipTrivia would continue past that whitespace to '('.
tests := []struct {
name string
text string
wantAfterOffset int // byte offset immediately after the line terminator
}{
{name: "LF", text: "x\n (", wantAfterOffset: len("x\n")},
{name: "CR", text: "x\r (", wantAfterOffset: len("x\r")},
{name: "CRLF", text: "x\r\n (", wantAfterOffset: len("x\r\n")},
{name: "line separator", text: "x\u2028 (", wantAfterOffset: len("x\u2028")},
{name: "paragraph separator", text: "x\u2029 (", wantAfterOffset: len("x\u2029")},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
after := SkipTriviaEx(test.text, 1, options)
assert.Equal(t, after, test.wantAfterOffset)
prev, _ := utf8.DecodeLastRuneInString(test.text[:after])
assert.Assert(t, stringutil.IsLineBreak(prev), "expected previous rune to be a line break, got %U", prev)
assert.Equal(t, test.text[after], byte(' '))
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
betterErrorForAccidentalCallLineSeparator.ts(3,26): error TS2349: This expression is not callable.
Type 'Number' has no call signatures.
betterErrorForAccidentalCallLineSeparator.ts(6,1): error TS2349: This expression is not callable.
Type 'String' has no call signatures.


==== betterErrorForAccidentalCallLineSeparator.ts (2 errors) ====
declare function foo(): string;

const a = (x: number) => x
 (1);
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallLineSeparator.ts:3:26: 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 betterErrorForAccidentalCallLineSeparator.ts:6:1: Are you missing a semicolon?
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/betterErrorForAccidentalCallLineSeparator.ts] ////

//// [betterErrorForAccidentalCallLineSeparator.ts]
declare function foo(): string;

const a = (x: number) => x
 (1);

foo()
 (1 as number).toString();


//// [betterErrorForAccidentalCallLineSeparator.js]
"use strict";
const a = (x) => x(1);
foo()(1).toString();
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/betterErrorForAccidentalCallLineSeparator.ts] ////

=== betterErrorForAccidentalCallLineSeparator.ts ===
declare function foo(): string;
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallLineSeparator.ts, 0, 0))

const a = (x: number) => x
>a : Symbol(a, Decl(betterErrorForAccidentalCallLineSeparator.ts, 2, 5))
>x : Symbol(x, Decl(betterErrorForAccidentalCallLineSeparator.ts, 2, 11))
>x : Symbol(x, Decl(betterErrorForAccidentalCallLineSeparator.ts, 2, 11))

(1);

foo()
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallLineSeparator.ts, 0, 0))

(1 as number).toString();

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/betterErrorForAccidentalCallLineSeparator.ts] ////

=== betterErrorForAccidentalCallLineSeparator.ts ===
declare function foo(): string;
>foo : () => string

const a = (x: number) => x
>a : (x: number) => any
>(x: number) => x
 (1) : (x: number) => any
>x : number
>x
 (1) : any
>x : number

(1);
>1 : 1

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: es2015
declare function foo(): string;

const a = (x: number) => x
 (1);

foo()
 (1 as number).toString();