Merge pull request #15170 from Homebrew/dependabot/bundler/Library/Homebrew/rubocop-rails-2.19.0

build(deps): bump rubocop-rails from 2.18.0 to 2.19.0 in /Library/Homebrew
This commit is contained in:
Mike McQuaid 2023-04-07 21:48:43 +01:00 committed by GitHub
commit a911e6305a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
147 changed files with 302 additions and 122 deletions

View File

@ -147,7 +147,7 @@ GEM
rubocop-performance (1.16.0) rubocop-performance (1.16.0)
rubocop (>= 1.7.0, < 2.0) rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0) rubocop-ast (>= 0.4.0)
rubocop-rails (2.18.0) rubocop-rails (2.19.0)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
rack (>= 1.1) rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0) rubocop (>= 1.33.0, < 2.0)

View File

@ -124,7 +124,7 @@ module RuboCop
end end
def check_on_system_block_content(component_precedence_list, on_system_block) def check_on_system_block_content(component_precedence_list, on_system_block)
if on_system_block.body.block_type? && !on_system_methods.include?(on_system_block.body.method_name) # rubocop:disable Style/InverseMethods (false positive) if on_system_block.body.block_type? && !on_system_methods.include?(on_system_block.body.method_name)
offending_node(on_system_block) offending_node(on_system_block)
problem "Nest `#{on_system_block.method_name}` blocks inside `#{on_system_block.body.method_name}` " \ problem "Nest `#{on_system_block.method_name}` blocks inside `#{on_system_block.body.method_name}` " \
"blocks when there is only one inner block." do |corrector| "blocks when there is only one inner block." do |corrector|

View File

@ -53,8 +53,7 @@ module RuboCop
desc_problem "Description shouldn't start with an article." if regex_match_group(desc, /^(the|an?)(?=\s)/i) desc_problem "Description shouldn't start with an article." if regex_match_group(desc, /^(the|an?)(?=\s)/i)
# Check if invalid lowercase words are at the start of a desc. # Check if invalid lowercase words are at the start of a desc.
if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) && # rubocop:disable Style/InverseMethods (false positive) if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) && regex_match_group(desc, /^[a-z]/)
regex_match_group(desc, /^[a-z]/)
desc_problem "Description should start with a capital letter." desc_problem "Description should start with a capital letter."
end end

View File

@ -514,6 +514,7 @@ class RuboCop::Cop::Rails::CreateTableWithTimestamps < ::RuboCop::Cop::Base
def created_at_or_updated_at_included?(param0); end def created_at_or_updated_at_included?(param0); end
def on_send(node); end def on_send(node); end
def timestamps_included?(param0); end def timestamps_included?(param0); end
def use_id_false_option?(param0); end
private private
@ -848,11 +849,11 @@ class RuboCop::Cop::Rails::FilePath < ::RuboCop::Cop::Base
private private
def build_message(require_to_s); end
def check_for_file_join_with_rails_root(node); end def check_for_file_join_with_rails_root(node); end
def check_for_rails_root_join_with_slash_separated_path(node); end def check_for_rails_root_join_with_slash_separated_path(node); end
def check_for_rails_root_join_with_string_arguments(node); end def check_for_rails_root_join_with_string_arguments(node); end
def message(_range); end def register_offense(node, require_to_s:); end
def register_offense(node); end
def string_with_slash?(node); end def string_with_slash?(node); end
end end
@ -986,6 +987,7 @@ class RuboCop::Cop::Rails::HttpPositionalArguments < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::TargetRailsVersion extend ::RuboCop::Cop::TargetRailsVersion
def http_request?(param0 = T.unsafe(nil)); end def http_request?(param0 = T.unsafe(nil)); end
def include_rack_test_methods?(param0 = T.unsafe(nil)); end
def kwsplat_hash?(param0 = T.unsafe(nil)); end def kwsplat_hash?(param0 = T.unsafe(nil)); end
def on_send(node); end def on_send(node); end
@ -999,6 +1001,7 @@ class RuboCop::Cop::Rails::HttpPositionalArguments < ::RuboCop::Cop::Base
def in_routing_block?(node); end def in_routing_block?(node); end
def needs_conversion?(data); end def needs_conversion?(data); end
def special_keyword_arg?(node); end def special_keyword_arg?(node); end
def use_rack_test_methods?; end
end end
RuboCop::Cop::Rails::HttpPositionalArguments::KEYWORD_ARGS = T.let(T.unsafe(nil), Array) RuboCop::Cop::Rails::HttpPositionalArguments::KEYWORD_ARGS = T.let(T.unsafe(nil), Array)
@ -1330,6 +1333,7 @@ RuboCop::Cop::Rails::Output::MSG = T.let(T.unsafe(nil), String)
RuboCop::Cop::Rails::Output::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array) RuboCop::Cop::Rails::Output::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array)
class RuboCop::Cop::Rails::OutputSafety < ::RuboCop::Cop::Base class RuboCop::Cop::Rails::OutputSafety < ::RuboCop::Cop::Base
def i18n_method?(param0); end
def on_csend(node); end def on_csend(node); end
def on_send(node); end def on_send(node); end
@ -1612,12 +1616,16 @@ RuboCop::Cop::Rails::RedundantTravelBack::MSG = T.let(T.unsafe(nil), String)
RuboCop::Cop::Rails::RedundantTravelBack::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array) RuboCop::Cop::Rails::RedundantTravelBack::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array)
class RuboCop::Cop::Rails::ReflectionClassName < ::RuboCop::Cop::Base class RuboCop::Cop::Rails::ReflectionClassName < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::AutoCorrector
def association_with_reflection(param0 = T.unsafe(nil)); end def association_with_reflection(param0 = T.unsafe(nil)); end
def const_or_string(param0 = T.unsafe(nil)); end
def on_send(node); end def on_send(node); end
def reflection_class_name(param0 = T.unsafe(nil)); end def reflection_class_name(param0 = T.unsafe(nil)); end
private private
def autocorrect(corrector, class_config); end
def reflection_class_value?(class_value); end def reflection_class_value?(class_value); end
def str_assigned?(reflection_class_name); end def str_assigned?(reflection_class_name); end
end end
@ -2018,6 +2026,20 @@ end
RuboCop::Cop::Rails::TableNameAssignment::MSG = T.let(T.unsafe(nil), String) RuboCop::Cop::Rails::TableNameAssignment::MSG = T.let(T.unsafe(nil), String)
class RuboCop::Cop::Rails::ThreeStateBooleanColumn < ::RuboCop::Cop::Base
def change_column_null?(param0, param1, param2); end
def on_send(node); end
def required_options?(param0 = T.unsafe(nil)); end
def three_state_boolean?(param0 = T.unsafe(nil)); end
private
def table_node(node); end
end
RuboCop::Cop::Rails::ThreeStateBooleanColumn::MSG = T.let(T.unsafe(nil), String)
RuboCop::Cop::Rails::ThreeStateBooleanColumn::RESTRICT_ON_SEND = T.let(T.unsafe(nil), Array)
class RuboCop::Cop::Rails::TimeZone < ::RuboCop::Cop::Base class RuboCop::Cop::Rails::TimeZone < ::RuboCop::Cop::Base
include ::RuboCop::Cop::ConfigurableEnforcedStyle include ::RuboCop::Cop::ConfigurableEnforcedStyle
extend ::RuboCop::Cop::AutoCorrector extend ::RuboCop::Cop::AutoCorrector

View File

@ -6509,6 +6509,7 @@ module RuboCop::AST::NodePattern::Sets
SET_ON_INTEL_ON_ARM = ::T.let(nil, ::T.untyped) SET_ON_INTEL_ON_ARM = ::T.let(nil, ::T.untyped)
SET_OR_NEWER_OR_OLDER = ::T.let(nil, ::T.untyped) SET_OR_NEWER_OR_OLDER = ::T.let(nil, ::T.untyped)
SET_SYSTEM_SHELL_OUTPUT_PIPE_OUTPUT = ::T.let(nil, ::T.untyped) SET_SYSTEM_SHELL_OUTPUT_PIPE_OUTPUT = ::T.let(nil, ::T.untyped)
SET_T_TRANSLATE_L_LOCALIZE = ::T.let(nil, ::T.untyped)
SET_WITH_WITHOUT = ::T.let(nil, ::T.untyped) SET_WITH_WITHOUT = ::T.let(nil, ::T.untyped)
SET____ETC_4 = ::T.let(nil, ::T.untyped) SET____ETC_4 = ::T.let(nil, ::T.untyped)
end end

View File

@ -7,7 +7,7 @@ homebrew_bootsnap_enabled = ENV["HOMEBREW_NO_BOOTSNAP"].nil? && !ENV["HOMEBREW_B
# portable ruby doesn't play nice with bootsnap # portable ruby doesn't play nice with bootsnap
homebrew_bootsnap_enabled &&= !RUBY_PATH.to_s.include?("/vendor/portable-ruby/") # rubocop:disable Style/InverseMethods homebrew_bootsnap_enabled &&= !RUBY_PATH.to_s.include?("/vendor/portable-ruby/")
homebrew_bootsnap_enabled &&= if ENV["HOMEBREW_MACOS_VERSION"] homebrew_bootsnap_enabled &&= if ENV["HOMEBREW_MACOS_VERSION"]
# Apple Silicon doesn't play nice with bootsnap # Apple Silicon doesn't play nice with bootsnap

View File

@ -106,7 +106,7 @@ $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-1.49.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-1.49.0/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-capybara-2.17.1/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-capybara-2.17.1/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-performance-1.16.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-performance-1.16.0/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rails-2.18.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rails-2.19.0/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rspec-2.19.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rspec-2.19.0/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-sorbet-0.7.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-sorbet-0.7.0/lib")
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/ruby-macho-3.0.0/lib") $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/ruby-macho-3.0.0/lib")

View File

