From 43115f78e1faef26b7b2ed8e1991b40a938819c7 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Tue, 28 May 2024 16:26:55 +0900 Subject: [PATCH 1/3] Revert "fix test case ruby codes" This reverts commit 3d6aeec033cc9f102293f36cc3ecb34e23f778c2. --- .../rufo/formatter_source_specs/break.rb.spec | 27 ++++------------ .../rufo/formatter_source_specs/next.rb.spec | 27 +++------------- .../rufo/formatter_source_specs/redo.rb.spec | 6 +--- .../rufo/formatter_source_specs/retry.rb.spec | 8 +---- .../rufo/formatter_source_specs/yield.rb.spec | 32 ++++--------------- 5 files changed, 19 insertions(+), 81 deletions(-) diff --git a/spec/lib/rufo/formatter_source_specs/break.rb.spec b/spec/lib/rufo/formatter_source_specs/break.rb.spec index 95174f3b..f5f463e0 100644 --- a/spec/lib/rufo/formatter_source_specs/break.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/break.rb.spec @@ -1,44 +1,29 @@ #~# ORIGINAL break -loop do break -end #~# EXPECTED -loop do - break -end +break #~# ORIGINAL -loop do break 1 -end #~# EXPECTED -loop do - break 1 -end +break 1 #~# ORIGINAL -loop do + break 1 , 2 -end #~# EXPECTED -loop do - break 1, 2 -end +break 1, 2 #~# ORIGINAL -loop do break 1 , 2 -end #~# EXPECTED -loop do - break 1, - 2 -end +break 1, + 2 diff --git a/spec/lib/rufo/formatter_source_specs/next.rb.spec b/spec/lib/rufo/formatter_source_specs/next.rb.spec index 390d0565..f1fa5c0c 100644 --- a/spec/lib/rufo/formatter_source_specs/next.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/next.rb.spec @@ -1,46 +1,29 @@ #~# ORIGINAL next -loop do next -end #~# EXPECTED -loop do - next -end +next #~# ORIGINAL -loop do next 1 -end #~# EXPECTED -loop do - next 1 -end +next 1 #~# ORIGINAL -loop do next 1 , 2 -end #~# EXPECTED -loop do - next 1, 2 -end +next 1, 2 #~# ORIGINAL -loop do next 1 , 2 -end #~# EXPECTED -loop do - next 1, - 2 -end - +next 1, + 2 diff --git a/spec/lib/rufo/formatter_source_specs/redo.rb.spec b/spec/lib/rufo/formatter_source_specs/redo.rb.spec index 118ffd87..1109b983 100644 --- a/spec/lib/rufo/formatter_source_specs/redo.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/redo.rb.spec @@ -1,10 +1,6 @@ #~# ORIGINAL redo -loop do redo -end #~# EXPECTED -loop do - redo -end +redo diff --git a/spec/lib/rufo/formatter_source_specs/retry.rb.spec b/spec/lib/rufo/formatter_source_specs/retry.rb.spec index 40e6a830..b57d483f 100644 --- a/spec/lib/rufo/formatter_source_specs/retry.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/retry.rb.spec @@ -1,12 +1,6 @@ #~# ORIGINAL retry -begin -rescue retry -end #~# EXPECTED -begin -rescue - retry -end +retry diff --git a/spec/lib/rufo/formatter_source_specs/yield.rb.spec b/spec/lib/rufo/formatter_source_specs/yield.rb.spec index 84cc7664..e51b615e 100644 --- a/spec/lib/rufo/formatter_source_specs/yield.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/yield.rb.spec @@ -1,56 +1,36 @@ #~# ORIGINAL yield -def foo yield -end #~# EXPECTED -def foo - yield -end +yield #~# ORIGINAL -def foo yield 1 -end #~# EXPECTED -def foo - yield 1 -end +yield 1 #~# ORIGINAL -def foo yield 1 , 2 -end #~# EXPECTED -def foo - yield 1, 2 -end +yield 1, 2 #~# ORIGINAL -def foo yield 1 , 2 -end #~# EXPECTED -def foo - yield 1, - 2 -end +yield 1, + 2 #~# ORIGINAL -def foo yield( 1 , 2 ) -end #~# EXPECTED -def foo - yield(1, 2) -end +yield(1, 2) From 6172bbb7c04b02c6028c87ea6d0dc1292866b771 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Tue, 28 May 2024 16:56:31 +0900 Subject: [PATCH 2/3] support formatting unparsable standalone keyword ref: #319 --- lib/rufo/formatter.rb | 1 + lib/rufo/parser.rb | 44 +++++++++++++++++++++++++++++ spec/lib/rufo/erb_formatter_spec.rb | 5 ++++ 3 files changed, 50 insertions(+) diff --git a/lib/rufo/formatter.rb b/lib/rufo/formatter.rb index 45ad0f46..ccd24755 100644 --- a/lib/rufo/formatter.rb +++ b/lib/rufo/formatter.rb @@ -18,6 +18,7 @@ def initialize(code, **options) @tokens = Rufo::Parser.lex(code).reverse! @sexp = Rufo::Parser.sexp(code) + @sexp ||= Rufo::Parser.sexp_unparsable_code(code) # sexp being nil means that the code is not valid. # Parse the code so we get better error messages. diff --git a/lib/rufo/parser.rb b/lib/rufo/parser.rb index 1cf84940..f49c9e02 100644 --- a/lib/rufo/parser.rb +++ b/lib/rufo/parser.rb @@ -10,4 +10,48 @@ def compile_error(msg) def on_parse_error(msg) raise ::Rufo::SyntaxError.new(msg, lineno) end + + def self.sexp_unparsable_code(code) + code_type = detect_unparsable_code_type(code) + + case code_type + when :yield + extract_original_code_sexp( + "def __rufo_dummy; #{code}; end", + ->(exp) { exp => [:def, *, [:bodystmt, exps, *]]; exps } + ) + when :next, :break, :redo + extract_original_code_sexp( + "loop do; #{code}; end", + ->(exp) { exp => [:method_add_block, *, [:do_block, nil, [:bodystmt, [[:void_stmt], *exps], *]]]; exps } + ) + when :retry + extract_original_code_sexp( + "begin; rescue; #{code}; end", + ->(exp) { exp => [:begin, [:bodystmt, Array, [:rescue, nil, nil, exps, *], *]]; exps } + ) + end + end + + def self.detect_unparsable_code_type(code) + tokens = self.lex(code) + token = tokens.find { |_, kind| kind != :on_sp && kind != :on_ignored_nl } + + case token + in [_, :on_kw, "yield" | "next" | "break" | "retry" | "redo" => kw, _] + kw.to_sym + else + nil + end + end + + def self.extract_original_code_sexp(decorated_code, extractor) + sexp = self.sexp(decorated_code) + return nil unless sexp + + # [:program, [exp]] + exp = sexp[1][0] + code_exps = extractor.call(exp) + [:program, code_exps] + end end diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index 88b96562..e45e20e3 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -84,5 +84,10 @@ result = subject.format("<% a(nil) { %>\nabc\n<% } %>") expect(result).to eql("<% a(nil) { %>\nabc\n<% } %>") end + + it "formats standalone 'yield'" do + result = subject.format("<%=yield%>") + expect(result).to eql("<%= yield %>") + end end end From f4bbebb16accbe955384f6a6bdc343dfd0db0d15 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Wed, 29 May 2024 13:04:40 +0900 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a11aac1..69754473 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Fixed +- Fix error when formatting ERB ([#319](https://github.com/ruby-formatter/rufo/issues/319)) ### Changed - - Dropped support for Ruby 2.7 as it is end of life. ### Added