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 (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.18.0)
rubocop-rails (2.19.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)

View File

@ -124,7 +124,7 @@ module RuboCop
end
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)
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|

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)
# 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)
regex_match_group(desc, /^[a-z]/)
if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) && regex_match_group(desc, /^[a-z]/)
desc_problem "Description should start with a capital letter."
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 on_send(node); end
def timestamps_included?(param0); end
def use_id_false_option?(param0); end
private
@ -848,11 +849,11 @@ class RuboCop::Cop::Rails::FilePath < ::RuboCop::Cop::Base
private
def build_message(require_to_s); 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_string_arguments(node); end
def message(_range); end
def register_offense(node); end
def register_offense(node, require_to_s:); end
def string_with_slash?(node); end
end
@ -986,6 +987,7 @@ class RuboCop::Cop::Rails::HttpPositionalArguments < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::TargetRailsVersion
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 on_send(node); end
@ -999,6 +1001,7 @@ class RuboCop::Cop::Rails::HttpPositionalArguments < ::RuboCop::Cop::Base
def in_routing_block?(node); end
def needs_conversion?(data); end
def special_keyword_arg?(node); end
def use_rack_test_methods?; end
end
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)
class RuboCop::Cop::Rails::OutputSafety < ::RuboCop::Cop::Base
def i18n_method?(param0); end
def on_csend(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)
class RuboCop::Cop::Rails::ReflectionClassName < ::RuboCop::Cop::Base
extend ::RuboCop::Cop::AutoCorrector
def association_with_reflection(param0 = T.unsafe(nil)); end
def const_or_string(param0 = T.unsafe(nil)); end
def on_send(node); end
def reflection_class_name(param0 = T.unsafe(nil)); end
private
def autocorrect(corrector, class_config); end
def reflection_class_value?(class_value); end
def str_assigned?(reflection_class_name); end
end
@ -2018,6 +2026,20 @@ end
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
include ::RuboCop::Cop::ConfigurableEnforcedStyle
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_OR_NEWER_OR_OLDER = ::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____ETC_4 = ::T.let(nil, ::T.untyped)
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
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"]
# 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-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-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-sorbet-0.7.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:
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'
SafeAutoCorrect: false
VersionAdded: '2.16'
@ -462,8 +460,9 @@ Rails/FindEach:
Description: 'Prefer all.find_each over all.each.'
StyleGuide: 'https://rails.rubystyle.guide#find-each'
Enabled: true
Safe: false
VersionAdded: '0.30'
VersionChanged: '2.9'
VersionChanged: '2.19'
Include:
- app/models/**/*.rb
AllowedMethods:
@ -844,8 +843,9 @@ Rails/RequireDependency:
Rails/ResponseParsedBody:
Description: Prefer `response.parsed_body` to `JSON.parse(response.body)`.
Enabled: pending
SafeAutoCorrect: false
Safe: false
VersionAdded: '2.18'
VersionChanged: '2.19'
Include:
- spec/controllers/**/*.rb
- spec/requests/**/*.rb
@ -997,6 +997,14 @@ Rails/TableNameAssignment:
Include:
- 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:
Description: 'Checks the correct usage of time zone aware methods.'
StyleGuide: 'https://rails.rubystyle.guide#time'
@ -1144,15 +1152,6 @@ Style/FormatStringToken:
AllowedMethods:
- 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:
AllowedMethods:
- define_method

View File

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

View File

@ -32,7 +32,7 @@ module RuboCop
def register_offense(offense_node)
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

View File

@ -138,7 +138,7 @@ module RuboCop
end
def strip_prefix_and_suffix(node, corrector)
expression = node.loc.expression
expression = node.source_range
corrector.remove_leading(expression, leading)
corrector.remove_trailing(expression, trailing)
end
@ -153,11 +153,11 @@ module RuboCop
end
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
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

View File

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

View File

@ -104,7 +104,7 @@ module RuboCop
end
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
end
@ -112,8 +112,7 @@ module RuboCop
annotation_line = node.first_line - 1
first_comment = nil
processed_source.comments_before_line(annotation_line)
.reverse_each do |comment|
processed_source.each_comment_in_lines(0..annotation_line).reverse_each do |comment|
if comment.location.line == annotation_line && !inline_comment?(comment)
first_comment = comment
annotation_line -= 1
@ -124,7 +123,11 @@ module RuboCop
end
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)
# rubocop:enable InternalAffairs/LocationExpression
end
def start_line_position(node)

