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

refactor: simplify toAssignable routine #11032

Merged
merged 5 commits into from Jan 20, 2020

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jan 20, 2020

Q                       A
Tests Pass? Yes
License MIT

This PR is a series of parser refactoring and is supposed to review commit-by-commit. It focuses on simplifying the toAssignable routine

RemoveisBinding parameter

The first commit removes the isBinding parameter. This parameter is used in toAssignableList in typescript plugin and meant to check if the TSAsExpression or TSTypeAssertion is allowed to casted as type parameters.

However it suffices to check state.maybeInArrowParameters since toAssignableList is called with true only in

setArrowFunctionParameters(

Other than that, this parameter is also used to skip MemberExpression check, which was copied from acorn.

case "MemberExpression":

Apparently the check here is now redundant because the default branch is noop.

Remove contextDescription parameter

The contextDescription was introduced in babel/babylon#123 in order to provide better error message for the default case of toAssignable
https://github.com/babel/babylon/pull/123/files#diff-3d279f04a39b1aaba84ac7b20b0f2625R62

However the error message was moved to checkLVal later so the context description is now redundant. It prevails in the calls of toAssignable so it is difficult for linters to detect this variable is unused.

Remove unnecessary nullish check

The nullish check not only contradict with the type annotation of toAssignable


where node is indeed a Node, but also can be proved unnecessary here. Among the possible syntax nodes where toAssignable will be called, only array elements can be nullish. However the nullish check of array elements has been implemented in

therefore the nullish check is unecessary.

Simplify on the ParenthesizedExpression

toAssignable will unwrap parenthesized expression again after it is unwrapped in the dedicate ParenthesizedExpression check.

const parenthesized = unwrapParenthesizedExpression(node);

The second unwrap is unnecessary. Also added a test case for the SyntaxError: Invalid left-hand side in parenthesized expression (1:1) error.

@JLHwung JLHwung added PR: Internal 🏠 A type of pull request used for our changelog categories pkg: parser labels Jan 20, 2020
@nicolo-ribaudo nicolo-ribaudo merged commit 43b23e0 into babel:master Jan 20, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the refactor-to-assignable branch January 20, 2020 20:04
@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 Apr 21, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 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: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants