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

dep(fmt): update ethers & solang #1909

Merged
merged 6 commits into from
Jun 10, 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
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["ethereum", "web3", "solidity", "linter"]
[dependencies]
indent_write = "2.2.0"
semver = "1.0.4"
solang-parser = "=0.1.13"
solang-parser = "=0.1.14"
itertools = "0.10.3"
thiserror = "1.0.30"

Expand Down
91 changes: 62 additions & 29 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use thiserror::Error;

use crate::{
comments::{CommentStringExt, CommentWithMetadata, Comments},
helpers,
solang_ext::*,
visit::{Visitable, Visitor},
};
Expand Down Expand Up @@ -1150,6 +1149,27 @@ impl<'a, W: Write> Formatter<'a, W> {

Ok(())
}

fn visit_ident_list(
&mut self,
loc: &mut Loc,
prefix: &str,
idents: &mut Vec<IdentifierPath>,
) -> Result<()> {
write_chunk!(self, loc.start(), "{}", prefix)?;
if !idents.is_empty() {
self.surrounded(loc.start(), "(", ")", Some(loc.end()), |fmt, _multiline| {
let args = fmt.items_to_chunks(
Some(loc.end()),
idents.iter_mut().map(|arg| Ok((arg.loc, arg))),
)?;
let multiline = fmt.are_chunks_separated_multiline("{}", &args, ", ")?;
fmt.write_chunks_separated(&args, ",", multiline)?;
Ok(())
})?;
}
Ok(())
}
}

// Traverse the Solidity Parse Tree and write to the code formatter
Expand Down Expand Up @@ -1646,6 +1666,23 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
Ok(())
}

fn visit_ident_path(&mut self, idents: &mut IdentifierPath) -> Result<(), Self::Error> {
rkrasiuk marked this conversation as resolved.
Show resolved Hide resolved
idents.identifiers.iter_mut().skip(1).for_each(|chunk| {
if !chunk.name.starts_with('.') {
chunk.name.insert(0, '.')
}
});
let chunks = self.items_to_chunks(
Some(idents.loc.end()),
idents.identifiers.iter_mut().map(|ident| Ok((ident.loc, ident))),
)?;
self.grouped(|fmt| {
let multiline = fmt.are_chunks_separated_multiline("{}", &chunks, "")?;
fmt.write_chunks_separated(&chunks, "", multiline)
})?;
Ok(())
}

