Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(syn-solidity): imports #252

Merged
merged 1 commit into from
Aug 30, 2023
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
27 changes: 27 additions & 0 deletions crates/sol-types/tests/ui/imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use alloy_sol_types::sol;

sol! {
import *;
}

sol! {
import * as foo;
}

sol! {
import * as foo from;
}

// OK
sol! {
import "path";
import "path" as foo;

import {} from "path";
import { a, b as c, d } from "path";

import * from "path";
import * as foo from "path";
}

fn main() {}
17 changes: 17 additions & 0 deletions crates/sol-types/tests/ui/imports.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: expected `from`
--> tests/ui/imports.rs:4:13
|
4 | import *;
| ^

error: expected `from`
--> tests/ui/imports.rs:8:20
|
8 | import * as foo;
| ^

error: expected string literal
--> tests/ui/imports.rs:12:25
|
12 | import * as foo from;
| ^
23 changes: 13 additions & 10 deletions crates/syn-solidity/src/item/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ impl Spanned for ImportDirective {
/// The path of an import directive.
#[derive(Clone, Debug)]
pub enum ImportPath {
/// A plain import directive: `import "foo.sol" as Foo;`
/// A plain import directive: `import "foo.sol" as Foo;`.
Plain(ImportPlain),
/// A list of import aliases: `import { Foo as Bar, Baz } from "foo.sol";`
/// A list of import aliases: `import { Foo as Bar, Baz } from "foo.sol";`.
Aliases(ImportAliases),
/// A glob import directive: `import * as Foo from "foo.sol";`
/// A glob import directive: `import * as Foo from "foo.sol";`.
Glob(ImportGlob),
}

Expand Down Expand Up @@ -155,7 +155,7 @@ impl ImportAlias {
}
}

/// A plain import directive: `import "foo.sol" as Foo;`
/// A plain import directive: `import "foo.sol" as Foo;`.
#[derive(Clone)]
pub struct ImportPlain {
pub path: LitStr,
Expand Down Expand Up @@ -198,11 +198,11 @@ impl Spanned for ImportPlain {
}
}

/// A list of import aliases: `import { Foo as Bar, Baz } from "foo.sol";`
/// A list of import aliases: `import { Foo as Bar, Baz } from "foo.sol";`.
#[derive(Clone)]
pub struct ImportAliases {
pub brace_token: Brace,
pub imports: Punctuated<(SolIdent, ImportAlias), Token![,]>,
pub imports: Punctuated<(SolIdent, Option<ImportAlias>), Token![,]>,
pub from_token: kw::from,
pub path: LitStr,
}
Expand All @@ -221,7 +221,10 @@ impl Parse for ImportAliases {
let content;
Ok(Self {
brace_token: braced!(content in input),
imports: content.parse_terminated(|c| Ok((c.parse()?, c.parse()?)), Token![,])?,
imports: content.parse_terminated(
|c| Ok((c.parse()?, c.call(ImportAlias::parse_opt)?)),
Token![,],
)?,
from_token: input.parse()?,
path: input.parse()?,
})
Expand All @@ -241,11 +244,11 @@ impl Spanned for ImportAliases {
}
}

/// A glob import directive: `import * as Foo from "foo.sol";`
/// A glob import directive: `import * as Foo from "foo.sol";`.
#[derive(Clone)]
pub struct ImportGlob {
pub star_token: Token![*],
pub alias: ImportAlias,
pub alias: Option<ImportAlias>,
pub from_token: kw::from,
pub path: LitStr,
}
Expand All @@ -263,7 +266,7 @@ impl Parse for ImportGlob {
fn parse(input: ParseStream<'_>) -> Result<Self> {
Ok(Self {
star_token: input.parse()?,
alias: input.parse()?,
alias: input.call(ImportAlias::parse_opt)?,
from_token: input.parse()?,
path: input.parse()?,
})
Expand Down
4 changes: 3 additions & 1 deletion crates/syn-solidity/src/lit/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ macro_rules! str_lit {
impl Parse for $name {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut values = Vec::new();
while Self::peek(&input.lookahead1()) {
let mut first = true;
while first || Self::peek(&input.lookahead1()) {
first = false;
values.push(input.parse()?);
}
Ok(Self { values })
Expand Down
12 changes: 8 additions & 4 deletions crates/syn-solidity/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,18 @@ macro_rules! make_visitor {
}
}
ImportPath::Aliases(ImportAliases { imports, path, .. }) => {
for (name, ImportAlias { alias, .. }) in imports {
for (name, alias) in imports {
v.visit_ident(name);
v.visit_ident(alias);
if let Some(ImportAlias { alias, .. }) = alias {
v.visit_ident(alias);
}
}
v.visit_lit_str(path);
}
ImportPath::Glob(ImportGlob { alias: ImportAlias { alias, .. }, path, .. }) => {
v.visit_ident(alias);
ImportPath::Glob(ImportGlob { alias, path, .. }) => {
if let Some(ImportAlias { alias, .. }) = alias {
v.visit_ident(alias);
}
v.visit_lit_str(path);
}
}
Expand Down