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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# of RuboCop built-in cops in this file, we need to monitor it for changes
# in rubocop-rails and keep it up to date.
#
# Last updated from rubocop-rails v2.31.0
# Last updated from rubocop-rails v2.32.0

# frozen_string_literal: true

Expand Down
27 changes: 23 additions & 4 deletions lib/standard/rails/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def rules(context)
LintRoller::Rules.new(
type: :object,
config_format: :rubocop,
value: without_extended_rule_configs(
rules_with_config_applied
)
value: sanitize_rules(rules_with_config_applied)
)
end

Expand All @@ -54,10 +52,31 @@ def rules_with_config_applied
# See: https://github.com/standardrb/standard-rails/issues/25#issuecomment-1881127173
def without_extended_rule_configs(rules)
rules.reject { |(name, _)|
["Style/InvertibleUnlessCondition", "Lint/SafeNavigationChain"].include?(name)
[
"Style/InvertibleUnlessCondition",
"Lint/SafeNavigationChain",
"Lint/UselessAccessModifier"
].include?(name)
}.to_h
end

# Normalize AllCops to avoid validator warnings without changing behavior:
# - MigratedSchemaVersion: rubocop-rails ships a UNIX-epoch default that
# skips nothing; removing it is equivalent unless a real timestamp is provided.
# - TargetRailsVersion: Standard may pre-seed a nil to placate merging; deleting
# only the nil value avoids a warning and lets RuboCop infer Rails version.
# See: https://github.com/standardrb/standard-rails/issues/72
def sanitize_rules(rules)
cleaned = without_extended_rule_configs(rules)
if cleaned["AllCops"].is_a?(Hash)
all_cops = cleaned["AllCops"].dup
all_cops.delete("MigratedSchemaVersion")
all_cops.delete("TargetRailsVersion") if all_cops.key?("TargetRailsVersion") && all_cops["TargetRailsVersion"].nil?
cleaned["AllCops"] = all_cops
end
cleaned
end

# This is not fantastic.
#
# When you `require "rubocop-rails"`, it will not only load the cops,
Expand Down
27 changes: 27 additions & 0 deletions test/standard/rails/plugin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,32 @@ def test_configuring_target_rails_version

assert_equal 5.2, result.value["AllCops"]["TargetRailsVersion"]
end

def test_filters_migrated_schema_version_from_all_cops
subject = Plugin.new({})

result = subject.rules(LintRoller::Context.new)

refute_includes result.value.fetch("AllCops", {}).keys, "MigratedSchemaVersion"
end

def test_removes_useless_access_modifier_block
subject = Plugin.new({})

result = subject.rules(LintRoller::Context.new)

assert_nil result.value["Lint/UselessAccessModifier"]
end

def test_no_parameter_warnings_when_validating_config
subject = Plugin.new({})
rules = subject.rules(LintRoller::Context.new)

_, err = capture_io do
RuboCop::Config.create(rules.value, "inline.yml", check: true)
end

assert_equal "", err
end
end
end