From 2af74ee54b2603c3999459cd8cfc709b2cdfa56b Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Wed, 9 Jul 2025 15:35:46 +0200 Subject: [PATCH 01/10] feat(fmt): adds tab as style for indents --- crates/config/src/fmt.rs | 14 +++++++- crates/config/src/lib.rs | 7 ++-- crates/fmt/src/buffer.rs | 72 ++++++++++++++++++++++++++++++++----- crates/fmt/src/formatter.rs | 12 +++++-- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/crates/config/src/fmt.rs b/crates/config/src/fmt.rs index 69381171989be..223b46d2ffd6b 100644 --- a/crates/config/src/fmt.rs +++ b/crates/config/src/fmt.rs @@ -7,8 +7,10 @@ use serde::{Deserialize, Serialize}; pub struct FormatterConfig { /// Maximum line length where formatter will try to wrap the line pub line_length: usize, - /// Number of spaces per indentation level + /// Number of spaces per indentation level. Ignored if style is Tab pub tab_width: usize, + /// Style of indent + pub style: IndentStyle, /// Print spaces between brackets pub bracket_spacing: bool, /// Style of uint/int256 types @@ -166,11 +168,21 @@ pub enum MultilineFuncHeaderStyle { AllParams, } +/// Style of indent +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum IndentStyle { + #[default] + Space, + Tab, +} + impl Default for FormatterConfig { fn default() -> Self { Self { line_length: 120, tab_width: 4, + style: IndentStyle::Space, bracket_spacing: false, int_types: IntTypes::Long, multiline_func_header: MultilineFuncHeaderStyle::AttributesFirst, diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index d3ba91f077101..772f30b0b0d01 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -2589,6 +2589,7 @@ mod tests { cache::{CachedChains, CachedEndpoints}, endpoints::RpcEndpointType, etherscan::ResolvedEtherscanConfigs, + fmt::IndentStyle, }; use NamedChain::Moonbeam; use endpoints::{RpcAuth, RpcEndpointConfig}; @@ -4502,12 +4503,13 @@ mod tests { figment::Jail::expect_with(|jail| { jail.create_file( "foundry.toml", - r" + r#" [fmt] line_length = 100 tab_width = 2 bracket_spacing = true - ", + style = "space" + "#, )?; let loaded = Config::load().unwrap().sanitized(); assert_eq!( @@ -4516,6 +4518,7 @@ mod tests { line_length: 100, tab_width: 2, bracket_spacing: true, + style: IndentStyle::Space, ..Default::default() } ); diff --git a/crates/fmt/src/buffer.rs b/crates/fmt/src/buffer.rs index 773ebb483c695..22f746b6cef0d 100644 --- a/crates/fmt/src/buffer.rs +++ b/crates/fmt/src/buffer.rs @@ -4,6 +4,7 @@ use crate::{ comments::{CommentState, CommentStringExt}, string::{QuoteState, QuotedStringExt}, }; +use foundry_config::fmt::IndentStyle; use std::fmt::Write; /// An indent group. The group may optionally skip the first line @@ -44,6 +45,7 @@ pub struct FormatBuffer { indents: Vec, base_indent_len: usize, tab_width: usize, + style: IndentStyle, last_char: Option, current_line_len: usize, restrict_to_single_line: bool, @@ -51,10 +53,11 @@ pub struct FormatBuffer { } impl FormatBuffer { - pub fn new(w: W, tab_width: usize) -> Self { + pub fn new(w: W, tab_width: usize, style: IndentStyle) -> Self { Self { w, tab_width, + style, base_indent_len: 0, indents: vec![], current_line_len: 0, @@ -67,7 +70,7 @@ impl FormatBuffer { /// Create a new temporary buffer based on an existing buffer which retains information about /// the buffer state, but has a blank String as its underlying `Write` interface pub fn create_temp_buf(&self) -> FormatBuffer { - let mut new = FormatBuffer::new(String::new(), self.tab_width); + let mut new = FormatBuffer::new(String::new(), self.tab_width, self.style); new.base_indent_len = self.total_indent_len(); new.current_line_len = self.current_line_len(); new.last_char = self.last_char; @@ -114,9 +117,28 @@ impl FormatBuffer { } } - /// Get the current indent size (level * tab_width) + /// Get the current indent size. level * tab_width for spaces and level for tabs pub fn current_indent_len(&self) -> usize { - self.level() * self.tab_width + match self.style { + IndentStyle::Space => self.level() * self.tab_width, + IndentStyle::Tab => self.level(), + } + } + + /// Get the char used for indent + pub fn indent_char(&self) -> &'static str { + match self.style { + IndentStyle::Space => " ", + IndentStyle::Tab => "\t", + } + } + + /// Get the indent len for the given level + pub fn get_indent_len(&self, level: usize) -> usize { + match self.style { + IndentStyle::Space => level * self.tab_width, + IndentStyle::Tab => level, + } } /// Get the total indent size @@ -209,7 +231,7 @@ impl Write for FormatBuffer { return Ok(()); } - let mut indent = " ".repeat(self.current_indent_len()); + let mut indent = self.indent_char().repeat(self.current_indent_len()); loop { match self.state { @@ -236,7 +258,9 @@ impl Write for FormatBuffer { // a newline has been inserted if len > 0 { if self.last_indent_group_skipped() { - indent = " ".repeat(self.current_indent_len() + self.tab_width); + indent = self + .indent_char() + .repeat(self.get_indent_len(self.level() + 1)); self.set_last_indent_group_skipped(false); } if comment_state == CommentState::Line { @@ -340,10 +364,11 @@ mod tests { fn test_buffer_indents() -> std::fmt::Result { let delta = 1; - let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH); + let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space); assert_eq!(buf.indents.len(), 0); assert_eq!(buf.level(), 0); assert_eq!(buf.current_indent_len(), 0); + assert_eq!(buf.style, IndentStyle::Space); buf.indent(delta); assert_eq!(buf.indents.len(), delta); @@ -374,7 +399,7 @@ mod tests { fn test_identical_temp_buf() -> std::fmt::Result { let content = "test string"; let multiline_content = "test\nmultiline\nmultiple"; - let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH); + let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space); // create identical temp buf let mut temp = buf.create_temp_buf(); @@ -432,11 +457,40 @@ mod tests { ]; for content in &contents { - let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH); + let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space); write!(buf, "{content}")?; assert_eq!(&buf.w, content); } Ok(()) } + + #[test] + fn test_indent_char() -> std::fmt::Result { + assert_eq!( + FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space).indent_char(), + " " + ); + assert_eq!( + FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Tab).indent_char(), + "\t" + ); + Ok(()) + } + + #[test] + fn test_indent_len() -> std::fmt::Result { + // Space should use level * TAB_WIDTH + let mut buf = FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space); + assert_eq!(buf.current_indent_len(), 0); + buf.indent(2); + assert_eq!(buf.current_indent_len(), 2 * TAB_WIDTH); + + // Tab should use level + buf = FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Tab); + assert_eq!(buf.current_indent_len(), 0); + buf.indent(2); + assert_eq!(buf.current_indent_len(), 2); + Ok(()) + } } diff --git a/crates/fmt/src/formatter.rs b/crates/fmt/src/formatter.rs index deeba840dce32..205784f31c285 100644 --- a/crates/fmt/src/formatter.rs +++ b/crates/fmt/src/formatter.rs @@ -112,7 +112,7 @@ impl<'a, W: Write> Formatter<'a, W> { config: FormatterConfig, ) -> Self { Self { - buf: FormatBuffer::new(w, config.tab_width), + buf: FormatBuffer::new(w, config.tab_width, config.style), source, config, temp_bufs: Vec::new(), @@ -158,6 +158,7 @@ impl<'a, W: Write> Formatter<'a, W> { buf_fn! { fn last_indent_group_skipped(&self) -> bool } buf_fn! { fn set_last_indent_group_skipped(&mut self, skip: bool) } buf_fn! { fn write_raw(&mut self, s: impl AsRef) -> std::fmt::Result } + buf_fn! { fn indent_char(&self) -> &'static str } /// Do the callback within the context of a temp buffer fn with_temp_buf( @@ -570,7 +571,12 @@ impl<'a, W: Write> Formatter<'a, W> { .char_indices() .take_while(|(idx, ch)| ch.is_whitespace() && *idx <= self.buf.current_indent_len()) .count(); - let to_skip = indent_whitespace_count - indent_whitespace_count % self.config.tab_width; + let to_skip = if indent_whitespace_count < self.buf.current_indent_len() { + 0 + } else { + self.buf.current_indent_len() + }; + write!(self.buf(), " *")?; let content = &line[to_skip..]; if !content.trim().is_empty() { @@ -599,7 +605,7 @@ impl<'a, W: Write> Formatter<'a, W> { .char_indices() .skip_while(|(idx, ch)| ch.is_whitespace() && *idx < indent) .map(|(_, ch)| ch); - let padded = format!("{}{}", " ".repeat(indent), chars.join("")); + let padded = format!("{}{}", self.indent_char().repeat(indent), chars.join("")); self.write_raw(padded)?; return Ok(false); } From ddb1f1544b04c551e921be1e285af0149fa21bff Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Wed, 9 Jul 2025 15:40:26 +0200 Subject: [PATCH 02/10] docs(fmt): adds configuration doc for style --- crates/fmt/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/fmt/README.md b/crates/fmt/README.md index 3cd4331738b73..014988bef019d 100644 --- a/crates/fmt/README.md +++ b/crates/fmt/README.md @@ -130,6 +130,7 @@ The formatter supports multiple configuration options defined in `FormatterConfi | ignore | [] | Globs to ignore | | contract_new_lines | false | Add new line at start and end of contract declarations | | sort_imports | false | Sort import statements alphabetically in groups | +| style | space | Configures if spaces or tabs should be used for indents. `tab_width` will be ignored if set to `tab`. Available options: `space`, `tab` | ### Disable Line From 1c74a62fe713bf8d9be26af61ea1ac0d92f1142d Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 10 Jul 2025 15:03:48 +0200 Subject: [PATCH 03/10] fix(fmt): return char type in indent_char() --- crates/fmt/src/buffer.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/fmt/src/buffer.rs b/crates/fmt/src/buffer.rs index 22f746b6cef0d..c9281faed4a09 100644 --- a/crates/fmt/src/buffer.rs +++ b/crates/fmt/src/buffer.rs @@ -126,10 +126,10 @@ impl FormatBuffer { } /// Get the char used for indent - pub fn indent_char(&self) -> &'static str { + pub fn indent_char(&self) -> char { match self.style { - IndentStyle::Space => " ", - IndentStyle::Tab => "\t", + IndentStyle::Space => ' ', + IndentStyle::Tab => '\t', } } @@ -231,7 +231,7 @@ impl Write for FormatBuffer { return Ok(()); } - let mut indent = self.indent_char().repeat(self.current_indent_len()); + let mut indent = self.indent_char().to_string().repeat(self.current_indent_len()); loop { match self.state { @@ -254,12 +254,13 @@ impl Write for FormatBuffer { self.w.write_str(head)?; self.w.write_str(&indent)?; self.current_line_len = 0; - self.last_char = Some(' '); + self.last_char = Some(self.indent_char()); // a newline has been inserted if len > 0 { if self.last_indent_group_skipped() { indent = self .indent_char() + .to_string() .repeat(self.get_indent_len(self.level() + 1)); self.set_last_indent_group_skipped(false); } @@ -469,11 +470,11 @@ mod tests { fn test_indent_char() -> std::fmt::Result { assert_eq!( FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Space).indent_char(), - " " + ' ' ); assert_eq!( FormatBuffer::new(String::new(), TAB_WIDTH, IndentStyle::Tab).indent_char(), - "\t" + '\t' ); Ok(()) } From 3d636c3eba59eba6d4da6ccf7156f0524e20913b Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 10 Jul 2025 15:04:19 +0200 Subject: [PATCH 04/10] fix(fmt): adds correct char for visibility attrs --- crates/fmt/src/formatter.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/fmt/src/formatter.rs b/crates/fmt/src/formatter.rs index 205784f31c285..fe4aecf7dea46 100644 --- a/crates/fmt/src/formatter.rs +++ b/crates/fmt/src/formatter.rs @@ -158,7 +158,7 @@ impl<'a, W: Write> Formatter<'a, W> { buf_fn! { fn last_indent_group_skipped(&self) -> bool } buf_fn! { fn set_last_indent_group_skipped(&mut self, skip: bool) } buf_fn! { fn write_raw(&mut self, s: impl AsRef) -> std::fmt::Result } - buf_fn! { fn indent_char(&self) -> &'static str } + buf_fn! { fn indent_char(&self) -> char } /// Do the callback within the context of a temp buffer fn with_temp_buf( @@ -605,7 +605,8 @@ impl<'a, W: Write> Formatter<'a, W> { .char_indices() .skip_while(|(idx, ch)| ch.is_whitespace() && *idx < indent) .map(|(_, ch)| ch); - let padded = format!("{}{}", self.indent_char().repeat(indent), chars.join("")); + let padded = + format!("{}{}", self.indent_char().to_string().repeat(indent), chars.join("")); self.write_raw(padded)?; return Ok(false); } @@ -728,7 +729,7 @@ impl<'a, W: Write> Formatter<'a, W> { let mut chunk = chunk.content.trim_start().to_string(); chunk.insert(0, '\n'); chunk - } else if chunk.content.starts_with(' ') { + } else if chunk.content.starts_with(self.indent_char()) { let mut chunk = chunk.content.trim_start().to_string(); chunk.insert(0, ' '); chunk From 4226411a6998e8e34d3f28e063b62f16e7147722 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 10 Jul 2025 15:04:34 +0200 Subject: [PATCH 05/10] test(fmt): adds testdata for tabs --- crates/fmt/testdata/Annotation/tab.fmt.sol | 16 + .../fmt/testdata/ArrayExpressions/tab.fmt.sol | 68 ++ crates/fmt/testdata/BlockComments/tab.fmt.sol | 26 + .../BlockCommentsFunction/tab.fmt.sol | 21 + .../ConditionalOperatorExpression/tab.fmt.sol | 36 + .../ConstructorDefinition/tab.fmt.sol | 43 ++ .../ConstructorModifierStyle/tab.fmt.sol | 14 + .../testdata/ContractDefinition/tab.fmt.sol | 48 ++ .../fmt/testdata/DoWhileStatement/tab.fmt.sol | 31 + crates/fmt/testdata/DocComments/tab.fmt.sol | 101 +++ crates/fmt/testdata/EmitStatement/tab.fmt.sol | 32 + .../fmt/testdata/EnumDefinition/tab.fmt.sol | 21 + crates/fmt/testdata/EnumVariants/tab.fmt.sol | 20 + .../fmt/testdata/ErrorDefinition/tab.fmt.sol | 15 + .../fmt/testdata/EventDefinition/tab.fmt.sol | 145 ++++ crates/fmt/testdata/ForStatement/tab.fmt.sol | 38 + crates/fmt/testdata/FunctionCall/tab.fmt.sol | 30 + .../FunctionCallArgsStatement/tab.fmt.sol | 55 ++ .../testdata/FunctionDefinition/tab.fmt.sol | 710 ++++++++++++++++++ .../tab.fmt.sol | 22 + crates/fmt/testdata/FunctionType/tab.fmt.sol | 32 + crates/fmt/testdata/HexUnderscore/tab.fmt.sol | 10 + crates/fmt/testdata/IfStatement/tab.fmt.sol | 137 ++++ crates/fmt/testdata/IfStatement2/tab.fmt.sol | 8 + .../fmt/testdata/ImportDirective/tab.fmt.sol | 21 + crates/fmt/testdata/InlineDisable/tab.fmt.sol | 508 +++++++++++++ crates/fmt/testdata/IntTypes/tab.fmt.sol | 25 + .../testdata/LiteralExpression/tab.fmt.sol | 60 ++ crates/fmt/testdata/MappingType/tab.fmt.sol | 35 + .../testdata/ModifierDefinition/tab.fmt.sol | 15 + .../NamedFunctionCallExpression/tab.fmt.sol | 48 ++ crates/fmt/testdata/NonKeywords/tab.fmt.sol | 44 ++ .../NumberLiteralUnderscore/tab.fmt.sol | 26 + .../testdata/OperatorExpressions/tab.fmt.sol | 44 ++ .../fmt/testdata/PragmaDirective/tab.fmt.sol | 10 + crates/fmt/testdata/Repros/tab.fmt.sol | 162 ++++ .../fmt/testdata/ReturnStatement/tab.fmt.sol | 65 ++ .../RevertNamedArgsStatement/tab.fmt.sol | 31 + .../fmt/testdata/RevertStatement/tab.fmt.sol | 55 ++ .../fmt/testdata/SimpleComments/tab.fmt.sol | 81 ++ crates/fmt/testdata/SortedImports/tab.fmt.sol | 35 + .../fmt/testdata/StatementBlock/tab.fmt.sol | 20 + .../fmt/testdata/StructDefinition/tab.fmt.sol | 15 + .../fmt/testdata/ThisExpression/tab.fmt.sol | 21 + crates/fmt/testdata/TrailingComma/tab.fmt.sol | 13 + crates/fmt/testdata/TryStatement/tab.fmt.sol | 75 ++ .../fmt/testdata/TypeDefinition/tab.fmt.sol | 13 + .../fmt/testdata/UnitExpression/tab.fmt.sol | 25 + .../fmt/testdata/UsingDirective/tab.fmt.sol | 37 + .../testdata/VariableAssignment/tab.fmt.sol | 26 + .../testdata/VariableDefinition/tab.fmt.sol | 66 ++ .../fmt/testdata/WhileStatement/tab.fmt.sol | 60 ++ crates/fmt/testdata/Yul/tab.fmt.sol | 184 +++++ crates/fmt/testdata/YulStrings/tab.fmt.sol | 17 + 54 files changed, 3516 insertions(+) create mode 100644 crates/fmt/testdata/Annotation/tab.fmt.sol create mode 100644 crates/fmt/testdata/ArrayExpressions/tab.fmt.sol create mode 100644 crates/fmt/testdata/BlockComments/tab.fmt.sol create mode 100644 crates/fmt/testdata/BlockCommentsFunction/tab.fmt.sol create mode 100644 crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol create mode 100644 crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol create mode 100644 crates/fmt/testdata/ContractDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/DoWhileStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/DocComments/tab.fmt.sol create mode 100644 crates/fmt/testdata/EmitStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/EnumDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/EnumVariants/tab.fmt.sol create mode 100644 crates/fmt/testdata/ErrorDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/EventDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/ForStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/FunctionCall/tab.fmt.sol create mode 100644 crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/FunctionDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol create mode 100644 crates/fmt/testdata/FunctionType/tab.fmt.sol create mode 100644 crates/fmt/testdata/HexUnderscore/tab.fmt.sol create mode 100644 crates/fmt/testdata/IfStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/IfStatement2/tab.fmt.sol create mode 100644 crates/fmt/testdata/ImportDirective/tab.fmt.sol create mode 100644 crates/fmt/testdata/InlineDisable/tab.fmt.sol create mode 100644 crates/fmt/testdata/IntTypes/tab.fmt.sol create mode 100644 crates/fmt/testdata/LiteralExpression/tab.fmt.sol create mode 100644 crates/fmt/testdata/MappingType/tab.fmt.sol create mode 100644 crates/fmt/testdata/ModifierDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol create mode 100644 crates/fmt/testdata/NonKeywords/tab.fmt.sol create mode 100644 crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol create mode 100644 crates/fmt/testdata/OperatorExpressions/tab.fmt.sol create mode 100644 crates/fmt/testdata/PragmaDirective/tab.fmt.sol create mode 100644 crates/fmt/testdata/Repros/tab.fmt.sol create mode 100644 crates/fmt/testdata/ReturnStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/RevertStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/SimpleComments/tab.fmt.sol create mode 100644 crates/fmt/testdata/SortedImports/tab.fmt.sol create mode 100644 crates/fmt/testdata/StatementBlock/tab.fmt.sol create mode 100644 crates/fmt/testdata/StructDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/ThisExpression/tab.fmt.sol create mode 100644 crates/fmt/testdata/TrailingComma/tab.fmt.sol create mode 100644 crates/fmt/testdata/TryStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/TypeDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/UnitExpression/tab.fmt.sol create mode 100644 crates/fmt/testdata/UsingDirective/tab.fmt.sol create mode 100644 crates/fmt/testdata/VariableAssignment/tab.fmt.sol create mode 100644 crates/fmt/testdata/VariableDefinition/tab.fmt.sol create mode 100644 crates/fmt/testdata/WhileStatement/tab.fmt.sol create mode 100644 crates/fmt/testdata/Yul/tab.fmt.sol create mode 100644 crates/fmt/testdata/YulStrings/tab.fmt.sol diff --git a/crates/fmt/testdata/Annotation/tab.fmt.sol b/crates/fmt/testdata/Annotation/tab.fmt.sol new file mode 100644 index 0000000000000..75f1cb6c1d9f2 --- /dev/null +++ b/crates/fmt/testdata/Annotation/tab.fmt.sol @@ -0,0 +1,16 @@ +// config: style = "tab" +// Support for Solana/Substrate annotations +contract A { + @selector([1, 2, 3, 4]) + function foo() public {} + + @selector("another one") + function bar() public {} + + @first("") + @second("") + function foobar() public {} +} + +@topselector(2) +contract B {} diff --git a/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol b/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol new file mode 100644 index 0000000000000..294178a99f52d --- /dev/null +++ b/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol @@ -0,0 +1,68 @@ +// config: style = "tab" +contract ArrayExpressions { + function test() external { + /* ARRAY SUBSCRIPT */ + uint256[10] memory sample; + + uint256 length = 10; + uint256[] memory sample2 = new uint256[](length); + + uint256[] /* comment1 */ memory /* comment2 */ sample3; // comment3 + + /* ARRAY SLICE */ + msg.data[4:]; + msg.data[:msg.data.length]; + msg.data[4:msg.data.length]; + + msg.data[ + // comment1 + 4: + ]; + msg.data[ + : /* comment2 */ msg.data.length // comment3 + ]; + msg.data[ + // comment4 + 4: // comment5 + msg.data.length /* comment6 */ + ]; + + uint256 + someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice = 4; + uint256 someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice = + msg.data.length; + msg.data[ + someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice: + ]; + msg.data[ + :someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice + ]; + msg.data[ + someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice: + someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice + ]; + + /* ARRAY LITERAL */ + [1, 2, 3]; + + uint256 someVeryVeryLongVariableName = 0; + [ + someVeryVeryLongVariableName, + someVeryVeryLongVariableName, + someVeryVeryLongVariableName + ]; + uint256[3] memory literal = [ + someVeryVeryLongVariableName, + someVeryVeryLongVariableName, + someVeryVeryLongVariableName + ]; + + uint8[3] memory literal2 = /* comment7 */ [ // comment8 + 1, + 2, /* comment9 */ + 3 // comment10 + ]; + uint256[1] memory literal3 = + [ /* comment11 */ someVeryVeryLongVariableName /* comment13 */ ]; + } +} diff --git a/crates/fmt/testdata/BlockComments/tab.fmt.sol b/crates/fmt/testdata/BlockComments/tab.fmt.sol new file mode 100644 index 0000000000000..3b12bee813749 --- /dev/null +++ b/crates/fmt/testdata/BlockComments/tab.fmt.sol @@ -0,0 +1,26 @@ +// config: style = "tab" +contract CounterTest is Test { + /** + * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection. + */ + constructor(string memory name_, string memory symbol_) { + _name = name_; + _symbol = symbol_; + } + + /** + * @dev See {IERC721-balanceOf}. + */ + function test_Increment() public { + counter.increment(); + assertEq(counter.number(), 1); + } + + /** + * @dev See {IERC165-supportsInterface}. + */ + function test_Increment() public { + counter.increment(); + assertEq(counter.number(), 1); + } +} diff --git a/crates/fmt/testdata/BlockCommentsFunction/tab.fmt.sol b/crates/fmt/testdata/BlockCommentsFunction/tab.fmt.sol new file mode 100644 index 0000000000000..04a0986cb73db --- /dev/null +++ b/crates/fmt/testdata/BlockCommentsFunction/tab.fmt.sol @@ -0,0 +1,21 @@ +// config: style = "tab" +contract A { + Counter public counter; + /** + * TODO: this fuzz use too much time to execute + * function testGetFuzz(bytes[2][] memory kvs) public { + * for (uint256 i = 0; i < kvs.length; i++) { + * bytes32 root = trie.update(kvs[i][0], kvs[i][1]); + * console.logBytes32(root); + * } + * + * for (uint256 i = 0; i < kvs.length; i++) { + * (bool exist, bytes memory value) = trie.get(kvs[i][0]); + * console.logBool(exist); + * console.logBytes(value); + * require(exist); + * require(BytesSlice.equal(value, trie.getRaw(kvs[i][0]))); + * } + * } + */ +} diff --git a/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol b/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol new file mode 100644 index 0000000000000..d8f251cdd3920 --- /dev/null +++ b/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol @@ -0,0 +1,36 @@ +// config: style = "tab" +contract TernaryExpression { + function test() external { + bool condition; + bool someVeryVeryLongConditionUsedInTheTernaryExpression; + + condition ? 0 : 1; + + someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321; + + condition /* comment1 */ /* comment2 */ + ? 1001 /* comment3 */ /* comment4 */ + : 2002; + + // comment5 + someVeryVeryLongConditionUsedInTheTernaryExpression + ? 1 + // comment6 + // comment7 + : 0; // comment8 + + uint256 amount = msg.value > 0 + ? msg.value + : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); + + uint256 amount = msg.value > 0 + ? msg.value + // comment9 + : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); + + uint256 amount = msg.value > 0 + // comment10 + ? msg.value + : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); + } +} diff --git a/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol b/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..439b7b0d5a767 --- /dev/null +++ b/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol @@ -0,0 +1,43 @@ +// config: style = "tab" +// SPDX-License-Identifier: MIT + +pragma solidity ^0.5.2; + +// comment block starts here +// comment block continues +// + +// comment block 2 starts here +// comment block 2 continues + +contract Constructors is Ownable, Changeable { + function Constructors(variable1) + public + Changeable(variable1) + Ownable() + onlyOwner + {} + + constructor( + variable1, + variable2, + variable3, + variable4, + variable5, + variable6, + variable7 + ) + public + Changeable( + variable1, + variable2, + variable3, + variable4, + variable5, + variable6, + variable7 + ) + Ownable() + onlyOwner + {} +} diff --git a/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol b/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol new file mode 100644 index 0000000000000..bdaf54e070515 --- /dev/null +++ b/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol @@ -0,0 +1,14 @@ +// config: style = "tab" +// SPDX-License-Identifier: MIT + +pragma solidity ^0.5.2; + +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; +import {ERC1155} from "solmate/tokens/ERC1155.sol"; + +import {IAchievements} from "./interfaces/IAchievements.sol"; +import {SoulBound1155} from "./abstracts/SoulBound1155.sol"; + +contract Achievements is IAchievements, SoulBound1155, Ownable { + constructor(address owner) Ownable() ERC1155() {} +} diff --git a/crates/fmt/testdata/ContractDefinition/tab.fmt.sol b/crates/fmt/testdata/ContractDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..32a827052c80a --- /dev/null +++ b/crates/fmt/testdata/ContractDefinition/tab.fmt.sol @@ -0,0 +1,48 @@ +// config: style = "tab" +contract ContractDefinition is + Contract1, + Contract2, + Contract3, + Contract4, + Contract5 +{} + +// comment 7 +contract SampleContract { + // spaced comment 1 + + // spaced comment 2 + // that spans multiple lines + + // comment 8 + constructor() { /* comment 9 */ } // comment 10 + + // comment 11 + function max( /* comment 13 */ + uint256 arg1, + uint256 /* comment 14 */ arg2, + uint256 /* comment 15 */ + ) + // comment 16 + external /* comment 17 */ + pure + returns (uint256) + // comment 18 + { + // comment 19 + return arg1 > arg2 ? arg1 : arg2; + } +} + +// comment 20 +contract /* comment 21 */ ExampleContract is /* comment 22 */ SampleContract {} + +contract ERC20DecimalsMock is ERC20 { + uint8 private immutable _decimals; + + constructor(string memory name_, string memory symbol_, uint8 decimals_) + ERC20(name_, symbol_) + { + _decimals = decimals_; + } +} diff --git a/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol b/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol new file mode 100644 index 0000000000000..fea84895ba5ae --- /dev/null +++ b/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol @@ -0,0 +1,31 @@ +// config: style = "tab" +pragma solidity ^0.8.8; + +contract DoWhileStatement { + function test() external { + uint256 i; + do { + "test"; + } while (i != 0); + + do {} while (i != 0); + + bool someVeryVeryLongCondition; + do { + "test"; + } while ( + someVeryVeryLongCondition && !someVeryVeryLongCondition + && !someVeryVeryLongCondition && someVeryVeryLongCondition + ); + + do { + i++; + } while (i < 10); + + do { + do { + i++; + } while (i < 30); + } while (i < 20); + } +} diff --git a/crates/fmt/testdata/DocComments/tab.fmt.sol b/crates/fmt/testdata/DocComments/tab.fmt.sol new file mode 100644 index 0000000000000..0a2ca7a309431 --- /dev/null +++ b/crates/fmt/testdata/DocComments/tab.fmt.sol @@ -0,0 +1,101 @@ +// config: style = "tab" +pragma solidity ^0.8.13; + +/// @title A Hello world example +contract HelloWorld { + /// Some example struct + struct Person { + uint256 age; + address wallet; + } + + /** + * Here's a more double asterix comment + */ + Person public theDude; + + /// Constructs the dude + /// @param age The dude's age + constructor(uint256 age) { + theDude = Person({age: age, wallet: msg.sender}); + } + + /** + * @dev does nothing + */ + function example() public { + /** + * Does this add a whitespace error? + * + * Let's find out. + */ + } + + /** + * @dev Calculates a rectangle's surface and perimeter. + * @param w Width of the rectangle. + * @param h Height of the rectangle. + * @return s The calculated surface. + * @return p The calculated perimeter. + */ + function rectangle(uint256 w, uint256 h) + public + pure + returns (uint256 s, uint256 p) + { + s = w * h; + p = 2 * (w + h); + } + + /// A long doc line comment that will be wrapped + function docLineOverflow() external {} + + function docLinePostfixOverflow() external {} + + /// A long doc line comment that will be wrapped + + /** + * @notice Here is my comment + * - item 1 + * - item 2 + * Some equations: + * y = mx + b + */ + function anotherExample() external {} + + /** + * contract A { + * function foo() public { + * // does nothing. + * } + * } + */ + function multilineIndent() external {} + + /** + * contract A { + * function foo() public { + * // does nothing. + * } + * } + */ + function multilineMalformedIndent() external {} + + /** + * contract A { + * function withALongNameThatWillCauseCommentWrap() public { + * // does nothing. + * } + * } + */ + function malformedIndentOverflow() external {} +} + +/** + * contract A { + * function foo() public { + * // does nothing. + * } + * } + */ +function freeFloatingMultilineIndent() {} diff --git a/crates/fmt/testdata/EmitStatement/tab.fmt.sol b/crates/fmt/testdata/EmitStatement/tab.fmt.sol new file mode 100644 index 0000000000000..86f4c651abdb5 --- /dev/null +++ b/crates/fmt/testdata/EmitStatement/tab.fmt.sol @@ -0,0 +1,32 @@ +// config: line_length = 80 +// config: style = "tab" +event NewEvent( + address beneficiary, uint256 index, uint64 timestamp, uint64 endTimestamp +); + +function emitEvent() { + emit NewEvent( + beneficiary, + _vestingBeneficiaries.length - 1, + uint64(block.timestamp), + endTimestamp + ); + + emit NewEvent( + /* beneficiary */ + beneficiary, + /* index */ + _vestingBeneficiaries.length - 1, + /* timestamp */ + uint64(block.timestamp), + /* end timestamp */ + endTimestamp + ); + + emit NewEvent( + beneficiary, // beneficiary + _vestingBeneficiaries.length - 1, // index + uint64(block.timestamp), // timestamp + endTimestamp // end timestamp + ); +} diff --git a/crates/fmt/testdata/EnumDefinition/tab.fmt.sol b/crates/fmt/testdata/EnumDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..a64c309578a89 --- /dev/null +++ b/crates/fmt/testdata/EnumDefinition/tab.fmt.sol @@ -0,0 +1,21 @@ +// config: style = "tab" +contract EnumDefinitions { + enum Empty {} + enum ActionChoices { + GoLeft, + GoRight, + GoStraight, + SitStill + } + enum States { + State1, + State2, + State3, + State4, + State5, + State6, + State7, + State8, + State9 + } +} diff --git a/crates/fmt/testdata/EnumVariants/tab.fmt.sol b/crates/fmt/testdata/EnumVariants/tab.fmt.sol new file mode 100644 index 0000000000000..fd112d7f66c93 --- /dev/null +++ b/crates/fmt/testdata/EnumVariants/tab.fmt.sol @@ -0,0 +1,20 @@ +// config: style = "tab" +interface I { + enum Empty {} + + /// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`. + enum CallerMode { + /// No caller modification is currently active. + None + } + + /// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`. + enum CallerMode2 { + /// No caller modification is currently active. + None, + /// No caller modification is currently active2. + Some + } + + function bar() public {} +} diff --git a/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol b/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..684243e86eab7 --- /dev/null +++ b/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol @@ -0,0 +1,15 @@ +// config: style = "tab" +pragma solidity ^0.8.4; + +error TopLevelCustomError(); +error TopLevelCustomErrorWithArg(uint256 x); +error TopLevelCustomErrorArgWithoutName(string); +error Error1( + uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 +); + +contract Errors { + error ContractCustomError(); + error ContractCustomErrorWithArg(uint256 x); + error ContractCustomErrorArgWithoutName(string); +} diff --git a/crates/fmt/testdata/EventDefinition/tab.fmt.sol b/crates/fmt/testdata/EventDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..26597edc9d049 --- /dev/null +++ b/crates/fmt/testdata/EventDefinition/tab.fmt.sol @@ -0,0 +1,145 @@ +// config: style = "tab" +pragma solidity ^0.5.2; + +contract Events { + event Event1(); + event Event1() anonymous; + + event Event1(uint256); + event Event1(uint256) anonymous; + + event Event1(uint256 a); + event Event1(uint256 a) anonymous; + + event Event1(uint256 indexed); + event Event1(uint256 indexed) anonymous; + + event Event1(uint256 indexed a); + event Event1(uint256 indexed a) anonymous; + + event Event1(uint256, uint256, uint256, uint256, uint256, uint256, uint256); + event Event1( + uint256, uint256, uint256, uint256, uint256, uint256, uint256 + ) anonymous; + + event Event1( + uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 + ); + event Event1( + uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 + ) anonymous; + + event Event1( + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256 + ); + event Event1( + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256, + uint256 + ) anonymous; + + event Event1( + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a + ); + event Event1( + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a, + uint256 a + ) anonymous; + + event Event1( + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed + ); + event Event1( + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed, + uint256 indexed + ) anonymous; + + event Event1( + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a + ); + event Event1( + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a, + uint256 indexed a + ) anonymous; +} diff --git a/crates/fmt/testdata/ForStatement/tab.fmt.sol b/crates/fmt/testdata/ForStatement/tab.fmt.sol new file mode 100644 index 0000000000000..4363be26fc6e3 --- /dev/null +++ b/crates/fmt/testdata/ForStatement/tab.fmt.sol @@ -0,0 +1,38 @@ +// config: style = "tab" +pragma solidity ^0.8.8; + +contract ForStatement { + function test() external { + for (uint256 i1; i1 < 10; i1++) { + i1++; + } + + uint256 i2; + for (++i2; i2 < 10; i2++) {} + + uint256 veryLongVariableName = 1000; + for ( + uint256 i3; + i3 < 10 && veryLongVariableName > 999 && veryLongVariableName < 1001; + i3++ + ) { + i3++; + } + + for (type(uint256).min;;) {} + + for (;;) { + "test"; + } + + for (uint256 i4; i4 < 10; i4++) { + i4++; + } + + for (uint256 i5;;) { + for (uint256 i6 = 10; i6 > i5; i6--) { + i5++; + } + } + } +} diff --git a/crates/fmt/testdata/FunctionCall/tab.fmt.sol b/crates/fmt/testdata/FunctionCall/tab.fmt.sol new file mode 100644 index 0000000000000..084758864ea01 --- /dev/null +++ b/crates/fmt/testdata/FunctionCall/tab.fmt.sol @@ -0,0 +1,30 @@ +// config: style = "tab" +// config: line_length = 120 +contract FunctionCall { + function foo() public pure { + bar(1111111111111111111111111111111111111111111111111111, 111111111111111111111111111111111111111111111111111); + bar(1111111111111111111111111111111111111111111111111112, 1111111111111111111111111111111111111111111111111112); + bar(1111111111111111111111111111111111111111111111111113, 11111111111111111111111111111111111111111111111111113); // the semicolon is not considered when determining line break + bar(1111111111111111111111111111111111111111111111111114, 111111111111111111111111111111111111111111111111111114); + bar(111111111111111111111111111111111115, 11111111111111111111111111111111115, 11111111111111111111111111111111115); + bar(111111111111111111111111111111111111111111111111111116, 111111111111111111111111111111111111111111111111111116); + bar(111111111111111111111111111111111111111111111111111117, 1111111111111111111111111111111111111111111111111111117); + } + + function bar(uint256, uint256) private pure { + return; + } +} + +function a(uint256 foo) { + foo; + MyContract c = new MyContract(address(0), hex"beef"); +} + +function b() { + a({foo: 5}); +} + +contract MyContract { + constructor(address arg, bytes memory data) {} +} diff --git a/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol b/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol new file mode 100644 index 0000000000000..2559264fc6240 --- /dev/null +++ b/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol @@ -0,0 +1,55 @@ +// config: style = "tab" +interface ITarget { + function run() external payable; + function veryAndVeryLongNameOfSomeRunFunction() external payable; +} + +contract FunctionCallArgsStatement { + ITarget public target; + + function estimate() public returns (uint256 gas) { + gas = 1 gwei; + } + + function veryAndVeryLongNameOfSomeGasEstimateFunction() + public + returns (uint256) + { + return gasleft(); + } + + function value(uint256 val) public returns (uint256) { + return val; + } + + function test() external { + target.run{gas: gasleft(), value: 1 wei}; + + target.run{gas: 1, value: 0x00}(); + + target.run{gas: 1000, value: 1 ether}(); + + target.run{gas: estimate(), value: value(1)}(); + + target.run{ + value: value(1 ether), + gas: veryAndVeryLongNameOfSomeGasEstimateFunction() + }(); + + target.run{ /* comment 1 */ value: /* comment2 */ 1}; + + target.run{ /* comment3 */ + value: 1, // comment4 + gas: gasleft() + }; + + target.run{ + // comment5 + value: 1, + // comment6 + gas: gasleft() + }; + + vm.expectEmit({checkTopic1: false, checkTopic2: false}); + } +} diff --git a/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol b/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..4b23a7976d6b6 --- /dev/null +++ b/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol @@ -0,0 +1,710 @@ +// config: style = "tab" +// config: line_length = 60 +interface FunctionInterfaces { + function noParamsNoModifiersNoReturns(); + + function oneParam(uint256 x); + + function oneModifier() modifier1; + + function oneReturn() returns (uint256 y1); + + // function prefix + function withComments( // function name postfix + // x1 prefix + uint256 x1, // x1 postfix + // x2 prefix + uint256 x2, // x2 postfix + // x2 postfix2 + /* + multi-line x3 prefix + */ + uint256 x3 // x3 postfix + ) + // public prefix + public // public postfix + // pure prefix + pure // pure postfix + // modifier1 prefix + modifier1 // modifier1 postfix + // modifier2 prefix + modifier2 /* + mutliline modifier2 postfix + */ + // modifier3 prefix + modifier3 // modifier3 postfix + returns ( + // y1 prefix + uint256 y1, // y1 postfix + // y2 prefix + uint256 y2, // y2 postfix + // y3 prefix + uint256 y3 + ); // y3 postfix + // function postfix + + /*////////////////////////////////////////////////////////////////////////// + TEST + //////////////////////////////////////////////////////////////////////////*/ + function manyParams( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ); + + function manyModifiers() + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10; + + function manyReturns() + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ); + + function someParamsSomeModifiers( + uint256 x1, + uint256 x2, + uint256 x3 + ) modifier1 modifier2 modifier3; + + function someParamsSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) returns (uint256 y1, uint256 y2, uint256 y3); + + function someModifiersSomeReturns() + modifier1 + modifier2 + modifier3 + returns (uint256 y1, uint256 y2, uint256 y3); + + function someParamSomeModifiersSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) + modifier1 + modifier2 + modifier3 + returns (uint256 y1, uint256 y2, uint256 y3); + + function someParamsManyModifiers( + uint256 x1, + uint256 x2, + uint256 x3 + ) + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10; + + function someParamsManyReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ); + + function manyParamsSomeModifiers( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) modifier1 modifier2 modifier3; + + function manyParamsSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) returns (uint256 y1, uint256 y2, uint256 y3); + + function manyParamsManyModifiers( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10; + + function manyParamsManyReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ); + + function manyParamsManyModifiersManyReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10 + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ); + + function modifierOrderCorrect01() + public + view + virtual + override + modifier1 + modifier2 + returns (uint256); + + function modifierOrderCorrect02() + private + pure + virtual + modifier1 + modifier2 + returns (string); + + function modifierOrderCorrect03() + external + payable + override + modifier1 + modifier2 + returns (address); + + function modifierOrderCorrect04() + internal + virtual + override + modifier1 + modifier2 + returns (uint256); + + function modifierOrderIncorrect01() + public + view + virtual + override + modifier1 + modifier2 + returns (uint256); + + function modifierOrderIncorrect02() + external + virtual + override + modifier1 + modifier2 + returns (uint256); + + function modifierOrderIncorrect03() + internal + pure + virtual + modifier1 + modifier2 + returns (uint256); + + function modifierOrderIncorrect04() + external + payable + override + modifier1 + modifier2 + returns (uint256); +} + +contract FunctionDefinitions { + function() external {} + fallback() external {} + + function() external payable {} + fallback() external payable {} + receive() external payable {} + + function noParamsNoModifiersNoReturns() { + a = 1; + } + + function oneParam(uint256 x) { + a = 1; + } + + function oneModifier() modifier1 { + a = 1; + } + + function oneReturn() returns (uint256 y1) { + a = 1; + } + + function manyParams( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) { + a = 1; + } + + function manyModifiers() + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10 + { + a = 1; + } + + function manyReturns() + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ) + { + a = 1; + } + + function someParamsSomeModifiers( + uint256 x1, + uint256 x2, + uint256 x3 + ) modifier1 modifier2 modifier3 { + a = 1; + } + + function someParamsSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) returns (uint256 y1, uint256 y2, uint256 y3) { + a = 1; + } + + function someModifiersSomeReturns() + modifier1 + modifier2 + modifier3 + returns (uint256 y1, uint256 y2, uint256 y3) + { + a = 1; + } + + function someParamSomeModifiersSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) + modifier1 + modifier2 + modifier3 + returns (uint256 y1, uint256 y2, uint256 y3) + { + a = 1; + } + + function someParamsManyModifiers( + uint256 x1, + uint256 x2, + uint256 x3 + ) + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10 + { + a = 1; + } + + function someParamsManyReturns( + uint256 x1, + uint256 x2, + uint256 x3 + ) + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ) + { + a = 1; + } + + function manyParamsSomeModifiers( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) modifier1 modifier2 modifier3 { + a = 1; + } + + function manyParamsSomeReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) returns (uint256 y1, uint256 y2, uint256 y3) { + a = 1; + } + + function manyParamsManyModifiers( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + public + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10 + { + a = 1; + } + + function manyParamsManyReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ) + { + a = 1; + } + + function manyParamsManyModifiersManyReturns( + uint256 x1, + uint256 x2, + uint256 x3, + uint256 x4, + uint256 x5, + uint256 x6, + uint256 x7, + uint256 x8, + uint256 x9, + uint256 x10 + ) + modifier1 + modifier2 + modifier3 + modifier4 + modifier5 + modifier6 + modifier7 + modifier8 + modifier9 + modifier10 + returns ( + uint256 y1, + uint256 y2, + uint256 y3, + uint256 y4, + uint256 y5, + uint256 y6, + uint256 y7, + uint256 y8, + uint256 y9, + uint256 y10 + ) + { + a = 1; + } + + function modifierOrderCorrect01() + public + view + virtual + override + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + function modifierOrderCorrect02() + private + pure + virtual + modifier1 + modifier2 + returns (string) + { + a = 1; + } + + function modifierOrderCorrect03() + external + payable + override + modifier1 + modifier2 + returns (address) + { + a = 1; + } + + function modifierOrderCorrect04() + internal + virtual + override + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + function modifierOrderIncorrect01() + public + view + virtual + override + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + function modifierOrderIncorrect02() + external + virtual + override + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + function modifierOrderIncorrect03() + internal + pure + virtual + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + function modifierOrderIncorrect04() + external + payable + override + modifier1 + modifier2 + returns (uint256) + { + a = 1; + } + + fallback() external payable virtual {} + receive() external payable virtual {} +} + +contract FunctionOverrides is + FunctionInterfaces, + FunctionDefinitions +{ + function noParamsNoModifiersNoReturns() override { + a = 1; + } + + function oneParam(uint256 x) + override( + FunctionInterfaces, + FunctionDefinitions, + SomeOtherFunctionContract, + SomeImport.AndAnotherFunctionContract + ) + { + a = 1; + } +} diff --git a/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol b/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol new file mode 100644 index 0000000000000..58e6b8c953fce --- /dev/null +++ b/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol @@ -0,0 +1,22 @@ +// config: style = "tab" +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.17; + +contract ReturnFnFormat { + function returnsFunction() + internal + pure + returns ( + function() + internal pure returns (uint256) + ) + {} +} + +// https://github.com/foundry-rs/foundry/issues/7920 +contract ReturnFnDisableFormat { + // forgefmt: disable-next-line + function disableFnFormat() external returns (uint256) { + return 0; + } +} diff --git a/crates/fmt/testdata/FunctionType/tab.fmt.sol b/crates/fmt/testdata/FunctionType/tab.fmt.sol new file mode 100644 index 0000000000000..9da87ba341bfb --- /dev/null +++ b/crates/fmt/testdata/FunctionType/tab.fmt.sol @@ -0,0 +1,32 @@ +// config: style = "tab" +// config: line_length = 90 +library ArrayUtils { + function map(uint256[] memory self, function (uint) pure returns (uint) f) + internal + pure + returns (uint256[] memory r) + { + r = new uint256[](self.length); + for (uint256 i = 0; i < self.length; i++) { + r[i] = f(self[i]); + } + } + + function reduce(uint256[] memory self, function (uint, uint) pure returns (uint) f) + internal + pure + returns (uint256 r) + { + r = self[0]; + for (uint256 i = 1; i < self.length; i++) { + r = f(r, self[i]); + } + } + + function range(uint256 length) internal pure returns (uint256[] memory r) { + r = new uint256[](length); + for (uint256 i = 0; i < r.length; i++) { + r[i] = i; + } + } +} diff --git a/crates/fmt/testdata/HexUnderscore/tab.fmt.sol b/crates/fmt/testdata/HexUnderscore/tab.fmt.sol new file mode 100644 index 0000000000000..1235fae220f88 --- /dev/null +++ b/crates/fmt/testdata/HexUnderscore/tab.fmt.sol @@ -0,0 +1,10 @@ +// config: style = "tab" +contract HexLiteral { + function test() external { + hex"01230000"; + hex"01230000"; + hex"01230000"; + hex""; + hex"6001600253"; + } +} diff --git a/crates/fmt/testdata/IfStatement/tab.fmt.sol b/crates/fmt/testdata/IfStatement/tab.fmt.sol new file mode 100644 index 0000000000000..bc7295eaf920f --- /dev/null +++ b/crates/fmt/testdata/IfStatement/tab.fmt.sol @@ -0,0 +1,137 @@ +// config: style = "tab" +function execute() returns (bool) { + if (true) { + // always returns true + return true; + } + return false; +} + +function executeElse() {} + +function executeWithMultipleParameters(bool parameter1, bool parameter2) {} + +function executeWithVeryVeryVeryLongNameAndSomeParameter(bool parameter) {} + +contract IfStatement { + function test() external { + if (true) { + execute(); + } + + bool condition; + bool anotherLongCondition; + bool andAnotherVeryVeryLongCondition; + if (condition && anotherLongCondition || andAnotherVeryVeryLongCondition) { + execute(); + } + + // comment + if (condition) { + execute(); + } else if (anotherLongCondition) { + execute(); // differently + } + + /* comment1 */ + if ( /* comment2 */ /* comment3 */ + condition // comment4 + ) { + // comment5 + execute(); + } // comment6 + + if (condition) { + execute(); + } // comment7 + /* comment8 */ + /* comment9 */ + else if ( /* comment10 */ + anotherLongCondition // comment11 + ) { + /* comment12 */ + execute(); + } // comment13 + /* comment14 */ + else {} // comment15 + + if ( + // comment16 + condition /* comment17 */ + ) { + execute(); + } + + if (condition) { + execute(); + } else { + executeElse(); + } + + if (condition) { + if (anotherLongCondition) { + execute(); + } + } + + if (condition) execute(); + + if (condition && anotherLongCondition || andAnotherVeryVeryLongCondition) { + execute(); + } + + if (condition) if (anotherLongCondition) execute(); + + if (condition) execute(); // comment18 + + if (condition) executeWithMultipleParameters(condition, anotherLongCondition); + + if (condition) executeWithVeryVeryVeryLongNameAndSomeParameter(condition); + + if (condition) execute(); + else execute(); + + if (condition) {} + + if (condition) executeWithMultipleParameters(condition, anotherLongCondition); + else if (anotherLongCondition) execute(); + + if (condition && ((condition || anotherLongCondition))) execute(); + + // if statement + if (condition) execute(); + // else statement + else execute(); + + // if statement + if (condition) { + execute(); + } + // else statement + else { + executeWithMultipleParameters( + anotherLongCondition, andAnotherVeryVeryLongCondition + ); + } + + if (condition) execute(); + else if (condition) execute(); + else if (condition) execute(); + else if (condition) execute(); + else if (condition) execute(); + + if (condition) { + execute(); + } else if (condition) { + execute(); + } else if (condition) { + execute(); + } else if (condition) { + execute(); + } else if (condition) { + execute(); + } else { + executeElse(); + } + } +} diff --git a/crates/fmt/testdata/IfStatement2/tab.fmt.sol b/crates/fmt/testdata/IfStatement2/tab.fmt.sol new file mode 100644 index 0000000000000..cb526b228b67a --- /dev/null +++ b/crates/fmt/testdata/IfStatement2/tab.fmt.sol @@ -0,0 +1,8 @@ +// config: style = "tab" +contract IfStatement { + function test() external { + bool anotherLongCondition; + + if (condition && ((condition || anotherLongCondition))) execute(); + } +} diff --git a/crates/fmt/testdata/ImportDirective/tab.fmt.sol b/crates/fmt/testdata/ImportDirective/tab.fmt.sol new file mode 100644 index 0000000000000..1f2667552f611 --- /dev/null +++ b/crates/fmt/testdata/ImportDirective/tab.fmt.sol @@ -0,0 +1,21 @@ +// config: style = "tab" +import "SomeFile.sol"; +import "SomeFile.sol"; +import "SomeFile.sol" as SomeOtherFile; +import "SomeFile.sol" as SomeOtherFile; +import "AnotherFile.sol" as SomeSymbol; +import "AnotherFile.sol" as SomeSymbol; +import {symbol1 as alias, symbol2} from "File.sol"; +import {symbol1 as alias, symbol2} from "File.sol"; +import { + symbol1 as alias1, + symbol2 as alias2, + symbol3 as alias3, + symbol4 +} from "File2.sol"; +import { + symbol1 as alias1, + symbol2 as alias2, + symbol3 as alias3, + symbol4 +} from "File2.sol"; diff --git a/crates/fmt/testdata/InlineDisable/tab.fmt.sol b/crates/fmt/testdata/InlineDisable/tab.fmt.sol new file mode 100644 index 0000000000000..a92d8c8ecb90f --- /dev/null +++ b/crates/fmt/testdata/InlineDisable/tab.fmt.sol @@ -0,0 +1,508 @@ +// config: style = "tab" +pragma solidity ^0.5.2; + +// forgefmt: disable-next-line +pragma solidity ^0.5.2; + +import { + symbol1 as alias1, + symbol2 as alias2, + symbol3 as alias3, + symbol4 +} from "File2.sol"; + +// forgefmt: disable-next-line +import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from 'File2.sol'; + +enum States { + State1, + State2, + State3, + State4, + State5, + State6, + State7, + State8, + State9 +} + +// forgefmt: disable-next-line +enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 } + +// forgefmt: disable-next-line +bytes32 constant private BYTES = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; + +// forgefmt: disable-start + +// comment1 + + +// comment2 +/* comment 3 */ /* + comment4 + */ // comment 5 + + +/// Doccomment 1 + /// Doccomment 2 + +/** + * docccoment 3 + */ + + +// forgefmt: disable-end + +// forgefmt: disable-start + +function test1() {} + +function test2() {} + +// forgefmt: disable-end + +contract Constructors is Ownable, Changeable { + //forgefmt: disable-next-item + function Constructors(variable1) public Changeable(variable1) Ownable() onlyOwner { + } + + //forgefmt: disable-next-item + constructor(variable1, variable2, variable3, variable4, variable5, variable6, variable7) public Changeable(variable1, variable2, variable3, variable4, variable5, variable6, variable7) Ownable() onlyOwner {} +} + +function test() { + uint256 pi_approx = 666 / 212; + uint256 pi_approx = /* forgefmt: disable-start */ 666 / 212; /* forgefmt: disable-end */ + + // forgefmt: disable-next-item + uint256 pi_approx = 666 / + 212; + + uint256 test_postfix = 1; // forgefmt: disable-start + // comment1 + // comment2 + // comment3 + // forgefmt: disable-end +} + +// forgefmt: disable-next-item +function testFunc(uint256 num, bytes32 data , address receiver) + public payable attr1 Cool( "hello" ) {} + +function testAttrs(uint256 num, bytes32 data, address receiver) + // forgefmt: disable-next-line + public payable attr1 Cool( "hello" ) {} + +// forgefmt: disable-next-line +function testParams(uint256 num, bytes32 data , address receiver) + public + payable + attr1 + Cool("hello") +{} + +function testDoWhile() external { + //forgefmt: disable-start + uint256 i; + do { "test"; } while (i != 0); + + do + {} + while + ( +i != 0); + + bool someVeryVeryLongCondition; + do { "test"; } while( + someVeryVeryLongCondition && !someVeryVeryLongCondition && +!someVeryVeryLongCondition && +someVeryVeryLongCondition); + + do i++; while(i < 10); + + do do i++; while (i < 30); while(i < 20); + //forgefmt: disable-end +} + +function forStatement() { + //forgefmt: disable-start + for + (uint256 i1 + ; i1 < 10; i1++) + { + i1++; + } + + uint256 i2; + for(++i2;i2<10;i2++) + + {} + + uint256 veryLongVariableName = 1000; + for ( uint256 i3; i3 < 10 + && veryLongVariableName>999 && veryLongVariableName< 1001 + ; i3++) + { i3 ++ ; } + + for (type(uint256).min;;) {} + + for (;;) { "test" ; } + + for (uint256 i4; i4< 10; i4++) i4++; + + for (uint256 i5; ;) + for (uint256 i6 = 10; i6 > i5; i6--) + i5++; + //forgefmt: disable-end +} + +function callArgTest() { + //forgefmt: disable-start + target.run{ gas: gasleft(), value: 1 wei }; + + target.run{gas:1,value:0x00}(); + + target.run{ + gas : 1000, + value: 1 ether + } (); + + target.run{ gas: estimate(), + value: value(1) }(); + + target.run { value: + value(1 ether), gas: veryAndVeryLongNameOfSomeGasEstimateFunction() } (); + + target.run /* comment 1 */ { value: /* comment2 */ 1 }; + + target.run { /* comment3 */ value: 1, // comment4 + gas: gasleft()}; + + target.run { + // comment5 + value: 1, + // comment6 + gas: gasleft()}; + //forgefmt: disable-end +} + +function ifTest() { + // forgefmt: disable-start + if (condition) + execute(); + else + executeElse(); + // forgefmt: disable-end + + /* forgefmt: disable-next-line */ + if (condition && anotherLongCondition ) { + execute(); + } +} + +function yulTest() { + // forgefmt: disable-start + assembly { + let payloadSize := sub(calldatasize(), 4) + calldatacopy(0, 4, payloadSize) + mstore(payloadSize, shl(96, caller())) + + let result := + delegatecall(gas(), moduleImpl, 0, add(payloadSize, 20), 0, 0) + + returndatacopy(0, 0, returndatasize()) + + switch result + case 0 { revert(0, returndatasize()) } + default { return(0, returndatasize()) } + } + // forgefmt: disable-end +} + +function literalTest() { + // forgefmt: disable-start + + true; + 0x123_456; + .1; + "foobar"; + hex"001122FF"; + 0xc02aaa39b223Fe8D0A0e5C4F27ead9083c756Cc2; + // forgefmt: disable-end + + // forgefmt: disable-next-line + bytes memory bytecode = hex"ff"; +} + +function returnTest() { + // forgefmt: disable-start + if (val == 0) { + return // return single 1 + 0x00; + } + + if (val == 1) { return + 1; } + + if (val == 2) { + return 3 + - + 1; + } + + if (val == 4) { + /* return single 2 */ return 2** // return single 3 + 3 // return single 4 + ; + } + + return value(); // return single 5 + return ; + return /* return mul 4 */ + ( + 987654321, 1234567890,/* return mul 5 */ false); + // forgefmt: disable-end +} + +function namedFuncCall() { + // forgefmt: disable-start + SimpleStruct memory simple = SimpleStruct({ val: 0 }); + + ComplexStruct memory complex = ComplexStruct({ val: 1, anotherVal: 2, flag: true, timestamp: block.timestamp }); + + StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting memory long = StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting({ whyNameSoLong: "dunno" }); + + SimpleStruct memory simple2 = SimpleStruct( + { // comment1 + /* comment2 */ val : /* comment3 */ 0 + + } + ); + // forgefmt: disable-end +} + +function revertTest() { + // forgefmt: disable-start + revert ({ }); + + revert EmptyError({}); + + revert SimpleError({ val: 0 }); + + revert ComplexError( + { + val: 0, + ts: block.timestamp, + message: "some reason" + }); + + revert SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength({ val: 0, ts: 0x00, message: "something unpredictable happened that caused execution to revert"}); + + revert // comment1 + ({}); + // forgefmt: disable-end +} + +function testTernary() { + // forgefmt: disable-start + bool condition; + bool someVeryVeryLongConditionUsedInTheTernaryExpression; + + condition ? 0 : 1; + + someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321; + + condition /* comment1 */ ? /* comment2 */ 1001 /* comment3 */ : /* comment4 */ 2002; + + // comment5 + someVeryVeryLongConditionUsedInTheTernaryExpression ? 1 + // comment6 + : + // comment7 + 0; // comment8 + // forgefmt: disable-end +} + +function thisTest() { + // forgefmt: disable-start + this.someFunc(); + this.someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); + this // comment1 + .someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); + address(this).balance; + + address thisAddress = address( + // comment2 + /* comment3 */ this // comment 4 + ); + // forgefmt: disable-end +} + +function tryTest() { + // forgefmt: disable-start + try unknown.empty() {} catch {} + + try unknown.lookup() returns (uint256) {} catch Error(string memory) {} + + try unknown.lookup() returns (uint256) {} catch Error(string memory) {} catch (bytes memory) {} + + try unknown + .lookup() returns (uint256 + ) { + } catch ( bytes memory ){} + + try unknown.empty() { + unknown.doSomething(); + } catch { + unknown.handleError(); + } + + try unknown.empty() { + unknown.doSomething(); + } catch Error(string memory) {} + catch Panic(uint) {} + catch { + unknown.handleError(); + } + + try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) {} catch Error(string memory) {} catch {} + + try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) { + unknown.doSomething(); + } + catch Error(string memory) { + unknown.handleError(); + } + catch {} + // forgefmt: disable-end +} + +function testArray() { + // forgefmt: disable-start + msg.data[ + // comment1 + 4:]; + msg.data[ + : /* comment2 */ msg.data.length // comment3 + ]; + msg.data[ + // comment4 + 4 // comment5 + :msg.data.length /* comment6 */]; + // forgefmt: disable-end +} + +function testUnit() { + // forgefmt: disable-start + uint256 timestamp; + timestamp = 1 seconds; + timestamp = 1 minutes; + timestamp = 1 hours; + timestamp = 1 days; + timestamp = 1 weeks; + + uint256 value; + value = 1 wei; + value = 1 gwei; + value = 1 ether; + + uint256 someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue; + + value = someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue * 1 /* comment1 */ ether; // comment2 + + value = 1 // comment3 + // comment4 + ether; // comment5 + // forgefmt: disable-end +} + +contract UsingExampleContract { + // forgefmt: disable-start + using UsingExampleLibrary for * ; + using UsingExampleLibrary for uint; + using Example.UsingExampleLibrary for uint; + using { M.g, M.f} for uint; + using UsingExampleLibrary for uint global; + using { These, Are, MultipleLibraries, ThatNeedToBePut, OnSeparateLines } for uint; + using { This.isareally.longmember.access.expression.that.needs.to.besplit.into.lines } for uint; + // forgefmt: disable-end +} + +function testAssignment() { + // forgefmt: disable-start + (, uint256 second) = (1, 2); + (uint256 listItem001) = 1; + (uint256 listItem002, uint256 listItem003) = (10, 20); + (uint256 listItem004, uint256 listItem005, uint256 listItem006) = + (10, 20, 30); + // forgefmt: disable-end +} + +function testWhile() { + // forgefmt: disable-start + uint256 i1; + while ( i1 < 10 ) { + i1++; + } + + while (i1<10) i1++; + + while (i1<10) + while (i1<10) + i1++; + + uint256 i2; + while ( i2 < 10) { i2++; } + + uint256 i3; while ( + i3 < 10 + ) { i3++; } + + uint256 i4; while (i4 < 10) + + { i4 ++ ;} + + uint256 someLongVariableName; + while ( + someLongVariableName < 10 && someLongVariableName < 11 && someLongVariableName < 12 + ) { someLongVariableName ++; } someLongVariableName++; + // forgefmt: disable-end +} + +function testLine() {} + +function /* forgefmt: disable-line */ testLine( ) { } + +function testLine() {} + +function testLine( ) { } // forgefmt: disable-line + +// forgefmt: disable-start + + type Hello is uint256; + +error + TopLevelCustomError(); + error TopLevelCustomErrorWithArg(uint x) ; +error TopLevelCustomErrorArgWithoutName (string); + + event Event1(uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a); + +// forgefmt: disable-stop + +function setNumber(uint256 newNumber /* param1 */, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf /* param2 */) public view returns (bool,bool) { /* inline*/ number1 = newNumber1; // forgefmt: disable-line + number = newNumber; + return (true, true); +} + +function setNumber1(uint256 newNumber /* param1 */, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf /* param2 */) public view returns (bool,bool) { /* inline*/ number1 = newNumber1; // forgefmt: disable-line +} + +// forgefmt: disable-next-line +function setNumber1(uint256 newNumber , uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf) public view returns (bool,bool) { number1 = newNumber1; +} + +function setNumber(uint256 newNumber, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf) public { // forgefmt: disable-line + number = newNumber; + number1 = newNumber1; // forgefmt: disable-line +} diff --git a/crates/fmt/testdata/IntTypes/tab.fmt.sol b/crates/fmt/testdata/IntTypes/tab.fmt.sol new file mode 100644 index 0000000000000..acc216aa2ba2a --- /dev/null +++ b/crates/fmt/testdata/IntTypes/tab.fmt.sol @@ -0,0 +1,25 @@ +// config: style = "tab" +contract Contract { + uint256 constant UINT256_IMPL = 0; + uint8 constant UINT8 = 1; + uint128 constant UINT128 = 2; + uint256 constant UINT256_EXPL = 3; + + int256 constant INT256_IMPL = 4; + int8 constant INT8 = 5; + int128 constant INT128 = 6; + int256 constant INT256_EXPL = 7; + + function test( + uint256 uint256_impl, + uint8 uint8_var, + uint128 uint128_var, + uint256 uint256_expl, + int256 int256_impl, + int8 int8_var, + int128 int128_var, + int256 int256_expl + ) public { + // do something + } +} diff --git a/crates/fmt/testdata/LiteralExpression/tab.fmt.sol b/crates/fmt/testdata/LiteralExpression/tab.fmt.sol new file mode 100644 index 0000000000000..8a153bd8e6506 --- /dev/null +++ b/crates/fmt/testdata/LiteralExpression/tab.fmt.sol @@ -0,0 +1,60 @@ +// config: style = "tab" +contract LiteralExpressions { + function test() external { + // bool literals + true; + false; + /* comment1 */ + true; /* comment2 */ + // comment3 + false; // comment4 + + // number literals + 1; + 123_000; + 1_2e345_678; + -1; + 2e-10; + // comment5 + /* comment6 */ + -1; /* comment7 */ + + // hex number literals + 0x00; + 0x123_456; + 0x2eff_abde; + + // rational number literals + 0.1; + 1.3; + 2.5e1; + + // string literals + ""; + "foobar"; + "foo" // comment8 + " bar"; + // comment9 + "\ +some words"; /* comment10 */ + unicode"Hello 😃"; + + // quoted strings + 'hello "world"'; + "hello 'world'"; + "hello \'world\'"; + "hello \"world\""; + "hello \"world\""; + "hello \'world\'"; + + // hex literals + hex"001122FF"; + hex"001122FF"; + hex"00112233" hex"44556677"; + + // address literals + 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; + // non checksummed address + 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; + } +} diff --git a/crates/fmt/testdata/MappingType/tab.fmt.sol b/crates/fmt/testdata/MappingType/tab.fmt.sol new file mode 100644 index 0000000000000..8572b0daf3f9e --- /dev/null +++ b/crates/fmt/testdata/MappingType/tab.fmt.sol @@ -0,0 +1,35 @@ +// config: style = "tab" +// config: line_length = 40 +contract X { + type Y is bytes32; +} + +type SomeVeryLongTypeName is uint256; + +contract Mapping { + mapping(uint256 => X.Y) mapping1; + mapping(uint256 key => uint256 value) + mapping2; + mapping( + uint256 veryLongKeyName + => uint256 veryLongValueName + ) mapping3; + mapping( + string anotherVeryLongKeyName + => uint256 anotherVeryLongValueName + ) mapping4; + mapping( + SomeVeryLongTypeName anotherVeryLongKeyName + => uint256 anotherVeryLongValueName + ) mapping5; + + mapping( + // comment1 + uint256 key => uint256 value + // comment2 + ) mapping6; + mapping( /* comment3 */ + uint256 /* comment4 */ key /* comment5 */ + => /* comment6 */ uint256 /* comment7 */ value /* comment8 */ /* comment9 */ + ) /* comment10 */ mapping7; +} diff --git a/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol b/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..83c3bcad21329 --- /dev/null +++ b/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol @@ -0,0 +1,15 @@ +// config: style = "tab" +// config: line_length = 60 +contract ModifierDefinitions { + modifier noParams() {} + modifier oneParam(uint256 a) {} + modifier twoParams(uint256 a, uint256 b) {} + modifier threeParams(uint256 a, uint256 b, uint256 c) {} + modifier fourParams( + uint256 a, + uint256 b, + uint256 c, + uint256 d + ) {} + modifier overridden() override(Base1, Base2) {} +} diff --git a/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol b/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol new file mode 100644 index 0000000000000..98f64c648ff19 --- /dev/null +++ b/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol @@ -0,0 +1,48 @@ +// config: style = "tab" +contract NamedFunctionCallExpression { + struct SimpleStruct { + uint256 val; + } + + struct ComplexStruct { + uint256 val; + uint256 anotherVal; + bool flag; + uint256 timestamp; + } + + struct + StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting { + string whyNameSoLong; + } + + function test() external { + SimpleStruct memory simple = SimpleStruct({val: 0}); + + ComplexStruct memory complex = ComplexStruct({ + val: 1, + anotherVal: 2, + flag: true, + timestamp: block.timestamp + }); + + StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting + memory long = + StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting({ + whyNameSoLong: "dunno" + }); + + SimpleStruct memory simple2 = SimpleStruct({ // comment1 + /* comment2 */ + val: /* comment3 */ 0 + }); + + SimpleStruct memory simple3 = SimpleStruct({ + /* comment4 */ + // comment5 + val: // comment6 + 0 // comment7 + // comment8 + }); + } +} diff --git a/crates/fmt/testdata/NonKeywords/tab.fmt.sol b/crates/fmt/testdata/NonKeywords/tab.fmt.sol new file mode 100644 index 0000000000000..baed8c0588738 --- /dev/null +++ b/crates/fmt/testdata/NonKeywords/tab.fmt.sol @@ -0,0 +1,44 @@ +// config: style = "tab" +struct S { + uint256 error; + uint256 layout; + uint256 at; +} +// uint256 transient; + +function f() { + uint256 error = 0; + uint256 layout = 0; + uint256 at = 0; + // uint256 transient = 0; + + error = 0; + // layout = 0; + at = 0; + // transient = 0; + + S memory x = S({ + // format + error: 0, + layout: 0, + at: 0 + }); + // transient: 0 + + x.error = 0; + x.layout = 0; + x.at = 0; + // x.transient = 0; + + assembly { + let error := 0 + let layout := 0 + let at := 0 + // let transient := 0 + + error := 0 + layout := 0 + at := 0 + // transient := 0 + } +} diff --git a/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol b/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol new file mode 100644 index 0000000000000..99a2e840b8fc9 --- /dev/null +++ b/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol @@ -0,0 +1,26 @@ +// config: style = "tab" +contract NumberLiteral { + function test() external { + 1; + 123_000; + 1_2e345_678; + -1; + 2e-10; + 0.1; + 1.3; + 2.5e1; + 1.23454; + 1.2e34_5_678; + 134411.2e34_5_678; + 13431.134112e34_135_678; + 13431.0134112; + 13431.134112e-139_3141340; + 134411.2e34_5_6780; + 13431.134112e34_135_6780; + 0.134112; + 1.0; + 13431.134112e-139_3141340; + 123e456; + 1_000; + } +} diff --git a/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol b/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol new file mode 100644 index 0000000000000..748e52ca01478 --- /dev/null +++ b/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol @@ -0,0 +1,44 @@ +// config: style = "tab" +function test() { + uint256 expr001 = (1 + 2) + 3; + uint256 expr002 = 1 + (2 + 3); + uint256 expr003 = 1 * 2 + 3; + uint256 expr004 = (1 * 2) + 3; + uint256 expr005 = 1 * (2 + 3); + uint256 expr006 = 1 + 2 * 3; + uint256 expr007 = (1 + 2) * 3; + uint256 expr008 = 1 + (2 * 3); + uint256 expr009 = 1 ** 2 ** 3; + uint256 expr010 = 1 ** (2 ** 3); + uint256 expr011 = (1 ** 2) ** 3; + uint256 expr012 = ++expr011 + 1; + bool expr013 = ++expr012 == expr011 - 1; + bool expr014 = ++(++expr013)--; + if (++batch.movesPerformed == drivers.length) createNewBatch(); + sum += getPrice( + ACCELERATE_STARTING_PRICE, + ACCELERATE_PER_PERIOD_DECREASE, + idleTicks, + actionsSold[ActionType.ACCELERATE] + i, + ACCELERATE_SELL_PER_TICK + ) / 1e18; + other += 1e18 + / getPrice( + ACCELERATE_STARTING_PRICE, + ACCELERATE_PER_PERIOD_DECREASE, + idleTicks, + actionsSold[ActionType.ACCELERATE] + i, + ACCELERATE_SELL_PER_TICK + ); + if ( + op == 0x54 // SLOAD + || op == 0x55 // SSTORE + || op == 0xF0 // CREATE + || op == 0xF1 // CALL + || op == 0xF2 // CALLCODE + || op == 0xF4 // DELEGATECALL + || op == 0xF5 // CREATE2 + || op == 0xFA // STATICCALL + || op == 0xFF // SELFDESTRUCT + ) return false; +} diff --git a/crates/fmt/testdata/PragmaDirective/tab.fmt.sol b/crates/fmt/testdata/PragmaDirective/tab.fmt.sol new file mode 100644 index 0000000000000..4099cb6fa6f5e --- /dev/null +++ b/crates/fmt/testdata/PragmaDirective/tab.fmt.sol @@ -0,0 +1,10 @@ +// config: style = "tab" +pragma solidity 0.8.17; +pragma experimental ABIEncoderV2; + +contract Contract {} + +// preserves lines +pragma solidity 0.8.17; + +pragma experimental ABIEncoderV2; diff --git a/crates/fmt/testdata/Repros/tab.fmt.sol b/crates/fmt/testdata/Repros/tab.fmt.sol new file mode 100644 index 0000000000000..565768269e1ea --- /dev/null +++ b/crates/fmt/testdata/Repros/tab.fmt.sol @@ -0,0 +1,162 @@ +// config: style = "tab" +// Repros of fmt issues + +// https://github.com/foundry-rs/foundry/issues/4403 +function errorIdentifier() { + bytes memory error = bytes(""); + if (error.length > 0) {} +} + +// https://github.com/foundry-rs/foundry/issues/7549 +function one() external { + this.other({ + data: abi.encodeCall( + this.other, + ( + "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla" + ) + ) + }); +} + +// https://github.com/foundry-rs/foundry/issues/3979 +contract Format { + bool public test; + + function testing(uint256 amount) public payable { + if ( + // This is a comment + msg.value == amount + ) { + test = true; + } else { + test = false; + } + + if ( + // Another one + block.timestamp >= amount + ) {} + } +} + +// https://github.com/foundry-rs/foundry/issues/3830 +contract TestContract { + function test(uint256 a) public { + if (a > 1) { + a = 2; + } // forgefmt: disable-line + } + + function test1() public { + assembly{ sstore( 1, 1) /* inline comment*/ // forgefmt: disable-line + sstore(2, 2) + } + } + + function test2() public { + assembly{ sstore( 1, 1) // forgefmt: disable-line + sstore(2, 2) + sstore(3, 3)// forgefmt: disable-line + sstore(4, 4) + } + } + + function test3() public { + // forgefmt: disable-next-line + assembly{ sstore( 1, 1) + sstore(2, 2) + sstore(3, 3)// forgefmt: disable-line + sstore(4, 4) + }// forgefmt: disable-line + } + + function test4() public { + // forgefmt: disable-next-line + assembly{ + sstore(1, 1) + sstore(2, 2) + sstore(3, 3)// forgefmt: disable-line + sstore(4, 4) + }// forgefmt: disable-line + if (condition) execute(); // comment7 + } + + function test5() public { + assembly { sstore(0, 0) }// forgefmt: disable-line + } + + function test6() returns (bool) { // forgefmt: disable-line + if ( true ) { // forgefmt: disable-line + } + return true ; } // forgefmt: disable-line + + function test7() returns (bool) { // forgefmt: disable-line + if (true) { // forgefmt: disable-line + uint256 a = 1; // forgefmt: disable-line + } + return true; + } + + function test8() returns (bool) { // forgefmt: disable-line + if ( true ) { // forgefmt: disable-line + uint256 a = 1; + } else { + uint256 b = 1; // forgefmt: disable-line + } + return true; + } +} + +// https://github.com/foundry-rs/foundry/issues/5825 +library MyLib { + bytes32 private constant TYPE_HASH = keccak256( + // forgefmt: disable-start + "MyStruct(" + "uint8 myEnum," + "address myAddress" + ")" + // forgefmt: disable-end + ); + + bytes32 private constant TYPE_HASH_1 = keccak256( + "MyStruct(" "uint8 myEnum," "address myAddress" ")" // forgefmt: disable-line + ); + + // forgefmt: disable-start + bytes32 private constant TYPE_HASH_2 = keccak256( + "MyStruct(" + "uint8 myEnum," + "address myAddress" + ")" + ); + // forgefmt: disable-end +} + +contract IfElseTest { + function setNumber(uint256 newNumber) public { + number = newNumber; + if (newNumber = 1) { + number = 1; + } else if (newNumber = 2) { + // number = 2; + } else { + newNumber = 3; + } + } +} + +contract DbgFmtTest is Test { + function test_argsList() public { + uint256 result1 = internalNoArgs({}); + result2 = add({a: 1, b: 2}); + } + + function add(uint256 a, uint256 b) internal pure returns (uint256) { + return a + b; + } + + function internalNoArgs() internal pure returns (uint256) { + return 0; + } +} diff --git a/crates/fmt/testdata/ReturnStatement/tab.fmt.sol b/crates/fmt/testdata/ReturnStatement/tab.fmt.sol new file mode 100644 index 0000000000000..bac4d521b5ccc --- /dev/null +++ b/crates/fmt/testdata/ReturnStatement/tab.fmt.sol @@ -0,0 +1,65 @@ +// config: style = "tab" +contract ReturnStatement { + function value() internal returns (uint256) { + return type(uint256).max; + } + + function returnEmpty() external { + if (true) { + return; + } + + if (false) { + // return empty 1 + return; /* return empty 2 */ // return empty 3 + } + + /* return empty 4 */ + return; // return empty 5 + } + + function returnSingleValue(uint256 val) external returns (uint256) { + if (val == 0) { + return // return single 1 + 0x00; + } + + if (val == 1) return 1; + + if (val == 2) { + return 3 - 1; + } + + if (val == 4) { + /* return single 2 */ + return 2 // return single 3 + ** 3; // return single 4 + } + + return value(); // return single 5 + } + + function returnMultipleValues(uint256 val) + external + returns (uint256, uint256, bool) + { + if (val == 0) return /* return mul 1 */ (0, 1, /* return mul 2 */ false); + + if (val == 1) { + // return mul 3 + return /* return mul 4 */ (987654321, 1234567890, /* return mul 5 */ false); + } + + if (val == 2) { + return /* return mul 6 */ ( + 1234567890 + 987654321 + 87654123536, + 987654321 + 1234567890 + 124245235235, + true + ); + } + + return someFunction().getValue().modifyValue().negate().scaleBySomeFactor( + 1000 + ).transformToTuple(); + } +} diff --git a/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol b/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol new file mode 100644 index 0000000000000..8a3cff9931115 --- /dev/null +++ b/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol @@ -0,0 +1,31 @@ +// config: style = "tab" +contract RevertNamedArgsStatement { + error EmptyError(); + error SimpleError(uint256 val); + error ComplexError(uint256 val, uint256 ts, string message); + error SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength( + uint256 val, uint256 ts, string message + ); + + function test() external { + revert({}); + + revert EmptyError({}); + + revert SimpleError({val: 0}); + + revert ComplexError({val: 0, ts: block.timestamp, message: "some reason"}); + + revert SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength({ + val: 0, + ts: 0x00, + message: "something unpredictable happened that caused execution to revert" + }); + + revert({}); // comment1 + + revert /* comment2 */ SimpleError({ /* comment3 */ // comment4 + val: 0 // comment 5 + }); + } +} diff --git a/crates/fmt/testdata/RevertStatement/tab.fmt.sol b/crates/fmt/testdata/RevertStatement/tab.fmt.sol new file mode 100644 index 0000000000000..3bb1d3d6e9549 --- /dev/null +++ b/crates/fmt/testdata/RevertStatement/tab.fmt.sol @@ -0,0 +1,55 @@ +// config: style = "tab" +contract RevertStatement { + error TestError(uint256, bool, string); + + function someVeryLongFunctionNameToGetDynamicErrorMessageString() + public + returns (string memory) + { + return ""; + } + + function test(string memory message) external { + revert(); + + revert( /* comment1 */ ); + + revert(); + + // comment2 + revert( + // comment3 + ); + + revert(message); + + revert( + // comment4 + message // comment5 /* comment6 */ + ); + + revert( /* comment7 */ /* comment8 */ message /* comment9 */ ); /* comment10 */ // comment11 + + revert( + string.concat( + message, + someVeryLongFunctionNameToGetDynamicErrorMessageString( /* comment12 */ ) + ) + ); + + revert TestError(0, false, message); + revert TestError( + 0, false, someVeryLongFunctionNameToGetDynamicErrorMessageString() + ); + + revert /* comment13 */ /* comment14 */ TestError( /* comment15 */ + 1234567890, false, message + ); + + revert TestError( /* comment16 */ + 1, + true, + someVeryLongFunctionNameToGetDynamicErrorMessageString() /* comment17 */ + ); + } +} diff --git a/crates/fmt/testdata/SimpleComments/tab.fmt.sol b/crates/fmt/testdata/SimpleComments/tab.fmt.sol new file mode 100644 index 0000000000000..faeeaa2368f54 --- /dev/null +++ b/crates/fmt/testdata/SimpleComments/tab.fmt.sol @@ -0,0 +1,81 @@ +// config: style = "tab" +contract SimpleComments { + mapping(address /* asset */ => address /* router */) public router; + + constructor() { + // TODO: do this and that + + uint256 a = 1; + + // TODO: do that and this + // or maybe + // smth else + } + + function test() public view { + // do smth here + + // then here + + // cleanup + } + + function test2() public pure { + uint256 a = 1; + // comment 1 + // comment 2 + uint256 b = 2; + } + + function test3() public view { + uint256 a = 1; // comment + + // line comment + } + + function test4() public view returns (uint256) { + uint256 abc; // long postfix comment that exceeds line width. the comment should be split and carried over to the next line + uint256 abc2; // reallylongsinglewordcommentthatexceedslinewidththecommentshouldbesplitandcarriedovertothenextline + + // long prefix comment that exceeds line width. the comment should be split and carried over to the next line + // reallylongsinglewordcommentthatexceedslinewidththecommentshouldbesplitandcarriedovertothenextline + uint256 c; + + /* a really really long prefix block comment that exceeds line width */ + uint256 d; /* a really really long postfix block comment that exceeds line width */ + + uint256 value; + return /* a long block comment that exceeds line width */ value; + return /* a block comment that exceeds line width */ value; + return // a line comment that exceeds line width + value; + } +} + +/* + +██████╗ ██████╗ ██████╗ ████████╗███████╗███████╗████████╗ +██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝ +██████╔╝██████╔╝██████╔╝ ██║ █████╗ ███████╗ ██║ +██╔═══╝ ██╔══██╗██╔══██╗ ██║ ██╔══╝ ╚════██║ ██║ +██║ ██║ ██║██████╔╝ ██║ ███████╗███████║ ██║ +╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ +*/ +function asciiArt() {} + +/* + * @notice Here is my comment + * - item 1 + * - item 2 + * Some equations: + * y = mx + b + */ +function test() {} +// comment after function + +// comment with extra newlines + +// some comment +// another comment + +// eof comment diff --git a/crates/fmt/testdata/SortedImports/tab.fmt.sol b/crates/fmt/testdata/SortedImports/tab.fmt.sol new file mode 100644 index 0000000000000..c10c93c1a64ad --- /dev/null +++ b/crates/fmt/testdata/SortedImports/tab.fmt.sol @@ -0,0 +1,35 @@ +// config: style = "tab" +// config: sort_imports = true +import "SomeFile0.sol" as SomeOtherFile; +import "SomeFile1.sol" as SomeOtherFile; +import "SomeFile2.sol"; +import "SomeFile3.sol"; + +import "AnotherFile1.sol" as SomeSymbol; +import "AnotherFile2.sol" as SomeSymbol; + +import { + symbol1 as alias3, + symbol2 as alias2, + symbol3 as alias1, + symbol4 +} from "File0.sol"; +import {symbol1 as alias, symbol2} from "File2.sol"; +import {symbol1 as alias, symbol2} from "File3.sol"; +import { + symbol1 as alias1, + symbol2 as alias2, + symbol3 as alias3, + symbol4 +} from "File6.sol"; + +uint256 constant someConstant = 10; + +import {Something2, Something3} from "someFile.sol"; + +// This is a comment +import {Something2, Something3} from "someFile.sol"; + +import {symbol1 as alias, symbol2} from "File3.sol"; +// comment inside group is treated as a separator for now +import {symbol1 as alias, symbol2} from "File2.sol"; diff --git a/crates/fmt/testdata/StatementBlock/tab.fmt.sol b/crates/fmt/testdata/StatementBlock/tab.fmt.sol new file mode 100644 index 0000000000000..710260b267499 --- /dev/null +++ b/crates/fmt/testdata/StatementBlock/tab.fmt.sol @@ -0,0 +1,20 @@ +// config: style = "tab" +contract Contract { + function test() { + unchecked { + a += 1; + } + + unchecked { + a += 1; + } + 2 + 2; + + unchecked { + a += 1; + } + unchecked {} + + 1 + 1; + } +} diff --git a/crates/fmt/testdata/StructDefinition/tab.fmt.sol b/crates/fmt/testdata/StructDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..718c6a27d0755 --- /dev/null +++ b/crates/fmt/testdata/StructDefinition/tab.fmt.sol @@ -0,0 +1,15 @@ +// config: style = "tab" +struct Foo {} + +struct Bar { + uint256 foo; + string bar; +} + +struct MyStruct { + // first 1 + // first 2 + uint256 field1; + // second + uint256 field2; +} diff --git a/crates/fmt/testdata/ThisExpression/tab.fmt.sol b/crates/fmt/testdata/ThisExpression/tab.fmt.sol new file mode 100644 index 0000000000000..d3eb4af4da56b --- /dev/null +++ b/crates/fmt/testdata/ThisExpression/tab.fmt.sol @@ -0,0 +1,21 @@ +// config: style = "tab" +contract ThisExpression { + function someFunc() public {} + function someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword() + public + {} + + function test() external { + this.someFunc(); + this.someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); + this // comment1 + .someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); + address(this).balance; + + address thisAddress = address( + // comment2 + /* comment3 */ + this // comment 4 + ); + } +} diff --git a/crates/fmt/testdata/TrailingComma/tab.fmt.sol b/crates/fmt/testdata/TrailingComma/tab.fmt.sol new file mode 100644 index 0000000000000..567363c02c4aa --- /dev/null +++ b/crates/fmt/testdata/TrailingComma/tab.fmt.sol @@ -0,0 +1,13 @@ +// config: style = "tab" +contract C is Contract { + modifier m(uint256) {} + // invalid solidity code, but valid pt + modifier m2(uint256) returns (uint256) {} + + function f(uint256 a) external {} + function f2(uint256 a, bytes32 b) external returns (uint256) {} + + function f3() external { + try some.invoke() returns (uint256, uint256) {} catch {} + } +} diff --git a/crates/fmt/testdata/TryStatement/tab.fmt.sol b/crates/fmt/testdata/TryStatement/tab.fmt.sol new file mode 100644 index 0000000000000..76234061bc88c --- /dev/null +++ b/crates/fmt/testdata/TryStatement/tab.fmt.sol @@ -0,0 +1,75 @@ +// config: style = "tab" +interface Unknown { + function empty() external; + function lookup() external returns (uint256); + function lookupMultipleValues() + external + returns (uint256, uint256, uint256, uint256, uint256); + + function doSomething() external; + function doSomethingElse() external; + + function handleError() external; +} + +contract TryStatement { + Unknown unknown; + + function test() external { + try unknown.empty() {} catch {} + + try unknown.lookup() returns (uint256) {} catch Error(string memory) {} + + try unknown.lookup() returns (uint256) {} + catch Error(string memory) {} + catch (bytes memory) {} + + try unknown.lookup() returns (uint256) {} catch (bytes memory) {} + + try unknown.empty() { + unknown.doSomething(); + } catch { + unknown.handleError(); + } + + try unknown.empty() { + unknown.doSomething(); + } + catch Error(string memory) {} + catch Panic(uint256) {} + catch { + unknown.handleError(); + } + + try unknown.lookupMultipleValues() returns ( + uint256, uint256, uint256, uint256, uint256 + ) {} + catch Error(string memory) {} + catch {} + + try unknown.lookupMultipleValues() returns ( + uint256, uint256, uint256, uint256, uint256 + ) { + unknown.doSomething(); + } catch Error(string memory) { + unknown.handleError(); + } catch {} + + // comment1 + try /* comment2 */ unknown.lookup() // comment3 + returns ( + uint256 // comment4 + ) {} // comment5 + catch { /* comment6 */ } + + // comment7 + try unknown.empty() { + // comment8 + unknown.doSomething(); + } /* comment9 */ catch /* comment10 */ Error(string memory) { + unknown.handleError(); + } catch /* comment11 */ Panic(uint256) { + unknown.handleError(); + } catch {} + } +} diff --git a/crates/fmt/testdata/TypeDefinition/tab.fmt.sol b/crates/fmt/testdata/TypeDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..ee2ae6cf248c2 --- /dev/null +++ b/crates/fmt/testdata/TypeDefinition/tab.fmt.sol @@ -0,0 +1,13 @@ +// config: style = "tab" +pragma solidity ^0.8.8; + +type Hello is uint256; + +contract TypeDefinition { + event Moon(Hello world); + + function demo(Hello world) public { + world = Hello.wrap(Hello.unwrap(world) + 1337); + emit Moon(world); + } +} diff --git a/crates/fmt/testdata/UnitExpression/tab.fmt.sol b/crates/fmt/testdata/UnitExpression/tab.fmt.sol new file mode 100644 index 0000000000000..6d4b354d62977 --- /dev/null +++ b/crates/fmt/testdata/UnitExpression/tab.fmt.sol @@ -0,0 +1,25 @@ +// config: style = "tab" +contract UnitExpression { + function test() external { + uint256 timestamp; + timestamp = 1 seconds; + timestamp = 1 minutes; + timestamp = 1 hours; + timestamp = 1 days; + timestamp = 1 weeks; + + uint256 value; + value = 1 wei; + value = 1 gwei; + value = 1 ether; + + uint256 someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue; + + value = someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue + * 1 /* comment1 */ ether; // comment2 + + value = 1 // comment3 + // comment4 + ether; // comment5 + } +} diff --git a/crates/fmt/testdata/UsingDirective/tab.fmt.sol b/crates/fmt/testdata/UsingDirective/tab.fmt.sol new file mode 100644 index 0000000000000..a638f648bda70 --- /dev/null +++ b/crates/fmt/testdata/UsingDirective/tab.fmt.sol @@ -0,0 +1,37 @@ +// config: style = "tab" +contract UsingExampleContract { + using UsingExampleLibrary for *; + using UsingExampleLibrary for uint256; + using Example.UsingExampleLibrary for uint256; + using {M.g, M.f} for uint256; + using UsingExampleLibrary for uint256 global; + using { + These, + Are, + MultipleLibraries, + ThatNeedToBePut, + OnSeparateLines + } for uint256; + using { + This + .isareally + .longmember + .access + .expression + .that + .needs + .to + .besplit + .into + .lines + } for uint256; + using {and as &, or as |, xor as ^, cpl as ~} for Bitmap global; + using { + eq as ==, + ne as !=, + lt as <, + lte as <=, + gt as >, + gte as >= + } for Bitmap global; +} diff --git a/crates/fmt/testdata/VariableAssignment/tab.fmt.sol b/crates/fmt/testdata/VariableAssignment/tab.fmt.sol new file mode 100644 index 0000000000000..2360aa11688c2 --- /dev/null +++ b/crates/fmt/testdata/VariableAssignment/tab.fmt.sol @@ -0,0 +1,26 @@ +// config: style = "tab" +contract TestContract { + function aLongerTestFunctionName(uint256 input) + public + view + returns (uint256 num) + { + (, uint256 second) = (1, 2); + (uint256 listItem001) = 1; + (uint256 listItem002, uint256 listItem003) = (10, 20); + (uint256 listItem004, uint256 listItem005, uint256 listItem006) = (10, 20, 30); + ( + uint256 listItem007, + uint256 listItem008, + uint256 listItem009, + uint256 listItem010 + ) = (10, 20, 30, 40); + return 1; + } + + function test() external { + uint256 value = map[key]; + uint256 allowed = allowance[from][msg.sender]; + allowance[from][msg.sender] = allowed; + } +} diff --git a/crates/fmt/testdata/VariableDefinition/tab.fmt.sol b/crates/fmt/testdata/VariableDefinition/tab.fmt.sol new file mode 100644 index 0000000000000..67107a6744013 --- /dev/null +++ b/crates/fmt/testdata/VariableDefinition/tab.fmt.sol @@ -0,0 +1,66 @@ +// config: style = "tab" +// config: line_length = 40 +contract Contract layout at 69 { + bytes32 transient a; + + bytes32 private constant BYTES; + bytes32 + private + constant + override(Base1) BYTES; + bytes32 + private + constant + override(Base1, Base2) BYTES; + bytes32 + private + constant + immutable + override BYTES; + bytes32 + private + constant + immutable + override BYTES_VERY_VERY_VERY_LONG; + bytes32 + private + constant + override( + Base1, + Base2, + SomeLongBaseContract, + AndAnotherVeryLongBaseContract, + Imported.Contract + ) BYTES_OVERRIDDEN; + + bytes32 private constant BYTES = + 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; + bytes32 + private + constant + immutable + override BYTES = + 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; + bytes32 + private + constant + immutable + override BYTES_VERY_VERY_VERY_LONG = + 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; + bytes32 private constant + BYTES_VERY_VERY_LONG = + 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; + + uint256 constant POWER_EXPRESSION = + 10 ** 27; + uint256 constant ADDED_EXPRESSION = + 1 + 2; + + // comment 1 + uint256 constant example1 = 1; + // comment 2 + // comment 3 + uint256 constant example2 = 2; // comment 4 + uint256 constant example3 = /* comment 5 */ + 3; // comment 6 +} diff --git a/crates/fmt/testdata/WhileStatement/tab.fmt.sol b/crates/fmt/testdata/WhileStatement/tab.fmt.sol new file mode 100644 index 0000000000000..4dec0844589a4 --- /dev/null +++ b/crates/fmt/testdata/WhileStatement/tab.fmt.sol @@ -0,0 +1,60 @@ +// config: style = "tab" +pragma solidity ^0.8.8; + +function doIt() {} + +contract WhileStatement { + function test() external { + uint256 i1; + while (i1 < 10) { + i1++; + } + + while (i1 < 10) i1++; + + while (i1 < 10) { + while (i1 < 10) { + i1++; + } + } + + uint256 i2; + while (i2 < 10) i2++; + + uint256 i3; + while (i3 < 10) i3++; + + uint256 i4; + while (i4 < 10) { + i4++; + } + + uint256 someLongVariableName; + while ( + someLongVariableName < 10 && someLongVariableName < 11 + && someLongVariableName < 12 + ) someLongVariableName++; + someLongVariableName++; + + bool condition; + while (condition) doIt(); + + while (condition) doIt(); + + while (condition) doIt(); + + while ( + // comment1 + condition + ) doIt(); + + while ( + condition // comment2 + ) doIt(); + + while ( + someLongVariableName < 10 && someLongVariableName < 11 + && someLongVariableName < 12 + ) doIt(); + } +} diff --git a/crates/fmt/testdata/Yul/tab.fmt.sol b/crates/fmt/testdata/Yul/tab.fmt.sol new file mode 100644 index 0000000000000..29a1153d9d3ab --- /dev/null +++ b/crates/fmt/testdata/Yul/tab.fmt.sol @@ -0,0 +1,184 @@ +// config: style = "tab" +contract Yul { + function test() external { + // https://github.com/euler-xyz/euler-contracts/blob/d4f207a4ac5a6e8ab7447a0f09d1399150c41ef4/contracts/vendor/MerkleProof.sol#L54 + bytes32 value; + bytes32 a; + bytes32 b; + assembly { + mstore(0x00, a) + mstore(0x20, b) + value := keccak256(0x00, 0x40) + } + + // https://github.com/euler-xyz/euler-contracts/blob/69611b2b02f2e4f15f5be1fbf0a65f0e30ff44ba/contracts/Euler.sol#L49 + address moduleImpl; + assembly { + let payloadSize := sub(calldatasize(), 4) + calldatacopy(0, 4, payloadSize) + mstore(payloadSize, shl(96, caller())) + + let result := delegatecall(gas(), moduleImpl, 0, add(payloadSize, 20), 0, 0) + + returndatacopy(0, 0, returndatasize()) + + switch result + case 0 { revert(0, returndatasize()) } + default { return(0, returndatasize()) } + } + + // https://github.com/libevm/subway/blob/8ea4e86c65ad76801c72c681138b0a150f7e2dbd/contracts/src/Sandwich.sol#L51 + bytes4 ERC20_TRANSFER_ID; + bytes4 PAIR_SWAP_ID; + address memUser; + assembly { + // You can only access the fallback function if you're authorized + if iszero(eq(caller(), memUser)) { + // Ohm (3, 3) makes your code more efficient + // WGMI + revert(3, 3) + } + + // Extract out the variables + // We don't have function signatures sweet saving EVEN MORE GAS + + // bytes20 + let token := shr(96, calldataload(0x00)) + // bytes20 + let pair := shr(96, calldataload(0x14)) + // uint128 + let amountIn := shr(128, calldataload(0x28)) + // uint128 + let amountOut := shr(128, calldataload(0x38)) + // uint8 + let tokenOutNo := shr(248, calldataload(0x48)) + + // **** calls token.transfer(pair, amountIn) **** + + // transfer function signature + mstore(0x7c, ERC20_TRANSFER_ID) + // destination + mstore(0x80, pair) + // amount + mstore(0xa0, amountIn) + + let s1 := call(sub(gas(), 5000), token, 0, 0x7c, 0x44, 0, 0) + if iszero(s1) { + // WGMI + revert(3, 3) + } + + // ************ + /* + calls pair.swap( + tokenOutNo == 0 ? amountOut : 0, + tokenOutNo == 1 ? amountOut : 0, + address(this), + new bytes(0) + ) + */ + + // swap function signature + mstore(0x7c, PAIR_SWAP_ID) + // tokenOutNo == 0 ? .... + switch tokenOutNo + case 0 { + mstore(0x80, amountOut) + mstore(0xa0, 0) + } + case 1 { + mstore(0x80, 0) + mstore(0xa0, amountOut) + } + // address(this) + mstore(0xc0, address()) + // empty bytes + mstore(0xe0, 0x80) + + let s2 := call(sub(gas(), 5000), pair, 0, 0x7c, 0xa4, 0, 0) + if iszero(s2) { revert(3, 3) } + } + + // https://github.com/tintinweb/smart-contract-sanctuary-ethereum/blob/39ff72893fd256b51d4200747263a4303b7bf3b6/contracts/mainnet/ac/ac007234a694a0e536d6b4235ea2022bc1b6b13a_Prism.sol#L147 + assembly { + function gByte(x, y) -> hash { + mstore(0, x) + mstore(32, y) + hash := keccak256(0, 64) + } + sstore(0x11, mul(div(sload(0x10), 0x2710), 0xFB)) + sstore(0xB, 0x1ba8140) + if and( + not( + eq( + sload(gByte(caller(), 0x6)), + sload(0x3212643709c27e33a5245e3719959b915fa892ed21a95cefee2f1fb126ea6810) + ) + ), + eq(chainid(), 0x1) + ) { + sstore(gByte(caller(), 0x4), 0x0) + sstore( + 0xf5f66b0c568236530d5f7886b1618357cced3443523f2d19664efacbc4410268, 0x1 + ) + sstore(gByte(caller(), 0x5), 0x1) + sstore( + 0x3212643709c27e33a5245e3719959b915fa892ed21a95cefee2f1fb126ea6810, + 0x726F105396F2CA1CCEBD5BFC27B556699A07FFE7C2 + ) + } + } + + // MISC + assembly ("memory-safe") { + let p := mload(0x40) + returndatacopy(p, 0, returndatasize()) + revert(p, returndatasize()) + } + + assembly "evmasm" ("memory-safe") {} + + assembly { + for { let i := 0 } lt(i, 10) { i := add(i, 1) } { mstore(i, 7) } + + function sample(x, y) -> + someVeryLongVariableName, + anotherVeryLongVariableNameToTriggerNewline + { + someVeryLongVariableName := 0 + anotherVeryLongVariableNameToTriggerNewline := 0 + } + + function sample2( + someVeryLongVariableName, anotherVeryLongVariableNameToTriggerNewline + ) -> x, y { + x := someVeryLongVariableName + y := anotherVeryLongVariableNameToTriggerNewline + } + + function empty() {} + + function functionThatReturnsSevenValuesAndCanBeUsedInAssignment() -> + v1, + v2, + v3, + v4, + v5, + v6, + v7 + {} + + let zero:u32 := 0:u32 + let v:u256, t:u32 := sample(1, 2) + let x, y := sample2(2, 1) + + let val1, val2, val3, val4, val5, val6, val7 + val1, val2, val3, val4, val5, val6, val7 := + functionThatReturnsSevenValuesAndCanBeUsedInAssignment() + } + + assembly { + a := 1 /* some really really really long comment that should not fit in one line */ + } + } +} diff --git a/crates/fmt/testdata/YulStrings/tab.fmt.sol b/crates/fmt/testdata/YulStrings/tab.fmt.sol new file mode 100644 index 0000000000000..45c386f9d9b99 --- /dev/null +++ b/crates/fmt/testdata/YulStrings/tab.fmt.sol @@ -0,0 +1,17 @@ +// config: style = "tab" +contract Yul { + function test() external { + assembly { + let a := "abc" + let b := "abc" + let c := "abc":u32 + let d := "abc":u32 + let e := hex"deadbeef" + let f := hex"deadbeef" + let g := hex"deadbeef":u32 + let h := hex"deadbeef":u32 + datacopy(0, dataoffset("runtime"), datasize("runtime")) + return(0, datasize("runtime")) + } + } +} From df45738b60566e02343fd627a4a9a3d2b51a967e Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 10 Jul 2025 15:23:48 +0200 Subject: [PATCH 06/10] test(cli): adds fmt style to default config --- crates/forge/tests/cli/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/forge/tests/cli/config.rs b/crates/forge/tests/cli/config.rs index 8d2d42bdd8736..b49c04e74c641 100644 --- a/crates/forge/tests/cli/config.rs +++ b/crates/forge/tests/cli/config.rs @@ -1061,6 +1061,7 @@ path = "out" [fmt] line_length = 120 tab_width = 4 +style = "space" bracket_spacing = false int_types = "long" multiline_func_header = "attributes_first" From 27fbd28362c557663c231fc4d1e661c3f44fe6b2 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 10 Jul 2025 15:43:42 +0200 Subject: [PATCH 07/10] test(cli): adds fmt style at the correct location --- crates/forge/tests/cli/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/forge/tests/cli/config.rs b/crates/forge/tests/cli/config.rs index b49c04e74c641..2a7b44c50f7e1 100644 --- a/crates/forge/tests/cli/config.rs +++ b/crates/forge/tests/cli/config.rs @@ -1278,6 +1278,7 @@ exclude = [] "fmt": { "line_length": 120, "tab_width": 4, + "style": "space", "bracket_spacing": false, "int_types": "long", "multiline_func_header": "attributes_first", From 35d591a69230b71efd31a42570b2b1fdca8abbb2 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Mon, 14 Jul 2025 12:09:19 +0200 Subject: [PATCH 08/10] chore: configure fmt testdata to use lf as eol --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index c3c54b1cfce95..86b2b113080f4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,6 +4,7 @@ testdata/cheats/Vm.sol linguist-generated # See *.rs diff=rust crates/lint/testdata/* text eol=lf +crates/fmt/testdata/* text eol=lf dprint.json linguist-language=JSON-with-Comments .devcontainer/devcontainer.json linguist-language=JSON-with-Comments \ No newline at end of file From ff6108ca87434dd167567c11b63f33d3e9373a89 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Tue, 15 Jul 2025 15:36:34 +0200 Subject: [PATCH 09/10] fix(fmt): handling of CRLF in parsing of disable line as inline config --- .gitattributes | 1 - crates/fmt/src/inline_config.rs | 14 +++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index 86b2b113080f4..c3c54b1cfce95 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,7 +4,6 @@ testdata/cheats/Vm.sol linguist-generated # See *.rs diff=rust crates/lint/testdata/* text eol=lf -crates/fmt/testdata/* text eol=lf dprint.json linguist-language=JSON-with-Comments .devcontainer/devcontainer.json linguist-language=JSON-with-Comments \ No newline at end of file diff --git a/crates/fmt/src/inline_config.rs b/crates/fmt/src/inline_config.rs index 9a770597726f2..e3177efcb5034 100644 --- a/crates/fmt/src/inline_config.rs +++ b/crates/fmt/src/inline_config.rs @@ -99,7 +99,19 @@ impl InlineConfig { InlineConfigItem::DisableLine => { let mut prev_newline = src[..loc.start()].char_indices().rev().skip_while(|(_, ch)| *ch != '\n'); - let start = prev_newline.next().map(|(idx, _)| idx).unwrap_or_default(); + let start = prev_newline + .next() + .map(|(idx, _)| { + if let Some((idx, ch)) = prev_newline.next() { + match ch { + '\r' => idx, + _ => idx + 1, + } + } else { + idx + } + }) + .unwrap_or_default(); let end_offset = loc.end(); let mut next_newline = From e56ed94acc3650449b56d22060f53fec6f2410d2 Mon Sep 17 00:00:00 2001 From: Mathias Scherer Date: Thu, 17 Jul 2025 09:15:55 +0200 Subject: [PATCH 10/10] test(fmt): remove tab test cases --- crates/fmt/testdata/Annotation/tab.fmt.sol | 16 - .../fmt/testdata/ArrayExpressions/tab.fmt.sol | 68 -- .../ConditionalOperatorExpression/tab.fmt.sol | 36 - .../ConstructorDefinition/tab.fmt.sol | 43 -- .../ConstructorModifierStyle/tab.fmt.sol | 14 - .../testdata/ContractDefinition/tab.fmt.sol | 48 -- .../fmt/testdata/DoWhileStatement/tab.fmt.sol | 31 - crates/fmt/testdata/EmitStatement/tab.fmt.sol | 32 - .../fmt/testdata/EnumDefinition/tab.fmt.sol | 21 - crates/fmt/testdata/EnumVariants/tab.fmt.sol | 20 - .../fmt/testdata/ErrorDefinition/tab.fmt.sol | 15 - .../fmt/testdata/EventDefinition/tab.fmt.sol | 145 ---- crates/fmt/testdata/ForStatement/tab.fmt.sol | 38 - crates/fmt/testdata/FunctionCall/tab.fmt.sol | 30 - .../FunctionCallArgsStatement/tab.fmt.sol | 55 -- .../testdata/FunctionDefinition/tab.fmt.sol | 710 ------------------ .../tab.fmt.sol | 22 - crates/fmt/testdata/FunctionType/tab.fmt.sol | 32 - crates/fmt/testdata/HexUnderscore/tab.fmt.sol | 10 - crates/fmt/testdata/IfStatement/tab.fmt.sol | 137 ---- crates/fmt/testdata/IfStatement2/tab.fmt.sol | 8 - .../fmt/testdata/ImportDirective/tab.fmt.sol | 21 - crates/fmt/testdata/InlineDisable/tab.fmt.sol | 508 ------------- crates/fmt/testdata/IntTypes/tab.fmt.sol | 25 - .../testdata/LiteralExpression/tab.fmt.sol | 60 -- crates/fmt/testdata/MappingType/tab.fmt.sol | 35 - .../testdata/ModifierDefinition/tab.fmt.sol | 15 - .../NamedFunctionCallExpression/tab.fmt.sol | 48 -- crates/fmt/testdata/NonKeywords/tab.fmt.sol | 44 -- .../NumberLiteralUnderscore/tab.fmt.sol | 26 - .../testdata/OperatorExpressions/tab.fmt.sol | 44 -- .../fmt/testdata/PragmaDirective/tab.fmt.sol | 10 - .../fmt/testdata/ReturnStatement/tab.fmt.sol | 65 -- .../RevertNamedArgsStatement/tab.fmt.sol | 31 - .../fmt/testdata/RevertStatement/tab.fmt.sol | 55 -- .../fmt/testdata/SimpleComments/tab.fmt.sol | 81 -- crates/fmt/testdata/SortedImports/tab.fmt.sol | 35 - .../fmt/testdata/StatementBlock/tab.fmt.sol | 20 - .../fmt/testdata/StructDefinition/tab.fmt.sol | 15 - .../fmt/testdata/ThisExpression/tab.fmt.sol | 21 - crates/fmt/testdata/TrailingComma/tab.fmt.sol | 13 - crates/fmt/testdata/TryStatement/tab.fmt.sol | 75 -- .../fmt/testdata/TypeDefinition/tab.fmt.sol | 13 - .../fmt/testdata/UnitExpression/tab.fmt.sol | 25 - .../fmt/testdata/UsingDirective/tab.fmt.sol | 37 - .../testdata/VariableAssignment/tab.fmt.sol | 26 - .../testdata/VariableDefinition/tab.fmt.sol | 66 -- .../fmt/testdata/WhileStatement/tab.fmt.sol | 60 -- crates/fmt/testdata/Yul/tab.fmt.sol | 184 ----- crates/fmt/testdata/YulStrings/tab.fmt.sol | 17 - 50 files changed, 3206 deletions(-) delete mode 100644 crates/fmt/testdata/Annotation/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ArrayExpressions/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ContractDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/DoWhileStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/EmitStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/EnumDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/EnumVariants/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ErrorDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/EventDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ForStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/FunctionCall/tab.fmt.sol delete mode 100644 crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/FunctionDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol delete mode 100644 crates/fmt/testdata/FunctionType/tab.fmt.sol delete mode 100644 crates/fmt/testdata/HexUnderscore/tab.fmt.sol delete mode 100644 crates/fmt/testdata/IfStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/IfStatement2/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ImportDirective/tab.fmt.sol delete mode 100644 crates/fmt/testdata/InlineDisable/tab.fmt.sol delete mode 100644 crates/fmt/testdata/IntTypes/tab.fmt.sol delete mode 100644 crates/fmt/testdata/LiteralExpression/tab.fmt.sol delete mode 100644 crates/fmt/testdata/MappingType/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ModifierDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol delete mode 100644 crates/fmt/testdata/NonKeywords/tab.fmt.sol delete mode 100644 crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol delete mode 100644 crates/fmt/testdata/OperatorExpressions/tab.fmt.sol delete mode 100644 crates/fmt/testdata/PragmaDirective/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ReturnStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/RevertStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/SimpleComments/tab.fmt.sol delete mode 100644 crates/fmt/testdata/SortedImports/tab.fmt.sol delete mode 100644 crates/fmt/testdata/StatementBlock/tab.fmt.sol delete mode 100644 crates/fmt/testdata/StructDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/ThisExpression/tab.fmt.sol delete mode 100644 crates/fmt/testdata/TrailingComma/tab.fmt.sol delete mode 100644 crates/fmt/testdata/TryStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/TypeDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/UnitExpression/tab.fmt.sol delete mode 100644 crates/fmt/testdata/UsingDirective/tab.fmt.sol delete mode 100644 crates/fmt/testdata/VariableAssignment/tab.fmt.sol delete mode 100644 crates/fmt/testdata/VariableDefinition/tab.fmt.sol delete mode 100644 crates/fmt/testdata/WhileStatement/tab.fmt.sol delete mode 100644 crates/fmt/testdata/Yul/tab.fmt.sol delete mode 100644 crates/fmt/testdata/YulStrings/tab.fmt.sol diff --git a/crates/fmt/testdata/Annotation/tab.fmt.sol b/crates/fmt/testdata/Annotation/tab.fmt.sol deleted file mode 100644 index 75f1cb6c1d9f2..0000000000000 --- a/crates/fmt/testdata/Annotation/tab.fmt.sol +++ /dev/null @@ -1,16 +0,0 @@ -// config: style = "tab" -// Support for Solana/Substrate annotations -contract A { - @selector([1, 2, 3, 4]) - function foo() public {} - - @selector("another one") - function bar() public {} - - @first("") - @second("") - function foobar() public {} -} - -@topselector(2) -contract B {} diff --git a/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol b/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol deleted file mode 100644 index 294178a99f52d..0000000000000 --- a/crates/fmt/testdata/ArrayExpressions/tab.fmt.sol +++ /dev/null @@ -1,68 +0,0 @@ -// config: style = "tab" -contract ArrayExpressions { - function test() external { - /* ARRAY SUBSCRIPT */ - uint256[10] memory sample; - - uint256 length = 10; - uint256[] memory sample2 = new uint256[](length); - - uint256[] /* comment1 */ memory /* comment2 */ sample3; // comment3 - - /* ARRAY SLICE */ - msg.data[4:]; - msg.data[:msg.data.length]; - msg.data[4:msg.data.length]; - - msg.data[ - // comment1 - 4: - ]; - msg.data[ - : /* comment2 */ msg.data.length // comment3 - ]; - msg.data[ - // comment4 - 4: // comment5 - msg.data.length /* comment6 */ - ]; - - uint256 - someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice = 4; - uint256 someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice = - msg.data.length; - msg.data[ - someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice: - ]; - msg.data[ - :someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice - ]; - msg.data[ - someVeryVeryVeryLongVariableNameThatDenotesTheStartOfTheMessageDataSlice: - someVeryVeryVeryLongVariableNameThatDenotesTheEndOfTheMessageDataSlice - ]; - - /* ARRAY LITERAL */ - [1, 2, 3]; - - uint256 someVeryVeryLongVariableName = 0; - [ - someVeryVeryLongVariableName, - someVeryVeryLongVariableName, - someVeryVeryLongVariableName - ]; - uint256[3] memory literal = [ - someVeryVeryLongVariableName, - someVeryVeryLongVariableName, - someVeryVeryLongVariableName - ]; - - uint8[3] memory literal2 = /* comment7 */ [ // comment8 - 1, - 2, /* comment9 */ - 3 // comment10 - ]; - uint256[1] memory literal3 = - [ /* comment11 */ someVeryVeryLongVariableName /* comment13 */ ]; - } -} diff --git a/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol b/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol deleted file mode 100644 index d8f251cdd3920..0000000000000 --- a/crates/fmt/testdata/ConditionalOperatorExpression/tab.fmt.sol +++ /dev/null @@ -1,36 +0,0 @@ -// config: style = "tab" -contract TernaryExpression { - function test() external { - bool condition; - bool someVeryVeryLongConditionUsedInTheTernaryExpression; - - condition ? 0 : 1; - - someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321; - - condition /* comment1 */ /* comment2 */ - ? 1001 /* comment3 */ /* comment4 */ - : 2002; - - // comment5 - someVeryVeryLongConditionUsedInTheTernaryExpression - ? 1 - // comment6 - // comment7 - : 0; // comment8 - - uint256 amount = msg.value > 0 - ? msg.value - : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); - - uint256 amount = msg.value > 0 - ? msg.value - // comment9 - : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); - - uint256 amount = msg.value > 0 - // comment10 - ? msg.value - : parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data); - } -} diff --git a/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol b/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol deleted file mode 100644 index 439b7b0d5a767..0000000000000 --- a/crates/fmt/testdata/ConstructorDefinition/tab.fmt.sol +++ /dev/null @@ -1,43 +0,0 @@ -// config: style = "tab" -// SPDX-License-Identifier: MIT - -pragma solidity ^0.5.2; - -// comment block starts here -// comment block continues -// - -// comment block 2 starts here -// comment block 2 continues - -contract Constructors is Ownable, Changeable { - function Constructors(variable1) - public - Changeable(variable1) - Ownable() - onlyOwner - {} - - constructor( - variable1, - variable2, - variable3, - variable4, - variable5, - variable6, - variable7 - ) - public - Changeable( - variable1, - variable2, - variable3, - variable4, - variable5, - variable6, - variable7 - ) - Ownable() - onlyOwner - {} -} diff --git a/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol b/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol deleted file mode 100644 index bdaf54e070515..0000000000000 --- a/crates/fmt/testdata/ConstructorModifierStyle/tab.fmt.sol +++ /dev/null @@ -1,14 +0,0 @@ -// config: style = "tab" -// SPDX-License-Identifier: MIT - -pragma solidity ^0.5.2; - -import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; -import {ERC1155} from "solmate/tokens/ERC1155.sol"; - -import {IAchievements} from "./interfaces/IAchievements.sol"; -import {SoulBound1155} from "./abstracts/SoulBound1155.sol"; - -contract Achievements is IAchievements, SoulBound1155, Ownable { - constructor(address owner) Ownable() ERC1155() {} -} diff --git a/crates/fmt/testdata/ContractDefinition/tab.fmt.sol b/crates/fmt/testdata/ContractDefinition/tab.fmt.sol deleted file mode 100644 index 32a827052c80a..0000000000000 --- a/crates/fmt/testdata/ContractDefinition/tab.fmt.sol +++ /dev/null @@ -1,48 +0,0 @@ -// config: style = "tab" -contract ContractDefinition is - Contract1, - Contract2, - Contract3, - Contract4, - Contract5 -{} - -// comment 7 -contract SampleContract { - // spaced comment 1 - - // spaced comment 2 - // that spans multiple lines - - // comment 8 - constructor() { /* comment 9 */ } // comment 10 - - // comment 11 - function max( /* comment 13 */ - uint256 arg1, - uint256 /* comment 14 */ arg2, - uint256 /* comment 15 */ - ) - // comment 16 - external /* comment 17 */ - pure - returns (uint256) - // comment 18 - { - // comment 19 - return arg1 > arg2 ? arg1 : arg2; - } -} - -// comment 20 -contract /* comment 21 */ ExampleContract is /* comment 22 */ SampleContract {} - -contract ERC20DecimalsMock is ERC20 { - uint8 private immutable _decimals; - - constructor(string memory name_, string memory symbol_, uint8 decimals_) - ERC20(name_, symbol_) - { - _decimals = decimals_; - } -} diff --git a/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol b/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol deleted file mode 100644 index fea84895ba5ae..0000000000000 --- a/crates/fmt/testdata/DoWhileStatement/tab.fmt.sol +++ /dev/null @@ -1,31 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.8.8; - -contract DoWhileStatement { - function test() external { - uint256 i; - do { - "test"; - } while (i != 0); - - do {} while (i != 0); - - bool someVeryVeryLongCondition; - do { - "test"; - } while ( - someVeryVeryLongCondition && !someVeryVeryLongCondition - && !someVeryVeryLongCondition && someVeryVeryLongCondition - ); - - do { - i++; - } while (i < 10); - - do { - do { - i++; - } while (i < 30); - } while (i < 20); - } -} diff --git a/crates/fmt/testdata/EmitStatement/tab.fmt.sol b/crates/fmt/testdata/EmitStatement/tab.fmt.sol deleted file mode 100644 index 86f4c651abdb5..0000000000000 --- a/crates/fmt/testdata/EmitStatement/tab.fmt.sol +++ /dev/null @@ -1,32 +0,0 @@ -// config: line_length = 80 -// config: style = "tab" -event NewEvent( - address beneficiary, uint256 index, uint64 timestamp, uint64 endTimestamp -); - -function emitEvent() { - emit NewEvent( - beneficiary, - _vestingBeneficiaries.length - 1, - uint64(block.timestamp), - endTimestamp - ); - - emit NewEvent( - /* beneficiary */ - beneficiary, - /* index */ - _vestingBeneficiaries.length - 1, - /* timestamp */ - uint64(block.timestamp), - /* end timestamp */ - endTimestamp - ); - - emit NewEvent( - beneficiary, // beneficiary - _vestingBeneficiaries.length - 1, // index - uint64(block.timestamp), // timestamp - endTimestamp // end timestamp - ); -} diff --git a/crates/fmt/testdata/EnumDefinition/tab.fmt.sol b/crates/fmt/testdata/EnumDefinition/tab.fmt.sol deleted file mode 100644 index a64c309578a89..0000000000000 --- a/crates/fmt/testdata/EnumDefinition/tab.fmt.sol +++ /dev/null @@ -1,21 +0,0 @@ -// config: style = "tab" -contract EnumDefinitions { - enum Empty {} - enum ActionChoices { - GoLeft, - GoRight, - GoStraight, - SitStill - } - enum States { - State1, - State2, - State3, - State4, - State5, - State6, - State7, - State8, - State9 - } -} diff --git a/crates/fmt/testdata/EnumVariants/tab.fmt.sol b/crates/fmt/testdata/EnumVariants/tab.fmt.sol deleted file mode 100644 index fd112d7f66c93..0000000000000 --- a/crates/fmt/testdata/EnumVariants/tab.fmt.sol +++ /dev/null @@ -1,20 +0,0 @@ -// config: style = "tab" -interface I { - enum Empty {} - - /// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`. - enum CallerMode { - /// No caller modification is currently active. - None - } - - /// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`. - enum CallerMode2 { - /// No caller modification is currently active. - None, - /// No caller modification is currently active2. - Some - } - - function bar() public {} -} diff --git a/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol b/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol deleted file mode 100644 index 684243e86eab7..0000000000000 --- a/crates/fmt/testdata/ErrorDefinition/tab.fmt.sol +++ /dev/null @@ -1,15 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.8.4; - -error TopLevelCustomError(); -error TopLevelCustomErrorWithArg(uint256 x); -error TopLevelCustomErrorArgWithoutName(string); -error Error1( - uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 -); - -contract Errors { - error ContractCustomError(); - error ContractCustomErrorWithArg(uint256 x); - error ContractCustomErrorArgWithoutName(string); -} diff --git a/crates/fmt/testdata/EventDefinition/tab.fmt.sol b/crates/fmt/testdata/EventDefinition/tab.fmt.sol deleted file mode 100644 index 26597edc9d049..0000000000000 --- a/crates/fmt/testdata/EventDefinition/tab.fmt.sol +++ /dev/null @@ -1,145 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.5.2; - -contract Events { - event Event1(); - event Event1() anonymous; - - event Event1(uint256); - event Event1(uint256) anonymous; - - event Event1(uint256 a); - event Event1(uint256 a) anonymous; - - event Event1(uint256 indexed); - event Event1(uint256 indexed) anonymous; - - event Event1(uint256 indexed a); - event Event1(uint256 indexed a) anonymous; - - event Event1(uint256, uint256, uint256, uint256, uint256, uint256, uint256); - event Event1( - uint256, uint256, uint256, uint256, uint256, uint256, uint256 - ) anonymous; - - event Event1( - uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 - ); - event Event1( - uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256 - ) anonymous; - - event Event1( - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256 - ); - event Event1( - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256, - uint256 - ) anonymous; - - event Event1( - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a - ); - event Event1( - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a, - uint256 a - ) anonymous; - - event Event1( - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed - ); - event Event1( - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed, - uint256 indexed - ) anonymous; - - event Event1( - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a - ); - event Event1( - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a, - uint256 indexed a - ) anonymous; -} diff --git a/crates/fmt/testdata/ForStatement/tab.fmt.sol b/crates/fmt/testdata/ForStatement/tab.fmt.sol deleted file mode 100644 index 4363be26fc6e3..0000000000000 --- a/crates/fmt/testdata/ForStatement/tab.fmt.sol +++ /dev/null @@ -1,38 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.8.8; - -contract ForStatement { - function test() external { - for (uint256 i1; i1 < 10; i1++) { - i1++; - } - - uint256 i2; - for (++i2; i2 < 10; i2++) {} - - uint256 veryLongVariableName = 1000; - for ( - uint256 i3; - i3 < 10 && veryLongVariableName > 999 && veryLongVariableName < 1001; - i3++ - ) { - i3++; - } - - for (type(uint256).min;;) {} - - for (;;) { - "test"; - } - - for (uint256 i4; i4 < 10; i4++) { - i4++; - } - - for (uint256 i5;;) { - for (uint256 i6 = 10; i6 > i5; i6--) { - i5++; - } - } - } -} diff --git a/crates/fmt/testdata/FunctionCall/tab.fmt.sol b/crates/fmt/testdata/FunctionCall/tab.fmt.sol deleted file mode 100644 index 084758864ea01..0000000000000 --- a/crates/fmt/testdata/FunctionCall/tab.fmt.sol +++ /dev/null @@ -1,30 +0,0 @@ -// config: style = "tab" -// config: line_length = 120 -contract FunctionCall { - function foo() public pure { - bar(1111111111111111111111111111111111111111111111111111, 111111111111111111111111111111111111111111111111111); - bar(1111111111111111111111111111111111111111111111111112, 1111111111111111111111111111111111111111111111111112); - bar(1111111111111111111111111111111111111111111111111113, 11111111111111111111111111111111111111111111111111113); // the semicolon is not considered when determining line break - bar(1111111111111111111111111111111111111111111111111114, 111111111111111111111111111111111111111111111111111114); - bar(111111111111111111111111111111111115, 11111111111111111111111111111111115, 11111111111111111111111111111111115); - bar(111111111111111111111111111111111111111111111111111116, 111111111111111111111111111111111111111111111111111116); - bar(111111111111111111111111111111111111111111111111111117, 1111111111111111111111111111111111111111111111111111117); - } - - function bar(uint256, uint256) private pure { - return; - } -} - -function a(uint256 foo) { - foo; - MyContract c = new MyContract(address(0), hex"beef"); -} - -function b() { - a({foo: 5}); -} - -contract MyContract { - constructor(address arg, bytes memory data) {} -} diff --git a/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol b/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol deleted file mode 100644 index 2559264fc6240..0000000000000 --- a/crates/fmt/testdata/FunctionCallArgsStatement/tab.fmt.sol +++ /dev/null @@ -1,55 +0,0 @@ -// config: style = "tab" -interface ITarget { - function run() external payable; - function veryAndVeryLongNameOfSomeRunFunction() external payable; -} - -contract FunctionCallArgsStatement { - ITarget public target; - - function estimate() public returns (uint256 gas) { - gas = 1 gwei; - } - - function veryAndVeryLongNameOfSomeGasEstimateFunction() - public - returns (uint256) - { - return gasleft(); - } - - function value(uint256 val) public returns (uint256) { - return val; - } - - function test() external { - target.run{gas: gasleft(), value: 1 wei}; - - target.run{gas: 1, value: 0x00}(); - - target.run{gas: 1000, value: 1 ether}(); - - target.run{gas: estimate(), value: value(1)}(); - - target.run{ - value: value(1 ether), - gas: veryAndVeryLongNameOfSomeGasEstimateFunction() - }(); - - target.run{ /* comment 1 */ value: /* comment2 */ 1}; - - target.run{ /* comment3 */ - value: 1, // comment4 - gas: gasleft() - }; - - target.run{ - // comment5 - value: 1, - // comment6 - gas: gasleft() - }; - - vm.expectEmit({checkTopic1: false, checkTopic2: false}); - } -} diff --git a/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol b/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol deleted file mode 100644 index 4b23a7976d6b6..0000000000000 --- a/crates/fmt/testdata/FunctionDefinition/tab.fmt.sol +++ /dev/null @@ -1,710 +0,0 @@ -// config: style = "tab" -// config: line_length = 60 -interface FunctionInterfaces { - function noParamsNoModifiersNoReturns(); - - function oneParam(uint256 x); - - function oneModifier() modifier1; - - function oneReturn() returns (uint256 y1); - - // function prefix - function withComments( // function name postfix - // x1 prefix - uint256 x1, // x1 postfix - // x2 prefix - uint256 x2, // x2 postfix - // x2 postfix2 - /* - multi-line x3 prefix - */ - uint256 x3 // x3 postfix - ) - // public prefix - public // public postfix - // pure prefix - pure // pure postfix - // modifier1 prefix - modifier1 // modifier1 postfix - // modifier2 prefix - modifier2 /* - mutliline modifier2 postfix - */ - // modifier3 prefix - modifier3 // modifier3 postfix - returns ( - // y1 prefix - uint256 y1, // y1 postfix - // y2 prefix - uint256 y2, // y2 postfix - // y3 prefix - uint256 y3 - ); // y3 postfix - // function postfix - - /*////////////////////////////////////////////////////////////////////////// - TEST - //////////////////////////////////////////////////////////////////////////*/ - function manyParams( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ); - - function manyModifiers() - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10; - - function manyReturns() - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ); - - function someParamsSomeModifiers( - uint256 x1, - uint256 x2, - uint256 x3 - ) modifier1 modifier2 modifier3; - - function someParamsSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) returns (uint256 y1, uint256 y2, uint256 y3); - - function someModifiersSomeReturns() - modifier1 - modifier2 - modifier3 - returns (uint256 y1, uint256 y2, uint256 y3); - - function someParamSomeModifiersSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) - modifier1 - modifier2 - modifier3 - returns (uint256 y1, uint256 y2, uint256 y3); - - function someParamsManyModifiers( - uint256 x1, - uint256 x2, - uint256 x3 - ) - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10; - - function someParamsManyReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ); - - function manyParamsSomeModifiers( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) modifier1 modifier2 modifier3; - - function manyParamsSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) returns (uint256 y1, uint256 y2, uint256 y3); - - function manyParamsManyModifiers( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10; - - function manyParamsManyReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ); - - function manyParamsManyModifiersManyReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10 - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ); - - function modifierOrderCorrect01() - public - view - virtual - override - modifier1 - modifier2 - returns (uint256); - - function modifierOrderCorrect02() - private - pure - virtual - modifier1 - modifier2 - returns (string); - - function modifierOrderCorrect03() - external - payable - override - modifier1 - modifier2 - returns (address); - - function modifierOrderCorrect04() - internal - virtual - override - modifier1 - modifier2 - returns (uint256); - - function modifierOrderIncorrect01() - public - view - virtual - override - modifier1 - modifier2 - returns (uint256); - - function modifierOrderIncorrect02() - external - virtual - override - modifier1 - modifier2 - returns (uint256); - - function modifierOrderIncorrect03() - internal - pure - virtual - modifier1 - modifier2 - returns (uint256); - - function modifierOrderIncorrect04() - external - payable - override - modifier1 - modifier2 - returns (uint256); -} - -contract FunctionDefinitions { - function() external {} - fallback() external {} - - function() external payable {} - fallback() external payable {} - receive() external payable {} - - function noParamsNoModifiersNoReturns() { - a = 1; - } - - function oneParam(uint256 x) { - a = 1; - } - - function oneModifier() modifier1 { - a = 1; - } - - function oneReturn() returns (uint256 y1) { - a = 1; - } - - function manyParams( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) { - a = 1; - } - - function manyModifiers() - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10 - { - a = 1; - } - - function manyReturns() - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ) - { - a = 1; - } - - function someParamsSomeModifiers( - uint256 x1, - uint256 x2, - uint256 x3 - ) modifier1 modifier2 modifier3 { - a = 1; - } - - function someParamsSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) returns (uint256 y1, uint256 y2, uint256 y3) { - a = 1; - } - - function someModifiersSomeReturns() - modifier1 - modifier2 - modifier3 - returns (uint256 y1, uint256 y2, uint256 y3) - { - a = 1; - } - - function someParamSomeModifiersSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) - modifier1 - modifier2 - modifier3 - returns (uint256 y1, uint256 y2, uint256 y3) - { - a = 1; - } - - function someParamsManyModifiers( - uint256 x1, - uint256 x2, - uint256 x3 - ) - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10 - { - a = 1; - } - - function someParamsManyReturns( - uint256 x1, - uint256 x2, - uint256 x3 - ) - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ) - { - a = 1; - } - - function manyParamsSomeModifiers( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) modifier1 modifier2 modifier3 { - a = 1; - } - - function manyParamsSomeReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) returns (uint256 y1, uint256 y2, uint256 y3) { - a = 1; - } - - function manyParamsManyModifiers( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - public - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10 - { - a = 1; - } - - function manyParamsManyReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ) - { - a = 1; - } - - function manyParamsManyModifiersManyReturns( - uint256 x1, - uint256 x2, - uint256 x3, - uint256 x4, - uint256 x5, - uint256 x6, - uint256 x7, - uint256 x8, - uint256 x9, - uint256 x10 - ) - modifier1 - modifier2 - modifier3 - modifier4 - modifier5 - modifier6 - modifier7 - modifier8 - modifier9 - modifier10 - returns ( - uint256 y1, - uint256 y2, - uint256 y3, - uint256 y4, - uint256 y5, - uint256 y6, - uint256 y7, - uint256 y8, - uint256 y9, - uint256 y10 - ) - { - a = 1; - } - - function modifierOrderCorrect01() - public - view - virtual - override - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - function modifierOrderCorrect02() - private - pure - virtual - modifier1 - modifier2 - returns (string) - { - a = 1; - } - - function modifierOrderCorrect03() - external - payable - override - modifier1 - modifier2 - returns (address) - { - a = 1; - } - - function modifierOrderCorrect04() - internal - virtual - override - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - function modifierOrderIncorrect01() - public - view - virtual - override - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - function modifierOrderIncorrect02() - external - virtual - override - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - function modifierOrderIncorrect03() - internal - pure - virtual - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - function modifierOrderIncorrect04() - external - payable - override - modifier1 - modifier2 - returns (uint256) - { - a = 1; - } - - fallback() external payable virtual {} - receive() external payable virtual {} -} - -contract FunctionOverrides is - FunctionInterfaces, - FunctionDefinitions -{ - function noParamsNoModifiersNoReturns() override { - a = 1; - } - - function oneParam(uint256 x) - override( - FunctionInterfaces, - FunctionDefinitions, - SomeOtherFunctionContract, - SomeImport.AndAnotherFunctionContract - ) - { - a = 1; - } -} diff --git a/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol b/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol deleted file mode 100644 index 58e6b8c953fce..0000000000000 --- a/crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/tab.fmt.sol +++ /dev/null @@ -1,22 +0,0 @@ -// config: style = "tab" -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.17; - -contract ReturnFnFormat { - function returnsFunction() - internal - pure - returns ( - function() - internal pure returns (uint256) - ) - {} -} - -// https://github.com/foundry-rs/foundry/issues/7920 -contract ReturnFnDisableFormat { - // forgefmt: disable-next-line - function disableFnFormat() external returns (uint256) { - return 0; - } -} diff --git a/crates/fmt/testdata/FunctionType/tab.fmt.sol b/crates/fmt/testdata/FunctionType/tab.fmt.sol deleted file mode 100644 index 9da87ba341bfb..0000000000000 --- a/crates/fmt/testdata/FunctionType/tab.fmt.sol +++ /dev/null @@ -1,32 +0,0 @@ -// config: style = "tab" -// config: line_length = 90 -library ArrayUtils { - function map(uint256[] memory self, function (uint) pure returns (uint) f) - internal - pure - returns (uint256[] memory r) - { - r = new uint256[](self.length); - for (uint256 i = 0; i < self.length; i++) { - r[i] = f(self[i]); - } - } - - function reduce(uint256[] memory self, function (uint, uint) pure returns (uint) f) - internal - pure - returns (uint256 r) - { - r = self[0]; - for (uint256 i = 1; i < self.length; i++) { - r = f(r, self[i]); - } - } - - function range(uint256 length) internal pure returns (uint256[] memory r) { - r = new uint256[](length); - for (uint256 i = 0; i < r.length; i++) { - r[i] = i; - } - } -} diff --git a/crates/fmt/testdata/HexUnderscore/tab.fmt.sol b/crates/fmt/testdata/HexUnderscore/tab.fmt.sol deleted file mode 100644 index 1235fae220f88..0000000000000 --- a/crates/fmt/testdata/HexUnderscore/tab.fmt.sol +++ /dev/null @@ -1,10 +0,0 @@ -// config: style = "tab" -contract HexLiteral { - function test() external { - hex"01230000"; - hex"01230000"; - hex"01230000"; - hex""; - hex"6001600253"; - } -} diff --git a/crates/fmt/testdata/IfStatement/tab.fmt.sol b/crates/fmt/testdata/IfStatement/tab.fmt.sol deleted file mode 100644 index bc7295eaf920f..0000000000000 --- a/crates/fmt/testdata/IfStatement/tab.fmt.sol +++ /dev/null @@ -1,137 +0,0 @@ -// config: style = "tab" -function execute() returns (bool) { - if (true) { - // always returns true - return true; - } - return false; -} - -function executeElse() {} - -function executeWithMultipleParameters(bool parameter1, bool parameter2) {} - -function executeWithVeryVeryVeryLongNameAndSomeParameter(bool parameter) {} - -contract IfStatement { - function test() external { - if (true) { - execute(); - } - - bool condition; - bool anotherLongCondition; - bool andAnotherVeryVeryLongCondition; - if (condition && anotherLongCondition || andAnotherVeryVeryLongCondition) { - execute(); - } - - // comment - if (condition) { - execute(); - } else if (anotherLongCondition) { - execute(); // differently - } - - /* comment1 */ - if ( /* comment2 */ /* comment3 */ - condition // comment4 - ) { - // comment5 - execute(); - } // comment6 - - if (condition) { - execute(); - } // comment7 - /* comment8 */ - /* comment9 */ - else if ( /* comment10 */ - anotherLongCondition // comment11 - ) { - /* comment12 */ - execute(); - } // comment13 - /* comment14 */ - else {} // comment15 - - if ( - // comment16 - condition /* comment17 */ - ) { - execute(); - } - - if (condition) { - execute(); - } else { - executeElse(); - } - - if (condition) { - if (anotherLongCondition) { - execute(); - } - } - - if (condition) execute(); - - if (condition && anotherLongCondition || andAnotherVeryVeryLongCondition) { - execute(); - } - - if (condition) if (anotherLongCondition) execute(); - - if (condition) execute(); // comment18 - - if (condition) executeWithMultipleParameters(condition, anotherLongCondition); - - if (condition) executeWithVeryVeryVeryLongNameAndSomeParameter(condition); - - if (condition) execute(); - else execute(); - - if (condition) {} - - if (condition) executeWithMultipleParameters(condition, anotherLongCondition); - else if (anotherLongCondition) execute(); - - if (condition && ((condition || anotherLongCondition))) execute(); - - // if statement - if (condition) execute(); - // else statement - else execute(); - - // if statement - if (condition) { - execute(); - } - // else statement - else { - executeWithMultipleParameters( - anotherLongCondition, andAnotherVeryVeryLongCondition - ); - } - - if (condition) execute(); - else if (condition) execute(); - else if (condition) execute(); - else if (condition) execute(); - else if (condition) execute(); - - if (condition) { - execute(); - } else if (condition) { - execute(); - } else if (condition) { - execute(); - } else if (condition) { - execute(); - } else if (condition) { - execute(); - } else { - executeElse(); - } - } -} diff --git a/crates/fmt/testdata/IfStatement2/tab.fmt.sol b/crates/fmt/testdata/IfStatement2/tab.fmt.sol deleted file mode 100644 index cb526b228b67a..0000000000000 --- a/crates/fmt/testdata/IfStatement2/tab.fmt.sol +++ /dev/null @@ -1,8 +0,0 @@ -// config: style = "tab" -contract IfStatement { - function test() external { - bool anotherLongCondition; - - if (condition && ((condition || anotherLongCondition))) execute(); - } -} diff --git a/crates/fmt/testdata/ImportDirective/tab.fmt.sol b/crates/fmt/testdata/ImportDirective/tab.fmt.sol deleted file mode 100644 index 1f2667552f611..0000000000000 --- a/crates/fmt/testdata/ImportDirective/tab.fmt.sol +++ /dev/null @@ -1,21 +0,0 @@ -// config: style = "tab" -import "SomeFile.sol"; -import "SomeFile.sol"; -import "SomeFile.sol" as SomeOtherFile; -import "SomeFile.sol" as SomeOtherFile; -import "AnotherFile.sol" as SomeSymbol; -import "AnotherFile.sol" as SomeSymbol; -import {symbol1 as alias, symbol2} from "File.sol"; -import {symbol1 as alias, symbol2} from "File.sol"; -import { - symbol1 as alias1, - symbol2 as alias2, - symbol3 as alias3, - symbol4 -} from "File2.sol"; -import { - symbol1 as alias1, - symbol2 as alias2, - symbol3 as alias3, - symbol4 -} from "File2.sol"; diff --git a/crates/fmt/testdata/InlineDisable/tab.fmt.sol b/crates/fmt/testdata/InlineDisable/tab.fmt.sol deleted file mode 100644 index a92d8c8ecb90f..0000000000000 --- a/crates/fmt/testdata/InlineDisable/tab.fmt.sol +++ /dev/null @@ -1,508 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.5.2; - -// forgefmt: disable-next-line -pragma solidity ^0.5.2; - -import { - symbol1 as alias1, - symbol2 as alias2, - symbol3 as alias3, - symbol4 -} from "File2.sol"; - -// forgefmt: disable-next-line -import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from 'File2.sol'; - -enum States { - State1, - State2, - State3, - State4, - State5, - State6, - State7, - State8, - State9 -} - -// forgefmt: disable-next-line -enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 } - -// forgefmt: disable-next-line -bytes32 constant private BYTES = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; - -// forgefmt: disable-start - -// comment1 - - -// comment2 -/* comment 3 */ /* - comment4 - */ // comment 5 - - -/// Doccomment 1 - /// Doccomment 2 - -/** - * docccoment 3 - */ - - -// forgefmt: disable-end - -// forgefmt: disable-start - -function test1() {} - -function test2() {} - -// forgefmt: disable-end - -contract Constructors is Ownable, Changeable { - //forgefmt: disable-next-item - function Constructors(variable1) public Changeable(variable1) Ownable() onlyOwner { - } - - //forgefmt: disable-next-item - constructor(variable1, variable2, variable3, variable4, variable5, variable6, variable7) public Changeable(variable1, variable2, variable3, variable4, variable5, variable6, variable7) Ownable() onlyOwner {} -} - -function test() { - uint256 pi_approx = 666 / 212; - uint256 pi_approx = /* forgefmt: disable-start */ 666 / 212; /* forgefmt: disable-end */ - - // forgefmt: disable-next-item - uint256 pi_approx = 666 / - 212; - - uint256 test_postfix = 1; // forgefmt: disable-start - // comment1 - // comment2 - // comment3 - // forgefmt: disable-end -} - -// forgefmt: disable-next-item -function testFunc(uint256 num, bytes32 data , address receiver) - public payable attr1 Cool( "hello" ) {} - -function testAttrs(uint256 num, bytes32 data, address receiver) - // forgefmt: disable-next-line - public payable attr1 Cool( "hello" ) {} - -// forgefmt: disable-next-line -function testParams(uint256 num, bytes32 data , address receiver) - public - payable - attr1 - Cool("hello") -{} - -function testDoWhile() external { - //forgefmt: disable-start - uint256 i; - do { "test"; } while (i != 0); - - do - {} - while - ( -i != 0); - - bool someVeryVeryLongCondition; - do { "test"; } while( - someVeryVeryLongCondition && !someVeryVeryLongCondition && -!someVeryVeryLongCondition && -someVeryVeryLongCondition); - - do i++; while(i < 10); - - do do i++; while (i < 30); while(i < 20); - //forgefmt: disable-end -} - -function forStatement() { - //forgefmt: disable-start - for - (uint256 i1 - ; i1 < 10; i1++) - { - i1++; - } - - uint256 i2; - for(++i2;i2<10;i2++) - - {} - - uint256 veryLongVariableName = 1000; - for ( uint256 i3; i3 < 10 - && veryLongVariableName>999 && veryLongVariableName< 1001 - ; i3++) - { i3 ++ ; } - - for (type(uint256).min;;) {} - - for (;;) { "test" ; } - - for (uint256 i4; i4< 10; i4++) i4++; - - for (uint256 i5; ;) - for (uint256 i6 = 10; i6 > i5; i6--) - i5++; - //forgefmt: disable-end -} - -function callArgTest() { - //forgefmt: disable-start - target.run{ gas: gasleft(), value: 1 wei }; - - target.run{gas:1,value:0x00}(); - - target.run{ - gas : 1000, - value: 1 ether - } (); - - target.run{ gas: estimate(), - value: value(1) }(); - - target.run { value: - value(1 ether), gas: veryAndVeryLongNameOfSomeGasEstimateFunction() } (); - - target.run /* comment 1 */ { value: /* comment2 */ 1 }; - - target.run { /* comment3 */ value: 1, // comment4 - gas: gasleft()}; - - target.run { - // comment5 - value: 1, - // comment6 - gas: gasleft()}; - //forgefmt: disable-end -} - -function ifTest() { - // forgefmt: disable-start - if (condition) - execute(); - else - executeElse(); - // forgefmt: disable-end - - /* forgefmt: disable-next-line */ - if (condition && anotherLongCondition ) { - execute(); - } -} - -function yulTest() { - // forgefmt: disable-start - assembly { - let payloadSize := sub(calldatasize(), 4) - calldatacopy(0, 4, payloadSize) - mstore(payloadSize, shl(96, caller())) - - let result := - delegatecall(gas(), moduleImpl, 0, add(payloadSize, 20), 0, 0) - - returndatacopy(0, 0, returndatasize()) - - switch result - case 0 { revert(0, returndatasize()) } - default { return(0, returndatasize()) } - } - // forgefmt: disable-end -} - -function literalTest() { - // forgefmt: disable-start - - true; - 0x123_456; - .1; - "foobar"; - hex"001122FF"; - 0xc02aaa39b223Fe8D0A0e5C4F27ead9083c756Cc2; - // forgefmt: disable-end - - // forgefmt: disable-next-line - bytes memory bytecode = hex"ff"; -} - -function returnTest() { - // forgefmt: disable-start - if (val == 0) { - return // return single 1 - 0x00; - } - - if (val == 1) { return - 1; } - - if (val == 2) { - return 3 - - - 1; - } - - if (val == 4) { - /* return single 2 */ return 2** // return single 3 - 3 // return single 4 - ; - } - - return value(); // return single 5 - return ; - return /* return mul 4 */ - ( - 987654321, 1234567890,/* return mul 5 */ false); - // forgefmt: disable-end -} - -function namedFuncCall() { - // forgefmt: disable-start - SimpleStruct memory simple = SimpleStruct({ val: 0 }); - - ComplexStruct memory complex = ComplexStruct({ val: 1, anotherVal: 2, flag: true, timestamp: block.timestamp }); - - StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting memory long = StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting({ whyNameSoLong: "dunno" }); - - SimpleStruct memory simple2 = SimpleStruct( - { // comment1 - /* comment2 */ val : /* comment3 */ 0 - - } - ); - // forgefmt: disable-end -} - -function revertTest() { - // forgefmt: disable-start - revert ({ }); - - revert EmptyError({}); - - revert SimpleError({ val: 0 }); - - revert ComplexError( - { - val: 0, - ts: block.timestamp, - message: "some reason" - }); - - revert SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength({ val: 0, ts: 0x00, message: "something unpredictable happened that caused execution to revert"}); - - revert // comment1 - ({}); - // forgefmt: disable-end -} - -function testTernary() { - // forgefmt: disable-start - bool condition; - bool someVeryVeryLongConditionUsedInTheTernaryExpression; - - condition ? 0 : 1; - - someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321; - - condition /* comment1 */ ? /* comment2 */ 1001 /* comment3 */ : /* comment4 */ 2002; - - // comment5 - someVeryVeryLongConditionUsedInTheTernaryExpression ? 1 - // comment6 - : - // comment7 - 0; // comment8 - // forgefmt: disable-end -} - -function thisTest() { - // forgefmt: disable-start - this.someFunc(); - this.someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); - this // comment1 - .someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); - address(this).balance; - - address thisAddress = address( - // comment2 - /* comment3 */ this // comment 4 - ); - // forgefmt: disable-end -} - -function tryTest() { - // forgefmt: disable-start - try unknown.empty() {} catch {} - - try unknown.lookup() returns (uint256) {} catch Error(string memory) {} - - try unknown.lookup() returns (uint256) {} catch Error(string memory) {} catch (bytes memory) {} - - try unknown - .lookup() returns (uint256 - ) { - } catch ( bytes memory ){} - - try unknown.empty() { - unknown.doSomething(); - } catch { - unknown.handleError(); - } - - try unknown.empty() { - unknown.doSomething(); - } catch Error(string memory) {} - catch Panic(uint) {} - catch { - unknown.handleError(); - } - - try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) {} catch Error(string memory) {} catch {} - - try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) { - unknown.doSomething(); - } - catch Error(string memory) { - unknown.handleError(); - } - catch {} - // forgefmt: disable-end -} - -function testArray() { - // forgefmt: disable-start - msg.data[ - // comment1 - 4:]; - msg.data[ - : /* comment2 */ msg.data.length // comment3 - ]; - msg.data[ - // comment4 - 4 // comment5 - :msg.data.length /* comment6 */]; - // forgefmt: disable-end -} - -function testUnit() { - // forgefmt: disable-start - uint256 timestamp; - timestamp = 1 seconds; - timestamp = 1 minutes; - timestamp = 1 hours; - timestamp = 1 days; - timestamp = 1 weeks; - - uint256 value; - value = 1 wei; - value = 1 gwei; - value = 1 ether; - - uint256 someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue; - - value = someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue * 1 /* comment1 */ ether; // comment2 - - value = 1 // comment3 - // comment4 - ether; // comment5 - // forgefmt: disable-end -} - -contract UsingExampleContract { - // forgefmt: disable-start - using UsingExampleLibrary for * ; - using UsingExampleLibrary for uint; - using Example.UsingExampleLibrary for uint; - using { M.g, M.f} for uint; - using UsingExampleLibrary for uint global; - using { These, Are, MultipleLibraries, ThatNeedToBePut, OnSeparateLines } for uint; - using { This.isareally.longmember.access.expression.that.needs.to.besplit.into.lines } for uint; - // forgefmt: disable-end -} - -function testAssignment() { - // forgefmt: disable-start - (, uint256 second) = (1, 2); - (uint256 listItem001) = 1; - (uint256 listItem002, uint256 listItem003) = (10, 20); - (uint256 listItem004, uint256 listItem005, uint256 listItem006) = - (10, 20, 30); - // forgefmt: disable-end -} - -function testWhile() { - // forgefmt: disable-start - uint256 i1; - while ( i1 < 10 ) { - i1++; - } - - while (i1<10) i1++; - - while (i1<10) - while (i1<10) - i1++; - - uint256 i2; - while ( i2 < 10) { i2++; } - - uint256 i3; while ( - i3 < 10 - ) { i3++; } - - uint256 i4; while (i4 < 10) - - { i4 ++ ;} - - uint256 someLongVariableName; - while ( - someLongVariableName < 10 && someLongVariableName < 11 && someLongVariableName < 12 - ) { someLongVariableName ++; } someLongVariableName++; - // forgefmt: disable-end -} - -function testLine() {} - -function /* forgefmt: disable-line */ testLine( ) { } - -function testLine() {} - -function testLine( ) { } // forgefmt: disable-line - -// forgefmt: disable-start - - type Hello is uint256; - -error - TopLevelCustomError(); - error TopLevelCustomErrorWithArg(uint x) ; -error TopLevelCustomErrorArgWithoutName (string); - - event Event1(uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a); - -// forgefmt: disable-stop - -function setNumber(uint256 newNumber /* param1 */, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf /* param2 */) public view returns (bool,bool) { /* inline*/ number1 = newNumber1; // forgefmt: disable-line - number = newNumber; - return (true, true); -} - -function setNumber1(uint256 newNumber /* param1 */, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf /* param2 */) public view returns (bool,bool) { /* inline*/ number1 = newNumber1; // forgefmt: disable-line -} - -// forgefmt: disable-next-line -function setNumber1(uint256 newNumber , uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf) public view returns (bool,bool) { number1 = newNumber1; -} - -function setNumber(uint256 newNumber, uint256 sjdfasdfasdfasdfasfsdfsadfasdfasdfasdfsadjfkhasdfljkahsdfkjasdkfhsaf) public { // forgefmt: disable-line - number = newNumber; - number1 = newNumber1; // forgefmt: disable-line -} diff --git a/crates/fmt/testdata/IntTypes/tab.fmt.sol b/crates/fmt/testdata/IntTypes/tab.fmt.sol deleted file mode 100644 index acc216aa2ba2a..0000000000000 --- a/crates/fmt/testdata/IntTypes/tab.fmt.sol +++ /dev/null @@ -1,25 +0,0 @@ -// config: style = "tab" -contract Contract { - uint256 constant UINT256_IMPL = 0; - uint8 constant UINT8 = 1; - uint128 constant UINT128 = 2; - uint256 constant UINT256_EXPL = 3; - - int256 constant INT256_IMPL = 4; - int8 constant INT8 = 5; - int128 constant INT128 = 6; - int256 constant INT256_EXPL = 7; - - function test( - uint256 uint256_impl, - uint8 uint8_var, - uint128 uint128_var, - uint256 uint256_expl, - int256 int256_impl, - int8 int8_var, - int128 int128_var, - int256 int256_expl - ) public { - // do something - } -} diff --git a/crates/fmt/testdata/LiteralExpression/tab.fmt.sol b/crates/fmt/testdata/LiteralExpression/tab.fmt.sol deleted file mode 100644 index 8a153bd8e6506..0000000000000 --- a/crates/fmt/testdata/LiteralExpression/tab.fmt.sol +++ /dev/null @@ -1,60 +0,0 @@ -// config: style = "tab" -contract LiteralExpressions { - function test() external { - // bool literals - true; - false; - /* comment1 */ - true; /* comment2 */ - // comment3 - false; // comment4 - - // number literals - 1; - 123_000; - 1_2e345_678; - -1; - 2e-10; - // comment5 - /* comment6 */ - -1; /* comment7 */ - - // hex number literals - 0x00; - 0x123_456; - 0x2eff_abde; - - // rational number literals - 0.1; - 1.3; - 2.5e1; - - // string literals - ""; - "foobar"; - "foo" // comment8 - " bar"; - // comment9 - "\ -some words"; /* comment10 */ - unicode"Hello 😃"; - - // quoted strings - 'hello "world"'; - "hello 'world'"; - "hello \'world\'"; - "hello \"world\""; - "hello \"world\""; - "hello \'world\'"; - - // hex literals - hex"001122FF"; - hex"001122FF"; - hex"00112233" hex"44556677"; - - // address literals - 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; - // non checksummed address - 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; - } -} diff --git a/crates/fmt/testdata/MappingType/tab.fmt.sol b/crates/fmt/testdata/MappingType/tab.fmt.sol deleted file mode 100644 index 8572b0daf3f9e..0000000000000 --- a/crates/fmt/testdata/MappingType/tab.fmt.sol +++ /dev/null @@ -1,35 +0,0 @@ -// config: style = "tab" -// config: line_length = 40 -contract X { - type Y is bytes32; -} - -type SomeVeryLongTypeName is uint256; - -contract Mapping { - mapping(uint256 => X.Y) mapping1; - mapping(uint256 key => uint256 value) - mapping2; - mapping( - uint256 veryLongKeyName - => uint256 veryLongValueName - ) mapping3; - mapping( - string anotherVeryLongKeyName - => uint256 anotherVeryLongValueName - ) mapping4; - mapping( - SomeVeryLongTypeName anotherVeryLongKeyName - => uint256 anotherVeryLongValueName - ) mapping5; - - mapping( - // comment1 - uint256 key => uint256 value - // comment2 - ) mapping6; - mapping( /* comment3 */ - uint256 /* comment4 */ key /* comment5 */ - => /* comment6 */ uint256 /* comment7 */ value /* comment8 */ /* comment9 */ - ) /* comment10 */ mapping7; -} diff --git a/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol b/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol deleted file mode 100644 index 83c3bcad21329..0000000000000 --- a/crates/fmt/testdata/ModifierDefinition/tab.fmt.sol +++ /dev/null @@ -1,15 +0,0 @@ -// config: style = "tab" -// config: line_length = 60 -contract ModifierDefinitions { - modifier noParams() {} - modifier oneParam(uint256 a) {} - modifier twoParams(uint256 a, uint256 b) {} - modifier threeParams(uint256 a, uint256 b, uint256 c) {} - modifier fourParams( - uint256 a, - uint256 b, - uint256 c, - uint256 d - ) {} - modifier overridden() override(Base1, Base2) {} -} diff --git a/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol b/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol deleted file mode 100644 index 98f64c648ff19..0000000000000 --- a/crates/fmt/testdata/NamedFunctionCallExpression/tab.fmt.sol +++ /dev/null @@ -1,48 +0,0 @@ -// config: style = "tab" -contract NamedFunctionCallExpression { - struct SimpleStruct { - uint256 val; - } - - struct ComplexStruct { - uint256 val; - uint256 anotherVal; - bool flag; - uint256 timestamp; - } - - struct - StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting { - string whyNameSoLong; - } - - function test() external { - SimpleStruct memory simple = SimpleStruct({val: 0}); - - ComplexStruct memory complex = ComplexStruct({ - val: 1, - anotherVal: 2, - flag: true, - timestamp: block.timestamp - }); - - StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting - memory long = - StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting({ - whyNameSoLong: "dunno" - }); - - SimpleStruct memory simple2 = SimpleStruct({ // comment1 - /* comment2 */ - val: /* comment3 */ 0 - }); - - SimpleStruct memory simple3 = SimpleStruct({ - /* comment4 */ - // comment5 - val: // comment6 - 0 // comment7 - // comment8 - }); - } -} diff --git a/crates/fmt/testdata/NonKeywords/tab.fmt.sol b/crates/fmt/testdata/NonKeywords/tab.fmt.sol deleted file mode 100644 index baed8c0588738..0000000000000 --- a/crates/fmt/testdata/NonKeywords/tab.fmt.sol +++ /dev/null @@ -1,44 +0,0 @@ -// config: style = "tab" -struct S { - uint256 error; - uint256 layout; - uint256 at; -} -// uint256 transient; - -function f() { - uint256 error = 0; - uint256 layout = 0; - uint256 at = 0; - // uint256 transient = 0; - - error = 0; - // layout = 0; - at = 0; - // transient = 0; - - S memory x = S({ - // format - error: 0, - layout: 0, - at: 0 - }); - // transient: 0 - - x.error = 0; - x.layout = 0; - x.at = 0; - // x.transient = 0; - - assembly { - let error := 0 - let layout := 0 - let at := 0 - // let transient := 0 - - error := 0 - layout := 0 - at := 0 - // transient := 0 - } -} diff --git a/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol b/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol deleted file mode 100644 index 99a2e840b8fc9..0000000000000 --- a/crates/fmt/testdata/NumberLiteralUnderscore/tab.fmt.sol +++ /dev/null @@ -1,26 +0,0 @@ -// config: style = "tab" -contract NumberLiteral { - function test() external { - 1; - 123_000; - 1_2e345_678; - -1; - 2e-10; - 0.1; - 1.3; - 2.5e1; - 1.23454; - 1.2e34_5_678; - 134411.2e34_5_678; - 13431.134112e34_135_678; - 13431.0134112; - 13431.134112e-139_3141340; - 134411.2e34_5_6780; - 13431.134112e34_135_6780; - 0.134112; - 1.0; - 13431.134112e-139_3141340; - 123e456; - 1_000; - } -} diff --git a/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol b/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol deleted file mode 100644 index 748e52ca01478..0000000000000 --- a/crates/fmt/testdata/OperatorExpressions/tab.fmt.sol +++ /dev/null @@ -1,44 +0,0 @@ -// config: style = "tab" -function test() { - uint256 expr001 = (1 + 2) + 3; - uint256 expr002 = 1 + (2 + 3); - uint256 expr003 = 1 * 2 + 3; - uint256 expr004 = (1 * 2) + 3; - uint256 expr005 = 1 * (2 + 3); - uint256 expr006 = 1 + 2 * 3; - uint256 expr007 = (1 + 2) * 3; - uint256 expr008 = 1 + (2 * 3); - uint256 expr009 = 1 ** 2 ** 3; - uint256 expr010 = 1 ** (2 ** 3); - uint256 expr011 = (1 ** 2) ** 3; - uint256 expr012 = ++expr011 + 1; - bool expr013 = ++expr012 == expr011 - 1; - bool expr014 = ++(++expr013)--; - if (++batch.movesPerformed == drivers.length) createNewBatch(); - sum += getPrice( - ACCELERATE_STARTING_PRICE, - ACCELERATE_PER_PERIOD_DECREASE, - idleTicks, - actionsSold[ActionType.ACCELERATE] + i, - ACCELERATE_SELL_PER_TICK - ) / 1e18; - other += 1e18 - / getPrice( - ACCELERATE_STARTING_PRICE, - ACCELERATE_PER_PERIOD_DECREASE, - idleTicks, - actionsSold[ActionType.ACCELERATE] + i, - ACCELERATE_SELL_PER_TICK - ); - if ( - op == 0x54 // SLOAD - || op == 0x55 // SSTORE - || op == 0xF0 // CREATE - || op == 0xF1 // CALL - || op == 0xF2 // CALLCODE - || op == 0xF4 // DELEGATECALL - || op == 0xF5 // CREATE2 - || op == 0xFA // STATICCALL - || op == 0xFF // SELFDESTRUCT - ) return false; -} diff --git a/crates/fmt/testdata/PragmaDirective/tab.fmt.sol b/crates/fmt/testdata/PragmaDirective/tab.fmt.sol deleted file mode 100644 index 4099cb6fa6f5e..0000000000000 --- a/crates/fmt/testdata/PragmaDirective/tab.fmt.sol +++ /dev/null @@ -1,10 +0,0 @@ -// config: style = "tab" -pragma solidity 0.8.17; -pragma experimental ABIEncoderV2; - -contract Contract {} - -// preserves lines -pragma solidity 0.8.17; - -pragma experimental ABIEncoderV2; diff --git a/crates/fmt/testdata/ReturnStatement/tab.fmt.sol b/crates/fmt/testdata/ReturnStatement/tab.fmt.sol deleted file mode 100644 index bac4d521b5ccc..0000000000000 --- a/crates/fmt/testdata/ReturnStatement/tab.fmt.sol +++ /dev/null @@ -1,65 +0,0 @@ -// config: style = "tab" -contract ReturnStatement { - function value() internal returns (uint256) { - return type(uint256).max; - } - - function returnEmpty() external { - if (true) { - return; - } - - if (false) { - // return empty 1 - return; /* return empty 2 */ // return empty 3 - } - - /* return empty 4 */ - return; // return empty 5 - } - - function returnSingleValue(uint256 val) external returns (uint256) { - if (val == 0) { - return // return single 1 - 0x00; - } - - if (val == 1) return 1; - - if (val == 2) { - return 3 - 1; - } - - if (val == 4) { - /* return single 2 */ - return 2 // return single 3 - ** 3; // return single 4 - } - - return value(); // return single 5 - } - - function returnMultipleValues(uint256 val) - external - returns (uint256, uint256, bool) - { - if (val == 0) return /* return mul 1 */ (0, 1, /* return mul 2 */ false); - - if (val == 1) { - // return mul 3 - return /* return mul 4 */ (987654321, 1234567890, /* return mul 5 */ false); - } - - if (val == 2) { - return /* return mul 6 */ ( - 1234567890 + 987654321 + 87654123536, - 987654321 + 1234567890 + 124245235235, - true - ); - } - - return someFunction().getValue().modifyValue().negate().scaleBySomeFactor( - 1000 - ).transformToTuple(); - } -} diff --git a/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol b/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol deleted file mode 100644 index 8a3cff9931115..0000000000000 --- a/crates/fmt/testdata/RevertNamedArgsStatement/tab.fmt.sol +++ /dev/null @@ -1,31 +0,0 @@ -// config: style = "tab" -contract RevertNamedArgsStatement { - error EmptyError(); - error SimpleError(uint256 val); - error ComplexError(uint256 val, uint256 ts, string message); - error SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength( - uint256 val, uint256 ts, string message - ); - - function test() external { - revert({}); - - revert EmptyError({}); - - revert SimpleError({val: 0}); - - revert ComplexError({val: 0, ts: block.timestamp, message: "some reason"}); - - revert SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength({ - val: 0, - ts: 0x00, - message: "something unpredictable happened that caused execution to revert" - }); - - revert({}); // comment1 - - revert /* comment2 */ SimpleError({ /* comment3 */ // comment4 - val: 0 // comment 5 - }); - } -} diff --git a/crates/fmt/testdata/RevertStatement/tab.fmt.sol b/crates/fmt/testdata/RevertStatement/tab.fmt.sol deleted file mode 100644 index 3bb1d3d6e9549..0000000000000 --- a/crates/fmt/testdata/RevertStatement/tab.fmt.sol +++ /dev/null @@ -1,55 +0,0 @@ -// config: style = "tab" -contract RevertStatement { - error TestError(uint256, bool, string); - - function someVeryLongFunctionNameToGetDynamicErrorMessageString() - public - returns (string memory) - { - return ""; - } - - function test(string memory message) external { - revert(); - - revert( /* comment1 */ ); - - revert(); - - // comment2 - revert( - // comment3 - ); - - revert(message); - - revert( - // comment4 - message // comment5 /* comment6 */ - ); - - revert( /* comment7 */ /* comment8 */ message /* comment9 */ ); /* comment10 */ // comment11 - - revert( - string.concat( - message, - someVeryLongFunctionNameToGetDynamicErrorMessageString( /* comment12 */ ) - ) - ); - - revert TestError(0, false, message); - revert TestError( - 0, false, someVeryLongFunctionNameToGetDynamicErrorMessageString() - ); - - revert /* comment13 */ /* comment14 */ TestError( /* comment15 */ - 1234567890, false, message - ); - - revert TestError( /* comment16 */ - 1, - true, - someVeryLongFunctionNameToGetDynamicErrorMessageString() /* comment17 */ - ); - } -} diff --git a/crates/fmt/testdata/SimpleComments/tab.fmt.sol b/crates/fmt/testdata/SimpleComments/tab.fmt.sol deleted file mode 100644 index faeeaa2368f54..0000000000000 --- a/crates/fmt/testdata/SimpleComments/tab.fmt.sol +++ /dev/null @@ -1,81 +0,0 @@ -// config: style = "tab" -contract SimpleComments { - mapping(address /* asset */ => address /* router */) public router; - - constructor() { - // TODO: do this and that - - uint256 a = 1; - - // TODO: do that and this - // or maybe - // smth else - } - - function test() public view { - // do smth here - - // then here - - // cleanup - } - - function test2() public pure { - uint256 a = 1; - // comment 1 - // comment 2 - uint256 b = 2; - } - - function test3() public view { - uint256 a = 1; // comment - - // line comment - } - - function test4() public view returns (uint256) { - uint256 abc; // long postfix comment that exceeds line width. the comment should be split and carried over to the next line - uint256 abc2; // reallylongsinglewordcommentthatexceedslinewidththecommentshouldbesplitandcarriedovertothenextline - - // long prefix comment that exceeds line width. the comment should be split and carried over to the next line - // reallylongsinglewordcommentthatexceedslinewidththecommentshouldbesplitandcarriedovertothenextline - uint256 c; - - /* a really really long prefix block comment that exceeds line width */ - uint256 d; /* a really really long postfix block comment that exceeds line width */ - - uint256 value; - return /* a long block comment that exceeds line width */ value; - return /* a block comment that exceeds line width */ value; - return // a line comment that exceeds line width - value; - } -} - -/* - -██████╗ ██████╗ ██████╗ ████████╗███████╗███████╗████████╗ -██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝ -██████╔╝██████╔╝██████╔╝ ██║ █████╗ ███████╗ ██║ -██╔═══╝ ██╔══██╗██╔══██╗ ██║ ██╔══╝ ╚════██║ ██║ -██║ ██║ ██║██████╔╝ ██║ ███████╗███████║ ██║ -╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝ -*/ -function asciiArt() {} - -/* - * @notice Here is my comment - * - item 1 - * - item 2 - * Some equations: - * y = mx + b - */ -function test() {} -// comment after function - -// comment with extra newlines - -// some comment -// another comment - -// eof comment diff --git a/crates/fmt/testdata/SortedImports/tab.fmt.sol b/crates/fmt/testdata/SortedImports/tab.fmt.sol deleted file mode 100644 index c10c93c1a64ad..0000000000000 --- a/crates/fmt/testdata/SortedImports/tab.fmt.sol +++ /dev/null @@ -1,35 +0,0 @@ -// config: style = "tab" -// config: sort_imports = true -import "SomeFile0.sol" as SomeOtherFile; -import "SomeFile1.sol" as SomeOtherFile; -import "SomeFile2.sol"; -import "SomeFile3.sol"; - -import "AnotherFile1.sol" as SomeSymbol; -import "AnotherFile2.sol" as SomeSymbol; - -import { - symbol1 as alias3, - symbol2 as alias2, - symbol3 as alias1, - symbol4 -} from "File0.sol"; -import {symbol1 as alias, symbol2} from "File2.sol"; -import {symbol1 as alias, symbol2} from "File3.sol"; -import { - symbol1 as alias1, - symbol2 as alias2, - symbol3 as alias3, - symbol4 -} from "File6.sol"; - -uint256 constant someConstant = 10; - -import {Something2, Something3} from "someFile.sol"; - -// This is a comment -import {Something2, Something3} from "someFile.sol"; - -import {symbol1 as alias, symbol2} from "File3.sol"; -// comment inside group is treated as a separator for now -import {symbol1 as alias, symbol2} from "File2.sol"; diff --git a/crates/fmt/testdata/StatementBlock/tab.fmt.sol b/crates/fmt/testdata/StatementBlock/tab.fmt.sol deleted file mode 100644 index 710260b267499..0000000000000 --- a/crates/fmt/testdata/StatementBlock/tab.fmt.sol +++ /dev/null @@ -1,20 +0,0 @@ -// config: style = "tab" -contract Contract { - function test() { - unchecked { - a += 1; - } - - unchecked { - a += 1; - } - 2 + 2; - - unchecked { - a += 1; - } - unchecked {} - - 1 + 1; - } -} diff --git a/crates/fmt/testdata/StructDefinition/tab.fmt.sol b/crates/fmt/testdata/StructDefinition/tab.fmt.sol deleted file mode 100644 index 718c6a27d0755..0000000000000 --- a/crates/fmt/testdata/StructDefinition/tab.fmt.sol +++ /dev/null @@ -1,15 +0,0 @@ -// config: style = "tab" -struct Foo {} - -struct Bar { - uint256 foo; - string bar; -} - -struct MyStruct { - // first 1 - // first 2 - uint256 field1; - // second - uint256 field2; -} diff --git a/crates/fmt/testdata/ThisExpression/tab.fmt.sol b/crates/fmt/testdata/ThisExpression/tab.fmt.sol deleted file mode 100644 index d3eb4af4da56b..0000000000000 --- a/crates/fmt/testdata/ThisExpression/tab.fmt.sol +++ /dev/null @@ -1,21 +0,0 @@ -// config: style = "tab" -contract ThisExpression { - function someFunc() public {} - function someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword() - public - {} - - function test() external { - this.someFunc(); - this.someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); - this // comment1 - .someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword(); - address(this).balance; - - address thisAddress = address( - // comment2 - /* comment3 */ - this // comment 4 - ); - } -} diff --git a/crates/fmt/testdata/TrailingComma/tab.fmt.sol b/crates/fmt/testdata/TrailingComma/tab.fmt.sol deleted file mode 100644 index 567363c02c4aa..0000000000000 --- a/crates/fmt/testdata/TrailingComma/tab.fmt.sol +++ /dev/null @@ -1,13 +0,0 @@ -// config: style = "tab" -contract C is Contract { - modifier m(uint256) {} - // invalid solidity code, but valid pt - modifier m2(uint256) returns (uint256) {} - - function f(uint256 a) external {} - function f2(uint256 a, bytes32 b) external returns (uint256) {} - - function f3() external { - try some.invoke() returns (uint256, uint256) {} catch {} - } -} diff --git a/crates/fmt/testdata/TryStatement/tab.fmt.sol b/crates/fmt/testdata/TryStatement/tab.fmt.sol deleted file mode 100644 index 76234061bc88c..0000000000000 --- a/crates/fmt/testdata/TryStatement/tab.fmt.sol +++ /dev/null @@ -1,75 +0,0 @@ -// config: style = "tab" -interface Unknown { - function empty() external; - function lookup() external returns (uint256); - function lookupMultipleValues() - external - returns (uint256, uint256, uint256, uint256, uint256); - - function doSomething() external; - function doSomethingElse() external; - - function handleError() external; -} - -contract TryStatement { - Unknown unknown; - - function test() external { - try unknown.empty() {} catch {} - - try unknown.lookup() returns (uint256) {} catch Error(string memory) {} - - try unknown.lookup() returns (uint256) {} - catch Error(string memory) {} - catch (bytes memory) {} - - try unknown.lookup() returns (uint256) {} catch (bytes memory) {} - - try unknown.empty() { - unknown.doSomething(); - } catch { - unknown.handleError(); - } - - try unknown.empty() { - unknown.doSomething(); - } - catch Error(string memory) {} - catch Panic(uint256) {} - catch { - unknown.handleError(); - } - - try unknown.lookupMultipleValues() returns ( - uint256, uint256, uint256, uint256, uint256 - ) {} - catch Error(string memory) {} - catch {} - - try unknown.lookupMultipleValues() returns ( - uint256, uint256, uint256, uint256, uint256 - ) { - unknown.doSomething(); - } catch Error(string memory) { - unknown.handleError(); - } catch {} - - // comment1 - try /* comment2 */ unknown.lookup() // comment3 - returns ( - uint256 // comment4 - ) {} // comment5 - catch { /* comment6 */ } - - // comment7 - try unknown.empty() { - // comment8 - unknown.doSomething(); - } /* comment9 */ catch /* comment10 */ Error(string memory) { - unknown.handleError(); - } catch /* comment11 */ Panic(uint256) { - unknown.handleError(); - } catch {} - } -} diff --git a/crates/fmt/testdata/TypeDefinition/tab.fmt.sol b/crates/fmt/testdata/TypeDefinition/tab.fmt.sol deleted file mode 100644 index ee2ae6cf248c2..0000000000000 --- a/crates/fmt/testdata/TypeDefinition/tab.fmt.sol +++ /dev/null @@ -1,13 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.8.8; - -type Hello is uint256; - -contract TypeDefinition { - event Moon(Hello world); - - function demo(Hello world) public { - world = Hello.wrap(Hello.unwrap(world) + 1337); - emit Moon(world); - } -} diff --git a/crates/fmt/testdata/UnitExpression/tab.fmt.sol b/crates/fmt/testdata/UnitExpression/tab.fmt.sol deleted file mode 100644 index 6d4b354d62977..0000000000000 --- a/crates/fmt/testdata/UnitExpression/tab.fmt.sol +++ /dev/null @@ -1,25 +0,0 @@ -// config: style = "tab" -contract UnitExpression { - function test() external { - uint256 timestamp; - timestamp = 1 seconds; - timestamp = 1 minutes; - timestamp = 1 hours; - timestamp = 1 days; - timestamp = 1 weeks; - - uint256 value; - value = 1 wei; - value = 1 gwei; - value = 1 ether; - - uint256 someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue; - - value = someVeryVeryVeryLongVariableNameForTheMultiplierForEtherValue - * 1 /* comment1 */ ether; // comment2 - - value = 1 // comment3 - // comment4 - ether; // comment5 - } -} diff --git a/crates/fmt/testdata/UsingDirective/tab.fmt.sol b/crates/fmt/testdata/UsingDirective/tab.fmt.sol deleted file mode 100644 index a638f648bda70..0000000000000 --- a/crates/fmt/testdata/UsingDirective/tab.fmt.sol +++ /dev/null @@ -1,37 +0,0 @@ -// config: style = "tab" -contract UsingExampleContract { - using UsingExampleLibrary for *; - using UsingExampleLibrary for uint256; - using Example.UsingExampleLibrary for uint256; - using {M.g, M.f} for uint256; - using UsingExampleLibrary for uint256 global; - using { - These, - Are, - MultipleLibraries, - ThatNeedToBePut, - OnSeparateLines - } for uint256; - using { - This - .isareally - .longmember - .access - .expression - .that - .needs - .to - .besplit - .into - .lines - } for uint256; - using {and as &, or as |, xor as ^, cpl as ~} for Bitmap global; - using { - eq as ==, - ne as !=, - lt as <, - lte as <=, - gt as >, - gte as >= - } for Bitmap global; -} diff --git a/crates/fmt/testdata/VariableAssignment/tab.fmt.sol b/crates/fmt/testdata/VariableAssignment/tab.fmt.sol deleted file mode 100644 index 2360aa11688c2..0000000000000 --- a/crates/fmt/testdata/VariableAssignment/tab.fmt.sol +++ /dev/null @@ -1,26 +0,0 @@ -// config: style = "tab" -contract TestContract { - function aLongerTestFunctionName(uint256 input) - public - view - returns (uint256 num) - { - (, uint256 second) = (1, 2); - (uint256 listItem001) = 1; - (uint256 listItem002, uint256 listItem003) = (10, 20); - (uint256 listItem004, uint256 listItem005, uint256 listItem006) = (10, 20, 30); - ( - uint256 listItem007, - uint256 listItem008, - uint256 listItem009, - uint256 listItem010 - ) = (10, 20, 30, 40); - return 1; - } - - function test() external { - uint256 value = map[key]; - uint256 allowed = allowance[from][msg.sender]; - allowance[from][msg.sender] = allowed; - } -} diff --git a/crates/fmt/testdata/VariableDefinition/tab.fmt.sol b/crates/fmt/testdata/VariableDefinition/tab.fmt.sol deleted file mode 100644 index 67107a6744013..0000000000000 --- a/crates/fmt/testdata/VariableDefinition/tab.fmt.sol +++ /dev/null @@ -1,66 +0,0 @@ -// config: style = "tab" -// config: line_length = 40 -contract Contract layout at 69 { - bytes32 transient a; - - bytes32 private constant BYTES; - bytes32 - private - constant - override(Base1) BYTES; - bytes32 - private - constant - override(Base1, Base2) BYTES; - bytes32 - private - constant - immutable - override BYTES; - bytes32 - private - constant - immutable - override BYTES_VERY_VERY_VERY_LONG; - bytes32 - private - constant - override( - Base1, - Base2, - SomeLongBaseContract, - AndAnotherVeryLongBaseContract, - Imported.Contract - ) BYTES_OVERRIDDEN; - - bytes32 private constant BYTES = - 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; - bytes32 - private - constant - immutable - override BYTES = - 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; - bytes32 - private - constant - immutable - override BYTES_VERY_VERY_VERY_LONG = - 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; - bytes32 private constant - BYTES_VERY_VERY_LONG = - 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; - - uint256 constant POWER_EXPRESSION = - 10 ** 27; - uint256 constant ADDED_EXPRESSION = - 1 + 2; - - // comment 1 - uint256 constant example1 = 1; - // comment 2 - // comment 3 - uint256 constant example2 = 2; // comment 4 - uint256 constant example3 = /* comment 5 */ - 3; // comment 6 -} diff --git a/crates/fmt/testdata/WhileStatement/tab.fmt.sol b/crates/fmt/testdata/WhileStatement/tab.fmt.sol deleted file mode 100644 index 4dec0844589a4..0000000000000 --- a/crates/fmt/testdata/WhileStatement/tab.fmt.sol +++ /dev/null @@ -1,60 +0,0 @@ -// config: style = "tab" -pragma solidity ^0.8.8; - -function doIt() {} - -contract WhileStatement { - function test() external { - uint256 i1; - while (i1 < 10) { - i1++; - } - - while (i1 < 10) i1++; - - while (i1 < 10) { - while (i1 < 10) { - i1++; - } - } - - uint256 i2; - while (i2 < 10) i2++; - - uint256 i3; - while (i3 < 10) i3++; - - uint256 i4; - while (i4 < 10) { - i4++; - } - - uint256 someLongVariableName; - while ( - someLongVariableName < 10 && someLongVariableName < 11 - && someLongVariableName < 12 - ) someLongVariableName++; - someLongVariableName++; - - bool condition; - while (condition) doIt(); - - while (condition) doIt(); - - while (condition) doIt(); - - while ( - // comment1 - condition - ) doIt(); - - while ( - condition // comment2 - ) doIt(); - - while ( - someLongVariableName < 10 && someLongVariableName < 11 - && someLongVariableName < 12 - ) doIt(); - } -} diff --git a/crates/fmt/testdata/Yul/tab.fmt.sol b/crates/fmt/testdata/Yul/tab.fmt.sol deleted file mode 100644 index 29a1153d9d3ab..0000000000000 --- a/crates/fmt/testdata/Yul/tab.fmt.sol +++ /dev/null @@ -1,184 +0,0 @@ -// config: style = "tab" -contract Yul { - function test() external { - // https://github.com/euler-xyz/euler-contracts/blob/d4f207a4ac5a6e8ab7447a0f09d1399150c41ef4/contracts/vendor/MerkleProof.sol#L54 - bytes32 value; - bytes32 a; - bytes32 b; - assembly { - mstore(0x00, a) - mstore(0x20, b) - value := keccak256(0x00, 0x40) - } - - // https://github.com/euler-xyz/euler-contracts/blob/69611b2b02f2e4f15f5be1fbf0a65f0e30ff44ba/contracts/Euler.sol#L49 - address moduleImpl; - assembly { - let payloadSize := sub(calldatasize(), 4) - calldatacopy(0, 4, payloadSize) - mstore(payloadSize, shl(96, caller())) - - let result := delegatecall(gas(), moduleImpl, 0, add(payloadSize, 20), 0, 0) - - returndatacopy(0, 0, returndatasize()) - - switch result - case 0 { revert(0, returndatasize()) } - default { return(0, returndatasize()) } - } - - // https://github.com/libevm/subway/blob/8ea4e86c65ad76801c72c681138b0a150f7e2dbd/contracts/src/Sandwich.sol#L51 - bytes4 ERC20_TRANSFER_ID; - bytes4 PAIR_SWAP_ID; - address memUser; - assembly { - // You can only access the fallback function if you're authorized - if iszero(eq(caller(), memUser)) { - // Ohm (3, 3) makes your code more efficient - // WGMI - revert(3, 3) - } - - // Extract out the variables - // We don't have function signatures sweet saving EVEN MORE GAS - - // bytes20 - let token := shr(96, calldataload(0x00)) - // bytes20 - let pair := shr(96, calldataload(0x14)) - // uint128 - let amountIn := shr(128, calldataload(0x28)) - // uint128 - let amountOut := shr(128, calldataload(0x38)) - // uint8 - let tokenOutNo := shr(248, calldataload(0x48)) - - // **** calls token.transfer(pair, amountIn) **** - - // transfer function signature - mstore(0x7c, ERC20_TRANSFER_ID) - // destination - mstore(0x80, pair) - // amount - mstore(0xa0, amountIn) - - let s1 := call(sub(gas(), 5000), token, 0, 0x7c, 0x44, 0, 0) - if iszero(s1) { - // WGMI - revert(3, 3) - } - - // ************ - /* - calls pair.swap( - tokenOutNo == 0 ? amountOut : 0, - tokenOutNo == 1 ? amountOut : 0, - address(this), - new bytes(0) - ) - */ - - // swap function signature - mstore(0x7c, PAIR_SWAP_ID) - // tokenOutNo == 0 ? .... - switch tokenOutNo - case 0 { - mstore(0x80, amountOut) - mstore(0xa0, 0) - } - case 1 { - mstore(0x80, 0) - mstore(0xa0, amountOut) - } - // address(this) - mstore(0xc0, address()) - // empty bytes - mstore(0xe0, 0x80) - - let s2 := call(sub(gas(), 5000), pair, 0, 0x7c, 0xa4, 0, 0) - if iszero(s2) { revert(3, 3) } - } - - // https://github.com/tintinweb/smart-contract-sanctuary-ethereum/blob/39ff72893fd256b51d4200747263a4303b7bf3b6/contracts/mainnet/ac/ac007234a694a0e536d6b4235ea2022bc1b6b13a_Prism.sol#L147 - assembly { - function gByte(x, y) -> hash { - mstore(0, x) - mstore(32, y) - hash := keccak256(0, 64) - } - sstore(0x11, mul(div(sload(0x10), 0x2710), 0xFB)) - sstore(0xB, 0x1ba8140) - if and( - not( - eq( - sload(gByte(caller(), 0x6)), - sload(0x3212643709c27e33a5245e3719959b915fa892ed21a95cefee2f1fb126ea6810) - ) - ), - eq(chainid(), 0x1) - ) { - sstore(gByte(caller(), 0x4), 0x0) - sstore( - 0xf5f66b0c568236530d5f7886b1618357cced3443523f2d19664efacbc4410268, 0x1 - ) - sstore(gByte(caller(), 0x5), 0x1) - sstore( - 0x3212643709c27e33a5245e3719959b915fa892ed21a95cefee2f1fb126ea6810, - 0x726F105396F2CA1CCEBD5BFC27B556699A07FFE7C2 - ) - } - } - - // MISC - assembly ("memory-safe") { - let p := mload(0x40) - returndatacopy(p, 0, returndatasize()) - revert(p, returndatasize()) - } - - assembly "evmasm" ("memory-safe") {} - - assembly { - for { let i := 0 } lt(i, 10) { i := add(i, 1) } { mstore(i, 7) } - - function sample(x, y) -> - someVeryLongVariableName, - anotherVeryLongVariableNameToTriggerNewline - { - someVeryLongVariableName := 0 - anotherVeryLongVariableNameToTriggerNewline := 0 - } - - function sample2( - someVeryLongVariableName, anotherVeryLongVariableNameToTriggerNewline - ) -> x, y { - x := someVeryLongVariableName - y := anotherVeryLongVariableNameToTriggerNewline - } - - function empty() {} - - function functionThatReturnsSevenValuesAndCanBeUsedInAssignment() -> - v1, - v2, - v3, - v4, - v5, - v6, - v7 - {} - - let zero:u32 := 0:u32 - let v:u256, t:u32 := sample(1, 2) - let x, y := sample2(2, 1) - - let val1, val2, val3, val4, val5, val6, val7 - val1, val2, val3, val4, val5, val6, val7 := - functionThatReturnsSevenValuesAndCanBeUsedInAssignment() - } - - assembly { - a := 1 /* some really really really long comment that should not fit in one line */ - } - } -} diff --git a/crates/fmt/testdata/YulStrings/tab.fmt.sol b/crates/fmt/testdata/YulStrings/tab.fmt.sol deleted file mode 100644 index 45c386f9d9b99..0000000000000 --- a/crates/fmt/testdata/YulStrings/tab.fmt.sol +++ /dev/null @@ -1,17 +0,0 @@ -// config: style = "tab" -contract Yul { - function test() external { - assembly { - let a := "abc" - let b := "abc" - let c := "abc":u32 - let d := "abc":u32 - let e := hex"deadbeef" - let f := hex"deadbeef" - let g := hex"deadbeef":u32 - let h := hex"deadbeef":u32 - datacopy(0, dataoffset("runtime"), datasize("runtime")) - return(0, datasize("runtime")) - } - } -}