Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libcst/_nodes/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,9 @@ def _codegen_impl(self, state: CodegenState) -> None:
state.add_token("if")
self.whitespace_after_if._codegen(state)
guard._codegen(state)
else:
self.whitespace_before_if._codegen(state)
self.whitespace_after_if._codegen(state)

self.whitespace_before_colon._codegen(state)
state.add_token(":")
Expand Down Expand Up @@ -3473,6 +3476,13 @@ def _codegen_impl(self, state: CodegenState) -> None:
state.add_token(" ")
elif isinstance(ws_after, BaseParenthesizableWhitespace):
ws_after._codegen(state)
else:
ws_before = self.whitespace_before_as
if isinstance(ws_before, BaseParenthesizableWhitespace):
ws_before._codegen(state)
ws_after = self.whitespace_after_as
if isinstance(ws_after, BaseParenthesizableWhitespace):
ws_after._codegen(state)
if name is None:
state.add_token("_")
else:
Expand Down
47 changes: 47 additions & 0 deletions libcst/metadata/tests/test_position_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,53 @@ def visit_Pass(self, node: cst.Pass) -> None:
wrapper = MetadataWrapper(parse_module("pass"))
wrapper.visit_batched([ABatchable()])

def test_match_statement_position_metadata(self) -> None:
test = self

class MatchPositionVisitor(CSTVisitor):
METADATA_DEPENDENCIES = (PositionProvider,)

def visit_Match(self, node: cst.Match) -> None:
test.assertEqual(
self.get_metadata(PositionProvider, node),
CodeRange((2, 0), (5, 16)),
)

def visit_MatchCase(self, node: cst.MatchCase) -> None:
if (
isinstance(node.pattern, cst.MatchAs)
and node.pattern.name
and node.pattern.name.value == "b"
):
test.assertEqual(
self.get_metadata(PositionProvider, node),
CodeRange((3, 4), (3, 16)),
)
elif (
isinstance(node.pattern, cst.MatchAs)
and node.pattern.name
and node.pattern.name.value == "c"
):
test.assertEqual(
self.get_metadata(PositionProvider, node),
CodeRange((4, 4), (4, 16)),
)
elif isinstance(node.pattern, cst.MatchAs) and not node.pattern.name:
test.assertEqual(
self.get_metadata(PositionProvider, node),
CodeRange((5, 4), (5, 16)),
)

code = """
match status:
case b: pass
case c: pass
case _: pass
"""

wrapper = MetadataWrapper(parse_module(code))
wrapper.visit(MatchPositionVisitor())


class PositionProvidingCodegenStateTest(UnitTest):
def test_codegen_initial_position(self) -> None:
Expand Down
Loading