Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow whitespace before non-null assertion #56384

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@
"category": "Error",
"code": 1288
},
"A non-null assertion may not be preceded by whitespace.": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like

Whitespace and comments are not allowed between the argument and '!' operator in a non-null assertion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also shockingly, we don't use the term "whitespace" anywhere in our messages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-null assertion operator must immediately follow its expression

I like Daniel's too though

"category": "Error",
"code": 1289
},

"'with' statements are not allowed in an async function block.": {
"category": "Error",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6438,8 +6438,12 @@ namespace Parser {

if (!questionDotToken) {
if (token() === SyntaxKind.ExclamationToken && !scanner.hasPrecedingLineBreak()) {
const start = scanner.getTokenFullStart();
nextToken();
expression = finishNode(factory.createNonNullExpression(expression), pos);
if (expression.end - start !== 1) {
parseErrorAt(start, expression.end, Diagnostics.A_non_null_assertion_may_not_be_preceded_by_whitespace);
}
continue;
}
const typeArguments = tryParse(parseTypeArgumentsInExpression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parseNoWhitespaceBeforeExclamation.ts(3,6): error TS1289: A non-null assertion may not be preceded by whitespace.
parseNoWhitespaceBeforeExclamation.ts(4,6): error TS1289: A non-null assertion may not be preceded by whitespace.
parseNoWhitespaceBeforeExclamation.ts(5,6): error TS1289: A non-null assertion may not be preceded by whitespace.


==== parseNoWhitespaceBeforeExclamation.ts (3 errors) ====
declare var value: any;

value !;
~~
!!! error TS1289: A non-null assertion may not be preceded by whitespace.
value !instanceof String;
~~
!!! error TS1289: A non-null assertion may not be preceded by whitespace.
value/* this is a comment */! instanceof String;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1289: A non-null assertion may not be preceded by whitespace.

14 changes: 14 additions & 0 deletions tests/baselines/reference/parseNoWhitespaceBeforeExclamation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////

//// [parseNoWhitespaceBeforeExclamation.ts]
declare var value: any;

value !;
value !instanceof String;
value/* this is a comment */! instanceof String;


//// [parseNoWhitespaceBeforeExclamation.js]
value;
value instanceof String;
value /* this is a comment */ instanceof String;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////

=== parseNoWhitespaceBeforeExclamation.ts ===
declare var value: any;
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))

value !;
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))

value !instanceof String;
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

value/* this is a comment */! instanceof String;
>value : Symbol(value, Decl(parseNoWhitespaceBeforeExclamation.ts, 0, 11))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

22 changes: 22 additions & 0 deletions tests/baselines/reference/parseNoWhitespaceBeforeExclamation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts] ////

=== parseNoWhitespaceBeforeExclamation.ts ===
declare var value: any;
>value : any

value !;
>value ! : any
>value : any

value !instanceof String;
>value !instanceof String : boolean
>value ! : any
>value : any
>String : StringConstructor

value/* this is a comment */! instanceof String;
>value/* this is a comment */! instanceof String : boolean
>value/* this is a comment */! : any
>value : any
>String : StringConstructor

5 changes: 5 additions & 0 deletions tests/cases/compiler/parseNoWhitespaceBeforeExclamation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare var value: any;

value !;
value !instanceof String;
value/* this is a comment */! instanceof String;
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
Loading