You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These codes should remove only from .pgen2 import token import statement, but actually it removes two additional lines!
fromtypingimportUnionimportlibcstascstclassTransformer(cst.CSTTransformer):
defleave_ImportFrom(
self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom
) ->Union[cst.RemovalSentinel, cst.ImportFrom]:
ifupdated_node.names[0].name.value=="token": # Hard coded name to remove.returncst.RemoveFromParent() # Here we have a bug!returnupdated_nodedefrefactor_string(source: str) ->str:
cst_tree=cst.parse_module(source)
returncst_tree.visit(Transformer()).codeif__name__=="__main__":
refactored_code=refactor_string(
"""# Python importsimport osfrom typing import Union# Local importsfrom .pgen2 import tokenfrom .pgen2 import driverfrom .pgen2.grammar import Grammaros, 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
The text was updated successfully, but these errors were encountered:
hadialqattan
changed the title
[Bug] on LibCST.RemoveFromParent() function, removes extra lines!!!
[BUG] LibCST.RemoveFromParent() function removes extra lines!!!
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?
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?
These codes should remove only
from .pgen2 import token
import statement, but actually it removes two additional lines!Expected output:
Actual output
The text was updated successfully, but these errors were encountered: