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

Parser refactoring #11871

Merged
merged 28 commits into from Aug 1, 2020
Merged

Parser refactoring #11871

merged 28 commits into from Aug 1, 2020

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jul 23, 2020

Q                       A
Fixed Issues? REPL 1, REPL 2
Patch: Bug Fix? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Refactors @babel/parser a bit. Focused on code readability and syntax upstream spec links.
Happens to fix a bug when adding grammar production comments.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 23, 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.

@JLHwung JLHwung added pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jul 23, 2020
@babel-bot
Copy link
Collaborator

babel-bot commented Jul 23, 2020

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

@@ -34,7 +34,6 @@
},
"computed": false,
"kind": "set",
"variance": null,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variance property exists in ClassProperty, and we don't have variance for ObjectMethod. This PR removes variance of fooProp accessor for the following cases:

a={set fooProp(value:number){}}

I think the new behaviour is preferred and it aligns to what we did for non-accessors.

@@ -1281,10 +1297,7 @@ export default class StatementParser extends ExpressionParser {
prop.static = false;
classBody.body.push(this.parseClassProperty(prop));
return true;
} else if (containsEsc) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch is unreachable because parseClassMemberFromModifier is guarded by isContextual call, which must return false when containsEsc.

@JLHwung JLHwung marked this pull request as ready for review July 23, 2020 23:12
) {
// https://tc39.es/ecma262/#prod-AsyncMethod
// https://tc39.es/ecma262/#prod-AsyncGeneratorMethod
if (prop.key.name === "async" && !this.hasPrecedingLineBreak()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing async and set, getter have been unified in parsePropertyDefinition -- so they can share same sanity checks. Thus we don't need passing containsEsc (A tokenizing level states) to parseObjectMethod, instead isAccessor is pass.


if (this.prodParam.hasYield && this.eat(tt.dot)) {
if (this.prodParam.hasYield && this.match(tt.dot)) {
const meta = this.createIdentifier(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid creating an extra meta node when tt.dot is not met, instead we create meta when we have to. The same technique is also applied for parseNewOrNewTarget.

@@ -594,163 +602,205 @@ export default class ExpressionParser extends LValParser {
state: N.ParseSubscriptState,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseSubscript has been broken into smaller pieces. I find it more easier to go through after refactoring: https://github.com/JLHwung/babel/blob/ee4d63ef00e5090f44ffedee718dd18fdc6e21e0/packages/babel-parser/src/parser/expression.js#L597

@JLHwung JLHwung added PR: Internal 🏠 A type of pull request used for our changelog categories PR: Bug Fix 🐛 A type of pull request used for our changelog categories and removed PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jul 23, 2020
@JLHwung
Copy link
Contributor Author

JLHwung commented Jul 23, 2020

I have changed the PR label because the bug fixed here is edging cases. It is more of a refactoring which is very likely invisible to users.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few nits, but overall I find that this improves the readability 👍

startLoc = startLoc || this.state.startLoc;
startPos = startPos || this.state.start;
left = left || this.parseBindingAtom();
startLoc = startLoc ?? this.state.startLoc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe now we can use ??=? 😄

Flow 😞


parseMaybeUnary(refExpressionErrors: ?ExpressionErrors): N.Expression {
// https://tc39.es/ecma262/#prod-UnaryExpression
parseUnary(refExpressionErrors: ?ExpressionErrors): N.Expression {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this was called parseMaybeUnary (like parseMaybeConditional, parseMaybeAssign) because it could return something which is not a unary expression, like a simple identifier.

I know that technically Identifier is a child of UnaryExpression in the spec, but it's just a side effect of how it's specified.

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor!

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@JLHwung JLHwung merged commit a4ebe29 into babel:main Aug 1, 2020
@JLHwung JLHwung deleted the parser-refactoring branch August 1, 2020 00:36
@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 31, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 31, 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

5 participants