Skip to content

Commit

Permalink
Prevent RBI deletion for gems that are still direct dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrocha committed Jan 7, 2025
1 parent 238e337 commit 13fdd65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/ruby_lsp/tapioca/lockfile_diff_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Tapioca
class LockfileDiffParser
GEM_NAME_PATTERN = /[\w\-]+/
DIFF_LINE_PATTERN = /[+-](.*#{GEM_NAME_PATTERN})\s*\(/
DIRECT_DEP_PATTERN = /^\s*(#{GEM_NAME_PATTERN})\s*\(/
ADDED_LINE_PATTERN = /^\+.*#{GEM_NAME_PATTERN} \(.*\)/
REMOVED_LINE_PATTERN = /^-.*#{GEM_NAME_PATTERN} \(.*\)/

Expand All @@ -28,10 +29,15 @@ def parse_added_or_modified_gems
end

def parse_removed_gems
direct_dependencies = @diff_content
.filter { |line| line.match?(DIRECT_DEP_PATTERN) }
.map { |line| line.match(DIRECT_DEP_PATTERN)[1].strip }
.uniq

@diff_content
.filter { |line| line.match?(REMOVED_LINE_PATTERN) }
.map { |line| extract_gem(line) }
.reject { |gem| @added_or_modified_gems.include?(gem) }
.reject { |gem| direct_dependencies.include?(gem) }
.uniq
end

Expand Down
11 changes: 11 additions & 0 deletions spec/tapioca/ruby_lsp/tapioca/lockfile_diff_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ class LockFileDiffParserSpec < Minitest::Spec
lockfile_parser = RubyLsp::Tapioca::LockfileDiffParser.new(diff_output)
assert_equal ["removed_gem", "outdated_gem"], lockfile_parser.removed_gems
end

it "ignores direct dependencies" do
diff_output = <<~DIFF
foo (1.1.1)
bar (1.2.3)
- foo (> 0)
DIFF

lockfile_parser = RubyLsp::Tapioca::LockfileDiffParser.new(diff_output)
assert_empty lockfile_parser.removed_gems
end
end

it "handles gem names with hyphens and underscores" do
Expand Down

0 comments on commit 13fdd65

Please sign in to comment.