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 switch support in NodePath#getCompletionRecords #13030

Merged
merged 11 commits into from Apr 2, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Mar 19, 2021

Q                       A
Fixed Issues? Re-enabled broken tests in do-expressions
Patch: Bug Fix? Y
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This PR reimplements switch support in NodePath#getCompletionRecords first introduced in #10070. It aims for unifying how we handle the AST structures inside switch and versa. A new tag (normal or break) is attributed to every completion record so we can correctly handle break completion in switch case clauses. The new implementation now can also handle the broken but disabled tests in do-expressions.

This PR also addressed how we handle declaration in completion records. I am aware that @bakkot is proposing to forbid do expression blocks which end in declaration, which means we could further throw an early error. But given that NodePath#getCompletionRecords is also used in popular NodePath#replaceWith, it will be great if we can correctly handle such scenarios.

Known issues:

This PR does not support LabeledStatements, neither does the main branch, though I think we could support labeled statement based on current approach.

I recommend reviewing this PR commit by commit since the real diff is not that readable.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Do Expressions labels Mar 19, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Mar 19, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 19, 2021

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 232af53:

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

@nicolo-ribaudo nicolo-ribaudo self-requested a review March 26, 2021 00:53
) {
markCompletionsAs(lastNormalCompletions, BREAK_COMPLETION);
completions = completions.concat(lastNormalCompletions);
statementCompletions.forEach(c => c.path.remove());
Copy link
Member

Choose a reason for hiding this comment

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

I know that this was already the previous behavior, but we should add a comment explaining that this function mutates the AST. I didn't expect it, given the function name.

shouldPopulateBreak: false,
inCaseClause: false,
});
const paths = [];
Copy link
Member

Choose a reason for hiding this comment

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

this can just be records.map(r => r.path) right?


type Completion = {
path: NodePath;
type: 0 | 1;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be:

Suggested change
type: 0 | 1;
type: NORMAL_COMPLETION | BREAK_COMPLETION;

?

Also, any reason for not using an enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wish we could use const enum here, one of few TypeScript features Babel does not support. 😔

Copy link
Member

Choose a reason for hiding this comment

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

We could make it

Suggested change
type: 0 | 1;
type: typeof NORMAL_COMPLETION | typeof BREAK_COMPLETION;

?

(I don't have a preference)

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 typeof operator does not account for the value of the const binding, so typeof NORMAL_COMPLETION is number.

Copy link
Member

Choose a reason for hiding this comment

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

Ok technically we could add as const to the declarations, but let's keep 0 | 1 at this point 😂

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.

Fantastic comments inside replaceBreakStatementInBreakCompletion!

@JLHwung JLHwung force-pushed the refactor-get-completion-records branch from 01f68ad to 232af53 Compare April 2, 2021 13:27
@nicolo-ribaudo
Copy link
Member

@JLHwung Is this ready to be merged?

@JLHwung JLHwung merged commit b577e44 into babel:main Apr 2, 2021
@JLHwung JLHwung deleted the refactor-get-completion-records branch April 2, 2021 17: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 Jul 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2021
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 PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Do Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants