Skip to content

Commit caa4a40

Browse files
authored
Detect RuboCop as a linter when its a transitive dependency (#2281)
1 parent 349edf3 commit caa4a40

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

lib/ruby_lsp/global_state.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def apply_options(options)
6969
@formatter = detect_formatter(direct_dependencies, all_dependencies) if @formatter == "auto"
7070

7171
specified_linters = options.dig(:initializationOptions, :linters)
72-
@linters = specified_linters || detect_linters(direct_dependencies)
72+
@linters = specified_linters || detect_linters(direct_dependencies, all_dependencies)
7373
@test_library = detect_test_library(direct_dependencies)
7474
@has_type_checker = detect_typechecker(direct_dependencies)
7575

@@ -127,10 +127,14 @@ def detect_formatter(direct_dependencies, all_dependencies)
127127

128128
# Try to detect if there are linters in the project's dependencies. For auto-detection, we always only consider a
129129
# single linter. To have multiple linters running, the user must configure them manually
130-
sig { params(dependencies: T::Array[String]).returns(T::Array[String]) }
131-
def detect_linters(dependencies)
130+
sig { params(dependencies: T::Array[String], all_dependencies: T::Array[String]).returns(T::Array[String]) }
131+
def detect_linters(dependencies, all_dependencies)
132132
linters = []
133-
linters << "rubocop" if dependencies.any?(/^rubocop/)
133+
134+
if dependencies.any?(/^rubocop/) || (all_dependencies.include?("rubocop") && dot_rubocop_yml_present)
135+
linters << "rubocop"
136+
end
137+
134138
linters
135139
end
136140

test/global_state_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ def test_specifying_empty_linters
176176
assert_empty(state.instance_variable_get(:@linters))
177177
end
178178

179+
def test_linter_auto_detection_with_rubocop_as_transitive_dependency
180+
state = GlobalState.new
181+
182+
stub_direct_dependencies("gem_with_config" => "1.2.3")
183+
stub_all_dependencies("gem_with_config", "rubocop")
184+
state.stubs(:dot_rubocop_yml_present).returns(true)
185+
186+
state.apply_options({})
187+
188+
assert_includes(state.instance_variable_get(:@linters), "rubocop")
189+
end
190+
179191
def test_apply_options_sets_experimental_features
180192
state = GlobalState.new
181193
refute_predicate(state, :experimental_features)

0 commit comments

Comments
 (0)