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

CodeRange provided by PositionProvider doesn't include decorators #972

Open
notEvil opened this issue Jul 12, 2023 · 2 comments
Open

CodeRange provided by PositionProvider doesn't include decorators #972

notEvil opened this issue Jul 12, 2023 · 2 comments
Labels
bug Something isn't working codemod Bundled codemods, visitors, metadata providers

Comments

@notEvil
Copy link

notEvil commented Jul 12, 2023

Hi,

since decorators are sub nodes of FunctionDef and ClassDef I expected the code range to cover them. Is this intended?

import libcst
import libcst.metadata as l_metadata


metadata_wrapper = libcst.MetadataWrapper(libcst.parse_module("""
@decorator
def function():
    pass

@decorator
class Class:
    pass
"""))

positions = metadata_wrapper.resolve(l_metadata.PositionProvider)

function_node, class_node = metadata_wrapper.module.body
print("function :", positions[function_node])
print("decorator:", positions[function_node.decorators[0]])
print()
print("class    :", positions[class_node])
print("decorator:", positions[class_node.decorators[0]])

# function : CodeRange(start=CodePosition(line=3, column=0), end=CodePosition(line=4, column=8))
# decorator: CodeRange(start=CodePosition(line=2, column=0), end=CodePosition(line=2, column=10))
#
# class    : CodeRange(start=CodePosition(line=7, column=0), end=CodePosition(line=8, column=8))
# decorator: CodeRange(start=CodePosition(line=6, column=0), end=CodePosition(line=6, column=10))
@zsol
Copy link
Member

zsol commented Jul 12, 2023 via email

@zsol zsol added bug Something isn't working codemod Bundled codemods, visitors, metadata providers labels Jul 12, 2023
@notEvil
Copy link
Author

notEvil commented Jul 25, 2023

I found another inconsistency related to code ranges: Tuple doesn't include parens

import libcst
import libcst.metadata as l_metadata


metadata_wrapper = libcst.MetadataWrapper(libcst.parse_module("""
(None,)
[None]
{None}
{None: None}
"""))

code_ranges = metadata_wrapper.resolve(l_metadata.PositionProvider)

tuple_node, list_node, set_node, dict_node = (
    cst_node.body[0].value for cst_node in metadata_wrapper.module.body
)  # SimpleStatementLine, Expr
print("tuple:", code_ranges[tuple_node])
print("list :", code_ranges[list_node])
print("set  :", code_ranges[set_node])
print("dict :", code_ranges[dict_node])

# tuple: CodeRange(start=CodePosition(line=2, column=1), end=CodePosition(line=2, column=6))
# list : CodeRange(start=CodePosition(line=3, column=0), end=CodePosition(line=3, column=6))
# set  : CodeRange(start=CodePosition(line=4, column=0), end=CodePosition(line=4, column=6))
# dict : CodeRange(start=CodePosition(line=5, column=0), end=CodePosition(line=5, column=12))

Should I open a separate issue for this?

edit:
It seems no node includes parens, since they are usually not significant. And I remember #458, so you can mark this as off-topic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working codemod Bundled codemods, visitors, metadata providers
Projects
None yet
Development

No branches or pull requests

2 participants