@ -53,8 +53,6 @@ Rails:
Rails/ActionControllerFlashBeforeRender: Rails/ActionControllerFlashBeforeRender:
Description: 'Use `flash.now` instead of `flash` before `render`.' Description: 'Use `flash.now` instead of `flash` before `render`.'
StyleGuide: 'https://rails.rubystyle.guide/#flash-before-render'
Reference: 'https://api.rubyonrails.org/classes/ActionController/FlashBeforeRender.html'
Enabled: 'pending' Enabled: 'pending'
SafeAutoCorrect: false SafeAutoCorrect: false
VersionAdded: '2.16' VersionAdded: '2.16'
@ -462,8 +460,9 @@ Rails/FindEach:
Description: 'Prefer all.find_each over all.each.' Description: 'Prefer all.find_each over all.each.'
StyleGuide: 'https://rails.rubystyle.guide#find-each' StyleGuide: 'https://rails.rubystyle.guide#find-each'
Enabled: true Enabled: true
Safe: false
VersionAdded: '0.30' VersionAdded: '0.30'
VersionChanged: '2.9' VersionChanged: '2.19'
Include: Include:
- app/models/**/*.rb - app/models/**/*.rb
AllowedMethods: AllowedMethods:
@ -844,8 +843,9 @@ Rails/RequireDependency:
Rails/ResponseParsedBody: Rails/ResponseParsedBody:
Description: Prefer `response.parsed_body` to `JSON.parse(response.body)`. Description: Prefer `response.parsed_body` to `JSON.parse(response.body)`.
Enabled: pending Enabled: pending
SafeAutoCorrect: false Safe: false
VersionAdded: '2.18' VersionAdded: '2.18'
VersionChanged: '2.19'
Include: Include:
- spec/controllers/**/*.rb - spec/controllers/**/*.rb
- spec/requests/**/*.rb - spec/requests/**/*.rb
@ -997,6 +997,14 @@ Rails/TableNameAssignment:
Include: Include:
- app/models/**/*.rb - app/models/**/*.rb
Rails/ThreeStateBooleanColumn:
Description: 'Add a default value and a `NOT NULL` constraint to boolean columns.'
StyleGuide: 'https://rails.rubystyle.guide/#three-state-boolean'
Enabled: pending
VersionAdded: '2.19'
Include:
- db/**/*.rb
Rails/TimeZone: Rails/TimeZone:
Description: 'Checks the correct usage of time zone aware methods.' Description: 'Checks the correct usage of time zone aware methods.'
StyleGuide: 'https://rails.rubystyle.guide#time' StyleGuide: 'https://rails.rubystyle.guide#time'
@ -1144,15 +1152,6 @@ Style/FormatStringToken:
AllowedMethods: AllowedMethods:
- redirect - redirect
Style/InverseMethods:
# `InverseMethods` are methods that can be inverted by a not (`not` or `!`)
# The relationship of inverse methods only needs to be defined in one direction.
# Keys and values both need to be defined as symbols.
InverseMethods:
:present?: :blank?
:include?: :exclude?
:valid?: :invalid?
Style/SymbolProc: Style/SymbolProc:
AllowedMethods: AllowedMethods:
- define_method - define_method

View File

@ -3,6 +3,7 @@
require 'rubocop' require 'rubocop'
require 'rack/utils' require 'rack/utils'
require 'active_support/inflector' require 'active_support/inflector'
require 'active_support/core_ext/object/blank'
require_relative 'rubocop/rails' require_relative 'rubocop/rails'
require_relative 'rubocop/rails/version' require_relative 'rubocop/rails/version'

View File

@ -32,7 +32,7 @@ module RuboCop
def register_offense(offense_node) def register_offense(offense_node)
add_offense(offense_node) do |corrector| add_offense(offense_node) do |corrector|
corrector.replace(offense_node.source_range, self.class::SUPERCLASS) corrector.replace(offense_node, self.class::SUPERCLASS)
end end
end end
end end

View File

@ -138,7 +138,7 @@ module RuboCop
end end
def strip_prefix_and_suffix(node, corrector) def strip_prefix_and_suffix(node, corrector)
expression = node.loc.expression expression = node.source_range
corrector.remove_leading(expression, leading) corrector.remove_leading(expression, leading)
corrector.remove_trailing(expression, trailing) corrector.remove_trailing(expression, trailing)
end end
@ -153,11 +153,11 @@ module RuboCop
end end
def set_new_arg_name(transformed_argname, corrector) def set_new_arg_name(transformed_argname, corrector)
corrector.replace(block_node.arguments.loc.expression, "|#{transformed_argname}|") corrector.replace(block_node.arguments, "|#{transformed_argname}|")
end end
def set_new_body_expression(transforming_body_expr, corrector) def set_new_body_expression(transforming_body_expr, corrector)
corrector.replace(block_node.body.loc.expression, transforming_body_expr.loc.expression.source) corrector.replace(block_node.body, transforming_body_expr.source)
end end
end end
end end

View File

@ -92,12 +92,11 @@ module RuboCop
end end
def range_with_comments(node) def range_with_comments(node)
ranges = [ # rubocop:todo InternalAffairs/LocationExpression
node, # Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
*processed_source.ast_with_comments[node] # which introduces https://github.com/rubocop/rubocop/pull/11630.
].map do |element| ranges = [node, *processed_source.ast_with_comments[node]].map { |comment| comment.loc.expression }
element.location.expression # rubocop:enable InternalAffairs/LocationExpression
end
ranges.reduce do |result, range| ranges.reduce do |result, range|
add_range(result, range) add_range(result, range)
end end

