Skip to content

Commit

Permalink
fix: fix formatter adding -- to multiple use statements (#773)
Browse files Browse the repository at this point in the history
* fix: fix formatter adding `--` to multiple `use` statements
* docs: update the workaround for the formatter: it turns out that we can format parsing but non-compiling code
  • Loading branch information
iqdecay authored Feb 13, 2022
1 parent 61af2ac commit 1631570
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/getting-started/temporary_workarounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ The optimizing pass of the compiler is not yet implemented, therefore bytecode w

## Formatter

Currently, we need to parse the Sway code before formatting it, and the best way to do this is to compile the code. Hence, **the formatter cannot work on Sway code that does not compile**. This requirement may be changed in the future.
Currently, we need to parse the Sway code before formatting it. Hence, **the formatter cannot work on Sway code that does not parse correctly**. This requirement may be changed in the future.
19 changes: 19 additions & 0 deletions sway-fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,23 @@ fn one_liner() -> bool {
let (_, formatted_code) = result.unwrap();
assert_eq!(correct_sway_code, formatted_code);
}

#[test]
// Test that the use statements with multiple imports are properly formatted
fn test_use_statement() {
let expected_sway = r#"script;
use std::chain::{panic,log_u8};
use std::chain::assert;
use std::hash::{HashMethod,hash_value,hash_pair};
use a::b::{c,d::{e,f}};
use a::b::{c,d::{self,f}};
fn main() {
}
"#;
let result = get_formatted_data(expected_sway.into(), OPTIONS);
assert!(result.is_ok());
let (_, formatted_code) = result.unwrap();
assert_eq!(formatted_code, expected_sway);
}
}
14 changes: 12 additions & 2 deletions sway-fmt/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn traverse_for_changes(parse_tree: &SwayParseTree) -> Vec<Change> {
let mut changes = vec![];

for node in &parse_tree.tree.root_nodes {
traverse_ast_node(node, &mut changes)
traverse_ast_node(node, &mut changes);
}

changes.sort_by(|a, b| a.start.cmp(&b.start));
Expand All @@ -69,7 +69,17 @@ fn traverse_ast_node(ast_node: &AstNode, changes: &mut Vec<Change>) {
}

AstNodeContent::UseStatement(_) => {
changes.push(Change::new(&ast_node.span, ChangeType::UseStatement));
// The AST generates one root node per use statement, we must avoid duplicating them
// while formatting
let next_span = &ast_node.span;
match changes.last() {
Some(last_change) => {
if last_change.start != next_span.start() {
changes.push(Change::new(next_span, ChangeType::UseStatement));
}
}
_ => changes.push(Change::new(next_span, ChangeType::UseStatement)),
}
}

AstNodeContent::IncludeStatement(_) => {
Expand Down

0 comments on commit 1631570

Please sign in to comment.