Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,14 @@
"category": "Error",
"code": 1544
},
"Confusing combination of '!' token and 'instanceof' keyword like 'a !instanceof b' is disallowed, as it might be misinterpreted as '!(a instanceof b)'": {
"category": "Error",
"code": 1545
},
"Confusing combination of '!' token and 'in' keyword like 'a !in b' is disallowed, as it might be misinterpreted as '!(a in b)'": {
"category": "Error",
"code": 1546
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,18 @@ export function createScanner(
}
return pos += 2, token = SyntaxKind.ExclamationEqualsToken;
}
const hasLeadingWhitespace = isWhiteSpaceLike(charCodeChecked(pos - 1));
pos++;
const nextIdentifierToken = lookAhead(() => {
tokenValue = scanIdentifierParts();
return getIdentifierToken();
});
if (hasLeadingWhitespace && nextIdentifierToken === SyntaxKind.InKeyword) {
error(Diagnostics.Confusing_combination_of_token_and_in_keyword_like_a_in_b_is_disallowed_as_it_might_be_misinterpreted_as_a_in_b);
}
if (hasLeadingWhitespace && nextIdentifierToken === SyntaxKind.InstanceOfKeyword) {
error(Diagnostics.Confusing_combination_of_token_and_instanceof_keyword_like_a_instanceof_b_is_disallowed_as_it_might_be_misinterpreted_as_a_instanceof_b);
}
return token = SyntaxKind.ExclamationToken;
case CharacterCodes.doubleQuote:
case CharacterCodes.singleQuote:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
confusingExclamationBeforeInAndInstanceOf.ts(8,4): error TS1545: Confusing combination of '!' token and 'instanceof' keyword like 'a !instanceof b' is disallowed, as it might be misinterpreted as '!(a instanceof b)'
confusingExclamationBeforeInAndInstanceOf.ts(9,4): error TS1545: Confusing combination of '!' token and 'instanceof' keyword like 'a !instanceof b' is disallowed, as it might be misinterpreted as '!(a instanceof b)'
confusingExclamationBeforeInAndInstanceOf.ts(15,4): error TS1546: Confusing combination of '!' token and 'in' keyword like 'a !in b' is disallowed, as it might be misinterpreted as '!(a in b)'
confusingExclamationBeforeInAndInstanceOf.ts(16,4): error TS1546: Confusing combination of '!' token and 'in' keyword like 'a !in b' is disallowed, as it might be misinterpreted as '!(a in b)'


==== confusingExclamationBeforeInAndInstanceOf.ts (4 errors) ====
declare let a: any;
declare let b: any;

a! instanceof b; // should work
a!instanceof b; // should work
a/**/!instanceof b; // should work
a!/**/instanceof b; // should work
a !instanceof b; // should error

!!! error TS1545: Confusing combination of '!' token and 'instanceof' keyword like 'a !instanceof b' is disallowed, as it might be misinterpreted as '!(a instanceof b)'
a !instanceof b; // should error

!!! error TS1545: Confusing combination of '!' token and 'instanceof' keyword like 'a !instanceof b' is disallowed, as it might be misinterpreted as '!(a instanceof b)'

a! in b; // should work
a!in b; // should work
a/**/!in b; // should work
a!/**/in b; // should work
a !in b; // should error

!!! error TS1546: Confusing combination of '!' token and 'in' keyword like 'a !in b' is disallowed, as it might be misinterpreted as '!(a in b)'
a !in b; // should error

!!! error TS1546: Confusing combination of '!' token and 'in' keyword like 'a !in b' is disallowed, as it might be misinterpreted as '!(a in b)'

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/confusingExclamationBeforeInAndInstanceOf.ts] ////

//// [confusingExclamationBeforeInAndInstanceOf.ts]
declare let a: any;
declare let b: any;

a! instanceof b; // should work
a!instanceof b; // should work
a/**/!instanceof b; // should work
a!/**/instanceof b; // should work
a !instanceof b; // should error
a !instanceof b; // should error

a! in b; // should work
a!in b; // should work
a/**/!in b; // should work
a!/**/in b; // should work
a !in b; // should error
a !in b; // should error


//// [confusingExclamationBeforeInAndInstanceOf.js]
a instanceof b; // should work
a instanceof b; // should work
a /**/ instanceof b; // should work
a /**/ instanceof b; // should work
a instanceof b; // should error
a instanceof b; // should error
a in b; // should work
a in b; // should work
a /**/ in b; // should work
a /**/ in b; // should work
a in b; // should error
a in b; // should error
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/compiler/confusingExclamationBeforeInAndInstanceOf.ts] ////

=== confusingExclamationBeforeInAndInstanceOf.ts ===
declare let a: any;
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))

declare let b: any;
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a! instanceof b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a!instanceof b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a/**/!instanceof b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a!/**/instanceof b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a !instanceof b; // should error
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a !instanceof b; // should error
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a! in b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a!in b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a/**/!in b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a!/**/in b; // should work
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a !in b; // should error
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

a !in b; // should error
>a : Symbol(a, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 0, 11))
>b : Symbol(b, Decl(confusingExclamationBeforeInAndInstanceOf.ts, 1, 11))

Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//// [tests/cases/compiler/confusingExclamationBeforeInAndInstanceOf.ts] ////

=== confusingExclamationBeforeInAndInstanceOf.ts ===
declare let a: any;
>a : any
> : ^^^

declare let b: any;
>b : any
> : ^^^

a! instanceof b; // should work
>a! instanceof b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a!instanceof b; // should work
>a!instanceof b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a/**/!instanceof b; // should work
>a/**/!instanceof b : boolean
> : ^^^^^^^
>a/**/! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a!/**/instanceof b; // should work
>a!/**/instanceof b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a !instanceof b; // should error
>a !instanceof b : boolean
> : ^^^^^^^
>a ! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a !instanceof b; // should error
>a !instanceof b : boolean
> : ^^^^^^^
>a ! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a! in b; // should work
>a! in b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a!in b; // should work
>a!in b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a/**/!in b; // should work
>a/**/!in b : boolean
> : ^^^^^^^
>a/**/! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a!/**/in b; // should work
>a!/**/in b : boolean
> : ^^^^^^^
>a! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a !in b; // should error
>a !in b : boolean
> : ^^^^^^^
>a ! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

a !in b; // should error
>a !in b : boolean
> : ^^^^^^^
>a ! : any
> : ^^^
>a : any
> : ^^^
>b : any
> : ^^^

16 changes: 16 additions & 0 deletions tests/cases/compiler/confusingExclamationBeforeInAndInstanceOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare let a: any;
declare let b: any;

a! instanceof b; // should work
a!instanceof b; // should work
a/**/!instanceof b; // should work
a!/**/instanceof b; // should work
a !instanceof b; // should error
a !instanceof b; // should error

a! in b; // should work
a!in b; // should work
a/**/!in b; // should work
a!/**/in b; // should work
a !in b; // should error
a !in b; // should error