Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 26da08f

Browse files
committed
Add first BlankLinesVisitor test
1 parent b753d7c commit 26da08f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

rewrite/rewrite/python/format/auto_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Optional, cast, TypeVar
22

3+
from .blank_lines import BlankLinesVisitor
34
from .normalize_format import NormalizeFormatVisitor
4-
from .. import BlankLinesVisitor, BlankLinesStyle
5-
from ..style import SpacesStyle, IntelliJ
5+
from ..style import BlankLinesStyle, SpacesStyle, IntelliJ
66
from ..visitor import PythonVisitor
77
from ... import Recipe, Tree, Cursor
88
from ...java import JavaSourceFile, MethodDeclaration, J, Space

rewrite/rewrite/python/format/blank_lines.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import Optional, TypeVar
44

55
from rewrite import Tree, P, Cursor
6-
from rewrite.java import J
7-
from rewrite.python import PythonVisitor, BlankLinesStyle
6+
from rewrite.java import J, Space, Statement
7+
from rewrite.python import PythonVisitor, BlankLinesStyle, CompilationUnit
88
from rewrite.visitor import T
99

1010
J2 = TypeVar('J2', bound=J)
@@ -16,6 +16,15 @@ def __init__(self, style: BlankLinesStyle, stop_after: Tree = None):
1616
self._stop_after = stop_after
1717
self._stop = False
1818

19+
def visit_compilation_unit(self, compilation_unit: CompilationUnit, p: P) -> J:
20+
if not compilation_unit.prefix.comments:
21+
compilation_unit = compilation_unit.with_prefix(Space.EMPTY)
22+
return super().visit_compilation_unit(compilation_unit, p)
23+
24+
def visit_statement(self, statement: Statement, p: P) -> J:
25+
statement = statement.with_prefix(Space.EMPTY)
26+
return super().visit_statement(statement, p)
27+
1928
def post_visit(self, tree: T, p: P) -> Optional[T]:
2029
if self._stop_after and tree == self._stop_after:
2130
self._stop = True
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from rewrite.java import Space
2+
from rewrite.python import IntelliJ
3+
from rewrite.python.format import BlankLinesVisitor
4+
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor
5+
6+
7+
def test_remove_leading_module_blank_lines():
8+
rewrite_run(
9+
# language=python
10+
python(
11+
"""
12+
13+
14+
print('foo')
15+
""",
16+
"""print('foo')
17+
"""
18+
),
19+
spec=RecipeSpec()
20+
.with_recipe(from_visitor(BlankLinesVisitor(IntelliJ.blank_lines())))
21+
)

0 commit comments

Comments
 (0)