Skip to content

Commit

Permalink
Merge pull request #267 from penelope-stripe/fix-test-dsl-style
Browse files Browse the repository at this point in the history
Fix handling of `it` and `describe` style tests
  • Loading branch information
fables-tales authored Dec 22, 2020
2 parents f15ee76 + 4a85635 commit 89f689d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
1 change: 0 additions & 1 deletion fixtures/small/more_methods_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
a.b(1).c

a do

end.after_block

a.<=(3)
Expand Down
16 changes: 16 additions & 0 deletions fixtures/small/rspec_its_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
it "a" \
"b" do
#hi
end

it "a" do
#hi
end

describe "foo" do
it "foo" do
end
end

RSpec.describe "bees" do
end
16 changes: 16 additions & 0 deletions fixtures/small/rspec_its_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
it "a" \
"b" do
#hi
end

it "a" do
#hi
end

describe "foo" do
it "foo" do
end
end

RSpec.describe "bees" do
end
53 changes: 44 additions & 9 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,11 @@ pub fn format_list_like_thing_items(
single_line: bool,
) -> bool {
let mut emitted_args = false;
let skip_magic_comments = args
.iter()
.any(|i| matches!(i, Expression::StringConcat(..)));
let args_count = args.len();

ps.magic_handle_comments_for_mulitiline_arrays(Box::new(|ps| {
let cls: Box<dyn FnOnce(&mut dyn ConcreteParserState)> = Box::new(|ps| {
for (idx, expr) in args.into_iter().enumerate() {
// this raise was present in the ruby source code of rubyfmt
// but I'm pretty sure it's categorically impossible now. Thanks
Expand Down Expand Up @@ -702,7 +704,13 @@ pub fn format_list_like_thing_items(
};
emitted_args = true;
}
}));
});

if skip_magic_comments {
cls(ps)
} else {
ps.magic_handle_comments_for_mulitiline_arrays(cls);
}
emitted_args
}

Expand Down Expand Up @@ -2214,7 +2222,29 @@ pub fn format_backref(ps: &mut dyn ConcreteParserState, backref: Backref) {
}
}

fn is_rspec_like_describe_call(cc: &[CallChainElement]) -> bool {
let is_bare_it_or_describe = match cc.get(0) {
Some(CallChainElement::IdentOrOpOrKeywordOrConst(IdentOrOpOrKeywordOrConst::Ident(
Ident(_, ident, _),
))) => ident == "it" || ident == "describe",
_ => false,
};

let is_rspec_describe = match (cc.get(0), cc.get(2)) {
(
Some(CallChainElement::VarRef(VarRef(_, VarRefType::Const(Const(_, c, _))))),
Some(CallChainElement::IdentOrOpOrKeywordOrConst(IdentOrOpOrKeywordOrConst::Ident(
Ident(_, i, _),
))),
) => c == "RSpec" && i == "describe",
_ => false,
};

is_bare_it_or_describe || is_rspec_describe
}

fn format_call_chain(ps: &mut dyn ConcreteParserState, cc: Vec<CallChainElement>) {
let elide_parens = is_rspec_like_describe_call(&cc);
for cc_elem in cc.into_iter() {
match cc_elem {
CallChainElement::Paren(p) => format_paren(ps, p),
Expand All @@ -2226,12 +2256,16 @@ fn format_call_chain(ps: &mut dyn ConcreteParserState, cc: Vec<CallChainElement>
CallChainElement::VarRef(vr) => format_var_ref(ps, vr),
CallChainElement::ArgsAddStarOrExpressionList(aas) => {
if !aas.is_empty() {
ps.breakable_of(
BreakableDelims::for_method_call(),
Box::new(|ps| {
format_list_like_thing(ps, aas, false);
}),
);
let cls: Box<dyn FnOnce(&mut dyn ConcreteParserState)> = Box::new(|ps| {
format_list_like_thing(ps, aas, elide_parens);
});

if elide_parens {
ps.emit_space();
cls(ps);
} else {
ps.breakable_of(BreakableDelims::for_method_call(), cls);
}
}
}
CallChainElement::DotTypeOrOp(d) => format_dot(ps, d),
Expand Down Expand Up @@ -2346,6 +2380,7 @@ pub fn format_do_block(ps: &mut dyn ConcreteParserState, do_block: DoBlock) {
true,
Box::new(|ps| {
ps.emit_newline();
ps.wind_line_forward();
format_bodystmt(ps, body);
}),
);
Expand Down

0 comments on commit 89f689d

Please sign in to comment.