From 452e43bd62d89b71e73ea507c974923d16f30a73 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 11 Nov 2023 14:46:26 +0100 Subject: [PATCH] Simplify access patterns for config Allow accessing properties directly Fixes #805 --- CHANGELOG.md | 4 ++ src/cli/config.rs | 28 ++++----- src/context.rs | 6 +- src/editorconfig.rs | 110 ++++++++++++++++----------------- src/formatters/functions.rs | 6 +- src/lib.rs | 61 ++++++++++++++---- tests/test_call_parentheses.rs | 7 ++- tests/test_no_call_parens.rs | 6 +- tests/test_quote_style.rs | 5 +- tests/tests.rs | 12 +++- 10 files changed, 149 insertions(+), 96 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e88b7a0a..d8c4e773 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Updated parser crate with following changes: + - Support Luau floor division (`//`) - Fix Luau string interpolation parsing - Fix Luau `\z` escape parsing +- Simplified access and modification patterns for StyLua configuration. You can now access the properties directly + - **Deprecated:** the old access patterns of `.property()` and `.with_property()` are now deprecated + ### Fixed - Wasm build now correctly supports configuring sort requires ([#818](https://github.com/JohnnyMorganz/StyLua/issues/818)) diff --git a/src/cli/config.rs b/src/cli/config.rs index ea54f23f..1b7ae4af 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -157,28 +157,28 @@ pub fn load_overrides(config: Config, opt: &Opt) -> Config { let mut new_config = config; if let Some(column_width) = opt.format_opts.column_width { - new_config = new_config.with_column_width(column_width); + new_config.column_width = column_width; }; if let Some(line_endings) = opt.format_opts.line_endings { - new_config = new_config.with_line_endings(line_endings.into()); + new_config.line_endings = line_endings.into(); }; if let Some(indent_type) = opt.format_opts.indent_type { - new_config = new_config.with_indent_type(indent_type.into()); + new_config.indent_type = indent_type.into(); }; if let Some(indent_width) = opt.format_opts.indent_width { - new_config = new_config.with_indent_width(indent_width); + new_config.indent_width = indent_width; }; if let Some(quote_style) = opt.format_opts.quote_style { - new_config = new_config.with_quote_style(quote_style.into()); + new_config.quote_style = quote_style.into(); }; if let Some(call_parentheses) = opt.format_opts.call_parentheses { - new_config = new_config.with_call_parentheses(call_parentheses.into()); + new_config.call_parentheses = call_parentheses.into(); }; if let Some(collapse_simple_statement) = opt.format_opts.collapse_simple_statement { - new_config = new_config.with_collapse_simple_statement(collapse_simple_statement.into()); + new_config.collapse_simple_statement = collapse_simple_statement.into(); } if opt.format_opts.sort_requires { - new_config = new_config.with_sort_requires(SortRequiresConfig::default().set_enabled(true)) + new_config.sort_requires = SortRequiresConfig { enabled: true } } new_config @@ -195,7 +195,7 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--column-width", "80"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.column_width(), 80); + assert_eq!(config.column_width, 80); } #[test] @@ -203,7 +203,7 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--line-endings", "Windows"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.line_endings(), LineEndings::Windows); + assert_eq!(config.line_endings, LineEndings::Windows); } #[test] @@ -211,7 +211,7 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--indent-type", "Spaces"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.indent_type(), IndentType::Spaces); + assert_eq!(config.indent_type, IndentType::Spaces); } #[test] @@ -219,7 +219,7 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--indent-width", "2"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.indent_width(), 2); + assert_eq!(config.indent_width, 2); } #[test] @@ -227,7 +227,7 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--quote-style", "ForceSingle"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.quote_style(), QuoteStyle::ForceSingle); + assert_eq!(config.quote_style, QuoteStyle::ForceSingle); } #[test] @@ -235,6 +235,6 @@ mod tests { let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--call-parentheses", "None"]); let default_config = Config::new(); let config = load_overrides(default_config, &override_opt); - assert_eq!(config.call_parentheses(), CallParenType::None); + assert_eq!(config.call_parentheses, CallParenType::None); } } diff --git a/src/context.rs b/src/context.rs index a44371c6..d6d21314 100644 --- a/src/context.rs +++ b/src/context.rs @@ -127,12 +127,14 @@ impl Context { FormatNode::Normal } + #[allow(deprecated)] pub fn should_omit_string_parens(&self) -> bool { self.config().no_call_parentheses || self.config().call_parentheses == CallParenType::None || self.config().call_parentheses == CallParenType::NoSingleString } + #[allow(deprecated)] pub fn should_omit_table_parens(&self) -> bool { self.config().no_call_parentheses || self.config().call_parentheses == CallParenType::None @@ -141,14 +143,14 @@ impl Context { pub fn should_collapse_simple_functions(&self) -> bool { matches!( - self.config().collapse_simple_statement(), + self.config().collapse_simple_statement, CollapseSimpleStatement::FunctionOnly | CollapseSimpleStatement::Always ) } pub fn should_collapse_simple_conditionals(&self) -> bool { matches!( - self.config().collapse_simple_statement(), + self.config().collapse_simple_statement, CollapseSimpleStatement::ConditionalOnly | CollapseSimpleStatement::Always ) } diff --git a/src/editorconfig.rs b/src/editorconfig.rs index 151d3a32..32ea6cfd 100644 --- a/src/editorconfig.rs +++ b/src/editorconfig.rs @@ -82,77 +82,73 @@ property_choice! { // Override StyLua config with EditorConfig properties fn load(mut config: Config, properties: &Properties) -> Config { if let Ok(end_of_line) = properties.get::() { - config = match end_of_line { - EndOfLine::Cr | EndOfLine::Lf => config.with_line_endings(LineEndings::Unix), - EndOfLine::CrLf => config.with_line_endings(LineEndings::Windows), + match end_of_line { + EndOfLine::Cr | EndOfLine::Lf => config.line_endings = LineEndings::Unix, + EndOfLine::CrLf => config.line_endings = LineEndings::Windows, } } if let Ok(indent_size) = properties.get::() { - config = match indent_size { - IndentSize::Value(indent_width) => config.with_indent_width(indent_width), + match indent_size { + IndentSize::Value(indent_width) => config.indent_width = indent_width, IndentSize::UseTabWidth => { - properties - .get::() - .map_or(config, |tab_width| match tab_width { - TabWidth::Value(indent_width) => config.with_indent_width(indent_width), - }) + if let Ok(TabWidth::Value(indent_width)) = properties.get::() { + config.indent_width = indent_width + } } } } if let Ok(indent_style) = properties.get::() { - config = match indent_style { - IndentStyle::Tabs => config.with_indent_type(IndentType::Tabs), - IndentStyle::Spaces => config.with_indent_type(IndentType::Spaces), + match indent_style { + IndentStyle::Tabs => config.indent_type = IndentType::Tabs, + IndentStyle::Spaces => config.indent_type = IndentType::Spaces, } } if let Ok(max_line_length) = properties.get::() { - config = match max_line_length { - MaxLineLen::Value(column_width) => config.with_column_width(column_width), - MaxLineLen::Off => config.with_column_width(usize::MAX), + match max_line_length { + MaxLineLen::Value(column_width) => config.column_width = column_width, + MaxLineLen::Off => config.column_width = usize::MAX, } } if let Ok(quote_type) = properties.get::() { - config = match quote_type { - QuoteTypeChoice::Double => config.with_quote_style(QuoteStyle::AutoPreferDouble), - QuoteTypeChoice::Single => config.with_quote_style(QuoteStyle::AutoPreferSingle), - QuoteTypeChoice::Auto => config, + match quote_type { + QuoteTypeChoice::Double => config.quote_style = QuoteStyle::AutoPreferDouble, + QuoteTypeChoice::Single => config.quote_style = QuoteStyle::AutoPreferSingle, + QuoteTypeChoice::Auto => (), } } if let Ok(call_parentheses) = properties.get::() { - config = match call_parentheses { - CallParenthesesChoice::Always => config.with_call_parentheses(CallParenType::Always), + match call_parentheses { + CallParenthesesChoice::Always => config.call_parentheses = CallParenType::Always, CallParenthesesChoice::NoSingleString => { - config.with_call_parentheses(CallParenType::NoSingleString) + config.call_parentheses = CallParenType::NoSingleString } CallParenthesesChoice::NoSingleTable => { - config.with_call_parentheses(CallParenType::NoSingleTable) + config.call_parentheses = CallParenType::NoSingleTable } - CallParenthesesChoice::None => config.with_call_parentheses(CallParenType::None), + CallParenthesesChoice::None => config.call_parentheses = CallParenType::None, } } if let Ok(collapse_simple_statement) = properties.get::() { - config = match collapse_simple_statement { + match collapse_simple_statement { CollapseSimpleStatementChoice::Never => { - config.with_collapse_simple_statement(CollapseSimpleStatement::Never) + config.collapse_simple_statement = CollapseSimpleStatement::Never } CollapseSimpleStatementChoice::FunctionOnly => { - config.with_collapse_simple_statement(CollapseSimpleStatement::FunctionOnly) + config.collapse_simple_statement = CollapseSimpleStatement::FunctionOnly } CollapseSimpleStatementChoice::ConditionalOnly => { - config.with_collapse_simple_statement(CollapseSimpleStatement::ConditionalOnly) + config.collapse_simple_statement = CollapseSimpleStatement::ConditionalOnly } CollapseSimpleStatementChoice::Always => { - config.with_collapse_simple_statement(CollapseSimpleStatement::Always) + config.collapse_simple_statement = CollapseSimpleStatement::Always } } } if let Ok(sort_requires) = properties.get::() { - config = match sort_requires { - SortRequiresChoice::True => { - config.with_sort_requires(SortRequiresConfig { enabled: true }) - } + match sort_requires { + SortRequiresChoice::True => config.sort_requires = SortRequiresConfig { enabled: true }, SortRequiresChoice::False => { - config.with_sort_requires(SortRequiresConfig { enabled: false }) + config.sort_requires = SortRequiresConfig { enabled: false } } } } @@ -189,7 +185,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("end_of_line", "CR"); let config = Config::from(&properties); - assert_eq!(config.line_endings(), LineEndings::Unix); + assert_eq!(config.line_endings, LineEndings::Unix); } #[test] @@ -197,7 +193,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("end_of_line", "lf"); let config = Config::from(&properties); - assert_eq!(config.line_endings(), LineEndings::Unix); + assert_eq!(config.line_endings, LineEndings::Unix); } #[test] @@ -205,7 +201,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("end_of_line", "CrLf"); let config = Config::from(&properties); - assert_eq!(config.line_endings(), LineEndings::Windows); + assert_eq!(config.line_endings, LineEndings::Windows); } #[test] @@ -213,7 +209,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("indent_size", "2"); let config = Config::from(&properties); - assert_eq!(config.indent_width(), 2); + assert_eq!(config.indent_width, 2); } #[test] @@ -222,7 +218,7 @@ mod tests { properties.insert_raw_for_key("tab_width", "8"); properties.insert_raw_for_key("indent_size", "tab"); let config = Config::from(&properties); - assert_eq!(config.indent_width(), 8); + assert_eq!(config.indent_width, 8); } #[test] @@ -230,7 +226,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("indent_style", "space"); let config = Config::from(&properties); - assert_eq!(config.indent_type(), IndentType::Spaces); + assert_eq!(config.indent_type, IndentType::Spaces); } #[test] @@ -238,7 +234,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("indent_style", "Tab"); let config = Config::from(&properties); - assert_eq!(config.indent_type(), IndentType::Tabs); + assert_eq!(config.indent_type, IndentType::Tabs); } #[test] @@ -246,7 +242,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("max_line_length", "80"); let config = Config::from(&properties); - assert_eq!(config.column_width(), 80); + assert_eq!(config.column_width, 80); } #[test] @@ -254,7 +250,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("max_line_length", "off"); let config = Config::from(&properties); - assert_eq!(config.column_width(), usize::MAX); + assert_eq!(config.column_width, usize::MAX); } #[test] @@ -262,7 +258,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("quote_type", "double"); let config = Config::from(&properties); - assert_eq!(config.quote_style(), QuoteStyle::AutoPreferDouble); + assert_eq!(config.quote_style, QuoteStyle::AutoPreferDouble); } #[test] @@ -270,7 +266,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("quote_type", "Single"); let config = Config::from(&properties); - assert_eq!(config.quote_style(), QuoteStyle::AutoPreferSingle); + assert_eq!(config.quote_style, QuoteStyle::AutoPreferSingle); } #[test] @@ -278,7 +274,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("quote_type", "auto"); let config = Config::from(&properties); - assert_eq!(config.quote_style(), QuoteStyle::AutoPreferDouble); + assert_eq!(config.quote_style, QuoteStyle::AutoPreferDouble); } #[test] @@ -286,7 +282,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("call_parentheses", "always"); let config = Config::from(&properties); - assert_eq!(config.call_parentheses(), CallParenType::Always); + assert_eq!(config.call_parentheses, CallParenType::Always); } #[test] @@ -294,7 +290,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("call_parentheses", "NoSingleString"); let config = Config::from(&properties); - assert_eq!(config.call_parentheses(), CallParenType::NoSingleString); + assert_eq!(config.call_parentheses, CallParenType::NoSingleString); } #[test] @@ -302,7 +298,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("call_parentheses", "NoSingleTable"); let config = Config::from(&properties); - assert_eq!(config.call_parentheses(), CallParenType::NoSingleTable); + assert_eq!(config.call_parentheses, CallParenType::NoSingleTable); } #[test] @@ -310,7 +306,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("call_parentheses", "None"); let config = Config::from(&properties); - assert_eq!(config.call_parentheses(), CallParenType::None); + assert_eq!(config.call_parentheses, CallParenType::None); } #[test] @@ -319,7 +315,7 @@ mod tests { properties.insert_raw_for_key("collapse_simple_statement", "Never"); let config = Config::from(&properties); assert_eq!( - config.collapse_simple_statement(), + config.collapse_simple_statement, CollapseSimpleStatement::Never ); } @@ -330,7 +326,7 @@ mod tests { properties.insert_raw_for_key("collapse_simple_statement", "FunctionOnly"); let config = Config::from(&properties); assert_eq!( - config.collapse_simple_statement(), + config.collapse_simple_statement, CollapseSimpleStatement::FunctionOnly ); } @@ -341,7 +337,7 @@ mod tests { properties.insert_raw_for_key("collapse_simple_statement", "ConditionalOnly"); let config = Config::from(&properties); assert_eq!( - config.collapse_simple_statement(), + config.collapse_simple_statement, CollapseSimpleStatement::ConditionalOnly ); } @@ -352,7 +348,7 @@ mod tests { properties.insert_raw_for_key("collapse_simple_statement", "always"); let config = Config::from(&properties); assert_eq!( - config.collapse_simple_statement(), + config.collapse_simple_statement, CollapseSimpleStatement::Always ); } @@ -362,7 +358,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("sort_requires", "true"); let config = Config::from(&properties); - assert!(config.sort_requires().enabled); + assert!(config.sort_requires.enabled); } #[test] @@ -370,7 +366,7 @@ mod tests { let mut properties = Properties::new(); properties.insert_raw_for_key("sort_requires", "false"); let config = Config::from(&properties); - assert!(!config.sort_requires().enabled); + assert!(!config.sort_requires.enabled); } #[test] diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index e7d7ff3a..33ff8c80 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -365,7 +365,7 @@ pub fn format_function_args( arguments, } => { // Handle config where parentheses are omitted, and there is only one argument - if ctx.config().call_parentheses() != CallParenType::Input + if ctx.config().call_parentheses != CallParenType::Input && (ctx.should_omit_string_parens() || ctx.should_omit_table_parens()) && arguments.len() == 1 && !matches!(call_next_node, FunctionCallNextNode::ObscureWithoutParens) @@ -483,7 +483,7 @@ pub fn format_function_args( } FunctionArgs::String(token_reference) => { - if ctx.config().call_parentheses() == CallParenType::Input + if ctx.config().call_parentheses == CallParenType::Input || (ctx.should_omit_string_parens() && !matches!(call_next_node, FunctionCallNextNode::ObscureWithoutParens)) { @@ -522,7 +522,7 @@ pub fn format_function_args( } FunctionArgs::TableConstructor(table_constructor) => { - if ctx.config().call_parentheses() == CallParenType::Input + if ctx.config().call_parentheses == CallParenType::Input || (ctx.should_omit_table_parens() && !matches!(call_next_node, FunctionCallNextNode::ObscureWithoutParens)) { diff --git a/src/lib.rs b/src/lib.rs index b90d44cf..91fd2af0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,8 +100,8 @@ pub enum CollapseSimpleStatement { #[derive(Debug, Copy, Clone, Deserialize)] #[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)] pub struct Range { - start: Option, - end: Option, + pub start: Option, + pub end: Option, } #[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)] @@ -120,7 +120,7 @@ impl Range { #[cfg_attr(feature = "serialize", derive(serde::Serialize))] pub struct SortRequiresConfig { /// Whether the sort requires codemod is enabled - enabled: bool, + pub enabled: bool, } #[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)] @@ -128,9 +128,11 @@ impl SortRequiresConfig { pub fn new() -> Self { SortRequiresConfig::default() } + #[deprecated(since = "0.19.0", note = "access `.enabled` directly instead")] pub fn enabled(&self) -> bool { self.enabled } + #[deprecated(since = "0.19.0", note = "modify `.enabled` directly instead")] pub fn set_enabled(&self, enabled: bool) -> Self { Self { enabled } } @@ -145,20 +147,21 @@ pub struct Config { /// The approximate line length to use when printing the code. /// This is used as a guide to determine when to wrap lines, but note /// that this is not a hard upper bound. - column_width: usize, + pub column_width: usize, /// The type of line endings to use. - line_endings: LineEndings, + pub line_endings: LineEndings, /// The type of indents to use. - indent_type: IndentType, + pub indent_type: IndentType, /// The width of a single indentation level. /// If `indent_type` is set to [`IndentType::Spaces`], then this is the number of spaces to use. /// If `indent_type` is set to [`IndentType::Tabs`], then this is used as a heuristic to guide when to wrap lines. - indent_width: usize, + pub indent_width: usize, /// The style of quotes to use in string literals. - quote_style: QuoteStyle, + pub quote_style: QuoteStyle, /// Whether to omit parentheses around function calls which take a single string literal or table. /// This is added for adoption reasons only, and is not recommended for new work. - no_call_parentheses: bool, + #[deprecated(note = "use `call_parentheses` instead")] + pub no_call_parentheses: bool, /// When to use call parentheses. /// if call_parentheses is set to [`CallParenType::Always`] call parentheses is always applied. /// if call_parentheses is set to [`CallParenType::NoSingleTable`] call parentheses is omitted when @@ -167,13 +170,13 @@ pub struct Config { /// function is called with only one table argument. /// if call_parentheses is set to [`CallParenType::None`] call parentheses is omitted when /// function is called with only one table or string argument (same as no_call_parentheses). - call_parentheses: CallParenType, + pub call_parentheses: CallParenType, /// Whether we should collapse simple structures like functions or guard statements /// if set to [`CollapseSimpleStatement::None`] structures are never collapsed. /// if set to [`CollapseSimpleStatement::FunctionOnly`] then simple functions (i.e., functions with a single laststmt) can be collapsed - collapse_simple_statement: CollapseSimpleStatement, + pub collapse_simple_statement: CollapseSimpleStatement, /// Configuration for the sort requires codemod - sort_requires: SortRequiresConfig, + pub sort_requires: SortRequiresConfig, } #[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)] @@ -184,45 +187,57 @@ impl Config { } /// Returns the current configured column width + #[deprecated(since = "0.19.0", note = "access `.column_width` directly instead")] pub fn column_width(&self) -> usize { self.column_width } /// Returns the current configured line endings + #[deprecated(since = "0.19.0", note = "access `.line_endings` directly instead")] pub fn line_endings(&self) -> LineEndings { self.line_endings } /// Returns the current configured indent type + #[deprecated(since = "0.19.0", note = "access `.indent_type` directly instead")] pub fn indent_type(&self) -> IndentType { self.indent_type } /// Returns the current configured indent width + #[deprecated(since = "0.19.0", note = "access `.indent_width` directly instead")] pub fn indent_width(&self) -> usize { self.indent_width } /// Returns the current configured quote style + #[deprecated(since = "0.19.0", note = "access `.quote_style` directly instead")] pub fn quote_style(&self) -> QuoteStyle { self.quote_style } /// Returns the current configured call parentheses style + #[deprecated(since = "0.19.0", note = "access `.call_parentheses` directly instead")] pub fn call_parentheses(&self) -> CallParenType { self.call_parentheses } + #[deprecated( + since = "0.19.0", + note = "access `.collapse_simple_statement` directly instead" + )] pub fn collapse_simple_statement(&self) -> CollapseSimpleStatement { self.collapse_simple_statement } /// Returns the current sort requires codemod configuration + #[deprecated(since = "0.19.0", note = "access `.sort_requires` directly instead")] pub fn sort_requires(&self) -> SortRequiresConfig { self.sort_requires } /// Returns a new config with the given column width + #[deprecated(since = "0.19.0", note = "modify `.column_width` directly instead")] pub fn with_column_width(self, column_width: usize) -> Self { Self { column_width, @@ -231,6 +246,7 @@ impl Config { } /// Returns a new config with the given line endings + #[deprecated(since = "0.19.0", note = "modify `.line_endings` directly instead")] pub fn with_line_endings(self, line_endings: LineEndings) -> Self { Self { line_endings, @@ -239,6 +255,7 @@ impl Config { } /// Returns a new config with the given indent type + #[deprecated(since = "0.19.0", note = "modify `.indent_type` directly instead")] pub fn with_indent_type(self, indent_type: IndentType) -> Self { Self { indent_type, @@ -247,6 +264,7 @@ impl Config { } /// Returns a new config with the given indent width + #[deprecated(since = "0.19.0", note = "modify `.indent_width` directly instead")] pub fn with_indent_width(self, indent_width: usize) -> Self { Self { indent_width, @@ -255,6 +273,7 @@ impl Config { } /// Returns a new config with the given quote style + #[deprecated(since = "0.19.0", note = "modify `.quote_style` directly instead")] pub fn with_quote_style(self, quote_style: QuoteStyle) -> Self { Self { quote_style, @@ -263,13 +282,17 @@ impl Config { } /// Returns a new config with the given value for `no_call_parentheses` + #[deprecated(note = "use `call_parentheses")] pub fn with_no_call_parentheses(self, no_call_parentheses: bool) -> Self { + #[allow(deprecated)] Self { no_call_parentheses, ..self } } + /// Returns a new config with the given call parentheses type + #[deprecated(since = "0.19.0", note = "modify `.call_parentheses` directly instead")] pub fn with_call_parentheses(self, call_parentheses: CallParenType) -> Self { Self { call_parentheses, @@ -277,6 +300,10 @@ impl Config { } } + #[deprecated( + since = "0.19.0", + note = "modify `.collapse_simple_statement` directly instead" + )] pub fn with_collapse_simple_statement( self, collapse_simple_statement: CollapseSimpleStatement, @@ -288,6 +315,7 @@ impl Config { } /// Returns a new config with the given sort requires configuration + #[deprecated(since = "0.19.0", note = "modify `.sort_requires` directly instead")] pub fn with_sort_requires(self, sort_requires: SortRequiresConfig) -> Self { Self { sort_requires, @@ -298,6 +326,7 @@ impl Config { impl Default for Config { fn default() -> Self { + #[allow(deprecated)] Self { column_width: 120, line_endings: LineEndings::default(), @@ -354,7 +383,7 @@ pub fn format_ast( let ctx = Context::new(config, range); // Perform require sorting beforehand if necessary - let input_ast = match config.sort_requires().enabled() { + let input_ast = match config.sort_requires.enabled { true => sort_requires::sort_requires(&ctx, input_ast), false => input_ast, }; @@ -453,36 +482,42 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_config_column_width() { let new_config = Config::new().with_column_width(80); assert_eq!(new_config.column_width(), 80); } #[test] + #[allow(deprecated)] fn test_config_line_endings() { let new_config = Config::new().with_line_endings(LineEndings::Windows); assert_eq!(new_config.line_endings(), LineEndings::Windows); } #[test] + #[allow(deprecated)] fn test_config_indent_type() { let new_config = Config::new().with_indent_type(IndentType::Spaces); assert_eq!(new_config.indent_type(), IndentType::Spaces); } #[test] + #[allow(deprecated)] fn test_config_indent_width() { let new_config = Config::new().with_indent_width(2); assert_eq!(new_config.indent_width(), 2); } #[test] + #[allow(deprecated)] fn test_config_quote_style() { let new_config = Config::new().with_quote_style(QuoteStyle::ForceDouble); assert_eq!(new_config.quote_style(), QuoteStyle::ForceDouble); } #[test] + #[allow(deprecated)] fn test_config_call_parentheses() { let new_config = Config::new().with_call_parentheses(CallParenType::None); assert_eq!(new_config.call_parentheses(), CallParenType::None); diff --git a/tests/test_call_parentheses.rs b/tests/test_call_parentheses.rs index 125dda44..8e153248 100644 --- a/tests/test_call_parentheses.rs +++ b/tests/test_call_parentheses.rs @@ -1,9 +1,12 @@ use stylua_lib::{format_code, CallParenType, Config, OutputVerification}; -fn format(paren_type: CallParenType, input: &str) -> String { +fn format(call_parentheses: CallParenType, input: &str) -> String { format_code( input, - Config::default().with_call_parentheses(paren_type), + Config { + call_parentheses, + ..Config::default() + }, None, OutputVerification::None, ) diff --git a/tests/test_no_call_parens.rs b/tests/test_no_call_parens.rs index 34dc2203..221b10fc 100644 --- a/tests/test_no_call_parens.rs +++ b/tests/test_no_call_parens.rs @@ -3,7 +3,11 @@ use stylua_lib::{format_code, Config, OutputVerification}; fn format(input: &str) -> String { format_code( input, - Config::default().with_no_call_parentheses(true), + #[allow(deprecated)] + Config { + no_call_parentheses: true, + ..Config::default() + }, None, OutputVerification::None, ) diff --git a/tests/test_quote_style.rs b/tests/test_quote_style.rs index 95259cd6..3ce8fd68 100644 --- a/tests/test_quote_style.rs +++ b/tests/test_quote_style.rs @@ -3,7 +3,10 @@ use stylua_lib::{format_code, Config, OutputVerification, QuoteStyle}; fn format(input: &str, quote_style: QuoteStyle) -> String { format_code( input, - Config::default().with_quote_style(quote_style), + Config { + quote_style, + ..Config::default() + }, None, OutputVerification::None, ) diff --git a/tests/tests.rs b/tests/tests.rs index 9fc11f0e..d38768ab 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -84,7 +84,10 @@ fn test_collapse_single_statement() { let contents = std::fs::read_to_string(path).unwrap(); insta::assert_snapshot!(format_code( &contents, - Config::default().with_collapse_simple_statement(CollapseSimpleStatement::Always), + Config { + collapse_simple_statement: CollapseSimpleStatement::Always, + ..Config::default() + }, None, OutputVerification::None ) @@ -103,7 +106,10 @@ fn test_collapse_single_statement_lua_52() { goto continue end "###, - Config::default().with_collapse_simple_statement(CollapseSimpleStatement::Always), + Config { + collapse_simple_statement: CollapseSimpleStatement::Always, + ..Config::default() + }, None, OutputVerification::None ) @@ -120,7 +126,7 @@ fn test_sort_requires() { let contents = std::fs::read_to_string(path).unwrap(); insta::assert_snapshot!(format_code( &contents, - Config::default().with_sort_requires(SortRequiresConfig::default().set_enabled(true)), + Config { sort_requires: SortRequiresConfig { enabled: true }, ..Config::default()}, None, OutputVerification::None )