Skip to content

Refactor switch support in NodePath#getCompletionRecords#13030

Merged
JLHwung merged 11 commits intobabel:mainfrom
JLHwung:refactor-get-completion-records
Apr 2, 2021
Merged

Refactor switch support in NodePath#getCompletionRecords#13030
JLHwung merged 11 commits intobabel:mainfrom
JLHwung:refactor-get-completion-records

Conversation

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

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
Copy Markdown
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
Copy Markdown
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?

Comment thread packages/babel-traverse/src/path/family.ts Outdated

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

@existentialism existentialism Apr 2, 2021

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

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.

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

Comment thread packages/babel-traverse/src/path/family.ts Outdated
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.

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
Copy Markdown
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.

5 participants