Skip to content

Commit

Permalink
Handle arbitrary expression types in aref_field nodes (#381)
Browse files Browse the repository at this point in the history
* Fixtures

* Handle all expression lists in aref fields

* Add fixtures for non-assign arefs

* Fix multiline item rendering for expressions with hard newlines in them
  • Loading branch information
reese authored Dec 3, 2022
1 parent 4d090e0 commit d6ee1c7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
20 changes: 20 additions & 0 deletions fixtures/small/aref_field_actual.rb
Original file line number Diff line number Diff line change
@@ -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]
44 changes: 44 additions & 0 deletions fixtures/small/aref_field_expected.rb
Original file line number Diff line number Diff line change
@@ -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]
30 changes: 17 additions & 13 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}),
);

Expand Down Expand Up @@ -2299,7 +2298,12 @@ fn format_constant_body(ps: &mut dyn ConcreteParserState, bodystmt: Box<BodyStmt
}));

ps.on_line(end_line);
ps.emit_end();
ps.with_start_of_line(
true,
Box::new(|ps| {
ps.emit_end();
}),
);
if ps.at_start_of_line() {
ps.emit_newline();
}
Expand Down
15 changes: 13 additions & 2 deletions librubyfmt/src/render_targets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::delimiters::BreakableDelims;
use crate::line_tokens::{AbstractLineToken, ConcreteLineTokenAndTargets};
use crate::line_tokens::{AbstractLineToken, ConcreteLineToken, ConcreteLineTokenAndTargets};
use crate::parser_state::FormattingContext;
use crate::types::{ColNumber, LineNumber};
use std::collections::HashSet;
Expand Down Expand Up @@ -144,7 +144,9 @@ impl AbstractTokenTarget for BreakableEntry {
}

fn is_multiline(&self) -> 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 {
Expand Down Expand Up @@ -177,4 +179,13 @@ impl BreakableEntry {
_ => false,
})
}

fn contains_hard_newline(&self) -> bool {
self.tokens.iter().any(|t| {
matches!(
t,
AbstractLineToken::ConcreteLineToken(ConcreteLineToken::HardNewLine)
)
})
}
}
2 changes: 1 addition & 1 deletion librubyfmt/src/ripper_tree_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def_tag!(aref_field_tag, "aref_field");
pub struct ArefField(
pub aref_field_tag,
pub Box<Expression>,
pub ArgsAddBlock,
pub Option<ArgsAddBlockOrExpressionList>,
pub LineCol,
);

Expand Down

0 comments on commit d6ee1c7

Please sign in to comment.