Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## HEAD (unreleased)

- Support "endless" oneline method definitions for Ruby 3+ (https://github.com/zombocom/dead_end/pull/80)
- Reduce timeout to 1 second (https://github.com/zombocom/dead_end/pull/79)
- Logically consecutive lines (such as chained methods are now joined) (https://github.com/zombocom/dead_end/pull/78)
- Output improvement for cases where the only line is an single `end` (https://github.com/zombocom/dead_end/pull/78)
Expand Down
34 changes: 34 additions & 0 deletions lib/dead_end/code_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def initialize(line:, index:, lex:)
end_count += 1 if lex.is_end?
end

kw_count -= oneliner_method_count

@is_kw = (kw_count - end_count) > 0
@is_end = (end_count - kw_count) > 0
end
Expand Down Expand Up @@ -195,5 +197,37 @@ def trailing_slash?

last.token == TRAILING_SLASH
end

# Endless method detection
#
# From https://github.com/ruby/irb/commit/826ae909c9c93a2ddca6f9cfcd9c94dbf53d44ab
# Detecting a "oneliner" seems to need a state machine.
# This can be done by looking mostly at the "state" (last value):
#
# ENDFN -> BEG (token = '=' ) -> END
#
private def oneliner_method_count
oneliner_count = 0
in_oneliner_def = nil

@lex.each do |lex|
if in_oneliner_def.nil?
in_oneliner_def = :ENDFN if lex.state.allbits?(Ripper::EXPR_ENDFN)
elsif lex.state.allbits?(Ripper::EXPR_ENDFN)
# Continue
elsif lex.state.allbits?(Ripper::EXPR_BEG)
in_oneliner_def = :BODY if lex.token == "="
elsif lex.state.allbits?(Ripper::EXPR_END)
# We found an endless method, count it
oneliner_count += 1 if in_oneliner_def == :BODY

in_oneliner_def = nil
else
in_oneliner_def = nil
end
end

oneliner_count
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/code_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

module DeadEnd
RSpec.describe CodeLine do
it "supports endless method definitions" do
skip("Unsupported ruby version") unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3")

line = CodeLine.from_source(<<~'EOM').first
def square(x) = x * x
EOM

expect(line.is_kw?).to be_falsey
expect(line.is_end?).to be_falsey
end

it "retains original line value, after being marked invisible" do
line = CodeLine.from_source(<<~'EOM').first
puts "lol"
Expand Down