View File

@ -104,7 +104,7 @@ module RuboCop
end end
def end_position_for(node) def end_position_for(node)
end_line = buffer.line_for_position(node.loc.expression.end_pos) end_line = buffer.line_for_position(node.source_range.end_pos)
buffer.line_range(end_line).end_pos buffer.line_range(end_line).end_pos
end end
@ -112,8 +112,7 @@ module RuboCop
annotation_line = node.first_line - 1 annotation_line = node.first_line - 1
first_comment = nil first_comment = nil
processed_source.comments_before_line(annotation_line) processed_source.each_comment_in_lines(0..annotation_line).reverse_each do |comment|
.reverse_each do |comment|
if comment.location.line == annotation_line && !inline_comment?(comment) if comment.location.line == annotation_line && !inline_comment?(comment)
first_comment = comment first_comment = comment
annotation_line -= 1 annotation_line -= 1
@ -124,7 +123,11 @@ module RuboCop
end end
def inline_comment?(comment) def inline_comment?(comment)
# rubocop:todo InternalAffairs/LocationExpression
# Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
# which introduces https://github.com/rubocop/rubocop/pull/11630.
!comment_line?(comment.loc.expression.source_line) !comment_line?(comment.loc.expression.source_line)
# rubocop:enable InternalAffairs/LocationExpression
end end
def start_line_position(node) def start_line_position(node)

View File

@ -42,7 +42,7 @@ module RuboCop
add_index_opts = '' add_index_opts = ''
if value.hash_type? if value.hash_type?
hash = value.loc.expression.adjust(begin_pos: 1, end_pos: -1).source.strip hash = value.source_range.adjust(begin_pos: 1, end_pos: -1).source.strip
add_index_opts = ", #{hash}" add_index_opts = ", #{hash}"
end end
@ -53,7 +53,7 @@ module RuboCop
private private
def index_range(pair_node) def index_range(pair_node)
range_with_surrounding_comma(range_with_surrounding_space(pair_node.loc.expression, side: :left), :left) range_with_surrounding_comma(range_with_surrounding_space(pair_node.source_range, side: :left), :left)
end end
end end
end end

View File

@ -36,7 +36,7 @@ module RuboCop
def autocorrect(node) def autocorrect(node)
lambda do |corrector| lambda do |corrector|
corrector.replace(node.source_range, self.class::SUPERCLASS) corrector.replace(node, self.class::SUPERCLASS)
end end
end end
end end

View File

@ -38,7 +38,7 @@ module RuboCop
return unless (star = star_bracket?(node)) return unless (star = star_bracket?(node))
add_offense(star) do |corrector| add_offense(star) do |corrector|
corrector.replace(star.loc.expression, 'Arel.star') corrector.replace(star, 'Arel.star')
end end
end end
end end

View File

@ -25,7 +25,7 @@ module RuboCop
return unless offensive?(node) return unless offensive?(node)
add_offense(node) do |corrector| add_offense(node) do |corrector|
expression = node.loc.expression expression = node.source_range
corrector.replace(expression, corrected_source(expression.source)) corrector.replace(expression, corrected_source(expression.source))
end end

View File

@ -80,7 +80,7 @@ module RuboCop
end end
add_offense(node.loc.selector, message: message) do |corrector| add_offense(node.loc.selector, message: message) do |corrector|
corrector.replace(option_node.loc.expression, replacement) corrector.replace(option_node, replacement)
end end
end end
end end

View File

@ -142,10 +142,10 @@ module RuboCop
if method_call if method_call
corrector.replace(node.loc.keyword, 'if') corrector.replace(node.loc.keyword, 'if')
range = method_call.loc.expression range = method_call.source_range
else else
variable1, _variable2 = nil_or_empty?(node) || not_present?(node) variable1, _variable2 = nil_or_empty?(node) || not_present?(node)
range = node.loc.expression range = node.source_range
end end
corrector.replace(range, replacement(variable1)) corrector.replace(range, replacement(variable1))
@ -153,9 +153,9 @@ module RuboCop
def unless_condition(node, method_call) def unless_condition(node, method_call)
if node.modifier_form? if node.modifier_form?
node.loc.keyword.join(node.loc.expression.end) node.loc.keyword.join(node.source_range.end)
else else
node.loc.expression.begin.join(method_call.loc.expression) node.source_range.begin.join(method_call.source_range)
end end
end end

View File

@ -94,9 +94,9 @@ module RuboCop
def offense_range(node) def offense_range(node)
end_pos = if node.parent&.block_type? && node.parent&.send_node == node end_pos = if node.parent&.block_type? && node.parent&.send_node == node
node.parent.loc.expression.end_pos node.parent.source_range.end_pos
else else
node.loc.expression.end_pos node.source_range.end_pos
end end
range_between(node.loc.selector.begin_pos, end_pos) range_between(node.loc.selector.begin_pos, end_pos)