fn visit_emit(&mut self, loc: Loc, event: &mut Expression) -> Result<()> {
self.grouped(|fmt| {
write_chunk!(fmt, loc.start(), "emit")?;
Expand Down Expand Up @@ -1825,23 +1862,17 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
FunctionAttribute::Virtual(loc) => write_chunk!(self, loc.end(), "virtual")?,
FunctionAttribute::Immutable(loc) => write_chunk!(self, loc.end(), "immutable")?,
FunctionAttribute::Override(loc, args) => {
write_chunk!(self, loc.start(), "override")?;
if !args.is_empty() {
self.surrounded(loc.start(), "(", ")", Some(loc.end()), |fmt, _multiline| {
let args = fmt.items_to_chunks(
Some(loc.end()),
args.iter_mut().map(|arg| Ok((arg.loc, arg))),
)?;
let multiline = fmt.are_chunks_separated_multiline("{}", &args, ", ")?;
fmt.write_chunks_separated(&args, ",", multiline)?;
Ok(())
})?;
}
self.visit_ident_list(loc, "override", args)?
}
FunctionAttribute::BaseOrModifier(loc, base) => {
let is_contract_base = self.context.contract.as_ref().map_or(false, |contract| {
contract.base.iter().any(|contract_base| {
helpers::namespace_matches(&contract_base.name, &base.name)
contract_base
.name
.identifiers
.iter()
.zip(&base.name.identifiers)
.all(|(l, r)| l.name == r.name)
})
});

Expand All @@ -1862,12 +1893,9 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
}

fn visit_base(&mut self, base: &mut Base) -> Result<()> {
let name_loc = LineOfCode::loc(&base.name);
let name_loc = &base.name.loc;
let mut name = self.chunked(name_loc.start(), Some(name_loc.end()), |fmt| {
fmt.grouped(|fmt| {
fmt.visit_expr(LineOfCode::loc(&base.name), &mut base.name)?;
Ok(())
})?;
fmt.visit_ident_path(&mut base.name)?;
Ok(())
})?;

Expand Down Expand Up @@ -2118,15 +2146,15 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {

let (is_library, mut list_chunks) = match &mut using.list {
UsingList::Library(library) => {
(true, vec![self.visit_to_chunk(library.loc().start(), None, library)?])
(true, vec![self.visit_to_chunk(library.loc.start(), None, library)?])
}
UsingList::Functions(funcs) => {
let mut funcs = funcs.iter_mut().peekable();
let mut chunks = Vec::new();
while let Some(func) = funcs.next() {
let next_byte_end = funcs.peek().map(|func| func.loc().start());
chunks.push(self.chunked(func.loc().start(), next_byte_end, |fmt| {
fmt.grouped(|fmt| fmt.visit_expr(func.loc(), func))?;
let next_byte_end = funcs.peek().map(|func| func.loc.start());
chunks.push(self.chunked(func.loc.start(), next_byte_end, |fmt| {
fmt.visit_ident_path(func)?;
Ok(())
})?);
}
Expand Down Expand Up @@ -2204,13 +2232,18 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {

fn visit_var_attribute(&mut self, attribute: &mut VariableAttribute) -> Result<()> {
let token = match attribute {
VariableAttribute::Visibility(visibility) => visibility.to_string(),
VariableAttribute::Constant(_) => "constant".to_string(),
VariableAttribute::Immutable(_) => "immutable".to_string(),
VariableAttribute::Override(_) => "override".to_string(),
VariableAttribute::Visibility(visibility) => Some(visibility.to_string()),
VariableAttribute::Constant(_) => Some("constant".to_string()),
VariableAttribute::Immutable(_) => Some("immutable".to_string()),
VariableAttribute::Override(loc, idents) => {
self.visit_ident_list(loc, "override", idents)?;
None
}
};
let loc = attribute.loc();
write_chunk!(self, loc.start(), loc.end(), "{}", token)?;
if let Some(token) = token {
let loc = attribute.loc();
write_chunk!(self, loc.start(), loc.end(), "{}", token)?;
}
Ok(())
}

Expand Down
19 changes: 0 additions & 19 deletions fmt/src/helpers.rs

This file was deleted.

1 change: 0 additions & 1 deletion fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod comments;
mod formatter;
mod helpers;
pub mod solang_ext;
mod visit;

Expand Down
11 changes: 9 additions & 2 deletions fmt/src/solang_ext/ast_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ impl AstEq for Loc {
}
}

impl AstEq for IdentifierPath {
fn ast_eq(&self, other: &Self) -> bool {
self.identifiers.ast_eq(&other.identifiers)
}
}

impl AstEq for SourceUnit {
fn ast_eq(&self, other: &Self) -> bool {
self.0.ast_eq(&other.0)
Expand Down Expand Up @@ -321,6 +327,7 @@ derive_ast_eq! { enum Statement {
loc,
dialect,
block,
flags,
},
}}
derive_ast_eq! { enum Expression {
Expand Down Expand Up @@ -422,7 +429,7 @@ derive_ast_eq! { enum YulExpression {
StringLiteral(string, ident),
Variable(ident),
FunctionCall(func),
Member(loc, expr, ident),
SuffixAccess(loc, expr, ident),
_
}}
derive_ast_eq! { enum YulSwitchOptions {
Expand Down Expand Up @@ -491,6 +498,6 @@ derive_ast_eq! { enum VariableAttribute {
Visibility(visi),
Constant(loc),
Immutable(loc),
Override(loc),
Override(loc, idents),
_
}}
6 changes: 3 additions & 3 deletions fmt/src/solang_ext/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl LineOfCode for YulExpression {
YulExpression::BoolLiteral(loc, _, _) |
YulExpression::NumberLiteral(loc, _, _, _) |
YulExpression::HexNumberLiteral(loc, _, _) |
YulExpression::Member(loc, _, _) => *loc,
YulExpression::SuffixAccess(loc, _, _) => *loc,
YulExpression::StringLiteral(literal, _) => literal.loc,
YulExpression::Variable(ident) => ident.loc,
YulExpression::FunctionCall(f) => f.loc,
Expand All @@ -157,7 +157,7 @@ impl LineOfCode for Statement {
use Statement::*;
match self {
Block { loc, unchecked: _checked, statements: _statements } => *loc,
Assembly { loc, dialect: _dialect, block: _block } => *loc,
Assembly { loc, dialect: _dialect, block: _block, flags: _flags } => *loc,
Args(loc, _) => *loc,
If(loc, _, _, _) => *loc,
While(loc, _, _) => *loc,
Expand Down Expand Up @@ -409,7 +409,7 @@ impl LineOfCode for VariableAttribute {
VariableAttribute::Visibility(visibility) => visibility.loc().unwrap(),
VariableAttribute::Constant(loc) |
VariableAttribute::Immutable(loc) |
VariableAttribute::Override(loc) => *loc,
VariableAttribute::Override(loc, _) => *loc,
}
}
}
Expand Down
Loading