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

[BUG] LibCST.RemoveFromParent() function removes extra lines!!! #390

Closed
hadialqattan opened this issue Sep 14, 2020 · 2 comments
Closed

Comments

@hadialqattan
Copy link

These codes should remove only from .pgen2 import token import statement, but actually it removes two additional lines!

from typing import Union

import libcst as cst


class Transformer(cst.CSTTransformer):
    def leave_ImportFrom(
        self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom
    ) -> Union[cst.RemovalSentinel, cst.ImportFrom]:
        if updated_node.names[0].name.value == "token":  # Hard coded name to remove.
            return cst.RemoveFromParent()  # Here we have a bug!
        return updated_node


def refactor_string(source: str) -> str:
    cst_tree = cst.parse_module(source)
    return cst_tree.visit(Transformer()).code


if __name__ == "__main__":
    refactored_code = refactor_string(
        """# Python imports
import os

from typing import Union

# Local imports
from .pgen2 import token
from .pgen2 import driver

from .pgen2.grammar import Grammar

os, Union, driver, Grammar"""
    )
    print(refactored_code)

Expected output:

# Python imports
 import os

 from typing import Union

# Local imports
-from .pgen2 import token
 from .pgen2 import driver

 from .pgen2.grammar import Grammar

os, Union, driver, Grammar

Actual output

# Python imports
 import os

 from typing import Union
-
-# Local imports
-from .pgen2 import token
 from .pgen2 import driver

 from .pgen2.grammar import Grammar
os, Union, driver, Grammar
@hadialqattan hadialqattan changed the title [Bug] on LibCST.RemoveFromParent() function, removes extra lines!!! [BUG] LibCST.RemoveFromParent() function removes extra lines!!! Sep 14, 2020
@thatch
Copy link
Contributor

thatch commented Sep 14, 2020

The representation that LibCST uses for comments and whitespace is to attach them to some node. In this case, you wanted to keep the comments, but removed the node that owned them, which is why they went away. I have a similar use case where I reorder nodes, and see the comments move (in a somewhat arbitrary-looking, but at least consistent, way).

Did you expect LibCST to move those to another node for you?

@hadialqattan
Copy link
Author

The representation that LibCST uses for comments and whitespace is to attach them to some node. In this case, you wanted to keep the comments, but removed the node that owned them, which is why they went away. I have a similar use case where I reorder nodes, and see the comments move (in a somewhat arbitrary-looking, but at least consistent, way).

Did you expect LibCST to move those to another node for you?

Clear, thank you!

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

2 participants