t3rn0 / ast-comments

Extension to the built-in ast module. Finds comments in source code and adds them to the parsed tree.
MIT License
31 stars 10 forks source link

Comment to multiple statements in one line #4

Closed t3rn0 closed 1 year ago

t3rn0 commented 2 years ago

Let's say we have code

>>> source = """a = 1; b = 2  # some comment"""
>>> ast.dump(ast.parse(source))
"Module(body=[Assign(targets=[Name(id='a', ctx=Store())], value=Constant(value=1, kind=None), type_comment=None), Assign(targets=[Name(id='b', ctx=Store())], value=Constant(value=2, kind=None), type_comment=None)], type_ignores=[])"
>>>
>>> astcom.dump(astcom.parse(source))
>>> "Module(body=[Assign(targets=[Name(id='a', ctx=Store())], value=Constant(value=1, kind=None), type_comment=None, comments=('some comment',)), Assign(targets=[Name(id='b', ctx=Store())], value=Constant(value=2, kind=None), type_comment=None, comments=())], type_ignores=[])"

Parsed tree has two nodes. Which node do we want to place the comment on? To "a"-part? To "b"-part? To both?

With the current version (0.1.2) the comment goes to "a"-part but I don't think it's the right choice