View File

@ -85,7 +85,7 @@ module RuboCop
end end
def correction_range(node) def correction_range(node)
range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end end
end end
end end

View File

@ -3,10 +3,12 @@
module RuboCop module RuboCop
module Cop module Cop
module Rails module Rails
# Checks the migration for which timestamps are not included # Checks the migration for which timestamps are not included when creating a new table.
# when creating a new table.
# In many cases, timestamps are useful information and should be added. # In many cases, timestamps are useful information and should be added.
# #
# NOTE: Allow `timestamps` not written when `id: false` because this emphasizes respecting
# user's editing intentions.
#
# @example # @example
# # bad # # bad
# create_table :users # create_table :users
@ -40,12 +42,23 @@ module RuboCop
# #
# t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' } # t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' }
# end # end
#
# # good
# create_table :users, articles, id: false do |t|
# t.integer :user_id
# t.integer :article_id
# end
#
class CreateTableWithTimestamps < Base class CreateTableWithTimestamps < Base
include ActiveRecordMigrationsHelper include ActiveRecordMigrationsHelper
MSG = 'Add timestamps when creating a new table.' MSG = 'Add timestamps when creating a new table.'
RESTRICT_ON_SEND = %i[create_table].freeze RESTRICT_ON_SEND = %i[create_table].freeze
def_node_search :use_id_false_option?, <<~PATTERN
(pair (sym :id) (false))
PATTERN
def_node_matcher :create_table_with_timestamps_proc?, <<~PATTERN def_node_matcher :create_table_with_timestamps_proc?, <<~PATTERN
(send nil? :create_table (sym _) ... (block-pass (sym :timestamps))) (send nil? :create_table (sym _) ... (block-pass (sym :timestamps)))
PATTERN PATTERN
@ -61,7 +74,7 @@ module RuboCop
PATTERN PATTERN
def on_send(node) def on_send(node)
return unless node.command?(:create_table) return if !node.command?(:create_table) || use_id_false_option?(node)
parent = node.parent parent = node.parent

View File

@ -24,6 +24,14 @@ module RuboCop
# # good # # good
# delegate :bar, to: :foo # delegate :bar, to: :foo
# #
# # bad
# def bar
# self.bar
# end
#
# # good
# delegate :bar, to: :self
#
# # good # # good
# def bar # def bar
# foo&.bar # foo&.bar
@ -60,7 +68,7 @@ module RuboCop
def_node_matcher :delegate?, <<~PATTERN def_node_matcher :delegate?, <<~PATTERN
(def _method_name _args (def _method_name _args
(send (send nil? _) _ ...)) (send {(send nil? _) (self)} _ ...))
PATTERN PATTERN
def on_def(node) def on_def(node)
@ -74,10 +82,14 @@ module RuboCop
def register_offense(node) def register_offense(node)
add_offense(node.loc.keyword) do |corrector| add_offense(node.loc.keyword) do |corrector|
delegation = ["delegate :#{node.body.method_name}", "to: :#{node.body.receiver.method_name}"] body = node.body
receiver = body.receiver.self_type? ? 'self' : ":#{body.receiver.method_name}"
delegation = ["delegate :#{body.method_name}", "to: #{receiver}"]
delegation << ['prefix: true'] if node.method?(prefixed_method_name(node.body)) delegation << ['prefix: true'] if node.method?(prefixed_method_name(node.body))
corrector.replace(node.source_range, delegation.join(', ')) corrector.replace(node, delegation.join(', '))
end end
end end
@ -106,6 +118,8 @@ module RuboCop
end end
def prefixed_method_name(body) def prefixed_method_name(body)
return '' if body.receiver.self_type?
[body.receiver.method_name, body.method_name].join('_').to_sym [body.receiver.method_name, body.method_name].join('_').to_sym
end end

View File

@ -27,7 +27,7 @@ module RuboCop
return unless (offending_node = allow_blank_option(node)) return unless (offending_node = allow_blank_option(node))
add_offense(offending_node) do |corrector| add_offense(offending_node) do |corrector|
corrector.replace(offending_node.key.source_range, 'allow_nil') corrector.replace(offending_node.key, 'allow_nil')
end end
end end
end end

View File

@ -78,7 +78,7 @@ module RuboCop
def on_send(node) def on_send(node)
duration_arithmetic_argument?(node) do |*operation| duration_arithmetic_argument?(node) do |*operation|
add_offense(node) do |corrector| add_offense(node) do |corrector|
corrector.replace(node.source_range, corrected_source(*operation)) corrector.replace(node, corrected_source(*operation))
end end
end end
end end

View File

@ -99,7 +99,7 @@ module RuboCop
def autocorrect_argument_keywords(corrector, node, keywords) def autocorrect_argument_keywords(corrector, node, keywords)
keywords.each.with_index do |keyword, idx| keywords.each.with_index do |keyword, idx|
corrector.insert_before(node.arguments[idx].loc.expression, keyword) corrector.insert_before(node.arguments[idx], keyword)
end end
end end

View File

