Skip to content

Commit a1441eb

Browse files
authored
Allow multiple public methods in scripts with at least one run method (#38)
* Allow multiple public methods as long as there is at least one run method * disable the stdsimd feature for the ahash crate * Revert previous commit and remove clippy * Update taplo `0.13.0` * Remove missing_docs flag from cargo doc * Add back clippy * Fix clippy nits * Fix clippy nits * Upgrade version, rename files * Fix names * Fix mod.rs import name
1 parent 910c40c commit a1441eb

File tree

12 files changed

+101
-77
lines changed

12 files changed

+101
-77
lines changed

Diff for: .github/workflows/ci.yml

+15-8
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,31 @@ jobs:
4141
profile: minimal
4242
toolchain: nightly
4343
override: true
44-
components: clippy, rustfmt
44+
components: rustfmt
4545

4646
- name: cargo fmt
4747
uses: actions-rs/cargo@v1
4848
with:
4949
command: fmt
5050
args: --all --check
5151

52-
- name: cargo clippy
53-
uses: actions-rs/clippy-check@v1
54-
with:
55-
args: --all --all-features -- -D warnings
56-
token: ${{ secrets.GITHUB_TOKEN }}
57-
5852
- name: cargo doc
5953
uses: actions-rs/cargo@v1
6054
env:
61-
RUSTDOCFLAGS: '-D missing_docs -D rustdoc::missing_doc_code_examples'
55+
RUSTDOCFLAGS: "-D rustdoc::missing_doc_code_examples"
6256
with:
6357
command: doc
6458
args: --workspace --all-features --no-deps --document-private-items
59+
60+
clippy:
61+
runs-on: ubuntu-latest
62+
timeout-minutes: 30
63+
steps:
64+
- uses: actions/checkout@v4
65+
- uses: dtolnay/rust-toolchain@clippy
66+
- uses: Swatinem/rust-cache@v2
67+
with:
68+
cache-on-failure: true
69+
- run: cargo clippy --workspace --all-targets --all-features
70+
env:
71+
RUSTFLAGS: -Dwarnings

Diff for: Cargo.lock

+30-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
license = "MIT"
88
name = "scopelint"
99
repository = "https://github.com/ScopeLift/scopelint"
10-
version = "0.0.20"
10+
version = "0.0.21"
1111

1212
[dependencies]
1313
clap = { version = "4.4.2", features = ["derive"] }
@@ -16,5 +16,5 @@
1616
once_cell = "1.16.0"
1717
regex = "1.6.0"
1818
solang-parser = "0.3.2"
19-
taplo = "0.12.1"
20-
walkdir = "2.3.2"
19+
taplo = "0.13.0"
20+
walkdir = "2.3.2"

Diff for: README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# ScopeLint
1+
# `ScopeLint`
22

33
A simple and opinionated tool designed for basic formatting/linting of Solidity and TOML code in foundry projects.
44

5-
- [Installation](#installation)
6-
- [Usage](#usage)
7-
- [`scopelint fmt`](#scopelint-fmt)
8-
- [`scopelint check`](#scopelint-check)
9-
- [`scopelint spec`](#scopelint-spec)
10-
5+
- [`ScopeLint`](#scopelint)
6+
- [Installation](#installation)
7+
- [Usage](#usage)
8+
- [`scopelint fmt`](#scopelint-fmt)
9+
- [`scopelint check`](#scopelint-check)
10+
- [`scopelint spec`](#scopelint-spec)
1111

1212
## Installation
1313

Diff for: src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn parse(file: &Path) -> Result<Parsed, Box<dyn Error>> {
8787
let src = &fs::read_to_string(file)?;
8888

8989
let (pt, comments) = solang_parser::parse(src, 0).map_err(|d| {
90-
eprintln!("{:?}", d);
90+
eprintln!("{d:?}");
9191
"Failed to parse file".to_string()
9292
})?;
9393

@@ -140,7 +140,7 @@ fn validate(paths: [&str; 3]) -> Result<report::Report, Box<dyn Error>> {
140140
// Run all checks.
141141
results.add_items(validators::test_names::validate(&parsed));
142142
results.add_items(validators::src_names_internal::validate(&parsed));
143-
results.add_items(validators::script_one_pubic_run_method::validate(&parsed));
143+
results.add_items(validators::script_has_public_run_method::validate(&parsed));
144144
results.add_items(validators::constant_names::validate(&parsed));
145145
}
146146
}

Diff for: src/check/validators/constant_names.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod tests {
7676

7777
#[test]
7878
fn test_validate() {
79-
let content = r#"
79+
let content = r"
8080
contract MyContract {
8181
// These have the constant or immutable keyword and should be valid.
8282
uint256 constant MAX_UINT256 = type(uint256).max;
@@ -90,7 +90,7 @@ mod tests {
9090
address alice = address(123);
9191
uint256 aliceBalance = 500;
9292
}
93-
"#;
93+
";
9494

9595
let expected_findings = ExpectedFindings::new(2);
9696
expected_findings.assert_eq(content, &validate);
@@ -135,11 +135,11 @@ mod tests {
135135
];
136136

137137
for name in allowed_names {
138-
assert_eq!(is_valid_constant_name(name), true, "{name}");
138+
assert!(is_valid_constant_name(name), "{name}");
139139
}
140140

141141
for name in disallowed_names {
142-
assert_eq!(is_valid_constant_name(name), false, "{name}");
142+
assert!(!is_valid_constant_name(name), "{name}");
143143
}
144144
}
145145
}

Diff for: src/check/validators/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod formatting;
55
pub mod constant_names;
66

77
/// Validates that a script has a single public method named `run`.
8-
pub mod script_one_pubic_run_method;
8+
pub mod script_has_public_run_method;
99

1010
/// Validates that internal and private function names are prefixed with an underscore.
1111
pub mod src_names_internal;

Diff for: src/check/validators/script_one_pubic_run_method.rs renamed to src/check/validators/script_has_public_run_method.rs

+32-34
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,18 @@ pub fn validate(parsed: &Parsed) -> Vec<InvalidItem> {
5050
"No `run` method found".to_string(),
5151
)]
5252
}
53-
1 => {
54-
if public_methods[0] == "run" {
53+
_ => {
54+
if public_methods.contains(&"run".to_string()) {
5555
Vec::new()
5656
} else {
5757
vec![InvalidItem::new(
5858
ValidatorKind::Script,
5959
parsed,
60-
contract_loc.unwrap(),
61-
"The only public method must be named `run`".to_string(),
60+
contract_loc.unwrap(), //
61+
"No `run` method found".to_string(),
6262
)]
6363
}
6464
}
65-
_ => {
66-
vec![InvalidItem::new(
67-
ValidatorKind::Script,
68-
parsed,
69-
contract_loc.unwrap(),
70-
format!("Scripts must have a single public method named `run` (excluding `setUp`), but the following methods were found: {public_methods:?}"),
71-
72-
)]
73-
}
7465
}
7566
}
7667

@@ -82,52 +73,59 @@ mod tests {
8273
#[test]
8374
fn test_validate() {
8475
// TODO add another test for the third match arm
85-
let content_good = r#"
76+
let content_good = r"
8677
contract MyContract {
8778
function run() public {}
8879
}
89-
"#;
80+
";
9081

91-
// The number after `bad` on the variable name indicates the match arm covered.
92-
let content_bad0 = r#"
93-
contract MyContract {}
94-
"#;
95-
96-
let content_bad1 = r#"
97-
contract MyContract {
98-
function notRun() public {}
99-
}
100-
"#;
101-
102-
let content_bad2_variant0 = r#"
82+
let content_good_variant0 = r"
10383
contract MyContract {
10484
function run() public {}
10585
function run(string memory config) public {}
10686
}
107-
"#;
87+
";
10888

109-
let content_bad2_variant1 = r#"
89+
let content_good_variant1 = r"
11090
contract MyContract {
11191
function run() public {}
11292
function foo() public {}
11393
}
114-
"#;
94+
";
95+
96+
let content_good_variant2 = r"
97+
contract MyContract {
98+
function run(address admin) public {}
99+
}
100+
";
101+
102+
// The number after `bad` on the variable name indicates the match arm covered.
103+
let content_bad0 = r"
104+
contract MyContract {}
105+
";
106+
107+
let content_bad1 = r"
108+
contract MyContract {
109+
function notRun() public {}
110+
}
111+
";
115112

116-
let content_bad2_variant2 = r#"
113+
let content_bad2_variant0 = r"
117114
contract MyContract {
118115
function foo() public {}
119116
function bar() public {}
120117
}
121-
"#;
118+
";
122119

123120
let expected_findings_good = ExpectedFindings::new(0);
124121
expected_findings_good.assert_eq(content_good, &validate);
122+
expected_findings_good.assert_eq(content_good_variant0, &validate);
123+
expected_findings_good.assert_eq(content_good_variant1, &validate);
124+
expected_findings_good.assert_eq(content_good_variant2, &validate);
125125

126126
let expected_findings_bad = ExpectedFindings { script: 1, ..Default::default() };
127127
expected_findings_bad.assert_eq(content_bad0, &validate);
128128
expected_findings_bad.assert_eq(content_bad1, &validate);
129129
expected_findings_bad.assert_eq(content_bad2_variant0, &validate);
130-
expected_findings_bad.assert_eq(content_bad2_variant1, &validate);
131-
expected_findings_bad.assert_eq(content_bad2_variant2, &validate);
132130
}
133131
}

Diff for: src/check/validators/src_names_internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod tests {
6262

6363
#[test]
6464
fn test_validate() {
65-
let content = r#"
65+
let content = r"
6666
contract MyContract {
6767
// Valid names for internal or private src methods.
6868
function _myInternalMethod() internal {}
@@ -76,7 +76,7 @@ mod tests {
7676
function myPublicMethod() public {}
7777
function myExternalMethod() external {}
7878
}
79-
"#;
79+
";
8080

8181
let expected_findings = ExpectedFindings { src: 2, ..ExpectedFindings::default() };
8282
expected_findings.assert_eq(content, &validate);

Diff for: src/check/validators/test_names.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mod tests {
9595

9696
#[test]
9797
fn test_validate() {
98-
let content = r#"
98+
let content = r"
9999
contract MyContract {
100100
// Good test names.
101101
function test_Description() public {}
@@ -117,7 +117,7 @@ mod tests {
117117
function _testDescription() public {}
118118
function _testDescriptionMoreInfo() public {}
119119
}
120-
"#;
120+
";
121121

122122
let expected_findings = ExpectedFindings { test: 3, ..ExpectedFindings::default() };
123123
expected_findings.assert_eq(content, &validate);
@@ -172,11 +172,11 @@ mod tests {
172172
];
173173

174174
for name in allowed_names {
175-
assert_eq!(is_valid_test_name(name), true, "{name}");
175+
assert!(is_valid_test_name(name), "{name}");
176176
}
177177

178178
for name in disallowed_names {
179-
assert_eq!(is_valid_test_name(name), false, "{name}");
179+
assert!(!is_valid_test_name(name), "{name}");
180180
}
181181
}
182182
}

Diff for: src/spec/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ fn trimmed_fn_name_to_requirement(trimmed_fn_name: &str) -> String {
256256
trimmed_fn_name
257257
.replace('_', ":")
258258
.chars()
259-
.enumerate()
260-
.map(|(_i, c)| if c.is_uppercase() { format!(" {c}") } else { c.to_string() })
259+
.map(|c| if c.is_uppercase() { format!(" {c}") } else { c.to_string() })
261260
.collect::<String>()
262261
}

0 commit comments

Comments
 (0)