From 1765f8e5191b42aad4c7b72b89bef1de05a4eed2 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Thu, 22 Jun 2023 18:58:09 +0200 Subject: [PATCH] do not add newline to all strings (#31) cc/ @AucaCoyan this PR moves the `add_newline_at_end_of_file` outside of `format_inner`. the responsibility is not those of `format_single_file`, `format_string` does not add a newline to all strings anymore. tests have been fixed :+1: one thing i'm struggling with is the following test ```rust #[test] fn remove_additional_lines() { let input = "let one = 1\n\n\n"; let expected = "let one = 1\n"; run_test(input, expected); } ``` which does not pass without the `\n` in `expected` :thinking: **any idea?** :confused: --- src/formatting.rs | 3 +-- src/lib.rs | 25 +++++++++---------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/formatting.rs b/src/formatting.rs index 85fb7af..bf096f6 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -104,7 +104,6 @@ pub(crate) fn format_inner(contents: &[u8], _config: &Config) -> Vec { start = span.end + 1; } - out = add_newline_at_end_of_file(out); out } @@ -116,7 +115,7 @@ fn insert_newline(mut bytes: Vec) -> Vec { } /// make sure there is a newline at the end of a buffer -fn add_newline_at_end_of_file(out: Vec) -> Vec { +pub(crate) fn add_newline_at_end_of_file(out: Vec) -> Vec { match out.last() { Some(&b'\n') => out, _ => insert_newline(out), diff --git a/src/lib.rs b/src/lib.rs index 88a9256..9c16639 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! //! It does not do anything more than that, which makes it so fast. use config::Config; -use formatting::format_inner; +use formatting::{add_newline_at_end_of_file, format_inner}; use log::{debug, trace}; use std::fs::File; use std::io::Write; @@ -16,7 +16,7 @@ pub fn format_single_file(file: &PathBuf, config: &Config) { let contents = std::fs::read(file) .unwrap_or_else(|_| panic!("something went wrong reading the file {}", file.display())); - let formatted_bytes = format_inner(&contents, config); + let formatted_bytes = add_newline_at_end_of_file(format_inner(&contents, config)); if formatted_bytes == contents { debug!("File is formatted correctly."); @@ -59,21 +59,21 @@ mod test { \"a\": null } ]"; - let expected = "[{\"a\":0},{},{\"a\":null}]\n"; + let expected = "[{\"a\":0},{},{\"a\":null}]"; run_test(input, expected); } #[test] fn echoes_primitive() { let input = "1.35"; - let expected = "1.35\n"; + let expected = input; run_test(input, expected); } #[test] fn handle_escaped_strings() { - let input = " \"hallo\\\"\""; - let expected = "\"hallo\\\"\"\n"; + let input = "\"hallo\\\"\""; + let expected = input; run_test(input, expected); } @@ -103,21 +103,14 @@ def my-func [ ]{ print(param1) } myfunc(one) -# final comment\n"; +# final comment"; run_test(input, expected); } #[test] fn ignore_whitespace_in_string() { let input = "\" hallo \""; - let expected = "\" hallo \"\n"; - run_test(input, expected); - } - - #[test] - fn add_new_line() { - let input = "null"; - let expected = "null\n"; + let expected = input; run_test(input, expected); } @@ -131,7 +124,7 @@ myfunc(one) #[test] fn remove_leading_whitespace() { let input = " 0"; - let expected = "0\n"; + let expected = "0"; run_test(input, expected); } }