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

Unexected 'Invalid left-hand side in for-in' #1920

Closed
pelchats opened this issue Mar 16, 2018 · 1 comment
Closed

Unexected 'Invalid left-hand side in for-in' #1920

pelchats opened this issue Mar 16, 2018 · 1 comment

Comments

@pelchats
Copy link

Esprima throws an error when parsing seemingly correct code with a for-in loop. The error seems to be sensitive to the code surrounding it.

Steps to reproduce

esprima.parse(`
  if (a) {
    for(f(); false;) {}
  } else
    for(x in y) {
      g()
    }
`)

Note that this is a ~minimal reproduction. The actual code that I was trying to parse is http://pagead2.googlesyndication.com/pagead/js/r20180312/r20170110/reactive_library.js

Expected output

I think the AST should be parsed successfully. I don't see anything wrong with the above code. In particular, changing the f() call in the first for loop somehow makes the error disappear. Likewise adding curly braces after else also makes the error disappear.

Chrome executes the following code just fine:

  var a = false;
  function f() {
    console.log('f called');
  }
  function g() {
    console.log('g called');
  }
  var c = 0;
  var e = 1;
  var y = [1];
  if (a) {
    for(f(); false;) {}
  } else
    for(x in y) {
      g()
  }
> g called

Actual output

Error: Line 5: Invalid left-hand side in for-in
@JosephPecoraro
Copy link
Contributor

As I understand it, this is because the parseForDeclaration() parses the init portion as an inheritCoverGrammar. And if the init portion causes isAssignmentTarget to be false, then it will affect later productions.

If the init is followed by in or of then the inherit works out as desired. But if it falls through to the classic for loop parse for (<init>;<condition>;<update>), then we really would have wanted to parse the init as an isolated expression. Note it should probably also parse the test and update portions as isolated.

The issue manifests inside the else because the else clause is immediately parsed as a sub-task of the if statement (this.alternate). The improper state coming out of the parseForDeclaration starts parsing the else clause statement with this.context.isAssignmentTarget being false instead of true.

I see two possible fixes:

  1. Fix the unexpected context state manipulation inside of parseForDeclaration. (Seems like the most correct solution, but requires a comment).
  2. Always adjust the state for else inside of something like parseIfClause. (Easy, but masks the issue.)

ashkulz pushed a commit to qtwebkit/webkit-mirror that referenced this issue Aug 21, 2019
…g of resources

https://bugs.webkit.org/show_bug.cgi?id=200935

Reviewed by Timothy Hatcher.

Address a few Esprima issues:

    Issue #1991 - Failure to parse template literal with destructuring assignment expression
    jquery/esprima#1991

    Issue #1920 - Invalid Left Hand Side in for-in
    jquery/esprima#1920

* UserInterface/External/Esprima/esprima.js:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@248923 268f45cc-cd09-0410-ab3c-d52691b4dbfc
JosephPecoraro added a commit to JosephPecoraro/esprima that referenced this issue Sep 11, 2019
JosephPecoraro added a commit to JosephPecoraro/esprima that referenced this issue Sep 11, 2019
JosephPecoraro added a commit to JosephPecoraro/esprima that referenced this issue Sep 11, 2019
@ariya ariya closed this as completed in a971d87 Oct 22, 2019
ariya pushed a commit that referenced this issue May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants