Skip to content

Commit

Permalink
Detect RuboCop as a linter when its a transitive dependency (#2281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jul 10, 2024
1 parent 349edf3 commit caa4a40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def apply_options(options)
@formatter = detect_formatter(direct_dependencies, all_dependencies) if @formatter == "auto"

specified_linters = options.dig(:initializationOptions, :linters)
@linters = specified_linters || detect_linters(direct_dependencies)
@linters = specified_linters || detect_linters(direct_dependencies, all_dependencies)
@test_library = detect_test_library(direct_dependencies)
@has_type_checker = detect_typechecker(direct_dependencies)

Expand Down Expand Up @@ -127,10 +127,14 @@ def detect_formatter(direct_dependencies, all_dependencies)

# Try to detect if there are linters in the project's dependencies. For auto-detection, we always only consider a
# single linter. To have multiple linters running, the user must configure them manually
sig { params(dependencies: T::Array[String]).returns(T::Array[String]) }
def detect_linters(dependencies)
sig { params(dependencies: T::Array[String], all_dependencies: T::Array[String]).returns(T::Array[String]) }
def detect_linters(dependencies, all_dependencies)
linters = []
linters << "rubocop" if dependencies.any?(/^rubocop/)

if dependencies.any?(/^rubocop/) || (all_dependencies.include?("rubocop") && dot_rubocop_yml_present)
linters << "rubocop"
end

linters
end

Expand Down
12 changes: 12 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ def test_specifying_empty_linters
assert_empty(state.instance_variable_get(:@linters))
end

def test_linter_auto_detection_with_rubocop_as_transitive_dependency
state = GlobalState.new

stub_direct_dependencies("gem_with_config" => "1.2.3")
stub_all_dependencies("gem_with_config", "rubocop")
state.stubs(:dot_rubocop_yml_present).returns(true)

state.apply_options({})

assert_includes(state.instance_variable_get(:@linters), "rubocop")
end

def test_apply_options_sets_experimental_features
state = GlobalState.new
refute_predicate(state, :experimental_features)
Expand Down

0 comments on commit caa4a40

Please sign in to comment.