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

Example use of refactor / RefactoringNormalizer #174

Closed
vtjeng opened this issue Feb 20, 2021 · 5 comments
Closed

Example use of refactor / RefactoringNormalizer #174

vtjeng opened this issue Feb 20, 2021 · 5 comments

Comments

@vtjeng
Copy link

vtjeng commented Feb 20, 2021

Do you have an example of the refactor function or the RefactoringNormalizer in use? I've tried searching in GitHub for examples (RefactoringNormalizer, parso refactor) but all that seems to come up is copies of your source code that others have included in their repos.

(I've read #67, but figured I should start a new thread with a more accurate title).


My specific use case is that I'd like to delete a particular named function from my code. Here's what I've come up with

class FunctionHidingNormalizer(Normalizer):
    """
    Hides any of the listed functions when `walk` is called on the normalizer.
    """
    def __init__(self, functions_names_to_hide):
        self.function_names_to_hide = function_names_to_hide

    def visit(self, node):
        if node.type == "funcdef" and node.name.value in self.function_names_to_hide:
            return ""
        return super().visit(node)

Calling FunctionHidingNormalizer(function_names).walk(parso.parse(src)) mostly works, but it doesn't seem to appropriately handle functions with decorators.

@davidhalter
Copy link
Owner

Duplicate of #165.

For now please read the code and maybe contribute the documentation. I probably won't do it.

@davidhalter
Copy link
Owner

IMO it's pretty obvious what happens if you read the code: A Dict[NodeOrLeaf, str] that goes through the tree and replaces all nodes in the dict with strings and leaves all other nodes in the shape that they were.

@isidentical
Copy link
Collaborator

Calling FunctionHidingNormalizer(function_names).walk(parso.parse(src)) mostly works, but it doesn't seem to appropriately handle functions with decorators.

The decorated functions use the tree symbol 'decorated', so you also have to handle it (just like how you handle 'funcdef'). For the rest of the symbols, you can check the grammar or use parso.parse() to figure out what kind of CST structure it has.

@vtjeng
Copy link
Author

vtjeng commented Feb 21, 2021

Thanks @isidentical - that worked well enough for me.

@davidhalter, that makes sense. Thanks for pointing me to the duplicate. Just wanted to make sure I wasn't reinventing the wheel. (It looks like, as RefactoringNormalizer is currently written, I would have to write my own normalizer, since it can't apply a function to the NodeOrLeaf, but instead has to have all the information beforehand in the Dict.)

@davidhalter
Copy link
Owner

@vtjeng I don't think so, but maybe I don't quite understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants