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

Add new method to check if node is null or not #13940

Merged
merged 2 commits into from Feb 1, 2022
Merged

Add new method to check if node is null or not #13940

merged 2 commits into from Feb 1, 2022

Conversation

danez
Copy link
Member

@danez danez commented Nov 8, 2021

Q                       A
Fixed Issues?
Patch: Bug Fix? n
Major: Breaking Change? n
Minor: New Feature? y
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? n
License MIT

While using babel traverse in a typescript project I stumbled upon an annoyance that I wasn't able to really fix in my code, besides defining the types for every single path manually.

The code looks like this:

function xyz(path: NodePath): void {
  if (path.isArrayExpression()) {
    path.get('elements').map(elementPath => {
      // elementPath: NodePath<Expression | SpreadElement | null>
      if (elementPath.node === null) return;
      otherFunc(elementPath as NodePath<Expression | SpreadElement>); // Have to define that because null is not possible anymore.
    });
  }
}

With the introduction of the new method it can be simplified to this, because TS now can figure out itself that node is not null anymore:

function xyz(path: NodePath): void {
  if (path.isArrayExpression()) {
    path.get('elements').map(elementPath => {
      // elementPath: NodePath<Expression | SpreadElement | null>
      if (!elementPath.hasNode()) return;
      otherFunc(elementPath);
    });
  }
}

This is especially useful when using babel with typescript, as typescript now knows that node is not null.
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 8, 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 8844447:

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

@JLHwung JLHwung added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Nov 8, 2021
Co-authored-by: Kristoffer K. <merceyz@users.noreply.github.com>
@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Dec 1, 2021

I wonder why the type refinement in your example doesn't work; it seems to work in simpler cases 🤔
https://www.typescriptlang.org/play?ts=4.4.4#code/MYGwhgzhAEByD2ATApgBTAFwBYB4AqAfNAN4BQ0F0AdksgIQBc0eA3KQL6mkYCeADsmb9kAQWgBeEtF4CmAIhFzo7NjMF5hAIQlS18zUpVdkADz7wAThmgAzAK5VgGAJbwq0AObIMCFBoEAFHyYWEy+aCE4VHYgINAAPkICYon+yJoEAJRMEBgWzl

EDIT: Oh ok, it doesn't work if you use path rather than path.node.*.

@nicolo-ribaudo nicolo-ribaudo added this to the v7.17.0 milestone Dec 13, 2021
@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Jan 11, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit b91c3e1 into babel:main Feb 1, 2022
@danez danez deleted the hasNode branch February 2, 2022 11:50
@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 May 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 5, 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 pkg: traverse PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants