Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix(abigen): safe ident underscore followed by numeric #970

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ fn expand_function_name(function: &Function, alias: Option<&MethodAlias>) -> Ide
if let Some(alias) = alias {
alias.function_name.clone()
} else {
util::safe_ident(&function.name.to_snake_case())
util::safe_ident(&util::safe_snake_case(&function.name))
}
}

Expand All @@ -567,7 +567,7 @@ fn expand_call_struct_name(function: &Function, alias: Option<&MethodAlias>) ->
let name = if let Some(alias) = alias {
format!("{}Call", alias.struct_name)
} else {
format!("{}Call", function.name.to_pascal_case())
format!("{}Call", util::safe_pascal_case(&function.name))
};
util::ident(&name)
}
Expand All @@ -577,7 +577,7 @@ fn expand_call_struct_variant_name(function: &Function, alias: Option<&MethodAli
if let Some(alias) = alias {
alias.struct_name.clone()
} else {
util::safe_ident(&function.name.to_pascal_case())
util::safe_ident(&util::safe_pascal_case(&function.name))
}
}

Expand Down
19 changes: 19 additions & 0 deletions ethers-contract/ethers-contract-abigen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ pub fn safe_ident(name: &str) -> Ident {
syn::parse_str::<SynIdent>(name).unwrap_or_else(|_| ident(&format!("{}_", name)))
}

/// Converts a `&str` to `snake_case` `String` while respecting identifier rules
pub fn safe_snake_case(ident: &str) -> String {
safe_identifier_name(ident.to_snake_case())
}

/// Converts a `&str` to `PascalCase` `String` while respecting identifier rules
pub fn safe_pascal_case(ident: &str) -> String {
safe_identifier_name(ident.to_pascal_case())
}

/// respects identifier rules, such as, an identifier must not start with a numeric char
fn safe_identifier_name(name: String) -> String {
if name.starts_with(|c: char| c.is_numeric()) {
format!("_{}", name)
} else {
name
}
}

/// Expands an identifier as snakecase and preserve any leading or trailing underscores
pub fn safe_snake_case_ident(name: &str) -> Ident {
let i = name.to_snake_case();
Expand Down
17 changes: 17 additions & 0 deletions ethers-contract/tests/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,23 @@ fn can_handle_unique_underscore_functions() {
let _contract_call = ConsoleLogCalls::Log2(call);
}

#[test]
fn can_handle_underscore_numeric() {
abigen!(
Test,
r#"[
_100pct(string)
]"#
);
let call = _100PctCall("message".to_string());

let provider = Arc::new(Provider::new(MockProvider::new()));
let contract = Test::new(Address::default(), Arc::clone(&provider));
// NOTE: this seems to be weird behaviour of `Inflector::to_snake_case` which turns "100pct" ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we fix this upstream? Is it right behavior?

// "10_0pct"
let _call = contract._10_0pct("hello".to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why it becomes 10_0pct and not 100_pct? @mattsse

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the comment right above this code line! It's an issue in the Inflector crate, which we'll track and solve separately

}

#[test]
fn can_handle_duplicates_with_same_name() {
abigen!(
Expand Down