@ -59,7 +59,7 @@ module RuboCop
private private
def replacement_range(node) def replacement_range(node)
stop = node.loc.expression.end stop = node.source_range.end
start = node.loc.selector.end start = node.loc.selector.end
if node.parenthesized_call? if node.parenthesized_call?

View File

@ -42,7 +42,7 @@ module RuboCop
"#{source(elem)} => #{index}" "#{source(elem)} => #{index}"
end.join(', ') end.join(', ')
corrector.replace(array.loc.expression, "{#{hash}}") corrector.replace(array, "{#{hash}}")
end end
end end
end end

View File

@ -84,7 +84,7 @@ module RuboCop
def autocorrect(corrector, node) def autocorrect(corrector, node)
replacement = build_predicate_method(node) replacement = build_predicate_method(node)
corrector.replace(node.source_range, replacement) corrector.replace(node, replacement)
end end
def build_predicate_method(node) def build_predicate_method(node)

View File

@ -3,34 +3,43 @@
module RuboCop module RuboCop
module Cop module Cop
module Rails module Rails
# Identifies usages of file path joining process # Identifies usages of file path joining process to use `Rails.root.join` clause.
# to use `Rails.root.join` clause. It is used to add uniformity when # It is used to add uniformity when joining paths.
# joining paths.
# #
# @example EnforcedStyle: slashes (default) # @example EnforcedStyle: slashes (default)
# # bad # # bad
# Rails.root.join('app', 'models', 'goober') # Rails.root.join('app', 'models', 'goober')
#
# # good
# Rails.root.join('app/models/goober')
#
# # bad
# File.join(Rails.root, 'app/models/goober') # File.join(Rails.root, 'app/models/goober')
# "#{Rails.root}/app/models/goober" # "#{Rails.root}/app/models/goober"
# #
# # good # # good
# Rails.root.join('app/models/goober') # Rails.root.join('app/models/goober').to_s
# #
# @example EnforcedStyle: arguments # @example EnforcedStyle: arguments
# # bad # # bad
# Rails.root.join('app/models/goober') # Rails.root.join('app/models/goober')
#
# # good
# Rails.root.join('app', 'models', 'goober')
#
# # bad
# File.join(Rails.root, 'app/models/goober') # File.join(Rails.root, 'app/models/goober')
# "#{Rails.root}/app/models/goober" # "#{Rails.root}/app/models/goober"
# #
# # good # # good
# Rails.root.join('app', 'models', 'goober') # Rails.root.join('app', 'models', 'goober').to_s
# #
class FilePath < Base class FilePath < Base
include ConfigurableEnforcedStyle include ConfigurableEnforcedStyle
include RangeHelp include RangeHelp
MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')`.' MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')%<to_s>s`.'
MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')`.' MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')%<to_s>s`.'
RESTRICT_ON_SEND = %i[join].freeze RESTRICT_ON_SEND = %i[join].freeze
def_node_matcher :file_join_nodes?, <<~PATTERN def_node_matcher :file_join_nodes?, <<~PATTERN
@ -53,7 +62,7 @@ module RuboCop
return unless last_child_source.start_with?('.') || last_child_source.include?(File::SEPARATOR) return unless last_child_source.start_with?('.') || last_child_source.include?(File::SEPARATOR)
return if last_child_source.start_with?(':') return if last_child_source.start_with?(':')
register_offense(node) register_offense(node, require_to_s: true)
end end
def on_send(node) def on_send(node)
@ -68,7 +77,7 @@ module RuboCop
return unless file_join_nodes?(node) return unless file_join_nodes?(node)
return unless node.arguments.any? { |e| rails_root_nodes?(e) } return unless node.arguments.any? { |e| rails_root_nodes?(e) }
register_offense(node) register_offense(node, require_to_s: true)
end end
def check_for_rails_root_join_with_string_arguments(node) def check_for_rails_root_join_with_string_arguments(node)
@ -78,7 +87,7 @@ module RuboCop
return unless node.arguments.size > 1 return unless node.arguments.size > 1
return unless node.arguments.all?(&:str_type?) return unless node.arguments.all?(&:str_type?)
register_offense(node) register_offense(node, require_to_s: false)
end end
def check_for_rails_root_join_with_slash_separated_path(node) def check_for_rails_root_join_with_slash_separated_path(node)
@ -87,21 +96,28 @@ module RuboCop
return unless rails_root_join_nodes?(node) return unless rails_root_join_nodes?(node)
return unless node.arguments.any? { |arg| string_with_slash?(arg) } return unless node.arguments.any? { |arg| string_with_slash?(arg) }
register_offense(node) register_offense(node, require_to_s: false)
end end
def string_with_slash?(node) def string_with_slash?(node)
node.str_type? && node.source.include?('/') node.str_type? && node.source.include?('/')
end end
def register_offense(node) def register_offense(node, require_to_s:)
line_range = node.loc.column...node.loc.last_column line_range = node.loc.column...node.loc.last_column
source_range = source_range(processed_source.buffer, node.first_line, line_range) source_range = source_range(processed_source.buffer, node.first_line, line_range)
add_offense(source_range) require_to_s = false if node.dstr_type?
message = build_message(require_to_s)
add_offense(source_range, message: message)
end end
def message(_range) def build_message(require_to_s)
format(style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES) message_template = style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES
to_s = require_to_s ? '.to_s' : ''
format(message_template, to_s: to_s)
end end
end end
end end

