diff --git a/fixtures/small/aref_field_actual.rb b/fixtures/small/aref_field_actual.rb index 0fe360c2..6a8e0013 100644 --- a/fixtures/small/aref_field_actual.rb +++ b/fixtures/small/aref_field_actual.rb @@ -1,3 +1,23 @@ # As above a[0] = b # So below + +a[puts a] = "" +a[puts(a)] = "" +a[()] = "" +a[class A; def foo; puts a; end; end] = "" +a[begin; ""; rescue StandardException; ""; end] = "" +# Users can override #[]= to not have args +a[] = "" +# Users can override #[]= to have multiple args +a[1, 2] = "" + +a[puts a] +a[puts(a)] +a[()] +a[class A; def foo; puts a; end; end] +a[begin; ""; rescue StandardException; ""; end] +# Users can override #[] to not have args +a[] +# Users can override #[] to have multiple args +a[1, 2] diff --git a/fixtures/small/aref_field_expected.rb b/fixtures/small/aref_field_expected.rb index 0fe360c2..dffcc289 100644 --- a/fixtures/small/aref_field_expected.rb +++ b/fixtures/small/aref_field_expected.rb @@ -1,3 +1,47 @@ # As above a[0] = b # So below + +a[puts(a)] = "" +a[puts(a)] = "" +a[()] = "" +a[ + class A + def foo + puts(a) + end + end +] = "" +a[ + begin + "" + rescue StandardException + "" + end +] = "" +# Users can override #[]= to not have args +a[] = "" +# Users can override #[]= to have multiple args +a[1, 2] = "" + +a[puts(a)] +a[puts(a)] +a[()] +a[ + class A + def foo + puts(a) + end + end +] +a[ + begin + "" + rescue StandardException + "" + end +] +# Users can override #[] to not have args +a[] +# Users can override #[] to have multiple args +a[1, 2] diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index 31a4a24e..77723844 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -1755,19 +1755,18 @@ pub fn format_aref_field(ps: &mut dyn ConcreteParserState, af: ArefField) { false, Box::new(|ps| { format_expression(ps, *af.1); - let aab = af.2; - match aab.2 { - ToProcExpr::Present(_) => { - panic!("got a to_proc in an aref_field, should be impossible"); - } - ToProcExpr::NotPresent(_) => { - format_array_fast_path( - ps, - end_line, - Some((aab.1).into_args_add_star_or_expression_list()), - ); + + let index_expr = af.2; + let arr_contents = match index_expr { + None => None, + Some(ArgsAddBlockOrExpressionList::ArgsAddBlock(aab)) => { + Some(aab.1.into_args_add_star_or_expression_list()) } - } + Some(ArgsAddBlockOrExpressionList::ExpressionList(expr_list)) => Some( + ArgsAddStarOrExpressionListOrArgsForward::ExpressionList(expr_list), + ), + }; + format_array_fast_path(ps, end_line, arr_contents); }), ); @@ -2299,7 +2298,12 @@ fn format_constant_body(ps: &mut dyn ConcreteParserState, bodystmt: Box bool { - self.line_numbers.len() > 1 || self.any_collapsing_newline_has_heredoc_content() + self.line_numbers.len() > 1 + || self.any_collapsing_newline_has_heredoc_content() + || self.contains_hard_newline() } fn len(&self) -> usize { @@ -177,4 +179,13 @@ impl BreakableEntry { _ => false, }) } + + fn contains_hard_newline(&self) -> bool { + self.tokens.iter().any(|t| { + matches!( + t, + AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine) + ) + }) + } } diff --git a/librubyfmt/src/ripper_tree_types.rs b/librubyfmt/src/ripper_tree_types.rs index 8cb70ae7..0bdfacc8 100644 --- a/librubyfmt/src/ripper_tree_types.rs +++ b/librubyfmt/src/ripper_tree_types.rs @@ -431,7 +431,7 @@ def_tag!(aref_field_tag, "aref_field"); pub struct ArefField( pub aref_field_tag, pub Box, - pub ArgsAddBlock, + pub Option, pub LineCol, );