From fe9a437b6d7ddc3f78665df1a576236555880c51 Mon Sep 17 00:00:00 2001 From: Andy <35195301+Andy53@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:37:05 -0600 Subject: [PATCH] feat: remove curly braces with fmt (#4529) # Description ## Problem\* Resolves https://github.com/noir-lang/noir/compare/master...Andy53:noir:fmt_remvoes_curly_braces_when_there_is_a_single_import?expand=1 Closes #4480 ## Summary\* Removes curly braces from imports when there is only a single import. `use dep::std::hash::{sha256};` becomes: `use dep::std::hash::sha256; ## Additional Context ## Documentation\* Check one: - [ X] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ X] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: jfecher --- noir_stdlib/src/array.nr | 2 +- tooling/nargo_fmt/src/rewrite/imports.rs | 9 ++++++++- tooling/nargo_fmt/tests/expected/contract.nr | 2 +- tooling/nargo_fmt/tests/expected/import_braces.nr | 1 + tooling/nargo_fmt/tests/input/contract.nr | 2 +- tooling/nargo_fmt/tests/input/import_braces.nr | 1 + 6 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 tooling/nargo_fmt/tests/expected/import_braces.nr create mode 100644 tooling/nargo_fmt/tests/input/import_braces.nr diff --git a/noir_stdlib/src/array.nr b/noir_stdlib/src/array.nr index 3da4b649174..baa4bef50cc 100644 --- a/noir_stdlib/src/array.nr +++ b/noir_stdlib/src/array.nr @@ -1,4 +1,4 @@ -use crate::cmp::{Ord}; +use crate::cmp::Ord; // TODO: Once we fully move to the new SSA pass this module can be removed and replaced // by the methods in the `slice` module diff --git a/tooling/nargo_fmt/src/rewrite/imports.rs b/tooling/nargo_fmt/src/rewrite/imports.rs index 2788f778140..55eb259bcdd 100644 --- a/tooling/nargo_fmt/src/rewrite/imports.rs +++ b/tooling/nargo_fmt/src/rewrite/imports.rs @@ -102,7 +102,14 @@ impl UseTree { let mut iter = self.path.iter().peekable(); while let Some(segment) = iter.next() { - let segment_str = segment.rewrite(visitor, shape); + let mut segment_str = segment.rewrite(visitor, shape); + if segment_str.contains('{') + && !segment_str.contains(',') + && !segment_str.contains("::") + { + let empty = ""; + segment_str = segment_str.replace(['{', '}'], empty); + } result.push_str(&segment_str); if iter.peek().is_some() { diff --git a/tooling/nargo_fmt/tests/expected/contract.nr b/tooling/nargo_fmt/tests/expected/contract.nr index c5b19a686d2..97a6ebd6b77 100644 --- a/tooling/nargo_fmt/tests/expected/contract.nr +++ b/tooling/nargo_fmt/tests/expected/contract.nr @@ -11,7 +11,7 @@ contract Benchmarking { context::Context, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, log::emit_unencrypted_log, state_vars::{Map, PublicMutable, PrivateSet}, types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN}, - types::address::{AztecAddress} + types::address::AztecAddress }; struct Storage { diff --git a/tooling/nargo_fmt/tests/expected/import_braces.nr b/tooling/nargo_fmt/tests/expected/import_braces.nr new file mode 100644 index 00000000000..49c9d09001e --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/import_braces.nr @@ -0,0 +1 @@ +use dep::std::hash::sha256; diff --git a/tooling/nargo_fmt/tests/input/contract.nr b/tooling/nargo_fmt/tests/input/contract.nr index c5b19a686d2..97a6ebd6b77 100644 --- a/tooling/nargo_fmt/tests/input/contract.nr +++ b/tooling/nargo_fmt/tests/input/contract.nr @@ -11,7 +11,7 @@ contract Benchmarking { context::Context, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, log::emit_unencrypted_log, state_vars::{Map, PublicMutable, PrivateSet}, types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN}, - types::address::{AztecAddress} + types::address::AztecAddress }; struct Storage { diff --git a/tooling/nargo_fmt/tests/input/import_braces.nr b/tooling/nargo_fmt/tests/input/import_braces.nr new file mode 100644 index 00000000000..88c7e9562a8 --- /dev/null +++ b/tooling/nargo_fmt/tests/input/import_braces.nr @@ -0,0 +1 @@ +use dep::std::hash::{sha256}; \ No newline at end of file