Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions tsc/internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ func (s *Scanner) scanASCIIWhile(pred func(byte) bool) {
s.pos += i
}

// ECMAScript WhiteSpace excludes NEXT LINE and ZERO WIDTH SPACE.
func isSourceWhiteSpaceSingleLine(ch rune) bool {
return ch != 0x0085 && ch != 0x200B && stringutil.IsWhiteSpaceSingleLine(ch)
}

func (s *Scanner) Scan() ast.Kind {
s.fullStartPos = s.pos
s.tokenFlags = ast.TokenFlagsNone
Expand All @@ -543,7 +548,7 @@ func (s *Scanner) Scan() ast.Kind {
}
for {
ch, size := s.charAndSize()
if !stringutil.IsWhiteSpaceSingleLine(ch) {
if !isSourceWhiteSpaceSingleLine(ch) {
break
}
s.pos += size
Expand Down Expand Up @@ -989,18 +994,16 @@ func (s *Scanner) Scan() ast.Kind {
s.token = ast.KindNonTextFileMarkerTrivia
break
}
if stringutil.IsWhiteSpaceSingleLine(ch) {
if isSourceWhiteSpaceSingleLine(ch) {
s.pos += size

// If we get here and it's not 0x0085 (nextLine), then we're handling non-ASCII whitespace.
// Handle skipTrivia like we do in the space case above.
if ch == 0x0085 || s.skipTrivia {
if s.skipTrivia {
continue
}

for {
ch, size = s.charAndSize()
if !stringutil.IsWhiteSpaceSingleLine(ch) {
if !isSourceWhiteSpaceSingleLine(ch) {
break
}
s.pos += size
Expand Down Expand Up @@ -2441,7 +2444,7 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
continue
}
default:
if ch > rune(maxAsciiCharacter) && stringutil.IsWhiteSpaceLike(ch) {
if ch > rune(maxAsciiCharacter) && (isSourceWhiteSpaceSingleLine(ch) || stringutil.IsLineBreak(ch)) {
pos += size
continue
}
Expand Down Expand Up @@ -2980,7 +2983,7 @@ func iterateCommentRanges(f *ast.NodeFactory, text string, pos int, trailing boo
}
break scan
default:
if ch > unicode.MaxASCII && stringutil.IsWhiteSpaceLike(ch) {
if ch > unicode.MaxASCII && (isSourceWhiteSpaceSingleLine(ch) || stringutil.IsLineBreak(ch)) {
if hasPendingCommentRange && stringutil.IsLineBreak(ch) {
pendingHasTrailingNewLine = true
}
Expand Down
45 changes: 45 additions & 0 deletions tsc/internal/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ func TestScanStringPreservesLoneSurrogates(t *testing.T) {
"🦀")
}

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

for name, separator := range map[string]string{"U+0085": "\u0085", "U+200B": "\u200B"} {
t.Run(name, func(t *testing.T) {
t.Parallel()
s := NewScanner()
s.SetSkipTrivia(false)
s.SetText("var " + separator + "x")

var errors int
s.SetOnError(func(message *diagnostics.Message, start, length int, args ...any) {
errors++
assert.Equal(t, message.Code(), diagnostics.Invalid_character.Code())
assert.Equal(t, start, len("var "))
assert.Equal(t, length, len(separator))
})

assert.Equal(t, s.Scan(), ast.KindVarKeyword)
assert.Equal(t, s.Scan(), ast.KindWhitespaceTrivia)
assert.Equal(t, s.Scan(), ast.KindUnknown)
assert.Equal(t, errors, 1)
assert.Equal(t, s.Scan(), ast.KindIdentifier)
assert.Equal(t, SkipTrivia(" "+separator+"x", 0), 1)
})
}
}

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

s := NewScanner()
s.SetText("var \t\v\f\u00a0\u200a\ufeffx")
assert.Equal(t, s.Scan(), ast.KindVarKeyword)
assert.Equal(t, s.Scan(), ast.KindIdentifier)
assert.Assert(t, !s.HasPrecedingLineBreak())

for _, lineTerminator := range []string{"\r", "\n", "\r\n", "\u2028", "\u2029"} {
s.SetText("var" + lineTerminator + "x")
assert.Equal(t, s.Scan(), ast.KindVarKeyword)
assert.Equal(t, s.Scan(), ast.KindIdentifier)
assert.Assert(t, s.HasPrecedingLineBreak(), "%q", lineTerminator)
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fileWithNextLine2.ts(3,8): error TS1127: Invalid character.
fileWithNextLine2.ts(3,9): error TS1134: Variable declaration expected.


==== fileWithNextLine2.ts (2 errors) ====
// Note: there is a nextline (0x85) char between the = and the 0.
// It should be reported as an invalid character.
var v =…0;
~
!!! error TS1127: Invalid character.
~
!!! error TS1134: Variable declaration expected.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

//// [fileWithNextLine2.ts]
// Note: there is a nextline (0x85) char between the = and the 0.
// it should be treated like a space
// It should be reported as an invalid character.
var v =…0;

//// [fileWithNextLine2.js]
"use strict";
// Note: there is a nextline (0x85) char between the = and the 0.
// it should be treated like a space
var v = 0;
// It should be reported as an invalid character.
var v = ;
0;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=== fileWithNextLine2.ts ===
// Note: there is a nextline (0x85) char between the = and the 0.
// it should be treated like a space
// It should be reported as an invalid character.
var v =…0;
>v : Symbol(v, Decl(fileWithNextLine2.ts, 2, 3))

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

=== fileWithNextLine2.ts ===
// Note: there is a nextline (0x85) char between the = and the 0.
// it should be treated like a space
// It should be reported as an invalid character.
var v =…0;
>v : number
>v : any
> : any
>0 : 0

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
fileWithNextLine3.ts(3,1): error TS1108: A 'return' statement can only be used within a function body.
fileWithNextLine3.ts(2,7): error TS1127: Invalid character.


==== fileWithNextLine3.ts (1 errors) ====
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
// The nextline (0x85) after return should be reported as an invalid character.
return…0;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
~
!!! error TS1127: Invalid character.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//// [tests/cases/compiler/fileWithNextLine3.ts] ////

//// [fileWithNextLine3.ts]
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
// The nextline (0x85) after return should be reported as an invalid character.
return…0;

//// [fileWithNextLine3.js]
"use strict";
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
return 0;
// The nextline (0x85) after return should be reported as an invalid character.
return ;
0;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

=== fileWithNextLine3.ts ===

// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
// The nextline (0x85) after return should be reported as an invalid character.
return…0;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//// [tests/cases/compiler/fileWithNextLine3.ts] ////

=== fileWithNextLine3.ts ===
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
// The nextline (0x85) after return should be reported as an invalid character.
return…0;
> : any
>0 : 0

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
separators.js(1,4): error TS1127: Invalid character.
separators.js(2,4): error TS1127: Invalid character.
separators.ts(1,4): error TS1127: Invalid character.
separators.ts(2,4): error TS1127: Invalid character.


==== separators.ts (2 errors) ====
var…nelTs; // U+0085
~
!!! error TS1127: Invalid character.
var​zwspTs; // U+200B
~
!!! error TS1127: Invalid character.
var hairSpaceTs; // U+200A
varbomTs; // U+FEFF
// U+2028 ends this comment
var lsTs = 1;
lsTs;

==== separators.js (2 errors) ====
var…nelJs; // U+0085
~
!!! error TS1127: Invalid character.
var​zwspJs; // U+200B
~
!!! error TS1127: Invalid character.
var nbspJs; // U+00A0
// U+2029 ends this comment
var psJs = 1;
psJs;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sourceFile:sourceMap-LineBreaks.ts
5 >Emitted(3, 36) Source(2, 36) + SourceIndex(0)
6 >Emitted(3, 37) Source(2, 37) + SourceIndex(0)
---
>>>var endsWithNextLine = 1;
>>>var endsWithNbspChar = 1;
1 >
2 >^^^^
3 > ^^^^^^^^^^^^^^^^
Expand All @@ -60,7 +60,7 @@ sourceFile:sourceMap-LineBreaks.ts
7 > ^->
1 > 
 >
2 >var
3 > endsWithNextLine
3 > endsWithNbspChar
4 > =
5 > 1
6 > ;
Expand All @@ -79,7 +79,7 @@ sourceFile:sourceMap-LineBreaks.ts
5 > ^
6 > ^
7 > ^^^^^^^^^^^^^^^->
1->…
1-> 
2 >var
3 > endsWithLineFeed
4 > =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var endsWithlineSeparator = 10;
var endsWithParagraphSeparator = 10;
>endsWithParagraphSeparator : Symbol(endsWithParagraphSeparator, Decl(sourceMap-LineBreaks.ts, 1, 3))

var endsWithNextLine = 1;…var endsWithLineFeed = 1;
>endsWithNextLine : Symbol(endsWithNextLine, Decl(sourceMap-LineBreaks.ts, 2, 3))
var endsWithNbspChar = 1; var endsWithLineFeed = 1;
>endsWithNbspChar : Symbol(endsWithNbspChar, Decl(sourceMap-LineBreaks.ts, 2, 3))
>endsWithLineFeed : Symbol(endsWithLineFeed, Decl(sourceMap-LineBreaks.ts, 2, 29))

var endsWithCarriageReturnLineFeed = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var endsWithParagraphSeparator = 10;
>endsWithParagraphSeparator : number
>10 : 10

var endsWithNextLine = 1;…var endsWithLineFeed = 1;
>endsWithNextLine : number
var endsWithNbspChar = 1; var endsWithLineFeed = 1;
>endsWithNbspChar : number
>1 : 1
>endsWithLineFeed : number
>1 : 1
Expand Down
2 changes: 1 addition & 1 deletion tsc/testdata/tests/cases/compiler/fileWithNextLine2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @target: es2015
// Note: there is a nextline (0x85) char between the = and the 0.
// it should be treated like a space
// It should be reported as an invalid character.
var v =…0;
3 changes: 1 addition & 2 deletions tsc/testdata/tests/cases/compiler/fileWithNextLine3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @target: es2015
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
// The nextline (0x85) after return should be reported as an invalid character.
return…0;
18 changes: 18 additions & 0 deletions tsc/testdata/tests/cases/compiler/invalidUnicodeWhitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @checkJs: true
// @noEmit: true
// @noTypesAndSymbols: true

// @filename: separators.ts
var…nelTs; // U+0085
var​zwspTs; // U+200B
var hairSpaceTs; // U+200A
varbomTs; // U+FEFF
// U+2028 ends this comment
var lsTs = 1;
lsTs;

// @filename: separators.js
var…nelJs; // U+0085
var​zwspJs; // U+200B
var nbspJs; // U+00A0
// U+2029 ends this comment
var psJs = 1;
psJs;
2 changes: 1 addition & 1 deletion tsc/testdata/tests/cases/compiler/sourceMap-LineBreaks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @target: ES5, ES2015
// @sourcemap: true
var endsWithlineSeparator = 10; 
var endsWithParagraphSeparator = 10; 
var endsWithNextLine = 1;…var endsWithLineFeed = 1;
var endsWithlineSeparator = 10; 
var endsWithParagraphSeparator = 10; 
var endsWithNbspChar = 1; var endsWithLineFeed = 1;
var endsWithCarriageReturnLineFeed = 1;
var endsWithCarriageReturn = 1;
var endsWithLineFeedCarriageReturn = 1;
Expand Down