diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 950b9a8e30fb8..f65d11c96b463 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -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" @@ -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) diff --git a/tsc/internal/scanner/scanner.go b/tsc/internal/scanner/scanner.go index 7dc5199678d93..508c2c8cc893e 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 0x2028, 0x2029: + 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..cacbb003b7a1e 100644 --- a/tsc/internal/scanner/scanner_test.go +++ b/tsc/internal/scanner/scanner_test.go @@ -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) + }) + } +} diff --git a/tsc/internal/testutil/tsbaseline/util.go b/tsc/internal/testutil/tsbaseline/util.go index bbd6a064de9c6..8e3725170ff0d 100644 --- a/tsc/internal/testutil/tsbaseline/util.go +++ b/tsc/internal/testutil/tsbaseline/util.go @@ -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(`[\^<>:"|?*%]`) diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.errors.txt b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.errors.txt new file mode 100644 index 0000000000000..a8067ca214231 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.errors.txt @@ -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(); + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.js b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.js new file mode 100644 index 0000000000000..dc425320e5152 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.js @@ -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(); diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.symbols b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.symbols new file mode 100644 index 0000000000000..d05a328e5b9ce --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.symbols @@ -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(); + diff --git a/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.types b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.types new file mode 100644 index 0000000000000..cb09210dfbea0 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/betterErrorForAccidentalCallNonstandardLineBreak.types @@ -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 + diff --git a/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCallNonstandardLineBreak.ts b/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCallNonstandardLineBreak.ts new file mode 100644 index 0000000000000..79fd03fd5081f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/betterErrorForAccidentalCallNonstandardLineBreak.ts @@ -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();