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

New error: Literal values in right-hand side of instanceof #2777

Closed
jugglinmike opened this issue Nov 21, 2015 · 0 comments · Fixed by chauncey-garrett/dotfiles#15 · May be fixed by ajesse11x/cjdns#1
Closed

New error: Literal values in right-hand side of instanceof #2777

jugglinmike opened this issue Nov 21, 2015 · 0 comments · Fixed by chauncey-garrett/dotfiles#15 · May be fixed by ajesse11x/cjdns#1

Comments

@jugglinmike
Copy link
Member

According to ES5 11.8.6:

11.8.6 The instanceof operator

The production RelationalExpression: RelationalExpression instanceof ShiftExpression is evaluated as follows:

  1. Let lref be the result of evaluating RelationalExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating ShiftExpression.
  4. Let rval be GetValue(rref).
  5. If Type(rval) is not Object, throw a TypeError exception.
  6. If rval does not have a [[HasInstance]] internal method, throw a TypeError
    exception.
  7. Return the result of calling the [[HasInstance]] internal method of rval
    with argument lval.

We can't completely enforce this statically, but there are some forms which we can reject
outright. The following instanceof applications should trigger an error:

var x;
x instanceof 0;
x instanceof '';
x instanceof null;
x instanceof undefined;
x instanceof {};
x instanceof [];
x instanceof /./;

And it probably wouldn't hurt to warn on the following cases, too:

x instanceof function() {};
x instanceof function MyUnusableFunction() {};
ehjay added a commit to ehjay/jshint that referenced this issue May 18, 2016
This patch adds a new parse error that is used when a literal or function
declaration is found on the right-hand side of the `instanceof` infix operator.
Specifically, it checks for number, string, object, array and regular expression
literals, as well as the null and undefined keywords and function declarations.

The following generate errors:

    x instanceof 0;
    x instanceof '';
    x instanceof null;
    x instanceof undefined;
    x instanceof {};
    x instanceof [];
    x instanceof /./;
    x instanceof function() {};
    x instanceof function MyUnusableFunction() {};

But this is okay:

    /x/ instanceof /x/.constructor;

as per jshintGH-2773

References:

    Closes jshintGH-2777
ehjay pushed a commit to ehjay/jshint that referenced this issue May 24, 2016
This patch adds a new error that is used when a literal is found on the
right-hand side of the `instanceof` infix operator. A warning is used when a
function declaration is found.

Literals generate errors:

    x instanceof 0;
    x instanceof '';
    x instanceof null;
    x instanceof undefined;
    x instanceof {};
    x instanceof [];
    x instanceof /./;
    x instanceof ``;

Function declarations generate warnings:

    x instanceof function() {};
    x instanceof function MyUnusableFunction() {};

But this is okay:

    x instanceof /x/.constructor;
    x instanceof "".constructor;

as per jshintGH-2773

References:

    Closes jshintGH-2777
ehjay pushed a commit to ehjay/jshint that referenced this issue May 29, 2016
This patch adds a new error that is used when a literal is found on the
right-hand side of the `instanceof` infix operator. A warning is used when a
function declaration is found.

Literals generate errors:

    x instanceof 0;
    x instanceof '';
    x instanceof null;
    x instanceof undefined;
    x instanceof {};
    x instanceof [];
    x instanceof /./;
    x instanceof ``;

Function declarations generate warnings:

    x instanceof function() {};
    x instanceof function MyUnusableFunction() {};

But this is okay:

    x instanceof /x/.constructor;
    x instanceof "".constructor;

as per jshintGH-2773

References:

    Closes jshintGH-2777
ehjay pushed a commit to ehjay/jshint that referenced this issue Jun 4, 2016
This patch adds a new error that is used when a literal is found on the
right-hand side of the `instanceof` infix operator. A warning is used when a
function declaration is found.

Literals generate errors:

    x instanceof 0;
    x instanceof '';
    x instanceof null;
    x instanceof undefined;
    x instanceof {};
    x instanceof [];
    x instanceof /./;
    x instanceof ``;

Function declarations generate warnings:

    x instanceof function() {};
    x instanceof function MyUnusableFunction() {};

But this is okay:

    x instanceof /x/.constructor;
    x instanceof "".constructor;

as per jshintGH-2773

References:

    Closes jshintGH-2777
jugglinmike pushed a commit to jugglinmike/jshint that referenced this issue Sep 4, 2016
This patch adds a new error that is used when a literal is found on the
right-hand side of the `instanceof` infix operator. A warning is used when a
function declaration is found.

Literals generate errors:

    x instanceof 0;
    x instanceof '';
    x instanceof null;
    x instanceof undefined;
    x instanceof {};
    x instanceof [];
    x instanceof /./;
    x instanceof ``;

Function declarations generate warnings:

    x instanceof function() {};
    x instanceof function MyUnusableFunction() {};

But this is okay:

    x instanceof /x/.constructor;
    x instanceof "".constructor;

as per jshintGH-2773

References:

    Closes jshintGH-2777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant