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

Correctly check reserved word for PropertyDefinition: IdentifierReference #11862

Merged
merged 3 commits into from Jul 29, 2020

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jul 21, 2020

Q                       A
Fixed Issues? Fixes #10411
Patch: Bug Fix? Yes
Tests Added + Pass? Yes
License MIT

For object shorthand property, e.g. ({ arguments }) is allowed because

PropertyDefinition -> IdentifierReference -> Identifier -> IdentifierName but not ReservedWord

and arguments and eval is not ReservedWord

For destructuring patterns, e.g. ({ arguments = 1 }) = {} is allowed (in sloppy mode) because

PropertyDefinition -> CoverInitializedName -> IdentifierReference -> Identifier -> IdentifierName but not ReservedWord

If the fourth argument of this.checkReservedWord is true, it should be applied on BindingIdentifier, where eval and arguments are forbidden. Therefore the fix here is to align the spec and pass false instead.

This PR fixes a downstream issue: angular/angular-cli#17439

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Jul 21, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 21, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bdce7c3:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 21, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/26313/

@nicolo-ribaudo
Copy link
Member

For destructuring patterns, e.g. ({ arguments = 1 }) = {} is allowed because

I don't think so: the destructuring pattern must match the "AssignmentPattern" production, and:

12.15.7.1 Static Semantics: Early Errors

AssignmentProperty : IdentifierReference Initializer[opt]

  • It is a Syntax Error if AssignmentTargetType of IdentifierReference is not simple.

12.1.3 Static Semantics: AssignmentTargetType

IdentifierReference : Identifier

  1. If this IdentifierReference is contained in strict mode code and StringValue of Identifier is "eval" or "arguments", return invalid.
  2. Return simple.

@JLHwung
Copy link
Contributor Author

JLHwung commented Jul 22, 2020

If this IdentifierReference is contained in strict mode code and StringValue of Identifier is "eval" or "arguments", return invalid.

As for destructuring, we will check the BindingIdentifier ReservedWord in checkLVal later:

this.state.strict &&
// "Global" reserved words have already been checked by parseIdentifier,
// unless they have been found in the id or parameters of a strict-mode
// function in a sloppy context.
(strictModeChanged
? isStrictBindReservedWord(expr.name, this.inModule)
: isStrictBindOnlyReservedWord(expr.name))

Thus the following test case still throws.

"use strict";
const { arguments } = foo();

Since we can not differentiate between CoverInitializedName and AssignmentProperty until we see an = or =>, I think the change here is okay.

@JLHwung JLHwung merged commit 01d4625 into babel:main Jul 29, 2020
@JLHwung JLHwung deleted the fix-10411 branch July 29, 2020 23:40
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 29, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[parser] arguments and eval as object shorthand should be valid in strict mode
5 participants