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

Improve transform-destructuring typings #14236

Merged
merged 8 commits into from Mar 7, 2022

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Feb 3, 2022

Q                       A
License MIT

This PR improves typings of plugin-transform-destructuring. I extracted these routines into a new module.

  • DestructuringTransformer
  • convertVariableDeclaration
  • convertAssignmentExpression

They are likely to be reused in the destructuring-private transformer I am currently working on. In this PR they are not exposed so it can ship in patch release.


if (
t.isIdentifier(node) &&
t.isReferenced(node, ancestors[ancestors.length - 1]) &&
Copy link
Contributor Author

@JLHwung JLHwung Feb 3, 2022

Choose a reason for hiding this comment

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

t.isReferenced(node, ancestors[ancestors.length - 1]) is changed to t.isReferenced(node, ancestors[ancestors.length - 1].node). This is a bug caught by type checker.

@@ -493,7 +43,7 @@ export default declare((api, options) => {

const specifiers = [];

for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) {
for (const name of Object.keys(path.getOuterBindingIdentifiers())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another bug caught by type checker.

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 3, 2022

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

@@ -560,6 +113,7 @@ export default declare((api, options) => {
path.ensureBlock();

const block = node.body;
// @ts-expect-error: ensureBlock ensures that node.body is a BlockStatement
Copy link
Member

Choose a reason for hiding this comment

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

Could you check if changing the ensureBlock signature to this helps?

function ensureBlock<T extends  t.Loop | t.WithStatement | t.Function | t.LabeledStatement | t.CatchClause>(
  this: NodePath<T>,
): assert this is NodePath<T & { node: t.BlockStatement }>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately we can't add type assertion without breaking change because:

  1. ensureBlock returns a t.Node while assertion predicates requires that the function returns void.
  2. this-type guards can only be added to a class method, though we can move it back to index or put it in a separate file and extends an interface (type with assertions) instead.

Comment on lines 11 to 16
for (const elem of pattern.elements) {
if (t.isRestElement(elem)) {
return true;
}
}
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: these could be "compacted" to pattern.elements.some(elem => t.isRestElement(elem))

@JLHwung JLHwung force-pushed the improve-destructuring-typings branch 2 times, most recently from 527552b to 57535de Compare February 11, 2022 21:29
@JLHwung JLHwung force-pushed the improve-destructuring-typings branch 2 times, most recently from c309266 to 446b07e Compare February 23, 2022 19:02
@JLHwung JLHwung force-pushed the improve-destructuring-typings branch from 446b07e to 85697cf Compare February 28, 2022 19:43
@existentialism existentialism added the PR: Internal 🏠 A type of pull request used for our changelog categories label Mar 7, 2022
@JLHwung JLHwung merged commit 4f80f5c into babel:main Mar 7, 2022
@JLHwung JLHwung deleted the improve-destructuring-typings branch March 7, 2022 21:14
@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 Jun 7, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 7, 2022
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: 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

4 participants