Skip to content

Commit

Permalink
fix: formatter didn't format >>= well (#6337)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves
noir-lang/noir_json_parser#16 (comment)

## Summary

I thought for "op assign" like `x += 1` there will always come one or
two tokens, then `=`, but that's not the case for `>>=` (it's `> >=`).

## Additional Context

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Oct 24, 2024
1 parent 21c6e8f commit 598230d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tooling/nargo_fmt/src/formatter/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,15 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> {
if formatter.is_at(Token::Assign) {
formatter.write_token(Token::Assign);
} else {
while formatter.token != Token::Assign {
formatter.write_current_token();
formatter.bump();
formatter.skip_comments_and_whitespace();
}
formatter.write_token(Token::Assign);
// This is something like `x += 1`, which is parsed as an
// Assign with an InfixExpression as its right-hand side: `x = x + 1`.
// There will always be two tokens here, like `+ =` or `> >=`.
formatter.write_current_token();
formatter.bump();
formatter.skip_comments_and_whitespace();
formatter.write_current_token();
formatter.bump();

is_op_assign = true;
}
formatter.write_space();
Expand Down Expand Up @@ -435,6 +438,16 @@ mod tests {
assert_format(src, expected);
}

#[test]
fn format_shift_right_assign() {
let src = " fn foo() { x >>= 2 ; } ";
let expected = "fn foo() {
x >>= 2;
}
";
assert_format(src, expected);
}

#[test]
fn format_comptime_let_statement() {
let src = " fn foo() { comptime let x = 1 ; } ";
Expand Down

0 comments on commit 598230d

Please sign in to comment.