View File

@ -65,11 +65,11 @@ module RuboCop
end end
def where_take_offense_range(node, where) def where_take_offense_range(node, where)
range_between(where.loc.selector.begin_pos, node.loc.expression.end_pos) range_between(where.loc.selector.begin_pos, node.source_range.end_pos)
end end
def find_by_offense_range(node) def find_by_offense_range(node)
range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end end
def build_good_method(id_value) def build_good_method(id_value)

View File

@ -3,8 +3,12 @@
module RuboCop module RuboCop
module Cop module Cop
module Rails module Rails
# Identifies usages of `all.each` and # Identifies usages of `all.each` and change them to use `all.find_each` instead.
# change them to use `all.find_each` instead. #
# @safety
# This cop is unsafe if the receiver object is not an Active Record object.
# Also, `all.each` returns an `Array` instance and `all.find_each` returns nil,
# so the return values are different.
# #
# @example # @example
# # bad # # bad

View File

@ -10,6 +10,9 @@ module RuboCop
# Rails/HttpPositionalArguments cop or set your TargetRailsVersion in your # Rails/HttpPositionalArguments cop or set your TargetRailsVersion in your
# .rubocop.yml file to 4.2. # .rubocop.yml file to 4.2.
# #
# NOTE: It does not detect any cases where `include Rack::Test::Methods` is used
# which makes the http methods incompatible behavior.
#
# @example # @example
# # bad # # bad
# get :new, { user_id: 1} # get :new, { user_id: 1}
@ -37,8 +40,15 @@ module RuboCop
(hash (kwsplat _)) (hash (kwsplat _))
PATTERN PATTERN
def_node_matcher :include_rack_test_methods?, <<~PATTERN
(send nil? :include
(const
(const
(const {nil? cbase} :Rack) :Test) :Methods))
PATTERN
def on_send(node) def on_send(node)
return if in_routing_block?(node) return if in_routing_block?(node) || use_rack_test_methods?
http_request?(node) do |data| http_request?(node) do |data|
return unless needs_conversion?(data) return unless needs_conversion?(data)
@ -56,7 +66,7 @@ module RuboCop
# that represents the path/action on the Rails controller # that represents the path/action on the Rails controller
# the data is the http parameters and environment sent in # the data is the http parameters and environment sent in
# the Rails 5 http call # the Rails 5 http call
corrector.replace(node.loc.expression, correction(node)) corrector.replace(node, correction(node))
end end
end end
end end
@ -67,6 +77,12 @@ module RuboCop
!!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.method_name) } !!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.method_name) }
end end
def use_rack_test_methods?
processed_source.ast.each_descendant(:send).any? do |node|
include_rack_test_methods?(node)
end
end
def needs_conversion?(data) def needs_conversion?(data)
return true unless data.hash_type? return true unless data.hash_type?
return false if kwsplat_hash?(data) return false if kwsplat_hash?(data)

View File

@ -66,7 +66,7 @@ module RuboCop
return unless checker.offensive? return unless checker.offensive?
add_offense(checker.node, message: checker.message) do |corrector| add_offense(checker.node, message: checker.message) do |corrector|
corrector.replace(checker.node.loc.expression, checker.preferred_style) corrector.replace(checker.node, checker.preferred_style)
end end
end end
end end

View File

@ -68,7 +68,7 @@ module RuboCop
def append_to_rel(rel_node, corrector) def append_to_rel(rel_node, corrector)
existing_rel = rel_node.children.last.value existing_rel = rel_node.children.last.value
str_range = rel_node.children.last.loc.expression.adjust(begin_pos: 1, end_pos: -1) str_range = rel_node.children.last.source_range.adjust(begin_pos: 1, end_pos: -1)
corrector.replace(str_range, "#{existing_rel} noopener") corrector.replace(str_range, "#{existing_rel} noopener")
end end

View File

@ -77,7 +77,7 @@ module RuboCop
corrector.replace(node.loc.name, "#{name}Mailer") corrector.replace(node.loc.name, "#{name}Mailer")
else else
name = node.children.last name = node.children.last
corrector.replace(node.source_range, "#{name}Mailer") corrector.replace(node, "#{name}Mailer")
end end
end end

View File

@ -39,7 +39,8 @@ module RuboCop
PATTERN PATTERN
def on_send(node) def on_send(node)
return unless (output?(node) || io_output?(node)) && node.arguments? return if node.parent&.call_type?
return unless output?(node) || io_output?(node)
range = offense_range(node) range = offense_range(node)
@ -56,7 +57,7 @@ module RuboCop
def offense_range(node) def offense_range(node)
if node.receiver if node.receiver
range_between(node.loc.expression.begin_pos, node.loc.selector.end_pos) range_between(node.source_range.begin_pos, node.loc.selector.end_pos)
else else
node.loc.selector node.loc.selector
end end

View File

