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 #319 #323

Merged
merged 3 commits into from
May 29, 2024
Merged
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rufo/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions lib/rufo/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/lib/rufo/erb_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 6 additions & 21 deletions spec/lib/rufo/formatter_source_specs/break.rb.spec
Original file line number Diff line number Diff line change
@@ -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
27 changes: 5 additions & 22 deletions spec/lib/rufo/formatter_source_specs/next.rb.spec
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions spec/lib/rufo/formatter_source_specs/redo.rb.spec
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#~# ORIGINAL redo
loop do

redo
end

#~# EXPECTED
loop do
redo
end
redo
8 changes: 1 addition & 7 deletions spec/lib/rufo/formatter_source_specs/retry.rb.spec
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#~# ORIGINAL retry
begin
rescue

retry
end

#~# EXPECTED
begin
rescue
retry
end
retry
32 changes: 6 additions & 26 deletions spec/lib/rufo/formatter_source_specs/yield.rb.spec
Original file line number Diff line number Diff line change
@@ -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)
Loading