Skip to content

Commit

Permalink
fix(check): properly measure scratch space length
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfertel committed Feb 10, 2024
1 parent 1ad7395 commit f753b7a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/check/violation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ fn get_insertion_offset(
/// # Process
/// The function works in several steps:
/// 1. It first creates a set of function names present in `contract_hir` to identify the functions
/// that need ordering.
/// that need to be sorted.
/// 2. It then iterates over the `violations` to correct the order of functions in `contract_sol`,
/// matching them with `contract_hir`.
/// 3. Functions not part of `contract_hir` are removed, as their correct position is unknown.
Expand Down Expand Up @@ -365,7 +365,7 @@ pub(crate) fn fix_order(
})
.collect();

// 4. a - Add the functions that appear in the tree to a blank
// 4. a - Add the functions that appear in the tree to the blank
// string `source`.
// b - Replace them with whitespace in `scratch`.
//
Expand All @@ -383,7 +383,6 @@ pub(crate) fn fix_order(

// 5. Replace the contract's body with the sorted functions and
// the extra functions contained in the scratch string.
let contract_body_len = contract_sol.loc().end() - contract_sol.loc().start();
// We know there is at least two parts because we found order violations.
let first_part_loc = contract_sol.parts[0].loc();
// If the functions in the solidity file are exactly the functions in the
Expand All @@ -394,7 +393,7 @@ pub(crate) fn fix_order(
"{}{}{}{}",
&ctx.src[..first_part_loc.start()],
source.join("\n\n"),
&scratch[first_part_loc.start()..contract_body_len],
&scratch[first_part_loc.start()..contract_sol.loc.end() - 1],
&ctx.src[contract_sol.loc().end() - 1..]
)
} else {
Expand All @@ -410,7 +409,7 @@ pub(crate) fn fix_order(
"{}{}{SEPARATOR}{}{}",
&ctx.src[..first_part_loc.start()],
source.join("\n\n"),
&scratch[first_part_loc.start()..contract_body_len],
&scratch[first_part_loc.start()..contract_sol.loc.end() - 1],
&ctx.src[contract_sol.loc().end() - 1..]
)
};
Expand Down
40 changes: 40 additions & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,46 @@ fn fixes_extra_codegen_tree() {
assert!(actual.contains("2 issues fixed."));
}

#[test]
fn fixes_extra_fn_plus_wrong_order() {
let cwd = env::current_dir().unwrap();
let binary_path = get_binary_path();
let tree_path = cwd
.join("tests")
.join("check")
.join("fix_extra_fn_plus_order.tree");

let output = cmd(&binary_path, "check", &tree_path, &["--fix", "--stdout"]);
let actual = String::from_utf8(output.stdout).unwrap();
println!("what {}", actual);

let expected = r"// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;
contract Foo {
function test_WhenB() external {
// it Y
}
function test_WhenA() external {
// it X
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ==================== BULLOAK AUTOGENERATED SEPARATOR ====================
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Code below this section could not be automatically moved by bulloak
// =========================================================================
function test_WhenTheMethodIsCalledASecondTime() external {
// it Z
}
}";

assert!(actual.contains(expected));
assert!(actual.contains("1 issue fixed."));
}

#[test]
fn fixes_invalid_structural_match() {
let binary_path = get_binary_path();
Expand Down
16 changes: 16 additions & 0 deletions tests/check/fix_extra_fn_plus_order.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract Foo {
function test_WhenA() external {
// it X
}

function test_WhenB() external {
// it Y
}

function test_WhenTheMethodIsCalledASecondTime() external {
// it Z
}
}
6 changes: 6 additions & 0 deletions tests/check/fix_extra_fn_plus_order.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// https://github.com/alexfertel/bulloak/issues/56
Foo
├── when b
│ └── it Y
└── when a
└── it X

0 comments on commit f753b7a

Please sign in to comment.