From 721efedd9bfbb86c2c987835cd58f9eeec1955b1 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 21 Jul 2023 12:45:23 +0200 Subject: [PATCH] Fix formatting lambda with empty arguments **Summary** Fix implemented in https://github.com/astral-sh/RustPython-Parser/pull/35: Previously, empty lambda arguments (e.g. `lambda: 1`) would get the range of the entire expression, which leads to incorrect comment placement. Now empty lambda arguments get an empty range between the `lambda` and the `:` tokens. **Test Plan** Added a regression test. 149 instances of unstable formatting remaining. ``` $ cargo run --bin ruff_dev --release -- format-dev --stability-check --error-file formatter-ecosystem-errors.txt --multi-project target/checkouts > formatter-ecosystem-progress.txt $ rg "Unstable formatting" target/formatter-ecosystem-errors.txt | wc -l 149 ``` --- Cargo.toml | 10 +++++----- .../test/fixtures/ruff/expression/lambda.py | 7 ++++++- .../snapshots/format@expression__lambda.py.snap | 15 ++++++++++++++- fuzz/Cargo.toml | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d06200abf37656..4651e08aa8656b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,11 +52,11 @@ wsl = { version = "0.1.0" } # v1.0.1 libcst = { git = "https://github.com/Instagram/LibCST.git", rev = "3cacca1a1029f05707e50703b49fe3dd860aa839", default-features = false } -ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" } -rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["num-bigint"]} -rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false, features = ["num-bigint"] } -rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false } -rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["full-lexer", "num-bigint"] } +ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" } +rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["num-bigint"]} +rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc", default-features = false, features = ["num-bigint"] } +rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc", default-features = false } +rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["full-lexer", "num-bigint"] } [profile.release] lto = "fat" diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py index 70005694568dc9..15a59fe11c9020 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py @@ -60,4 +60,9 @@ a = ( lambda # Dangling : 1 -) \ No newline at end of file +) + +# Regression test: lambda empty arguments ranges were too long, leading to unstable +# formatting +(lambda:(# +),) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap index 9e9c2d59f67512..59326cf7191895 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap @@ -66,7 +66,13 @@ z) # Trailing a = ( lambda # Dangling : 1 -)``` +) + +# Regression test: lambda empty arguments ranges were too long, leading to unstable +# formatting +(lambda:(# +),) +``` ## Output ```py @@ -130,6 +136,13 @@ lambda x: lambda y: lambda z: ( a = ( lambda: 1 # Dangling ) + +# Regression test: lambda empty arguments ranges were too long, leading to unstable +# formatting +( + lambda: ( # + ), +) ``` diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index d2922b09490352..58e72aa25d09fb 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -24,7 +24,7 @@ ruff_python_ast = { path = "../crates/ruff_python_ast" } ruff_python_formatter = { path = "../crates/ruff_python_formatter" } similar = { version = "2.2.1" } -rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["full-lexer", "num-bigint"] } +rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["full-lexer", "num-bigint"] } # Prevent this from interfering with workspaces [workspace]