unimport icon indicating copy to clipboard operation
unimport copied to clipboard

[BUG] - Remove comment and white space lines

Open hakancelikdev opened this issue 5 years ago • 5 comments
trafficstars

Code

# 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

Expected situation

 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 situation

 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

hakancelikdev avatar Sep 12 '20 09:09 hakancelikdev

I did some code debugging, I ended with a bug on LibCST.RemoveFromParent() function!

from typing import Union

import libcst as cst


class RemoveUnusedImportTransformer(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.
            return cst.RemoveFromParent()  # Here we have a bug!
        return updated_node


def refactor_string(source: str) -> str:
    transformer = RemoveUnusedImportTransformer()
    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)

Output:

# Python imports
import os

from typing import Union
from .pgen2 import driver

from .pgen2.grammar import Grammar

os, Union, driver, Grammar

hadialqattan avatar Sep 14 '20 16:09 hadialqattan

If that's the case, I'm gonna open an issue on the LibCST repo.

hadialqattan avatar Sep 14 '20 16:09 hadialqattan

If that's the case, I'm gonna open an issue on the LibCST repo.

Yes I think the issue need to open.

hakancelikdev avatar Sep 14 '20 17:09 hakancelikdev

Please take a look at Instagram/LibCST#390

hadialqattan avatar Sep 14 '20 19:09 hadialqattan

I think we should implement a method that preserves node comment/empty-line from removal!

hadialqattan avatar Sep 14 '20 19:09 hadialqattan