Skip to content

Disallow instantiation expressions on the right side of instanceof #53323

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

Merged
Merged
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
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34353,6 +34353,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function checkExpressionWithTypeArguments(node: ExpressionWithTypeArguments | TypeQueryNode) {
checkGrammarExpressionWithTypeArguments(node);
forEach(node.typeArguments, checkSourceElement);
if (node.kind === SyntaxKind.ExpressionWithTypeArguments) {
const parent = walkUpParenthesizedExpressions(node.parent);
if (parent.kind === SyntaxKind.BinaryExpression && (parent as BinaryExpression).operatorToken.kind === SyntaxKind.InstanceOfKeyword && isNodeDescendantOf(node, (parent as BinaryExpression).right)) {
error(node, Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_not_be_an_instantiation_expression);
}
}
const exprType = node.kind === SyntaxKind.ExpressionWithTypeArguments ? checkExpression(node.expression) :
isThisIdentifier(node.exprName) ? checkThisExpression(node.exprName) :
checkExpression(node.exprName);
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3615,6 +3615,10 @@
"category": "Error",
"code": 2847
},
"The right-hand side of an 'instanceof' expression must not be an instantiation expression.": {
"category": "Error",
"code": 2848
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
tests/cases/compiler/instanceofOnInstantiationExpression.ts(10,21): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.
tests/cases/compiler/instanceofOnInstantiationExpression.ts(11,22): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.
tests/cases/compiler/instanceofOnInstantiationExpression.ts(12,23): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.


==== tests/cases/compiler/instanceofOnInstantiationExpression.ts (3 errors) ====
declare class Box<T> {
value: T;
}


declare const maybeBox: unknown;

maybeBox instanceof Box; // OK

maybeBox instanceof Box<number>; // error
~~~~~~~~~~~
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.
maybeBox instanceof (Box<number>); // error
~~~~~~~~~~~
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.
maybeBox instanceof ((Box<number>)); // error
~~~~~~~~~~~
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression.

Box<number> instanceof Object; // OK
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 am checking only the right side here, but, should these even be legal? They seem useless in another way.

Copy link
Member

Choose a reason for hiding this comment

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

Is the instantiation expression pointless? Yes. Is it harmful? No. Is it misleading? Probably not really on the LHS, since the only reason I can think of why you'd want one on the LHS is to "satisfy" the RHS, but the RHS forbids them now, soooo...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I don't really care either way. I guess this PR is fine as-is.

(Box<number>) instanceof Object; // OK
((Box<number>)) instanceof Object; // OK

27 changes: 27 additions & 0 deletions tests/baselines/reference/instanceofOnInstantiationExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [instanceofOnInstantiationExpression.ts]
declare class Box<T> {
value: T;
}


declare const maybeBox: unknown;

maybeBox instanceof Box; // OK

maybeBox instanceof Box<number>; // error
maybeBox instanceof (Box<number>); // error
maybeBox instanceof ((Box<number>)); // error

Box<number> instanceof Object; // OK
(Box<number>) instanceof Object; // OK
((Box<number>)) instanceof Object; // OK


//// [instanceofOnInstantiationExpression.js]
maybeBox instanceof Box; // OK
maybeBox instanceof (Box); // error
maybeBox instanceof (Box); // error
maybeBox instanceof ((Box)); // error
(Box) instanceof Object; // OK
(Box) instanceof Object; // OK
((Box)) instanceof Object; // OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/instanceofOnInstantiationExpression.ts ===
declare class Box<T> {
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))
>T : Symbol(T, Decl(instanceofOnInstantiationExpression.ts, 0, 18))

value: T;
>value : Symbol(Box.value, Decl(instanceofOnInstantiationExpression.ts, 0, 22))
>T : Symbol(T, Decl(instanceofOnInstantiationExpression.ts, 0, 18))
}


declare const maybeBox: unknown;
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13))

maybeBox instanceof Box; // OK
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13))
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))

maybeBox instanceof Box<number>; // error
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13))
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))

maybeBox instanceof (Box<number>); // error
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13))
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))

maybeBox instanceof ((Box<number>)); // error
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13))
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))

Box<number> instanceof Object; // OK
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

(Box<number>) instanceof Object; // OK
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

((Box<number>)) instanceof Object; // OK
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
=== tests/cases/compiler/instanceofOnInstantiationExpression.ts ===
declare class Box<T> {
>Box : Box<T>

value: T;
>value : T
}


declare const maybeBox: unknown;
>maybeBox : unknown

maybeBox instanceof Box; // OK
>maybeBox instanceof Box : boolean
>maybeBox : unknown
>Box : typeof Box

maybeBox instanceof Box<number>; // error
>maybeBox instanceof Box<number> : boolean
>maybeBox : unknown
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box

maybeBox instanceof (Box<number>); // error
>maybeBox instanceof (Box<number>) : boolean
>maybeBox : unknown
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; }
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box

maybeBox instanceof ((Box<number>)); // error
>maybeBox instanceof ((Box<number>)) : boolean
>maybeBox : unknown
>((Box<number>)) : { new (): Box<number>; prototype: Box<any>; }
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; }
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box

Box<number> instanceof Object; // OK
>Box<number> instanceof Object : boolean
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box
>Object : ObjectConstructor

(Box<number>) instanceof Object; // OK
>(Box<number>) instanceof Object : boolean
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; }
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box
>Object : ObjectConstructor

((Box<number>)) instanceof Object; // OK
>((Box<number>)) instanceof Object : boolean
>((Box<number>)) : { new (): Box<number>; prototype: Box<any>; }
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; }
>Box<number> : { new (): Box<number>; prototype: Box<any>; }
>Box : typeof Box
>Object : ObjectConstructor

16 changes: 16 additions & 0 deletions tests/cases/compiler/instanceofOnInstantiationExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare class Box<T> {
value: T;
}


declare const maybeBox: unknown;

maybeBox instanceof Box; // OK

maybeBox instanceof Box<number>; // error
maybeBox instanceof (Box<number>); // error
maybeBox instanceof ((Box<number>)); // error

Box<number> instanceof Object; // OK
(Box<number>) instanceof Object; // OK
((Box<number>)) instanceof Object; // OK