Skip to content

Disallow whitespace before non-null assertion #56384

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

Closed
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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,10 @@
"category": "Error",
"code": 1537
},
"A non-null assertion must be preceded by an identifier.": {
Copy link
Member

Choose a reason for hiding this comment

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

identifier? Isn't that misleading?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess a[b]!. bleh

"category": "Error",
"code": 1538
},

"The types of '{0}' are incompatible between these types.": {
"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 @@ -6451,8 +6451,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_must_be_preceded_by_an_identifier);
}
continue;
}
const typeArguments = tryParse(parseTypeArgumentsInExpression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
parseNoWhitespaceBeforeExclamation.ts(3,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(4,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(5,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(6,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(7,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(8,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(9,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(10,6): error TS1538: A non-null assertion must be preceded by an identifier.
parseNoWhitespaceBeforeExclamation.ts(11,6): error TS1538: A non-null assertion must be preceded by an identifier.


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

value !;
~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value !instanceof String;
~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value ! instanceof String
~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value/* this is a comment */!instanceof String;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value/* this is a comment */! instanceof String;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value !in String;
~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value ! in String
~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value/* this is a comment */!in String;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.
value/* this is a comment */! in String;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1538: A non-null assertion must be preceded by an identifier.

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

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

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


//// [parseNoWhitespaceBeforeExclamation.js]
value;
value instanceof String;
value instanceof String;
value /* this is a comment */ instanceof String;
value /* this is a comment */ instanceof String;
value in String;
value in String;
value /* this is a comment */ in String;
value /* this is a comment */ in String;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [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 ! 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, --, --))

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, --, --))

value !in 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 ! in 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 */!in 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 */! in 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, --, --))

93 changes: 93 additions & 0 deletions tests/baselines/reference/parseNoWhitespaceBeforeExclamation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//// [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 ! 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
> : ^^^^^^^^^^^^^^^^^

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
> : ^^^^^^^^^^^^^^^^^

value !in String;
>value !in String : boolean
> : ^^^^^^^
>value ! : any
> : ^^^
>value : any
> : ^^^
>String : StringConstructor
> : ^^^^^^^^^^^^^^^^^

value ! in String
>value ! in String : boolean
> : ^^^^^^^
>value ! : any
> : ^^^
>value : any
> : ^^^
>String : StringConstructor
> : ^^^^^^^^^^^^^^^^^

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

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

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

value !;
value !instanceof String;
value ! instanceof String
value/* this is a comment */!instanceof String;
value/* this is a comment */! instanceof String;
value !in String;
value ! in String
value/* this is a comment */!in String;
value/* this is a comment */! in String;
10 changes: 5 additions & 5 deletions tests/cases/fourslash/formattingNonNullAssertionOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

format.document();
goTo.marker("1");
verify.currentLineContentIs("'bar'!;");
verify.currentLineContentIs("'bar' !;");
goTo.marker("2");
verify.currentLineContentIs("('bar')!;");
verify.currentLineContentIs("('bar') !;");
goTo.marker("3");
verify.currentLineContentIs("'bar'[1]!;");
verify.currentLineContentIs("'bar'[1] !;");
goTo.marker("4");
verify.currentLineContentIs("var bar = 'bar'.foo!;");
verify.currentLineContentIs("var bar = 'bar'.foo !;");
goTo.marker("5");
verify.currentLineContentIs("var foo = bar!;");
verify.currentLineContentIs("var foo = bar !;");