Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix special case when formatting paren-wrapped MLhs #419

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fixtures/small/mlhs_paren_actual.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
call do |arg, (paren1, paren2, (paren3, paren4))|
end

(a, *b) = ARGV
2 changes: 2 additions & 0 deletions fixtures/small/mlhs_paren_expected.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
call do |arg, (paren1, paren2, (paren3, paren4))|
end

(a, *b) = ARGV
75 changes: 43 additions & 32 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
}
}),
);
}),
);

Expand Down