From efba6d4b749298665d4a0108d1011d1597b2ab37 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 8 Feb 2025 14:12:07 +0900 Subject: [PATCH 1/3] add spec ref: #331 --- .../method_definition.rb.spec | 10 ++++++++++ .../method_definition_with_receiver.rb.spec | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spec/lib/rufo/formatter_source_specs/method_definition.rb.spec b/spec/lib/rufo/formatter_source_specs/method_definition.rb.spec index c0d995be..bdf487f8 100644 --- a/spec/lib/rufo/formatter_source_specs/method_definition.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/method_definition.rb.spec @@ -371,3 +371,13 @@ end def foo(a, ...) p(...) end + +#~# ORIGINAL issue_331 +def foo a: + a +end + +#~# EXPECTED +def foo(a:) + a +end diff --git a/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec b/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec index c497d363..48e4416c 100644 --- a/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec @@ -65,3 +65,18 @@ def a x;x end;def b y;y end;def c z;z end # comment def a(x); x end def b(y); y end def c(z); z end # comment + +#~# ORIGINAL issue_331 +#~# parens_in_def: :dynamic +class User + def self.by_uid uid: + joins(:authentications).where(authentications: { uid: }).first + end +end + +#~# EXPECTED +class User + def self.by_uid uid: + joins(:authentications).where(authentications: { uid: }).first + end +end From 052f241b3406ad30051ff65d66d16a324557b7dd Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Thu, 28 May 2026 20:49:43 +0900 Subject: [PATCH 2/3] fix one-line collapse on parens-less def with keyword arg A kwarg without a default value (e.g. `def foo a:`) followed by a newline-and-body had its trailing newline eaten by `skip_space_or_newline` in visit_params. With no newline left, indent_body folded the body onto the same line, producing syntactically invalid output like `def foo a: ...body... end`. Skip only spaces when the label has no default value; the consume_space path already handles space+newline for the value-bearing case. Fixes #331 --- lib/rufo/formatter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rufo/formatter.rb b/lib/rufo/formatter.rb index 35305203..d37d2d5b 100644 --- a/lib/rufo/formatter.rb +++ b/lib/rufo/formatter.rb @@ -2147,10 +2147,11 @@ def visit_params(node) # [:@label, "b:", [1, 20]] write label[1] next_token - skip_space_or_newline if value consume_space visit value + else + skip_space end end needs_comma = true From 30d5d87e659100e4671899e3b65f77ff3e75f8f1 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Thu, 28 May 2026 20:52:56 +0900 Subject: [PATCH 3/3] move issue_331 receiver spec to Ruby 3.1+ directory The spec relies on shorthand hash literal `{ uid: }`, which is Ruby 3.1+ syntax, so it cannot be parsed on Ruby 3.0. Move it under 3.1/ so it is only loaded on supported versions. --- .../3.1/method_definition_with_receiver.rb.spec | 14 ++++++++++++++ .../method_definition_with_receiver.rb.spec | 15 --------------- 2 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 spec/lib/rufo/formatter_source_specs/3.1/method_definition_with_receiver.rb.spec diff --git a/spec/lib/rufo/formatter_source_specs/3.1/method_definition_with_receiver.rb.spec b/spec/lib/rufo/formatter_source_specs/3.1/method_definition_with_receiver.rb.spec new file mode 100644 index 00000000..8e60ac04 --- /dev/null +++ b/spec/lib/rufo/formatter_source_specs/3.1/method_definition_with_receiver.rb.spec @@ -0,0 +1,14 @@ +#~# ORIGINAL issue_331 +#~# parens_in_def: :dynamic +class User + def self.by_uid uid: + joins(:authentications).where(authentications: { uid: }).first + end +end + +#~# EXPECTED +class User + def self.by_uid uid: + joins(:authentications).where(authentications: { uid: }).first + end +end diff --git a/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec b/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec index 48e4416c..c497d363 100644 --- a/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/method_definition_with_receiver.rb.spec @@ -65,18 +65,3 @@ def a x;x end;def b y;y end;def c z;z end # comment def a(x); x end def b(y); y end def c(z); z end # comment - -#~# ORIGINAL issue_331 -#~# parens_in_def: :dynamic -class User - def self.by_uid uid: - joins(:authentications).where(authentications: { uid: }).first - end -end - -#~# EXPECTED -class User - def self.by_uid uid: - joins(:authentications).where(authentications: { uid: }).first - end -end