mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-17 13:57:25 +00:00
[ruff] improve handling of intermixed comments inside from-imports (#20561)
Resolves a crash when attempting to format code like: ``` from x import (a as # whatever b) ``` Reworks the way comments are associated with nodes when parsing modules, so that all possible comment positions can be retained and reproduced during formatting. Overall follows Black's formatting style for multi-line import statements. Fixes issue #19138
This commit is contained in:
parent
23ebfe7777
commit
8fb29eafb8
4 changed files with 269 additions and 6 deletions
47
crates/ruff_python_formatter/resources/test/fixtures/black/cases/import_comments.py
vendored
Normal file
47
crates/ruff_python_formatter/resources/test/fixtures/black/cases/import_comments.py
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
# ensure trailing comments are preserved
|
||||
import x # comment
|
||||
from x import a # comment
|
||||
from x import a, b # comment
|
||||
from x import a as b # comment
|
||||
from x import a as b, b as c # comment
|
||||
|
||||
from x import (
|
||||
a, # comment
|
||||
)
|
||||
from x import (
|
||||
a, # comment
|
||||
b,
|
||||
)
|
||||
|
||||
# ensure comma is added
|
||||
from x import (
|
||||
a # comment
|
||||
)
|
||||
|
||||
# follow black style by merging cases without own-line comments
|
||||
from x import (
|
||||
a # alpha
|
||||
, # beta
|
||||
b,
|
||||
)
|
||||
|
||||
# ensure intermixed comments are all preserved
|
||||
from x import ( # one
|
||||
# two
|
||||
a # three
|
||||
# four
|
||||
, # five
|
||||
# six
|
||||
) # seven
|
||||
|
||||
from x import ( # alpha
|
||||
# bravo
|
||||
a # charlie
|
||||
# delta
|
||||
as # echo
|
||||
# foxtrot
|
||||
b # golf
|
||||
# hotel
|
||||
, # india
|
||||
# juliet
|
||||
) # kilo
|
46
crates/ruff_python_formatter/resources/test/fixtures/black/cases/import_comments.py.expect
vendored
Normal file
46
crates/ruff_python_formatter/resources/test/fixtures/black/cases/import_comments.py.expect
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
# ensure trailing comments are preserved
|
||||
import x # comment
|
||||
from x import a # comment
|
||||
from x import a, b # comment
|
||||
from x import a as b # comment
|
||||
from x import a as b, b as c # comment
|
||||
|
||||
from x import (
|
||||
a, # comment
|
||||
)
|
||||
from x import (
|
||||
a, # comment
|
||||
b,
|
||||
)
|
||||
|
||||
# ensure comma is added
|
||||
from x import (
|
||||
a, # comment
|
||||
)
|
||||
|
||||
# follow black style by merging cases without own-line comments
|
||||
from x import (
|
||||
a, # alpha # beta
|
||||
b,
|
||||
)
|
||||
|
||||
# ensure intermixed comments are all preserved
|
||||
from x import ( # one
|
||||
# two
|
||||
a # three
|
||||
# four
|
||||
, # five
|
||||
# six
|
||||
) # seven
|
||||
|
||||
from x import ( # alpha
|
||||
# bravo
|
||||
a # charlie
|
||||
# delta
|
||||
as # echo
|
||||
# foxtrot
|
||||
b # golf
|
||||
# hotel
|
||||
, # india
|
||||
# juliet
|
||||
) # kilo
|
Loading…
Add table
Add a link
Reference in a new issue