View File

@ -42,7 +42,7 @@ module RuboCop
add_index_opts = ''
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}"
end
@ -53,7 +53,7 @@ module RuboCop
private
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -94,9 +94,9 @@ module RuboCop
def offense_range(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
node.loc.expression.end_pos
node.source_range.end_pos
end
range_between(node.loc.selector.begin_pos, end_pos)

View File

@ -85,7 +85,7 @@ module RuboCop
end
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

View File

@ -3,10 +3,12 @@
module RuboCop
module Cop
module Rails
# Checks the migration for which timestamps are not included
# when creating a new table.
# Checks the migration for which timestamps are not included when creating a new table.
# 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
# # bad
# create_table :users
@ -40,12 +42,23 @@ module RuboCop
#
# t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' }
# end
#
# # good
# create_table :users, articles, id: false do |t|
# t.integer :user_id
# t.integer :article_id
# end
#
class CreateTableWithTimestamps < Base
include ActiveRecordMigrationsHelper
MSG = 'Add timestamps when creating a new table.'
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
(send nil? :create_table (sym _) ... (block-pass (sym :timestamps)))
PATTERN
@ -61,7 +74,7 @@ module RuboCop
PATTERN
def on_send(node)
return unless node.command?(:create_table)
return if !node.command?(:create_table) || use_id_false_option?(node)
parent = node.parent

View File

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

View File

@ -27,7 +27,7 @@ module RuboCop
return unless (offending_node = allow_blank_option(node))
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

View File

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

View File

@ -99,7 +99,7 @@ module RuboCop
def autocorrect_argument_keywords(corrector, node, keywords)
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

View File

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

View File

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

View File

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

View File

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

View File

@ -65,11 +65,11 @@ module RuboCop
end
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
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
def build_good_method(id_value)

View File

@ -3,8 +3,12 @@
module RuboCop
module Cop
module Rails
# Identifies usages of `all.each` and
# change them to use `all.find_each` instead.
# Identifies usages of `all.each` and 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
# # bad

View File

@ -10,6 +10,9 @@ module RuboCop
# Rails/HttpPositionalArguments cop or set your TargetRailsVersion in your
# .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
# # bad
# get :new, { user_id: 1}
@ -37,8 +40,15 @@ module RuboCop
(hash (kwsplat _))
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)
return if in_routing_block?(node)
return if in_routing_block?(node) || use_rack_test_methods?
http_request?(node) do |data|
return unless needs_conversion?(data)
@ -56,7 +66,7 @@ module RuboCop
# that represents the path/action on the Rails controller
# the data is the http parameters and environment sent in
# the Rails 5 http call
corrector.replace(node.loc.expression, correction(node))
corrector.replace(node, correction(node))
end
end
end
@ -67,6 +77,12 @@ module RuboCop
!!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.method_name) }
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)
return true unless data.hash_type?
return false if kwsplat_hash?(data)

View File

@ -66,7 +66,7 @@ module RuboCop
return unless checker.offensive?
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

View File

@ -68,7 +68,7 @@ module RuboCop
def append_to_rel(rel_node, corrector)
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")
end

View File

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

View File

@ -39,7 +39,8 @@ module RuboCop
PATTERN
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)
@ -56,7 +57,7 @@ module RuboCop
def offense_range(node)
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
node.loc.selector
end

View File

@ -66,8 +66,12 @@ module RuboCop
MSG = 'Tagging a string as html safe may be a security risk.'
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)
return if non_interpolated_string?(node)
return if non_interpolated_string?(node) || i18n_method?(node)
return unless looks_like_rails_html_safe?(node) ||
looks_like_rails_raw?(node) ||

View File

@ -43,7 +43,7 @@ module RuboCop
def on_block(node)
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?
block_argument = argument.children.first.source

View File

@ -51,7 +51,7 @@ module RuboCop
private
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

View File

@ -93,7 +93,7 @@ module RuboCop
def register_offense(node, receiver, other)
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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ module RuboCop
return if within_shadowing_method?(node)
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

View File

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

View File

@ -40,7 +40,7 @@ module RuboCop
def on_send(node)
association_with_foreign_key(node) do |type, name, options, foreign_key_pair, 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_comma(range, :left)

View File

@ -89,7 +89,7 @@ module RuboCop
private
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(block_argument_range(send_node)) unless node.numblock_type?
end

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