Skip to content

Commit

Permalink
fix(sol-macro): don't double attributes in JSON input (#583)
Browse files Browse the repository at this point in the history
* fix(sol-macro): don't double attributes in JSON input

* comment
  • Loading branch information
DaniPopes authored Mar 22, 2024
1 parent 1bac767 commit ff33969
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
3 changes: 1 addition & 2 deletions crates/sol-macro-input/src/expander.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use proc_macro2::TokenStream;

use crate::SolInput;
use proc_macro2::TokenStream;

/// Expands a `SolInput` into a `TokenStream`.
pub trait SolInputExpander {
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro-input/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl Parse for SolInput {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let attrs = Attribute::parse_inner(input)?;

// ignore attributes when peeking
// Ignore outer attributes when peeking.
let fork = input.fork();
let _inner = Attribute::parse_outer(&fork)?;
let _fork_outer = Attribute::parse_outer(&fork)?;

if fork.peek(LitStr) || (fork.peek(Ident) && fork.peek2(Token![,]) && fork.peek3(LitStr)) {
Self::parse_abigen(attrs, input)
Expand Down
26 changes: 10 additions & 16 deletions crates/sol-macro-input/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{SolInput, SolInputKind};

use alloy_json_abi::{ContractObject, JsonAbi, ToSolConfig};
use proc_macro2::{Ident, TokenStream};
use quote::{quote, TokenStreamExt};
Expand All @@ -8,18 +7,13 @@ use syn::Result;
impl SolInput {
/// Normalize JSON ABI inputs into Sol inputs.
pub fn normalize_json(self) -> Result<Self> {
if !matches!(self.kind, SolInputKind::Json(..)) {
return Ok(self);
}

// irrefutable, as we just checked
let SolInput {
attrs,
path,
kind: SolInputKind::Json(name, ContractObject { abi, bytecode, deployed_bytecode }),
} = self
else {
panic!()
return Ok(self);
};

let mut abi = abi.ok_or_else(|| syn::Error::new(name.span(), "ABI not found in JSON"))?;
Expand All @@ -37,15 +31,15 @@ impl SolInput {
let attrs_iter = attrs.iter();
let doc_str = format!(
"\n\n\
Generated by the following Solidity interface...
```solidity
{sol}
```
...which was generated by the following JSON ABI:
```json
{json_s}
```",
Generated by the following Solidity interface...
```solidity
{sol}
```
...which was generated by the following JSON ABI:
```json
{json_s}
```",
json_s = serde_json::to_string_pretty(&abi).unwrap()
);
let tokens = quote! {
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-macro-input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

extern crate syn_solidity as ast;

/// Tools for working with `#[...]` attributes.
mod attr;
pub use attr::{derives_mapped, docs_str, mk_doc, ContainsSolAttrs, SolAttrs};
Expand All @@ -26,5 +28,3 @@ pub use expander::SolInputExpander;

#[cfg(feature = "json")]
mod json;

extern crate syn_solidity as ast;
1 change: 0 additions & 1 deletion crates/sol-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
alloy-sol-macro-input.workspace = true

syn-solidity = { workspace = true, features = ["visit", "visit-mut"] }

proc-macro2.workspace = true
Expand Down
12 changes: 10 additions & 2 deletions crates/sol-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ struct SolMacroExpander;
impl SolInputExpander for SolMacroExpander {
fn expand(&mut self, input: &SolInput) -> syn::Result<proc_macro2::TokenStream> {
let input = input.clone();
// Convert JSON input to Solidity input

#[cfg(feature = "json")]
let is_json = matches!(input.kind, SolInputKind::Json { .. });

// Convert JSON input to Solidity input
#[cfg(feature = "json")]
let input = input.normalize_json()?;

Expand All @@ -253,7 +256,12 @@ impl SolInputExpander for SolMacroExpander {

let tokens = match kind {
SolInputKind::Sol(mut file) => {
file.attrs.extend(attrs);
// Attributes have already been added to the inner contract generated in
// `normalize_json`.
#[cfg(feature = "json")]
if !is_json {
file.attrs.extend(attrs);
}
crate::expand::expand(file)
}
SolInputKind::Type(ty) => {
Expand Down
5 changes: 5 additions & 0 deletions crates/sol-types/tests/macros/sol/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ use std::borrow::Cow;
fn large_array() {
sol!(
#[sol(abi)]
#[derive(Debug)]
LargeArray,
"../json-abi/tests/abi/LargeArray.json"
);

let call = LargeArray::callWithLongArrayCall { longArray: [0; 128] };
let _ = format!("{call:#?}");

assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])");
let contract = LargeArray::abi::contract();
assert_eq!(
Expand Down

0 comments on commit ff33969

Please sign in to comment.