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
9 changes: 7 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 @@ -8774,8 +8775,12 @@ 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)
pos := scanner.SkipTriviaEx(text, node.Expression().End(), &options)
if pos > node.Expression().End() {
prevRune, _ := utf8.DecodeLastRuneInString(text[:pos])
if stringutil.IsLineBreak(prevRune) {
relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon)
}
}
}
c.invocationError(node.Expression(), apparentType, SignatureKindCall, relatedInformation)
Expand Down
7 changes: 7 additions & 0 deletions tsc/internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,13 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
}
canConsumeStar = options.InJSDoc
continue
case 0x2028, 0x2029:
pos += size
if options.StopAfterLineBreak {
return pos
}
canConsumeStar = options.InJSDoc
continue
case '\t', '\v', '\f', ' ':
pos++
continue
Expand Down
31 changes: 31 additions & 0 deletions tsc/internal/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,34 @@ func TestRegExpLiteralReplacementCharacterInClassRange(t *testing.T) {
})
}
}

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

tests := []struct {
name string
text string
pos int
expected int
}{
{name: "LF", text: "x\n(1)", pos: 1, expected: 2},
{name: "CRLF", text: "x\r\n(1)", pos: 1, expected: 3},
{name: "CR", text: "x\r(1)", pos: 1, expected: 2},
{name: "LineSeparator U+2028", text: "x\u2028(1)", pos: 1, expected: 4},
{name: "ParagraphSeparator U+2029", text: "x\u2029(1)", pos: 1, expected: 4},
{name: "U+2028 with trailing spaces", text: "x\u2028 (1)", pos: 1, expected: 4},
{name: "U+2029 with trailing spaces", text: "x\u2029 (1)", pos: 1, expected: 4},
{name: "spaces then U+2028", text: "x \u2028(1)", pos: 1, expected: 7},
{name: "single line comment before U+2028", text: "x // note\u2028(1)", pos: 1, expected: 12},
{name: "spaces only", text: "x (1)", pos: 1, expected: 4},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
options := SkipTriviaOptions{StopAfterLineBreak: true}
actual := SkipTriviaEx(test.text, test.pos, &options)
assert.Equal(t, actual, test.expected)
})
}
}
2 changes: 1 addition & 1 deletion tsc/internal/testutil/tsbaseline/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
lineDelimiter = regexp.MustCompile("\r?\n")
lineDelimiter = regexp.MustCompile("\r\n|[\n\r\u2028\u2029]")
nonWhitespace = regexp.MustCompile(`\S`)
tsExtension = regexp.MustCompile(`\.tsx?$`)
testPathCharacters = regexp.MustCompile(`[\^<>:"|?*%]`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
betterErrorForAccidentalCallNonstandardLineBreak.ts(2,26): error TS2349: This expression is not callable.
Type 'Number' has no call signatures.
betterErrorForAccidentalCallNonstandardLineBreak.ts(6,26): error TS2349: This expression is not callable.
Type 'Number' has no call signatures.
betterErrorForAccidentalCallNonstandardLineBreak.ts(11,1): error TS2349: This expression is not callable.
Type 'String' has no call signatures.
betterErrorForAccidentalCallNonstandardLineBreak.ts(14,1): error TS2349: This expression is not callable.
Type 'String' has no call signatures.
betterErrorForAccidentalCallNonstandardLineBreak.ts(17,1): error TS2349: This expression is not callable.
Type 'String' has no call signatures.
betterErrorForAccidentalCallNonstandardLineBreak.ts(20,1): error TS2349: This expression is not callable.
Type 'String' has no call signatures.


==== betterErrorForAccidentalCallNonstandardLineBreak.ts (6 errors) ====
// Repro from #65433 with U+2028 (Line Separator)
const a = (x: number) => x
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:2:26: Are you missing a semicolon?
(1);

// With U+2029 (Paragraph Separator)
const b = (x: number) => x
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:6:26: Are you missing a semicolon?
(1);

declare function foo(): string;

foo()
~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:11:1: Are you missing a semicolon?
(1 as number).toString();

foo()
~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:14:1: Are you missing a semicolon?
(1 as number).toString();

foo()
~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:17:1: Are you missing a semicolon?
(1 + 2).toString();

foo() // comment
~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS2734 betterErrorForAccidentalCallNonstandardLineBreak.ts:20:1: Are you missing a semicolon?
(1 as number).toString();

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

//// [betterErrorForAccidentalCallNonstandardLineBreak.ts]
// Repro from #65433 with U+2028 (Line Separator)
const a = (x: number) => x
(1);

// With U+2029 (Paragraph Separator)
const b = (x: number) => x
(1);

declare function foo(): string;

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

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

foo()
 (1 + 2).toString();

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


//// [betterErrorForAccidentalCallNonstandardLineBreak.js]
"use strict";
// Repro from #65433 with U+2028 (Line Separator)
const a = (x) => x(1);
// With U+2029 (Paragraph Separator)
const b = (x) => x(1);
foo()(1).toString();
foo()(1).toString();
foo()(1 + 2).toString();
foo() // comment
(1).toString();
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [tests/cases/compiler/betterErrorForAccidentalCallNonstandardLineBreak.ts] ////

=== betterErrorForAccidentalCallNonstandardLineBreak.ts ===
// Repro from #65433 with U+2028 (Line Separator)
const a = (x: number) => x
>a : Symbol(a, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 1, 5))
>x : Symbol(x, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 1, 11))
>x : Symbol(x, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 1, 11))

(1);

// With U+2029 (Paragraph Separator)
const b = (x: number) => x
>b : Symbol(b, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 5, 5))
>x : Symbol(x, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 5, 11))
>x : Symbol(x, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 5, 11))

(1);

declare function foo(): string;
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 6, 4))

foo()
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 6, 4))

(1 as number).toString();

foo()
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 6, 4))

(1 as number).toString();

foo()
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 6, 4))

(1 + 2).toString();

foo() // comment
>foo : Symbol(foo, Decl(betterErrorForAccidentalCallNonstandardLineBreak.ts, 6, 4))

(1 as number).toString();

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

=== betterErrorForAccidentalCallNonstandardLineBreak.ts ===
// Repro from #65433 with U+2028 (Line Separator)
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

// With U+2029 (Paragraph Separator)
const b = (x: number) => x
>b : (x: number) => any
>(x: number) => x(1) : (x: number) => any
>x : number
>x(1) : any
>x : number

(1);
>1 : 1

declare function foo(): string;
>foo : () => string

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

foo()
>foo() (1 + 2).toString() : any
>foo() (1 + 2).toString : any
>foo() (1 + 2) : any
>foo() : string
>foo : () => string

(1 + 2).toString();
>1 + 2 : number
>1 : 1
>2 : 2
>toString : any

foo() // comment
>foo() // comment(1 as number).toString() : any
>foo() // comment(1 as number).toString : any
>foo() // comment(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,17 @@
// @target: es2015

// Repro from #65433 with U+2028 (Line Separator)
const a = (x: number) => x
(1);

// With U+2029 (Paragraph Separator)
const b = (x: number) => x
(1);

declare function foo(): string;

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

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

foo()
 (1 + 2).toString();

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