Skip to content

Parser refactoring#11871

Merged
JLHwung merged 28 commits intobabel:mainfrom
JLHwung:parser-refactoring
Aug 1, 2020
Merged

Parser refactoring#11871
JLHwung merged 28 commits intobabel:mainfrom
JLHwung:parser-refactoring

Conversation

@JLHwung
Copy link
Copy Markdown
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
Copy Markdown

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 force-pushed the parser-refactoring branch from 0621f12 to 5dd4f7c Compare July 23, 2020 00:27
@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
Copy Markdown
Collaborator

babel-bot commented Jul 23, 2020

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

@JLHwung JLHwung force-pushed the parser-refactoring branch from 5dd4f7c to a4578f4 Compare July 23, 2020 01:42
},
"computed": false,
"kind": "set",
"variance": null,
Copy link
Copy Markdown
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.

prop.static = false;
classBody.body.push(this.parseClassProperty(prop));
return true;
} else if (containsEsc) {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@JLHwung JLHwung force-pushed the parser-refactoring branch from 206786e to 1d693d8 Compare July 29, 2020 22:35
Copy link
Copy Markdown
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
Copy Markdown
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.

5 participants