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 helper functions for common ways of filtering nodes #1137

Conversation

zaicruvoir1rominet
Copy link
Contributor

@zaicruvoir1rominet zaicruvoir1rominet commented Apr 29, 2024

Summary

Currently, the way to print out the CST is using libcst.tool.dump.
There are many useful inner functions in libcst.tool which could be used by 3rd parties to provide their own custom representation of LibCST graphs.

The goal of this PR is to expose some of these functions: functions which help to filter all the non-relevant data from the CST.

Changes

Following functions have been created in libcst.helpers:

  • get_node_fields
  • is_whitespace_node_field
  • is_syntax_node_field
  • get_field_default_value
  • is_default_node_field
  • filter_node_fields

And libcst.tool.dump now uses filter_node_fields.

Test Plan

Parse a simple CST, then check CST-nodes have their fields properly filtered when using filter functions.

…so that other libraries may provide their own custom representation of LibCST graphs.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 29, 2024
Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

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

High level making these public makes sense, but:

  1. it's worth extracting _is_syntax and _is_whitespace into their own public functions (with docstrings and tests)
  2. move all of these under libcst.helpers, please

libcst/tool.py Outdated Show resolved Hide resolved
libcst/tool.py Outdated Show resolved Hide resolved
@zaicruvoir1rominet
Copy link
Contributor Author

zaicruvoir1rominet commented May 1, 2024

Following functions - with docstrings and tests - have been created in libcst.helpers:

  • get_node_fields
  • is_whitespace_node_field
  • is_syntax_node_field
  • get_field_default_value
  • is_default_node_field
  • filter_node_fields

All above functions are now part the Sphinx Helpers doc.

libcst.tool.dump now uses filter_node_fields.

@zaicruvoir1rominet
Copy link
Contributor Author

zaicruvoir1rominet commented May 1, 2024

(Please check the tests ; I was not really sure how to properly cover use cases, so do tell if work needs to be done there)

Copy link

codecov bot commented May 12, 2024

Codecov Report

Attention: Patch coverage is 97.98658% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 91.15%. Comparing base (9f54920) to head (fd3646b).
Report is 9 commits behind head on main.

Files Patch % Lines
libcst/helpers/node_fields.py 93.75% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1137      +/-   ##
==========================================
+ Coverage   91.12%   91.15%   +0.02%     
==========================================
  Files         256      258       +2     
  Lines       26567    26689     +122     
==========================================
+ Hits        24210    24328     +118     
- Misses       2357     2361       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zsol
Copy link
Member

zsol commented May 12, 2024

Gotta fix the type errors and this is good to go.

Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

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

Thank you!

@zsol zsol changed the title Make nodes fields filtering process public Add helper functions for common ways of filtering nodes May 13, 2024
@zsol zsol merged commit efc53af into Instagram:main May 13, 2024
22 checks passed
@zaicruvoir1rominet zaicruvoir1rominet deleted the feature/make_nodes_filtering_process_public branch May 13, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants