Skip to content

Commit f0079d2

Browse files
committed
Disallow whitespace before non-null assertion
1 parent 6e9ac5b commit f0079d2

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@
951951
"category": "Error",
952952
"code": 1288
953953
},
954+
"A non-null assertion may not be preceded by whitespace.": {
955+
"category": "Error",
956+
"code": 1289
957+
},
954958

955959
"'with' statements are not allowed in an async function block.": {
956960
"category": "Error",

src/compiler/parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6438,8 +6438,12 @@ namespace Parser {
64386438

64396439
if (!questionDotToken) {
64406440
if (token() === SyntaxKind.ExclamationToken && !scanner.hasPrecedingLineBreak()) {
6441+
const start = scanner.getTokenFullStart();
64416442
nextToken();
64426443
expression = finishNode(factory.createNonNullExpression(expression), pos);
6444+
if (expression.end - start !== 1) {
6445+
parseErrorAt(start, expression.end, Diagnostics.A_non_null_assertion_may_not_be_preceded_by_whitespace);
6446+
}
64436447
continue;
64446448
}
64456449
const typeArguments = tryParse(parseTypeArgumentsInExpression);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
parseNoWhitespaceBeforeExclamation.ts(3,6): error TS1289: A non-null assertion may not be preceded by whitespace.
2+
parseNoWhitespaceBeforeExclamation.ts(4,6): error TS1289: A non-null assertion may not be preceded by whitespace.
3+
parseNoWhitespaceBeforeExclamation.ts(5,6): error TS1289: A non-null assertion may not be preceded by whitespace.
4+
5+
6+
==== parseNoWhitespaceBeforeExclamation.ts (3 errors) ====
7+
declare var value: any;
8+
9+
value !;
10+
~~
11+
!!! error TS1289: A non-null assertion may not be preceded by whitespace.
12+
value !instanceof String;
13+
~~
14+
!!! error TS1289: A non-null assertion may not be preceded by whitespace.
15+
value/* this is a comment */! instanceof String;
16+
~~~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS1289: A non-null assertion may not be preceded by whitespace.
18+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////
2+
3+
//// [parseNoWhitespaceBeforeExclamation.ts]
4+
declare var value: any;
5+
6+
value !;
7+
value !instanceof String;
8+
value/* this is a comment */! instanceof String;
9+
10+
11+
//// [parseNoWhitespaceBeforeExclamation.js]
12+
value;
13+
value instanceof String;
14+
value /* this is a comment */ instanceof String;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////
2+
3+
=== parseNoWhitespaceBeforeExclamation.ts ===
4+
declare var value: any;
5+
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
6+
7+
value !;
8+
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
9+
10+
value !instanceof String;
11+
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
12+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
13+
14+
value/* this is a comment */! instanceof String;
15+
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
16+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
17+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////
2+
3+
=== parseNoWhitespaceBeforeExclamation.ts ===
4+
declare var value: any;
5+
>value : any
6+
7+
value !;
8+
>value ! : any
9+
>value : any
10+
11+
value !instanceof String;
12+
>value !instanceof String : boolean
13+
>value ! : any
14+
>value : any
15+
>String : StringConstructor
16+
17+
value/* this is a comment */! instanceof String;
18+
>value/* this is a comment */! instanceof String : boolean
19+
>value/* this is a comment */! : any
20+
>value : any
21+
>String : StringConstructor
22+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare var value: any;
2+
3+
value !;
4+
value !instanceof String;
5+
value/* this is a comment */! instanceof String;

0 commit comments

Comments
 (0)