Skip to content

Commit

Permalink
chore: custom configuration for formatter tests (#3202)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
kek kek kek and kevaundray authored Oct 30, 2023
1 parent 7d126c5 commit 191f836
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 18 deletions.
8 changes: 7 additions & 1 deletion tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {
let input_source_path = file.path();
let input_source = std::fs::read_to_string(input_source_path).unwrap();

let config = input_source
.lines()
.flat_map(|line| line.strip_prefix("//@"))
.collect::<Vec<_>>()
.join("\n");

let output_source_path = outputs_dir.join(file_name);
let output_source = std::fs::read_to_string(output_source_path).unwrap();

Expand All @@ -55,7 +61,7 @@ fn format_{test_name}() {{
let (parsed_module, errors) = noirc_frontend::parse_program(&input);
assert!(errors.is_empty());
let config = nargo_fmt::Config::default();
let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(&input, parsed_module, &config);
Expand Down
15 changes: 10 additions & 5 deletions tooling/nargo_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,28 @@ config! {
max_width: usize, 100, "Maximum width of each line";
tab_spaces: usize, 4, "Number of spaces per tab";
remove_nested_parens: bool, true, "Remove nested parens";
error_on_lost_comment: bool, true, "Error if unable to get comments";
short_array_element_width_threshold: usize, 10, "Width threshold for an array element to be considered short";
array_width: usize, 100, "Maximum width of an array literal before falling back to vertical formatting";
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else expressions";
}

impl Config {
pub fn read(path: &Path) -> Result<Self, ConfigError> {
let mut config = Self::default();
let config_path = path.join("noirfmt.toml");

let raw_toml = match std::fs::read_to_string(&config_path) {
Ok(t) => t,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => String::new(),
let input = match std::fs::read_to_string(&config_path) {
Ok(input) => input,
Err(cause) if cause.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(cause) => return Err(ConfigError::ReadFailed(config_path, cause)),
};
let toml = toml::from_str(&raw_toml).map_err(ConfigError::MalformedFile)?;

Self::of(&input)
}

pub fn of(s: &str) -> Result<Self, ConfigError> {
let mut config = Self::default();
let toml = toml::from_str(s).map_err(ConfigError::MalformedFile)?;
config.fill_from_toml(toml);
Ok(config)
}
Expand Down
8 changes: 0 additions & 8 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ use noirc_frontend::lexer::Lexer;
use noirc_frontend::token::Token;
use noirc_frontend::{Expression, Ident};

pub(crate) fn recover_comment_removed(original: &str, new: String) -> String {
if changed_comment_content(original, &new) {
original.to_string()
} else {
new
}
}

pub(crate) fn changed_comment_content(original: &str, new: &str) -> bool {
comments(original).ne(comments(new))
}
Expand Down
10 changes: 9 additions & 1 deletion tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ impl<'me> FmtVisitor<'me> {

#[track_caller]
fn push_rewrite(&mut self, rewrite: String, span: Span) {
let rewrite = utils::recover_comment_removed(self.slice(span), rewrite);
let original = self.slice(span);
let changed_comment_content = utils::changed_comment_content(original, &rewrite);

if changed_comment_content && self.config.error_on_lost_comment {
panic!("not formatted because a comment would be lost: {rewrite:?}");
}

let rewrite = if changed_comment_content { original.to_string() } else { rewrite };

self.format_missing_indent(span.start(), true);
self.push_str(&rewrite);
}
Expand Down
2 changes: 0 additions & 2 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ use crate::{
impl FmtVisitor<'_> {
pub(crate) fn visit_expr(&mut self, expr: Expression, expr_type: ExpressionType) {
let span = expr.span;

let rewrite = self.format_expr(expr, expr_type);
self.push_rewrite(rewrite, span);

self.last_position = span.end();
}

Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/if.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn main() {
let (x,y) = if is_square(gx1) {
(x1, sqrt(gx1))
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/infix.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn foo() {
40 + 2;
!40 + 2;
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/let.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn let_() {
let fn_call = my_function(some_function(10, "arg1", another_function()), another_func(20, some_function(), 30));
let array = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]];
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/literals.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn main() {
[1, 2, 3, 4, 5];

Expand Down
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/expected/nested_parens.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@remove_nested_parens=false
fn main() {
((()));
((((((((()))))))));
}
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/unary_operators.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn main() {
-1;
-/*test*/1;
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/if.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn main() {
let (x,y) = if is_square(gx1) {(x1, sqrt(gx1))} else {(x2, sqrt(gx2))};

Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/infix.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn foo() {
40 + 2;
!40+2;
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/let.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn let_() {
let fn_call = my_function(some_function( 10, "arg1", another_function() ),another_func (20, some_function() , 30 ));
let array = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]];
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/tests/input/literals.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

//@error_on_lost_comment=false
fn main() {
[1,2,3,4,5];

Expand Down
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/input/nested_parens.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@remove_nested_parens=false
fn main() {
((()));
((((((((()))))))));
}
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/unary_operators.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@error_on_lost_comment=false
fn main() {
-1;
-/*test*/1;
Expand Down

0 comments on commit 191f836

Please sign in to comment.