Instagram / Instagram/LibCST

FlattenSentinel Type in Visitor Leave_X Calls Does Not Handle Empty Lines

Open
#803 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

For example, if you wanted to replace an if statement with a new block of code, including the prior empty lines, you can't return that type, because `EmptyLine` does not inherit from `BaseStatement`.

```
@mark_no_op
def leave_If(
self, original_node: "If", updated_node: "If"
) -> Union["BaseStatement", FlattenSentinel["BaseStatement"], RemovalSentinel]:
return updated_node
```

Given some code like:

```
# Comment
if some_statement:
print("Hello")
```

In order to correctly collapse the conditional, you must return a FlattenSentinel that includes both the EmptyLine that contains the comment (which is owned by `leading_lines` of the If node), as well as the node for the print statement. Today the return type doesn't allow this.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.