From aa4d3eb1fd2c713127e7d60a8a607f52971e8d26 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Sat, 11 Mar 2023 20:19:06 +0000 Subject: [PATCH 1/2] Fixtures --- fixtures/small/mlhs_paren_actual.rb | 2 ++ fixtures/small/mlhs_paren_expected.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/fixtures/small/mlhs_paren_actual.rb b/fixtures/small/mlhs_paren_actual.rb index 68c55ec2c..076af1e92 100644 --- a/fixtures/small/mlhs_paren_actual.rb +++ b/fixtures/small/mlhs_paren_actual.rb @@ -1,2 +1,4 @@ call do |arg, (paren1, paren2, (paren3, paren4))| end + +(a, *b) = ARGV diff --git a/fixtures/small/mlhs_paren_expected.rb b/fixtures/small/mlhs_paren_expected.rb index 68c55ec2c..076af1e92 100644 --- a/fixtures/small/mlhs_paren_expected.rb +++ b/fixtures/small/mlhs_paren_expected.rb @@ -1,2 +1,4 @@ call do |arg, (paren1, paren2, (paren3, paren4))| end + +(a, *b) = ARGV From 8b02d9ef4242403697280aa4708d354fe6566855 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Sat, 11 Mar 2023 20:19:39 +0000 Subject: [PATCH 2/2] Fix special case handling for rest params in assigned fields --- librubyfmt/src/format.rs | 75 +++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index b8feed896..e17f5976c 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -390,10 +390,16 @@ pub fn format_mlhs(ps: &mut dyn ConcreteParserState, mlhs: MLhs) { MLhsInner::Field(f) => format_field(ps, f), MLhsInner::Ident(i) => format_ident(ps, i), MLhsInner::RestParam(rp) => { + let special_case = + if ps.current_formatting_context() == FormattingContext::Assign { + SpecialCase::RestParamOutsideOfParamDef + } else { + SpecialCase::NoSpecialCase + }; format_rest_param( ps, Some(RestParamOr0OrExcessedComma::RestParam(rp)), - SpecialCase::NoSpecialCase, + special_case, ); } MLhsInner::VarField(vf) => format_var_field(ps, vf), @@ -1926,41 +1932,46 @@ pub fn format_massign(ps: &mut dyn ConcreteParserState, massign: MAssign) { ps.emit_indent(); } - ps.with_start_of_line( - false, + ps.with_formatting_context( + FormattingContext::Assign, Box::new(|ps| { - match massign.1 { - AssignableListOrMLhs::AssignableList(al) => { - let length = al.len(); - for (idx, v) in al.into_iter().enumerate() { - let is_rest_param = matches!(v, Assignable::RestParam(..)); - format_assignable(ps, v); - let last = idx == length - 1; - if !last { - ps.emit_comma_space(); + ps.with_start_of_line( + false, + Box::new(|ps| { + match massign.1 { + AssignableListOrMLhs::AssignableList(al) => { + let length = al.len(); + for (idx, v) in al.into_iter().enumerate() { + let is_rest_param = matches!(v, Assignable::RestParam(..)); + format_assignable(ps, v); + let last = idx == length - 1; + if !last { + ps.emit_comma_space(); + } + // `*foo = []` is valid ruby, but + // `*foo, = []` is not (but `foo, = []` is!), + // so in cases where the only assignable is a rest param, + // leave the comma out + if length == 1 && !is_rest_param { + ps.emit_comma(); + } + } } - // `*foo = []` is valid ruby, but - // `*foo, = []` is not (but `foo, = []` is!), - // so in cases where the only assignable is a rest param, - // leave the comma out - if length == 1 && !is_rest_param { - ps.emit_comma(); + AssignableListOrMLhs::MLhs(mlhs) => format_mlhs(ps, mlhs), + } + ps.emit_space(); + ps.emit_ident("=".to_string()); + ps.emit_space(); + match massign.2 { + MRHSOrArray::MRHS(mrhs) => { + format_mrhs(ps, Some(mrhs)); + } + MRHSOrArray::Array(array) => { + format_array(ps, array); } } - } - AssignableListOrMLhs::MLhs(mlhs) => format_mlhs(ps, mlhs), - } - ps.emit_space(); - ps.emit_ident("=".to_string()); - ps.emit_space(); - match massign.2 { - MRHSOrArray::MRHS(mrhs) => { - format_mrhs(ps, Some(mrhs)); - } - MRHSOrArray::Array(array) => { - format_array(ps, array); - } - } + }), + ); }), );