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

Parser: Should not treat assignment to group as pattern #8796

Open
pvdz opened this Issue Oct 2, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@pvdz

pvdz commented Oct 2, 2018

Bug Report

Current Behavior

When assigning to a group that is an assignment the parser currently creates a Pattern node for the group. It should consider this a syntax error.

Input Code

(x = x) = x

Expected behavior/code

Syntax error.

Babel Configuration (.babelrc, package.json, cli command)

        babelAst = babelParse(code, {
          sourceType: goalMode === GOAL_MODULE ? 'module' : 'script',
          strictMode: startInStrictMode ? true : false,
          plugins: ['estree'],
        }).program;

Environment

  • Babel version(s): v7.0.0 (regular)
  • Node/npm version: node 10
  • OS: linux
  • Monorepo only on sundays
  • How you are using Babel: code

Possible Solution

Whatever the mechanism for checking assignable is in Babel should do so properly across parens.

Additional context/Screenshots

Current:

{ type: 'Program',
  body:
   [ { type: 'ExpressionStatement',
       expression:
        { type: 'AssignmentExpression',
          left:
           { type: 'AssignmentPattern',
             left: { type: 'Identifier', name: 'x' },
             right: { type: 'Identifier', name: 'x' } },
          operator: '=',
          right: { type: 'Identifier', name: 'x' } } } ] }

Expected:

https://tc39.github.io/ecma262/#sec-assignment-operators-static-semantics-assignmenttargettype

This rule says that an assignment itself is not a valid assignment.

Note that assignments are right associative, so x = x = x is really x = (x = x), which is why this is still valid while still considering assignments invalid assignment targets.

https://tc39.github.io/ecma262/#sec-semantics-static-semantics-assignmenttargettype

This rule says that the AssignmentTargetType state of a (non-arrow) group is the AssignmentTargetType of its contents.

In other words; it's an invalid AssignmentTargetType and it should throw a syntax error.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Oct 2, 2018

Collaborator

Hey @pvdz! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Oct 2, 2018

Hey @pvdz! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Oct 2, 2018

Member

Is that code actually valid?

Member

nicolo-ribaudo commented Oct 2, 2018

Is that code actually valid?

@pvdz

This comment has been minimized.

Show comment
Hide comment
@pvdz

pvdz Oct 2, 2018

Now that you mention it, no.

I double checked it and according to the spec x = x is not a valid AssignmentTargetType, which means you can't assign to it when wrapped in parens;

https://tc39.github.io/ecma262/#sec-assignment-operators-static-semantics-assignmenttargettype

LeftHandSideExpression=AssignmentExpression is invalid assignment target

https://tc39.github.io/ecma262/#sec-semantics-static-semantics-assignmenttargettype

PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
Return AssignmentTargetType of expr.

I'll update the ticket accordingly. And the bug in my parser which uncovered this problem in the first place. It was checking the rhs of an assignment for this state where it shouldn't even consider it at all.

Thanks for keeping me on my toes :)

pvdz commented Oct 2, 2018

Now that you mention it, no.

I double checked it and according to the spec x = x is not a valid AssignmentTargetType, which means you can't assign to it when wrapped in parens;

https://tc39.github.io/ecma262/#sec-assignment-operators-static-semantics-assignmenttargettype

LeftHandSideExpression=AssignmentExpression is invalid assignment target

https://tc39.github.io/ecma262/#sec-semantics-static-semantics-assignmenttargettype

PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
Return AssignmentTargetType of expr.

I'll update the ticket accordingly. And the bug in my parser which uncovered this problem in the first place. It was checking the rhs of an assignment for this state where it shouldn't even consider it at all.

Thanks for keeping me on my toes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment