diff --git a/CHANGELOG.md b/CHANGELOG.md index db587eb..a77e2b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## HEAD (unreleased) +## 3.1.1 + +- Fix case where Ripper lexing identified incorrect code as a keyword (https://github.com/zombocom/dead_end/pull/122) + ## 3.1.0 - Add support for Ruby 3.1 by updating `require_relative` logic (https://github.com/zombocom/dead_end/pull/120) diff --git a/Gemfile.lock b/Gemfile.lock index b95d231..febfe2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - dead_end (3.1.0) + dead_end (3.1.1) GEM remote: https://rubygems.org/ @@ -62,4 +62,4 @@ DEPENDENCIES standard BUNDLED WITH - 2.2.30 + 2.3.4 diff --git a/lib/dead_end/lex_all.rb b/lib/dead_end/lex_all.rb index 08973ce..ec62461 100644 --- a/lib/dead_end/lex_all.rb +++ b/lib/dead_end/lex_all.rb @@ -25,7 +25,10 @@ def initialize(source:, source_lines: nil) lineno = @lex.last.pos.first + 1 end - @lex.map! { |elem| LexValue.new(elem.pos.first, elem.event, elem.tok, elem.state) } + last_lex = nil + @lex.map! { |elem| + last_lex = LexValue.new(elem.pos.first, elem.event, elem.tok, elem.state, last_lex) + } end def to_a diff --git a/lib/dead_end/lex_value.rb b/lib/dead_end/lex_value.rb index 3119953..8bb07b8 100644 --- a/lib/dead_end/lex_value.rb +++ b/lib/dead_end/lex_value.rb @@ -15,19 +15,21 @@ module DeadEnd class LexValue attr_reader :line, :type, :token, :state - def initialize(line, type, token, state) + def initialize(line, type, token, state, last_lex = nil) @line = line @type = type @token = token @state = state - set_kw_end + set_kw_end(last_lex) end - private def set_kw_end + private def set_kw_end(last_lex) @is_end = false @is_kw = false return if type != :on_kw + # + return if last_lex && last_lex.fname? # https://github.com/ruby/ruby/commit/776759e300e4659bb7468e2b97c8c2d4359a2953 case token when "if", "unless", "while", "until" @@ -41,6 +43,10 @@ def initialize(line, type, token, state) end end + def fname? + state.allbits?(Ripper::EXPR_FNAME) + end + def ignore_newline? type == :on_ignored_nl end diff --git a/lib/dead_end/version.rb b/lib/dead_end/version.rb index b346985..037061e 100644 --- a/lib/dead_end/version.rb +++ b/lib/dead_end/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DeadEnd - VERSION = "3.1.0" + VERSION = "3.1.1" end diff --git a/spec/unit/code_line_spec.rb b/spec/unit/code_line_spec.rb index 1eb1a86..cf3e75d 100644 --- a/spec/unit/code_line_spec.rb +++ b/spec/unit/code_line_spec.rb @@ -4,6 +4,18 @@ module DeadEnd RSpec.describe CodeLine do + it "bug in keyword detection" do + lines = CodeLine.from_source(<<~'EOM') + def to_json(*opts) + { + type: :module, + }.to_json(*opts) + end + EOM + expect(lines.count(&:is_kw?)).to eq(1) + expect(lines.count(&:is_end?)).to eq(1) + end + it "supports endless method definitions" do skip("Unsupported ruby version") unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3")