From 5c19b44f351ab0f6fc2fd2823e2d5096fb3be671 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 13 Oct 2023 17:13:42 -0400 Subject: [PATCH] Add trailing zero between dot and exponential --- .../test/fixtures/ruff/expression/number.py | 7 +++++ .../src/expression/number.rs | 11 ++++--- .../format@expression__number.py.snap | 30 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py create mode 100644 crates/ruff_python_formatter/tests/snapshots/format@expression__number.py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py new file mode 100644 index 00000000000000..afed916b61aa53 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py @@ -0,0 +1,7 @@ +.1 +1. +1E+1 +1E-1 +1.E+1 +1.0E+1 +1.1E+1 diff --git a/crates/ruff_python_formatter/src/expression/number.rs b/crates/ruff_python_formatter/src/expression/number.rs index 130640f48ff1a1..b26ba950614c4e 100644 --- a/crates/ruff_python_formatter/src/expression/number.rs +++ b/crates/ruff_python_formatter/src/expression/number.rs @@ -142,7 +142,7 @@ fn normalize_floating_number(input: &str) -> Cow { let mut chars = input.char_indices(); - let fraction_ends_with_dot = if let Some((index, '.')) = chars.next() { + let mut prev_char_is_dot = if let Some((index, '.')) = chars.next() { // Add a leading `0` if `input` starts with `.`. output.push('0'); output.push('.'); @@ -155,8 +155,8 @@ fn normalize_floating_number(input: &str) -> Cow { loop { match chars.next() { Some((index, c @ ('e' | 'E'))) => { - if fraction_ends_with_dot { - // Add `0` if fraction part ends with `.`. + if prev_char_is_dot { + // Add `0` if the `e` immediately follows a `.` (e.g., `1.e1`). output.push_str(&input[last_index..index]); output.push('0'); last_index = index; @@ -177,7 +177,10 @@ fn normalize_floating_number(input: &str) -> Cow { break; } - Some(_) => continue, + Some((_index, c)) => { + prev_char_is_dot = c == '.'; + continue; + } None => { if input.ends_with('.') { // Add `0` if fraction part ends with `.`. diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__number.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__number.py.snap new file mode 100644 index 00000000000000..c237b3c297ec17 --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__number.py.snap @@ -0,0 +1,30 @@ +--- +source: crates/ruff_python_formatter/tests/fixtures.rs +input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py +--- +## Input +```py +.1 +1. +1E+1 +1E-1 +1.E+1 +1.0E+1 +1.1E+1 +-1.e49 +``` + +## Output +```py +0.1 +1.0 +1e1 +1e-1 +1.0e1 +1.0e1 +1.1e1 +-1.0e49 +``` + + +