@ -66,8 +66,12 @@ module RuboCop
MSG = 'Tagging a string as html safe may be a security risk.' MSG = 'Tagging a string as html safe may be a security risk.'
RESTRICT_ON_SEND = %i[html_safe raw safe_concat].freeze RESTRICT_ON_SEND = %i[html_safe raw safe_concat].freeze
def_node_search :i18n_method?, <<~PATTERN
(send {nil? (const {nil? cbase} :I18n)} {:t :translate :l :localize} ...)
PATTERN
def on_send(node) def on_send(node)
return if non_interpolated_string?(node) return if non_interpolated_string?(node) || i18n_method?(node)
return unless looks_like_rails_html_safe?(node) || return unless looks_like_rails_html_safe?(node) ||
looks_like_rails_raw?(node) || looks_like_rails_raw?(node) ||

View File

@ -43,7 +43,7 @@ module RuboCop
def on_block(node) def on_block(node)
pluck_candidate?(node) do |argument, key| pluck_candidate?(node) do |argument, key|
next unless use_one_block_argument?(argument) next if key.regexp_type? || !use_one_block_argument?(argument)
match = if node.block_type? match = if node.block_type?
block_argument = argument.children.first.source block_argument = argument.children.first.source

View File

@ -51,7 +51,7 @@ module RuboCop
private private
def offense_range(node) def offense_range(node)
range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end end
end end
end end

View File

@ -93,7 +93,7 @@ module RuboCop
def register_offense(node, receiver, other) def register_offense(node, receiver, other)
add_offense(node, message: message(node, receiver, other)) do |corrector| add_offense(node, message: message(node, receiver, other)) do |corrector|
corrector.replace(node.source_range, replacement(receiver, other, node.left_sibling)) corrector.replace(node, replacement(receiver, other, node.left_sibling))
end end
end end

View File

@ -128,10 +128,10 @@ module RuboCop
if method_call if method_call
corrector.replace(node.loc.keyword, 'if') corrector.replace(node.loc.keyword, 'if')
range = method_call.loc.expression range = method_call.source_range
else else
variable1, _variable2 = exists_and_not_empty?(node) || not_blank?(node) variable1, _variable2 = exists_and_not_empty?(node) || not_blank?(node)
range = node.loc.expression range = node.source_range
end end
corrector.replace(range, replacement(variable1)) corrector.replace(range, replacement(variable1))
@ -141,9 +141,9 @@ module RuboCop
def unless_condition(node, method_call) def unless_condition(node, method_call)
if node.modifier_form? if node.modifier_form?
node.loc.keyword.join(node.loc.expression.end) node.loc.keyword.join(node.source_range.end)
else else
node.loc.expression.begin.join(method_call.loc.expression) node.source_range.begin.join(method_call.source_range)
end end
end end

View File

@ -48,7 +48,7 @@ module RuboCop
task_name = task_method.arguments[0] task_name = task_method.arguments[0]
task_dependency = correct_task_dependency(task_name) task_dependency = correct_task_dependency(task_name)
corrector.replace(task_name.loc.expression, task_dependency) corrector.replace(task_name, task_dependency)
end end
end end
end end

View File

@ -52,7 +52,7 @@ module RuboCop
return if within_shadowing_method?(node) return if within_shadowing_method?(node)
add_offense(node, message: build_message(node)) do |corrector| add_offense(node, message: build_message(node)) do |corrector|
corrector.replace(node.source_range, node_replacement(node)) corrector.replace(node, node_replacement(node))
end end
end end

View File

@ -62,7 +62,7 @@ module RuboCop
elsif prv_sib elsif prv_sib
corrector.remove(range_between(node_end(prv_sib), node_end(allow_nil))) corrector.remove(range_between(node_end(prv_sib), node_end(allow_nil)))
else else
corrector.remove(allow_nil.loc.expression) corrector.remove(allow_nil)
end end
end end
end end
@ -87,11 +87,11 @@ module RuboCop
end end
def node_beg(node) def node_beg(node)
node.loc.expression.begin_pos node.source_range.begin_pos
end end
def node_end(node) def node_end(node)
node.loc.expression.end_pos node.source_range.end_pos
end end
end end
end end

View File

@ -40,7 +40,7 @@ module RuboCop
def on_send(node) def on_send(node)
association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key| association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
if redundant?(node, type, name, options, foreign_key) if redundant?(node, type, name, options, foreign_key)
add_offense(foreign_key_pair.loc.expression) do |corrector| add_offense(foreign_key_pair.source_range) do |corrector|
range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left) range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
range = range_with_surrounding_comma(range, :left) range = range_with_surrounding_comma(range, :left)

View File

@ -89,7 +89,7 @@ module RuboCop
private private
def autocorrect(corrector, send_node, node) def autocorrect(corrector, send_node, node)
corrector.remove(send_node.receiver.source_range) corrector.remove(send_node.receiver)
corrector.remove(send_node.loc.dot) corrector.remove(send_node.loc.dot)
corrector.remove(block_argument_range(send_node)) unless node.numblock_type? corrector.remove(block_argument_range(send_node)) unless node.numblock_type?
end end

Some files were not shown because too many files have changed in this diff Show More