From c4c1ce1bc76036333f53cb094c31158aedd3108a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 17 Aug 2018 04:31:10 +0900 Subject: [PATCH] Enable `Layout/EmptyLineAfterGuardClause` cop by default This PR enables `Layout/EmptyLineAfterGuardClause` cop by default. This cop has been introduced by #5522, with setting that is disabled by default. After that, I improved this cop by #5679, #5700, #5720, and #5760. I think that this cop can be enabled by default as the background of the issue #5376. And this PR applies auto-correction using this cop. --- CHANGELOG.md | 1 + Rakefile | 2 ++ config/disabled.yml | 4 ---- config/enabled.yml | 4 ++++ lib/rubocop/ast/node.rb | 6 ++++++ lib/rubocop/ast/traversal.rb | 10 ++++++++++ lib/rubocop/cli.rb | 1 + lib/rubocop/config.rb | 4 ++++ lib/rubocop/config_loader.rb | 2 ++ lib/rubocop/config_loader_resolver.rb | 2 ++ lib/rubocop/cop/autocorrect_logic.rb | 1 + lib/rubocop/cop/bundler/ordered_gems.rb | 2 ++ lib/rubocop/cop/commissioner.rb | 2 ++ lib/rubocop/cop/cop.rb | 3 +++ lib/rubocop/cop/corrector.rb | 2 ++ lib/rubocop/cop/correctors/alignment_corrector.rb | 1 + lib/rubocop/cop/correctors/line_break_corrector.rb | 2 ++ lib/rubocop/cop/correctors/space_corrector.rb | 2 ++ lib/rubocop/cop/force.rb | 1 + lib/rubocop/cop/gemspec/ordered_dependencies.rb | 1 + lib/rubocop/cop/generator.rb | 1 + lib/rubocop/cop/layout/class_structure.rb | 4 ++++ lib/rubocop/cop/layout/else_alignment.rb | 1 + lib/rubocop/cop/layout/empty_line_between_defs.rb | 2 ++ lib/rubocop/cop/layout/empty_lines_around_arguments.rb | 1 + .../empty_lines_around_exception_handling_keywords.rb | 1 + lib/rubocop/cop/layout/end_of_line.rb | 1 + lib/rubocop/cop/layout/extra_spacing.rb | 1 + lib/rubocop/cop/layout/indent_array.rb | 1 + lib/rubocop/cop/layout/indent_heredoc.rb | 3 +++ lib/rubocop/cop/layout/indentation_width.rb | 2 ++ .../cop/layout/multiline_method_call_indentation.rb | 1 + .../cop/layout/multiline_operation_indentation.rb | 1 + lib/rubocop/cop/layout/rescue_ensure_alignment.rb | 2 ++ lib/rubocop/cop/layout/space_after_method_name.rb | 1 + lib/rubocop/cop/layout/space_around_keyword.rb | 2 ++ lib/rubocop/cop/layout/space_around_operators.rb | 1 + lib/rubocop/cop/layout/space_before_comment.rb | 1 + .../cop/layout/space_inside_array_literal_brackets.rb | 2 ++ .../cop/layout/space_inside_reference_brackets.rb | 2 ++ lib/rubocop/cop/layout/tab.rb | 1 + lib/rubocop/cop/layout/trailing_whitespace.rb | 1 + lib/rubocop/cop/lint/deprecated_class_methods.rb | 1 + lib/rubocop/cop/lint/duplicate_methods.rb | 3 +++ lib/rubocop/cop/lint/format_parameter_mismatch.rb | 1 + lib/rubocop/cop/lint/ineffective_access_modifier.rb | 1 + lib/rubocop/cop/lint/interpolation_check.rb | 2 ++ lib/rubocop/cop/lint/literal_as_condition.rb | 1 + lib/rubocop/cop/lint/missing_cop_enable_directive.rb | 1 + lib/rubocop/cop/lint/nested_method_definition.rb | 1 + lib/rubocop/cop/lint/rescue_exception.rb | 1 + lib/rubocop/cop/lint/rescue_type.rb | 1 + lib/rubocop/cop/lint/safe_navigation_consistency.rb | 2 ++ lib/rubocop/cop/lint/script_permission.rb | 1 + lib/rubocop/cop/lint/shadowed_exception.rb | 2 ++ lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb | 1 + lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb | 1 + lib/rubocop/cop/lint/unneeded_require_statement.rb | 1 + lib/rubocop/cop/lint/unreachable_code.rb | 2 ++ lib/rubocop/cop/lint/useless_assignment.rb | 1 + lib/rubocop/cop/lint/useless_setter_call.rb | 3 +++ lib/rubocop/cop/lint/void.rb | 1 + lib/rubocop/cop/message_annotator.rb | 1 + lib/rubocop/cop/metrics/block_length.rb | 1 + lib/rubocop/cop/metrics/block_nesting.rb | 1 + lib/rubocop/cop/metrics/line_length.rb | 5 +++++ lib/rubocop/cop/metrics/method_length.rb | 1 + lib/rubocop/cop/mixin/annotation_comment.rb | 1 + lib/rubocop/cop/mixin/classish_length.rb | 1 + lib/rubocop/cop/mixin/configurable_enforced_style.rb | 1 + lib/rubocop/cop/mixin/configurable_formatting.rb | 1 + lib/rubocop/cop/mixin/empty_lines_around_body.rb | 2 ++ lib/rubocop/cop/mixin/empty_parameter.rb | 1 + .../cop/mixin/multiline_expression_indentation.rb | 1 + lib/rubocop/cop/mixin/percent_literal.rb | 2 ++ lib/rubocop/cop/mixin/preceding_following_alignment.rb | 2 ++ lib/rubocop/cop/mixin/statement_modifier.rb | 1 + lib/rubocop/cop/mixin/string_literals_help.rb | 1 + lib/rubocop/cop/mixin/surrounding_space.rb | 4 ++++ lib/rubocop/cop/mixin/trailing_comma.rb | 1 + lib/rubocop/cop/mixin/uncommunicative_name.rb | 2 ++ lib/rubocop/cop/naming/ascii_identifiers.rb | 1 + .../cop/naming/binary_operator_parameter_name.rb | 1 + lib/rubocop/cop/naming/file_name.rb | 2 ++ .../cop/naming/memoized_instance_variable_name.rb | 1 + .../cop/naming/uncommunicative_block_param_name.rb | 1 + .../cop/naming/uncommunicative_method_param_name.rb | 1 + lib/rubocop/cop/naming/variable_name.rb | 1 + lib/rubocop/cop/performance/compare_with_block.rb | 3 +++ lib/rubocop/cop/performance/regexp_match.rb | 1 + lib/rubocop/cop/performance/sample.rb | 2 ++ lib/rubocop/cop/performance/string_replacement.rb | 1 + lib/rubocop/cop/rails/active_support_aliases.rb | 1 + lib/rubocop/cop/rails/bulk_change_table.rb | 7 +++++++ lib/rubocop/cop/rails/create_table_with_timestamps.rb | 1 + lib/rubocop/cop/rails/dynamic_find_by.rb | 1 + lib/rubocop/cop/rails/http_status.rb | 2 ++ lib/rubocop/cop/rails/inverse_of.rb | 4 ++++ .../cop/rails/lexically_scoped_action_filter.rb | 1 + .../cop/rails/redundant_receiver_in_with_options.rb | 1 + lib/rubocop/cop/rails/reversible_migration.rb | 1 + lib/rubocop/cop/rails/save_bang.rb | 6 ++++++ lib/rubocop/cop/rails/time_zone.rb | 1 + lib/rubocop/cop/security/eval.rb | 1 + lib/rubocop/cop/security/open.rb | 1 + lib/rubocop/cop/severity.rb | 1 + lib/rubocop/cop/style/ascii_comments.rb | 1 + lib/rubocop/cop/style/block_delimiters.rb | 1 + lib/rubocop/cop/style/class_and_module_children.rb | 3 +++ lib/rubocop/cop/style/colon_method_definition.rb | 1 + lib/rubocop/cop/style/commented_keyword.rb | 2 ++ lib/rubocop/cop/style/conditional_assignment.rb | 2 ++ lib/rubocop/cop/style/copyright.rb | 5 +++++ lib/rubocop/cop/style/empty_case_condition.rb | 1 + lib/rubocop/cop/style/empty_else.rb | 2 ++ lib/rubocop/cop/style/empty_lambda_parameter.rb | 1 + lib/rubocop/cop/style/eval_with_location.rb | 2 ++ lib/rubocop/cop/style/if_with_semicolon.rb | 1 + lib/rubocop/cop/style/inverse_methods.rb | 1 + lib/rubocop/cop/style/lambda.rb | 1 + lib/rubocop/cop/style/missing_else.rb | 1 + lib/rubocop/cop/style/multiline_memoization.rb | 1 + lib/rubocop/cop/style/multiple_comparison.rb | 1 + lib/rubocop/cop/style/or_assignment.rb | 2 ++ lib/rubocop/cop/style/random_with_offset.rb | 1 + lib/rubocop/cop/style/redundant_conditional.rb | 1 + lib/rubocop/cop/style/redundant_parentheses.rb | 5 +++++ lib/rubocop/cop/style/redundant_return.rb | 1 + lib/rubocop/cop/style/rescue_modifier.rb | 1 + lib/rubocop/cop/style/rescue_standard_error.rb | 1 + lib/rubocop/cop/style/safe_navigation.rb | 4 ++++ lib/rubocop/cop/style/semicolon.rb | 4 ++++ lib/rubocop/cop/style/signal_exception.rb | 1 + lib/rubocop/cop/style/string_hash_keys.rb | 1 + .../cop/style/trailing_comma_in_array_literal.rb | 1 + lib/rubocop/cop/style/trailing_method_end_statement.rb | 1 + lib/rubocop/cop/style/trailing_underscore_variable.rb | 1 + lib/rubocop/cop/style/unneeded_condition.rb | 1 + lib/rubocop/cop/style/unneeded_percent_q.rb | 2 ++ lib/rubocop/cop/team.rb | 1 + lib/rubocop/cop/variable_force.rb | 5 +++++ lib/rubocop/cop/variable_force/assignment.rb | 4 ++++ lib/rubocop/cop/variable_force/branch.rb | 4 ++++ lib/rubocop/cop/variable_force/branchable.rb | 2 ++ lib/rubocop/cop/variable_force/scope.rb | 6 ++++++ lib/rubocop/cop/variable_force/variable_table.rb | 1 + lib/rubocop/file_finder.rb | 2 ++ lib/rubocop/formatter/disabled_config_formatter.rb | 8 ++++---- lib/rubocop/formatter/file_list_formatter.rb | 1 + lib/rubocop/formatter/simple_text_formatter.rb | 1 + lib/rubocop/options.rb | 3 +++ lib/rubocop/path_util.rb | 1 + lib/rubocop/processed_source.rb | 4 ++++ lib/rubocop/remote_config.rb | 1 + lib/rubocop/result_cache.rb | 1 + lib/rubocop/rspec/cop_helper.rb | 3 +++ lib/rubocop/runner.rb | 1 + lib/rubocop/target_finder.rb | 2 ++ manual/cops_layout.md | 2 +- spec/rubocop/cli/cli_auto_gen_config_spec.rb | 2 ++ spec/rubocop/cop/corrector_spec.rb | 1 + spec/rubocop/options_spec.rb | 1 + spec/rubocop/result_cache_spec.rb | 1 + 163 files changed, 292 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdd9ffa1acbf..1f316249c6b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ * [#4832](https://github.com/rubocop-hq/rubocop/issues/4832): Change the path pattern (`*`) to match the hidden file. ([@koic][]) * `Style/For` now highlights the entire statement rather than just the keyword. ([@rrosenblum][]) * Disable `Performance/CaseWhenSplat` and its auto-correction by default. ([@rrosenblum][]) +* [#6235](https://github.com/rubocop-hq/rubocop/pull/6235): Enable `Layout/EmptyLineAfterGuardClause` cop by default. ([@koic][]) ## 0.58.2 (2018-07-23) diff --git a/Rakefile b/Rakefile index d82a4725b097..427ae7a1863e 100644 --- a/Rakefile +++ b/Rakefile @@ -116,8 +116,10 @@ task documentation_syntax_check: :yard_for_generate_documentation do cops = RuboCop::Cop::Cop.registry cops.each do |cop| next if %i[RSpec Capybara FactoryBot].include?(cop.department) + examples = YARD::Registry.all(:class).find do |code_object| next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge + break code_object.tags('example') end diff --git a/config/disabled.yml b/config/disabled.yml index c90e5fde8300..11fd548e8d65 100644 --- a/config/disabled.yml +++ b/config/disabled.yml @@ -14,10 +14,6 @@ Layout/ClassStructure: StyleGuide: 'https://github.com/rubocop-hq/ruby-style-guide#consistent-classes' Enabled: false -Layout/EmptyLineAfterGuardClause: - Description: 'Add empty line after guard clause.' - Enabled: false - Layout/FirstArrayElementLineBreak: Description: >- Checks for a line break before the first element in a diff --git a/config/enabled.yml b/config/enabled.yml index 8cbd4b2c0fbc..7e6c4a377a9d 100644 --- a/config/enabled.yml +++ b/config/enabled.yml @@ -127,6 +127,10 @@ Layout/EmptyComment: Description: 'Checks empty comment.' Enabled: true +Layout/EmptyLineAfterGuardClause: + Description: 'Add empty line after guard clause.' + Enabled: true + Layout/EmptyLineAfterMagicComment: Description: 'Add an empty line after magic comments to separate them from code.' StyleGuide: '#separate-magic-comments-from-code' diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index 9913d761349c..9d195ccf4e5a 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -181,6 +181,7 @@ def each_child_node(*types) children.each do |child| next unless child.is_a?(Node) + yield child if types.empty? || types.include?(child.type) end @@ -279,6 +280,7 @@ def last_line def line_count return 0 unless source_range + source_range.last_line - source_range.first_line + 1 end @@ -302,6 +304,7 @@ def source_length def const_name return unless const_type? + namespace, name = *self if namespace && !namespace.cbase_type? "#{namespace.const_name}::#{name}" @@ -436,12 +439,14 @@ def operator_keyword? def unary_operation? return false unless loc.respond_to?(:selector) && loc.selector + Cop::Util.operator?(loc.selector.source.to_sym) && source_range.begin_pos == loc.selector.begin_pos end def binary_operation? return false unless loc.respond_to?(:selector) && loc.selector + Cop::Util.operator?(method_name) && source_range.begin_pos != loc.selector.begin_pos end @@ -613,6 +618,7 @@ def parent_module_name_for_block(ancestor) # `class_eval` with no receiver applies to whatever module or class # we are currently in return unless (receiver = ancestor.receiver) + yield unless receiver.const_type? receiver.const_name elsif !new_class_or_module_block?(ancestor) diff --git a/lib/rubocop/ast/traversal.rb b/lib/rubocop/ast/traversal.rb index c01cd5a77223..64d20d62ba72 100644 --- a/lib/rubocop/ast/traversal.rb +++ b/lib/rubocop/ast/traversal.rb @@ -9,6 +9,7 @@ module AST module Traversal def walk(node) return if node.nil? + send(:"on_#{node.type}", node) nil end @@ -65,6 +66,7 @@ def on_#{type}(node) def on_const(node) return unless (child = node.children[0]) + send(:"on_#{child.type}", child) end @@ -74,6 +76,7 @@ def on_casgn(node) send(:"on_#{child.type}", child) end return unless (child = children[2]) + send(:"on_#{child.type}", child) end @@ -85,6 +88,7 @@ def on_class(node) send(:"on_#{child.type}", child) end return unless (child = children[2]) + send(:"on_#{child.type}", child) end @@ -92,12 +96,14 @@ def on_def(node) children = node.children on_args(children[1]) return unless (child = children[2]) + send(:"on_#{child.type}", child) end def on_send(node) node.children.each_with_index do |child, i| next if i == 1 + send(:"on_#{child.type}", child) if child end nil @@ -119,6 +125,7 @@ def on_defs(node) send(:"on_#{child.type}", child) on_args(children[2]) return unless (child = children[3]) + send(:"on_#{child.type}", child) end @@ -130,6 +137,7 @@ def on_if(node) send(:"on_#{child.type}", child) end return unless (child = children[2]) + send(:"on_#{child.type}", child) end @@ -138,6 +146,7 @@ def on_while(node) child = children[0] send(:"on_#{child.type}", child) return unless (child = children[1]) + send(:"on_#{child.type}", child) end @@ -151,6 +160,7 @@ def on_block(node) send(:"on_#{child.type}", child) # can be send, zsuper... on_args(children[1]) return unless (child = children[2]) + send(:"on_#{child.type}", child) end diff --git a/lib/rubocop/cli.rb b/lib/rubocop/cli.rb index 8a5cc7337120..4c866f08df09 100644 --- a/lib/rubocop/cli.rb +++ b/lib/rubocop/cli.rb @@ -279,6 +279,7 @@ def maybe_print_corrected_source # So a delimiter is needed for tools to easily identify where the # autocorrected source begins return unless @options[:stdin] && @options[:auto_correct] + puts '=' * 20 print @options[:stdin] end diff --git a/lib/rubocop/config.rb b/lib/rubocop/config.rb index 26ddc32c67db..6c2f26a817b8 100644 --- a/lib/rubocop/config.rb +++ b/lib/rubocop/config.rb @@ -495,6 +495,7 @@ def validate_syntax_cop def validate_section_presence(name) return unless key?(name) && self[name].nil? + raise ValidationError, "empty section #{name} found in #{smart_loaded_path}" end @@ -558,6 +559,7 @@ def obsolete_parameter_message(cop, parameter, alternative) def obsolete_cops OBSOLETE_COPS.map do |cop_name, message| next unless key?(cop_name) || key?(Cop::Badge.parse(cop_name).cop_name) + message + "\n(obsolete configuration found in #{smart_loaded_path}," \ ' please update it)' end @@ -627,6 +629,7 @@ def read_ruby_version_from_bundler_lock_file # "RUBY VERSION" line. in_ruby_section ||= line.match(/^\s*RUBY\s*VERSION\s*$/) next unless in_ruby_section + # We currently only allow this feature to work with MRI ruby. If jruby # (or something else) is used by the project, it's lock file will have a # line that looks like: @@ -657,6 +660,7 @@ def read_rails_version_from_bundler_lock_file def bundler_lock_file_path return nil unless loaded_path + base_path = base_dir_for_path_parameters ['gems.locked', 'Gemfile.lock'].each do |file_name| path = find_file_upwards(file_name, base_path) diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index 5fc08a25ac75..a39b33821a0d 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -95,6 +95,7 @@ def add_excludes_from_files(config, config_file) return if found_files.empty? return if PathUtil.relative_path(found_files.last) == PathUtil.relative_path(config_file) + print 'AllCops/Exclude ' if debug? config.add_excludes_from_higher_level(load_file(found_files.last)) end @@ -129,6 +130,7 @@ def add_inheritance_from_auto_generated_file if File.exist?(DOTFILE) files = Array(load_yaml_configuration(DOTFILE)['inherit_from']) return if files.include?(AUTO_GENERATED_FILE) + files.unshift(AUTO_GENERATED_FILE) file_string = "\n - " + files.join("\n - ") if files.size > 1 rubocop_yml_contents = IO.read(DOTFILE, encoding: Encoding::UTF_8) diff --git a/lib/rubocop/config_loader_resolver.rb b/lib/rubocop/config_loader_resolver.rb index cd552e38f588..0b10922a30bd 100644 --- a/lib/rubocop/config_loader_resolver.rb +++ b/lib/rubocop/config_loader_resolver.rb @@ -23,6 +23,7 @@ def resolve_inheritance(path, hash, file, debug) .reverse.each_with_index do |base_config, index| base_config.each do |k, v| next unless v.is_a?(Hash) + if hash.key?(k) v = merge(v, hash[k], cop_name: k, file: file, debug: debug, @@ -102,6 +103,7 @@ def duplicate_setting?(base_hash, derived_hash, key, inherited_file) return false if inherited_file.start_with?('..') # Legitimate override return false if base_hash[key] == derived_hash[key] # Same value return false if remote_file?(inherited_file) # Can't change + Gem.path.none? { |dir| inherited_file.start_with?(dir) } # Can change? end diff --git a/lib/rubocop/cop/autocorrect_logic.rb b/lib/rubocop/cop/autocorrect_logic.rb index 451b87d7019e..b4f6653cd13d 100644 --- a/lib/rubocop/cop/autocorrect_logic.rb +++ b/lib/rubocop/cop/autocorrect_logic.rb @@ -19,6 +19,7 @@ def support_autocorrect? def autocorrect_enabled? # allow turning off autocorrect on a cop by cop basis return true unless cop_config + cop_config['AutoCorrect'] != false end end diff --git a/lib/rubocop/cop/bundler/ordered_gems.rb b/lib/rubocop/cop/bundler/ordered_gems.rb index 9fe3433e0d16..93d903a57013 100644 --- a/lib/rubocop/cop/bundler/ordered_gems.rb +++ b/lib/rubocop/cop/bundler/ordered_gems.rb @@ -34,6 +34,7 @@ class OrderedGems < Cop def investigate(processed_source) return if processed_source.blank? + gem_declarations(processed_source.ast) .each_cons(2) do |previous, current| next unless consecutive_lines(previous, current) @@ -41,6 +42,7 @@ def investigate(processed_source) gem_name(current), gem_name(previous) ) + register_offense(previous, current) end end diff --git a/lib/rubocop/cop/commissioner.rb b/lib/rubocop/cop/commissioner.rb index d483d447926c..014aea6bac79 100644 --- a/lib/rubocop/cop/commissioner.rb +++ b/lib/rubocop/cop/commissioner.rb @@ -29,6 +29,7 @@ def initialize(cops, forces = [], options = {}) Parser::Meta::NODE_TYPES.each do |node_type| method_name = :"on_#{node_type}" next unless method_defined?(method_name) + define_method(method_name) do |node| trigger_responding_cops(method_name, node) super(node) unless NO_CHILD_NODES.include?(node_type) @@ -105,6 +106,7 @@ def with_cop_error_handling(cop, node = nil) yield rescue StandardError => e raise e if @options[:raise_error] + if node line = node.first_line column = node.loc.column diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb index 1a4d8fdecff6..a5c1a310e5c3 100644 --- a/lib/rubocop/cop/cop.rb +++ b/lib/rubocop/cop/cop.rb @@ -152,6 +152,7 @@ def correct(node) @corrected_nodes[node] = true correction = autocorrect(node) return :uncorrected unless correction + @corrections << correction :corrected end @@ -204,6 +205,7 @@ def annotate(message) def file_name_matches_any?(file, parameter, default_result) patterns = cop_config[parameter] return default_result unless patterns + path = nil patterns.any? do |pattern| # Try to match the absolute path, as Exclude properties are absolute. @@ -217,6 +219,7 @@ def file_name_matches_any?(file, parameter, default_result) def enabled_line?(line_number) return true unless @processed_source + @processed_source.comment_config.cop_enabled_at_line?(self, line_number) end diff --git a/lib/rubocop/cop/corrector.rb b/lib/rubocop/cop/corrector.rb index d310a14402ca..d0cae9284261 100644 --- a/lib/rubocop/cop/corrector.rb +++ b/lib/rubocop/cop/corrector.rb @@ -34,6 +34,7 @@ def initialize(source_buffer, corrections = []) @source_buffer = source_buffer raise 'source_buffer should be a Parser::Source::Buffer' unless \ source_buffer.is_a? Parser::Source::Buffer + @corrections = corrections @source_rewriter = Parser::Source::TreeRewriter.new( source_buffer, @@ -155,6 +156,7 @@ def remove_trailing(range, size) # :nodoc: def validate_range(range) return if range.source_buffer == @source_buffer + unless range.source_buffer.is_a?(Parser::Source::Buffer) # actually this should be enforced by parser gem raise 'Corrector expected range source buffer to be a '\ diff --git a/lib/rubocop/cop/correctors/alignment_corrector.rb b/lib/rubocop/cop/correctors/alignment_corrector.rb index e1d7b86fb29e..3a136e4c9077 100644 --- a/lib/rubocop/cop/correctors/alignment_corrector.rb +++ b/lib/rubocop/cop/correctors/alignment_corrector.rb @@ -14,6 +14,7 @@ class << self def correct(processed_source, node, column_delta) return unless node + @processed_source = processed_source expr = node.respond_to?(:loc) ? node.loc.expression : node return if block_comment_within?(expr) diff --git a/lib/rubocop/cop/correctors/line_break_corrector.rb b/lib/rubocop/cop/correctors/line_break_corrector.rb index 67c7ef02d22f..d0dfb1179f6b 100644 --- a/lib/rubocop/cop/correctors/line_break_corrector.rb +++ b/lib/rubocop/cop/correctors/line_break_corrector.rb @@ -36,6 +36,7 @@ def break_line_before(range:, node:, corrector:, indent_steps: 1, def move_comment(eol_comment:, node:, corrector:) return unless eol_comment + text = eol_comment.loc.expression.source corrector.insert_before(node.source_range, text + "\n" + (' ' * node.loc.keyword.column)) @@ -46,6 +47,7 @@ def move_comment(eol_comment:, node:, corrector:) def remove_semicolon(node, corrector) return unless semicolon(node) + corrector.remove(semicolon(node).pos) end diff --git a/lib/rubocop/cop/correctors/space_corrector.rb b/lib/rubocop/cop/correctors/space_corrector.rb index 699c8fcdfe4d..c3542669537a 100644 --- a/lib/rubocop/cop/correctors/space_corrector.rb +++ b/lib/rubocop/cop/correctors/space_corrector.rb @@ -29,6 +29,7 @@ def remove_space(processed_source, corrector, left_token, right_token) corrector.remove(range) end return unless right_token.space_before? + range = side_space_range(range: right_token.pos, side: :left) corrector.remove(range) end @@ -39,6 +40,7 @@ def add_space(processed_source, corrector, left_token, right_token) corrector.insert_after(left_token.pos, ' ') end return if right_token.space_before? + corrector.insert_before(right_token.pos, ' ') end end diff --git a/lib/rubocop/cop/force.rb b/lib/rubocop/cop/force.rb index e13536f1f1d1..aafcbe99d863 100644 --- a/lib/rubocop/cop/force.rb +++ b/lib/rubocop/cop/force.rb @@ -29,6 +29,7 @@ def name def run_hook(method_name, *args) cops.each do |cop| next unless cop.respond_to?(method_name) + cop.send(method_name, *args) end end diff --git a/lib/rubocop/cop/gemspec/ordered_dependencies.rb b/lib/rubocop/cop/gemspec/ordered_dependencies.rb index 66c80da15769..461d1035aae6 100644 --- a/lib/rubocop/cop/gemspec/ordered_dependencies.rb +++ b/lib/rubocop/cop/gemspec/ordered_dependencies.rb @@ -61,6 +61,7 @@ class OrderedDependencies < Cop def investigate(processed_source) return if processed_source.blank? + dependency_declarations(processed_source.ast) .each_cons(2) do |previous, current| next unless consecutive_lines(previous, current) diff --git a/lib/rubocop/cop/generator.rb b/lib/rubocop/cop/generator.rb index 2d90a7ddebc1..36e1364b29c1 100644 --- a/lib/rubocop/cop/generator.rb +++ b/lib/rubocop/cop/generator.rb @@ -203,6 +203,7 @@ def source_path def snake_case(camel_case_string) return 'rspec' if camel_case_string == 'RSpec' + camel_case_string .gsub(/([^A-Z])([A-Z]+)/, '\1_\2') .gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2') diff --git a/lib/rubocop/cop/layout/class_structure.rb b/lib/rubocop/cop/layout/class_structure.rb index 81791a708758..281bb30020d1 100644 --- a/lib/rubocop/cop/layout/class_structure.rb +++ b/lib/rubocop/cop/layout/class_structure.rb @@ -162,6 +162,7 @@ def autocorrect(node) # @return String otherwise trying to {humanize_node} of the current node def classify(node) return node.to_s unless node.respond_to?(:type) + case node.type when :block classify(node.send_node) @@ -185,6 +186,7 @@ def walk_over_nested_class_definition(class_node) class_elements(class_node).each do |node| classification = classify(node) next if ignore?(classification) + yield node, classification end end @@ -192,6 +194,7 @@ def walk_over_nested_class_definition(class_node) def class_elements(class_node) *, class_def = class_node.children return [] unless class_def + if class_def.def_type? || class_def.send_type? [class_def] else @@ -241,6 +244,7 @@ def humanize_node(node) method_name, = *node if node.def_type? return :initializer if method_name == :initialize + return "#{node_visibility(node)}_methods" end HUMANIZED_NODE_TYPE[node.type] || node.type diff --git a/lib/rubocop/cop/layout/else_alignment.rb b/lib/rubocop/cop/layout/else_alignment.rb index 7f06be52264a..22d2b2ea96f6 100644 --- a/lib/rubocop/cop/layout/else_alignment.rb +++ b/lib/rubocop/cop/layout/else_alignment.rb @@ -120,6 +120,7 @@ def check_alignment(base_range, else_range) @column_delta = effective_column(base_range) - else_range.column return if @column_delta.zero? + message = format( MSG, else_range: else_range.source, diff --git a/lib/rubocop/cop/layout/empty_line_between_defs.rb b/lib/rubocop/cop/layout/empty_line_between_defs.rb index 6c7236f6342b..6f5b8b31c566 100644 --- a/lib/rubocop/cop/layout/empty_line_between_defs.rb +++ b/lib/rubocop/cop/layout/empty_line_between_defs.rb @@ -80,6 +80,7 @@ def autocorrect(node) def def_node?(node) return unless node + node.def_type? || node.defs_type? end @@ -88,6 +89,7 @@ def multiple_blank_lines_groups?(first_def_node, second_def_node) blank_start = lines.each_index.select { |i| lines[i].blank? }.max non_blank_end = lines.each_index.reject { |i| lines[i].blank? }.min return false if blank_start.nil? || non_blank_end.nil? + blank_start > non_blank_end end diff --git a/lib/rubocop/cop/layout/empty_lines_around_arguments.rb b/lib/rubocop/cop/layout/empty_lines_around_arguments.rb index affa02f811f1..26d2d0c62289 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_arguments.rb @@ -45,6 +45,7 @@ class EmptyLinesAroundArguments < Cop def on_send(node) return if node.single_line? || node.arguments.empty? + extra_lines(node) { |range| add_offense(node, location: range) } end diff --git a/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb b/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb index 5fbbb72aab6b..f7b72aba4fe8 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb @@ -104,6 +104,7 @@ def style def keyword_locations(node) return [] unless node + case node.type when :rescue keyword_locations_in_rescue(node) diff --git a/lib/rubocop/cop/layout/end_of_line.rb b/lib/rubocop/cop/layout/end_of_line.rb index cea63bee8fc1..1701ad7ef7a0 100644 --- a/lib/rubocop/cop/layout/end_of_line.rb +++ b/lib/rubocop/cop/layout/end_of_line.rb @@ -51,6 +51,7 @@ def investigate(processed_source) processed_source.raw_source.each_line.with_index do |line, index| break if index >= last_line + msg = offense_message(line) next unless msg next if unimportant_missing_cr?(index, last_line, line) diff --git a/lib/rubocop/cop/layout/extra_spacing.rb b/lib/rubocop/cop/layout/extra_spacing.rb index 3889bcc0dde4..d370c865fd09 100644 --- a/lib/rubocop/cop/layout/extra_spacing.rb +++ b/lib/rubocop/cop/layout/extra_spacing.rb @@ -89,6 +89,7 @@ def check_assignment(token) message = format(MSG_UNALIGNED_ASGN, location: 'following') end return if aligned_assignment?(token.pos, assignment_line) + add_offense(token.pos, location: token.pos, message: message) end diff --git a/lib/rubocop/cop/layout/indent_array.rb b/lib/rubocop/cop/layout/indent_array.rb index 7150931d54da..e39096d13ab4 100644 --- a/lib/rubocop/cop/layout/indent_array.rb +++ b/lib/rubocop/cop/layout/indent_array.rb @@ -114,6 +114,7 @@ def check(array_node, left_parenthesis) first_elem = array_node.values.first if first_elem return if first_elem.source_range.line == left_bracket.line + check_first(first_elem, left_bracket, left_parenthesis, 0) end diff --git a/lib/rubocop/cop/layout/indent_heredoc.rb b/lib/rubocop/cop/layout/indent_heredoc.rb index 22166907136b..cb9a93cf4fb1 100644 --- a/lib/rubocop/cop/layout/indent_heredoc.rb +++ b/lib/rubocop/cop/layout/indent_heredoc.rb @@ -100,6 +100,7 @@ def on_heredoc(node) end return if too_long_line?(node) + add_offense(node, location: :heredoc_body) end @@ -174,6 +175,7 @@ def ruby23_width_message(indentation_width) def too_long_line?(node) return false if config.for_cop('Metrics/LineLength')['AllowHeredoc'] + body = heredoc_body(node) expected_indent = base_indent_level(node) + indentation_width @@ -191,6 +193,7 @@ def max_line_length def correct_by_squiggly(node) return if target_ruby_version < 2.3 + lambda do |corrector| if heredoc_indent_type(node) == '~' adjust_squiggly(corrector, node) diff --git a/lib/rubocop/cop/layout/indentation_width.rb b/lib/rubocop/cop/layout/indentation_width.rb index c2a4611839e4..fee5906bc5bf 100644 --- a/lib/rubocop/cop/layout/indentation_width.rb +++ b/lib/rubocop/cop/layout/indentation_width.rb @@ -70,6 +70,7 @@ def on_kwbegin(node) # Check indentation against end keyword but only if it's first on its # line. return unless begins_its_line?(node.loc.end) + check_indentation(node.loc.end, node.children.first) end @@ -165,6 +166,7 @@ def check_members(base, members) else members.first.children.each do |member| next if member.send_type? && member.access_modifier? + check_indentation(base, member) end end diff --git a/lib/rubocop/cop/layout/multiline_method_call_indentation.rb b/lib/rubocop/cop/layout/multiline_method_call_indentation.rb index c74db957ce63..4127712c2033 100644 --- a/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +++ b/lib/rubocop/cop/layout/multiline_method_call_indentation.rb @@ -196,6 +196,7 @@ def semantic_alignment_node(node) node = node.parent until node.loc.dot return if node.loc.dot.line != node.first_line + node end diff --git a/lib/rubocop/cop/layout/multiline_operation_indentation.rb b/lib/rubocop/cop/layout/multiline_operation_indentation.rb index 5955ac8d105b..d5cd0e7fd5ff 100644 --- a/lib/rubocop/cop/layout/multiline_operation_indentation.rb +++ b/lib/rubocop/cop/layout/multiline_operation_indentation.rb @@ -62,6 +62,7 @@ def autocorrect(node) def relevant_node?(node) return false if node.unary_operation? + !node.loc.dot # Don't check method calls with dot operator. end diff --git a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb index a21979beb39d..73d0960e9ee7 100644 --- a/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +++ b/lib/rubocop/cop/layout/rescue_ensure_alignment.rb @@ -40,6 +40,7 @@ def investigate(processed_source) @modifier_locations = processed_source.tokens.each_with_object([]) do |token, locations| next unless token.rescue_modifier? + locations << token.pos end end @@ -77,6 +78,7 @@ def format_message(kw_loc, end_loc) def modifier?(node) return false unless @modifier_locations.respond_to?(:include?) + @modifier_locations.include?(node.loc.keyword) end diff --git a/lib/rubocop/cop/layout/space_after_method_name.rb b/lib/rubocop/cop/layout/space_after_method_name.rb index 7cb7bf135490..d1205e36f18a 100644 --- a/lib/rubocop/cop/layout/space_after_method_name.rb +++ b/lib/rubocop/cop/layout/space_after_method_name.rb @@ -23,6 +23,7 @@ class SpaceAfterMethodName < Cop def on_def(node) args = node.arguments return unless args.loc.begin && args.loc.begin.is?('(') + expr = args.source_range pos_before_left_paren = range_between(expr.begin_pos - 1, expr.begin_pos) diff --git a/lib/rubocop/cop/layout/space_around_keyword.rb b/lib/rubocop/cop/layout/space_around_keyword.rb index 12fe74ba83e5..d17c5110cc37 100644 --- a/lib/rubocop/cop/layout/space_around_keyword.rb +++ b/lib/rubocop/cop/layout/space_around_keyword.rb @@ -140,6 +140,7 @@ def autocorrect(range) def check(node, locations, begin_keyword = DO) locations.each do |loc| next unless node.loc.respond_to?(loc) + range = node.loc.public_send(loc) next unless range @@ -182,6 +183,7 @@ def offense(range, msg) def space_before_missing?(range) pos = range.begin_pos - 1 return false if pos < 0 + range.source_buffer.source[pos] !~ /[\s\(\|\{\[;,\*\=]/ end diff --git a/lib/rubocop/cop/layout/space_around_operators.rb b/lib/rubocop/cop/layout/space_around_operators.rb index 1715a1f3c520..de7cbc5d6bfd 100644 --- a/lib/rubocop/cop/layout/space_around_operators.rb +++ b/lib/rubocop/cop/layout/space_around_operators.rb @@ -71,6 +71,7 @@ def on_special_asgn(node) _, _, right, = *node return unless right + check_operator(node.loc.operator, right.source_range) end diff --git a/lib/rubocop/cop/layout/space_before_comment.rb b/lib/rubocop/cop/layout/space_before_comment.rb index b3062ffc402a..0d4e639eaa0f 100644 --- a/lib/rubocop/cop/layout/space_before_comment.rb +++ b/lib/rubocop/cop/layout/space_before_comment.rb @@ -19,6 +19,7 @@ def investigate(processed_source) processed_source.tokens.each_cons(2) do |token1, token2| next unless token2.comment? next unless token1.line == token2.line + if token1.pos.end == token2.pos.begin add_offense(token2.pos, location: token2.pos) end diff --git a/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb b/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb index 8283c4944980..e62ca36e2c43 100644 --- a/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +++ b/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb @@ -71,6 +71,7 @@ class SpaceInsideArrayLiteralBrackets < Cop def on_array(node) return unless node.square_brackets? + left, right = array_brackets(node) if empty_brackets?(left, right) return empty_offenses(node, left, right, EMPTY_MSG) @@ -125,6 +126,7 @@ def next_to_newline?(node, token) def end_has_own_line?(token) line, col = line_and_column_for(token) return true if col == -1 + processed_source.lines[line][0..col].delete(' ').empty? end diff --git a/lib/rubocop/cop/layout/space_inside_reference_brackets.rb b/lib/rubocop/cop/layout/space_inside_reference_brackets.rb index cc9546ca577c..5725b56af650 100644 --- a/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +++ b/lib/rubocop/cop/layout/space_inside_reference_brackets.rb @@ -65,9 +65,11 @@ class SpaceInsideReferenceBrackets < Cop def on_send(node) return if node.multiline? return unless bracket_method?(node) + tokens = tokens(node) left_token = left_ref_bracket(node, tokens) return unless left_token + right_token = closing_bracket(tokens, left_token) if empty_brackets?(left_token, right_token) diff --git a/lib/rubocop/cop/layout/tab.rb b/lib/rubocop/cop/layout/tab.rb index 957992d2f677..531f88e4c369 100644 --- a/lib/rubocop/cop/layout/tab.rb +++ b/lib/rubocop/cop/layout/tab.rb @@ -32,6 +32,7 @@ def investigate(processed_source) processed_source.lines.each.with_index(1) do |line, lineno| match = line.match(/^([^\t]*)\t+/) next unless match + prefix = match.captures[0] col = prefix.length next if in_string_literal?(str_ranges, lineno, col) diff --git a/lib/rubocop/cop/layout/trailing_whitespace.rb b/lib/rubocop/cop/layout/trailing_whitespace.rb index dbbb9b3cb6c4..f6bdc6303e1d 100644 --- a/lib/rubocop/cop/layout/trailing_whitespace.rb +++ b/lib/rubocop/cop/layout/trailing_whitespace.rb @@ -49,6 +49,7 @@ def inside_heredoc?(heredoc_ranges, line_number) def extract_heredoc_ranges(ast) return [] unless ast + ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node| body = node.location.heredoc_body (body.first_line...body.last_line) diff --git a/lib/rubocop/cop/lint/deprecated_class_methods.rb b/lib/rubocop/cop/lint/deprecated_class_methods.rb index 4ff740f1a443..aafb12611722 100644 --- a/lib/rubocop/cop/lint/deprecated_class_methods.rb +++ b/lib/rubocop/cop/lint/deprecated_class_methods.rb @@ -69,6 +69,7 @@ def check(node) DEPRECATED_METHODS_OBJECT.each do |data| next unless data.class_nodes.include?(node.receiver) next unless node.method?(data.deprecated_method) + yield data end end diff --git a/lib/rubocop/cop/lint/duplicate_methods.rb b/lib/rubocop/cop/lint/duplicate_methods.rb index 1d7039bda183..51c2c89e971a 100644 --- a/lib/rubocop/cop/lint/duplicate_methods.rb +++ b/lib/rubocop/cop/lint/duplicate_methods.rb @@ -138,6 +138,7 @@ def message_for_dup(node, method_name) def found_instance_method(node, name) return unless (scope = node.parent_module_name) + if scope =~ /\A#\Z/ found_method(node, "#{Regexp.last_match(1)}.#{name}") else @@ -179,6 +180,7 @@ def found_attr(node, args, readable: false, writable: false) args.each do |arg| name = sym_name(arg) next unless name + found_instance_method(node, name) if readable found_instance_method(node, "#{name}=") if writable end @@ -198,6 +200,7 @@ def lookup_constant(node, const_name) end break if namespace.nil? + namespace, mod_name = *namespace end end diff --git a/lib/rubocop/cop/lint/format_parameter_mismatch.rb b/lib/rubocop/cop/lint/format_parameter_mismatch.rb index f3066508d203..85ef20ff23c3 100644 --- a/lib/rubocop/cop/lint/format_parameter_mismatch.rb +++ b/lib/rubocop/cop/lint/format_parameter_mismatch.rb @@ -48,6 +48,7 @@ def offending_node?(node) num_of_format_args, num_of_expected_fields = count_matches(node) return false if num_of_format_args == :unknown + matched_arguments_count?(num_of_expected_fields, num_of_format_args) end diff --git a/lib/rubocop/cop/lint/ineffective_access_modifier.rb b/lib/rubocop/cop/lint/ineffective_access_modifier.rb index 2073a7ad6aac..5541b44d69d8 100644 --- a/lib/rubocop/cop/lint/ineffective_access_modifier.rb +++ b/lib/rubocop/cop/lint/ineffective_access_modifier.rb @@ -104,6 +104,7 @@ def ineffective_modifier(node, ignored_methods, modifier = nil, &block) modifier = child if access_modifier?(child) when :defs next if correct_visibility?(child, modifier, ignored_methods) + yield child, modifier when :kwbegin ineffective_modifier(child, ignored_methods, modifier, &block) diff --git a/lib/rubocop/cop/lint/interpolation_check.rb b/lib/rubocop/cop/lint/interpolation_check.rb index ab7ef6a297a1..5fae13c5370f 100644 --- a/lib/rubocop/cop/lint/interpolation_check.rb +++ b/lib/rubocop/cop/lint/interpolation_check.rb @@ -22,9 +22,11 @@ class InterpolationCheck < Cop def on_str(node) return if heredoc?(node) + parent = node.parent return if parent && (parent.dstr_type? || parent.regexp_type?) return unless node.source.scrub =~ /(? 1 end diff --git a/lib/rubocop/cop/mixin/uncommunicative_name.rb b/lib/rubocop/cop/mixin/uncommunicative_name.rb index f403c073a528..927b9d6973eb 100644 --- a/lib/rubocop/cop/mixin/uncommunicative_name.rb +++ b/lib/rubocop/cop/mixin/uncommunicative_name.rb @@ -16,6 +16,7 @@ def check(node, args) name = arg.children.first.to_s next if (arg.restarg_type? || arg.kwrestarg_type?) && name.empty? next if allowed_names.include?(name) + range = arg_range(arg, name.size) issue_offenses(node, range, name) end @@ -28,6 +29,7 @@ def issue_offenses(node, range, name) case_offense(node, range) if uppercase?(name) length_offense(node, range) unless long_enough?(name) return if allow_nums + num_offense(node, range) if ends_with_num?(name) end diff --git a/lib/rubocop/cop/naming/ascii_identifiers.rb b/lib/rubocop/cop/naming/ascii_identifiers.rb index 299612de0393..8bad6572bc71 100644 --- a/lib/rubocop/cop/naming/ascii_identifiers.rb +++ b/lib/rubocop/cop/naming/ascii_identifiers.rb @@ -44,6 +44,7 @@ class AsciiIdentifiers < Cop def investigate(processed_source) processed_source.each_token do |token| next unless token.type == :tIDENTIFIER && !token.text.ascii_only? + add_offense(token, location: first_offense_range(token)) end end diff --git a/lib/rubocop/cop/naming/binary_operator_parameter_name.rb b/lib/rubocop/cop/naming/binary_operator_parameter_name.rb index 6caf5cfa75ef..3a1e6a66b4b8 100644 --- a/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +++ b/lib/rubocop/cop/naming/binary_operator_parameter_name.rb @@ -34,6 +34,7 @@ def on_def(node) def op_method?(name) return false if BLACKLISTED.include?(name) + name !~ /\A\w/ || OP_LIKE_METHODS.include?(name) end end diff --git a/lib/rubocop/cop/naming/file_name.rb b/lib/rubocop/cop/naming/file_name.rb index 80359eacdee7..6b950aaed529 100644 --- a/lib/rubocop/cop/naming/file_name.rb +++ b/lib/rubocop/cop/naming/file_name.rb @@ -53,6 +53,7 @@ def for_bad_filename(file_path) else return if ignore_executable_scripts? && processed_source.start_with?('#!') + other_message(basename) end @@ -122,6 +123,7 @@ def match_namespace(node, namespace, expected) node.each_ancestor(:class, :module, :sclass, :casgn) do |ancestor| return false if ancestor.sclass_type? + match_partial.call(ancestor.defined_module) end diff --git a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb index 619e80b40b46..5f87754c3b97 100644 --- a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +++ b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb @@ -127,6 +127,7 @@ def style_parameter_name def matches?(method_name, ivar_assign) return true if ivar_assign.nil? || method_name == :initialize + method_name = method_name.to_s.delete('!?') variable = ivar_assign.children.first variable_name = variable.to_s.sub('@', '') diff --git a/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb b/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb index 3d208f471cba..d99c8e050408 100644 --- a/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +++ b/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb @@ -40,6 +40,7 @@ class UncommunicativeBlockParamName < Cop def on_block(node) return unless node.arguments? + check(node, node.arguments) end end diff --git a/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb b/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb index 887cacc9ab12..3e5b77006f15 100644 --- a/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +++ b/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb @@ -48,6 +48,7 @@ class UncommunicativeMethodParamName < Cop def on_def(node) return unless node.arguments? + check(node, node.arguments) end alias on_defs on_def diff --git a/lib/rubocop/cop/naming/variable_name.rb b/lib/rubocop/cop/naming/variable_name.rb index 39b33f6036f3..b99a12eff9c9 100644 --- a/lib/rubocop/cop/naming/variable_name.rb +++ b/lib/rubocop/cop/naming/variable_name.rb @@ -27,6 +27,7 @@ class VariableName < Cop def on_lvasgn(node) name, = *node return unless name + check_name(node, name, node.loc.name) end alias on_ivasgn on_lvasgn diff --git a/lib/rubocop/cop/performance/compare_with_block.rb b/lib/rubocop/cop/performance/compare_with_block.rb index 0adc4318f987..a9b80a75a104 100644 --- a/lib/rubocop/cop/performance/compare_with_block.rb +++ b/lib/rubocop/cop/performance/compare_with_block.rb @@ -48,6 +48,7 @@ def on_block(node) compare?(node) do |send, var_a, var_b, body| replaceable_body?(body, var_a, var_b) do |method, args_a, args_b| return unless slow_compare?(method, args_a, args_b) + range = compare_range(send, node) add_offense( @@ -78,8 +79,10 @@ def autocorrect(node) def slow_compare?(method, args_a, args_b) return false unless args_a == args_b + if method == :[] return false unless args_a.size == 1 + key = args_a.first return false unless %i[sym str int].include?(key.type) else diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index 5d73f564afc2..8ed66af1a55e 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -101,6 +101,7 @@ class RegexpMatch < Cop def match_with_lvasgn?(node) return false unless node.match_with_lvasgn_type? + regexp, _rhs = *node regexp.to_regexp.named_captures.empty? end diff --git a/lib/rubocop/cop/performance/sample.rb b/lib/rubocop/cop/performance/sample.rb index eaf4f488ac1f..5e2ef75d25da 100644 --- a/lib/rubocop/cop/performance/sample.rb +++ b/lib/rubocop/cop/performance/sample.rb @@ -88,12 +88,14 @@ def sample_size_for_one_arg(arg) def sample_size_for_two_args(first, second) return :unknown unless first.int_type? && first.to_a.first.zero? + second.int_type? ? second.to_a.first : :unknown end def range_size(range_node) vals = range_node.to_a return :unknown unless vals.all?(&:int_type?) + low, high = vals.map { |val| val.children[0] } return :unknown unless low.zero? && high >= 0 diff --git a/lib/rubocop/cop/performance/string_replacement.rb b/lib/rubocop/cop/performance/string_replacement.rb index 2fff46c88081..49d4fe0f2a63 100644 --- a/lib/rubocop/cop/performance/string_replacement.rb +++ b/lib/rubocop/cop/performance/string_replacement.rb @@ -87,6 +87,7 @@ def accept_first_param?(first_param) unless first_param.str_type? return true if options return true unless first_source =~ DETERMINISTIC_REGEX + # This must be done after checking DETERMINISTIC_REGEX # Otherwise things like \s will trip us up first_source = interpret_string_escapes(first_source) diff --git a/lib/rubocop/cop/rails/active_support_aliases.rb b/lib/rubocop/cop/rails/active_support_aliases.rb index 3a37f0557ed3..a6c8b464af04 100644 --- a/lib/rubocop/cop/rails/active_support_aliases.rb +++ b/lib/rubocop/cop/rails/active_support_aliases.rb @@ -46,6 +46,7 @@ def on_send(node) def autocorrect(node) return false if append(node) + lambda do |corrector| method_name = node.loc.selector.source replacement = ALIASES[method_name.to_sym][:original] diff --git a/lib/rubocop/cop/rails/bulk_change_table.rb b/lib/rubocop/cop/rails/bulk_change_table.rb index df6ec749fe73..86950e3b2f79 100644 --- a/lib/rubocop/cop/rails/bulk_change_table.rb +++ b/lib/rubocop/cop/rails/bulk_change_table.rb @@ -155,6 +155,7 @@ def on_send(node) return unless node.command?(:change_table) return if include_bulk_options?(node) return unless node.block_node + send_nodes = node.block_node.body.each_child_node(:send).to_a transformations = send_nodes.select do |send_node| @@ -171,6 +172,7 @@ def include_bulk_options?(node) # arguments: [{(sym :table)(str "table")} (hash (pair (sym :bulk) _))] options = node.arguments[1] return false unless options + options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk } end @@ -181,6 +183,7 @@ def database def database_from_yaml return nil unless database_yaml + case database_yaml['adapter'] when 'mysql2' MYSQL @@ -191,10 +194,13 @@ def database_from_yaml def database_yaml return nil unless File.exist?('config/database.yml') + yaml = YAML.load_file('config/database.yml') return nil unless yaml.is_a? Hash + config = yaml['development'] return nil unless config.is_a?(Hash) + config rescue Psych::SyntaxError nil @@ -236,6 +242,7 @@ def add_offense_for_alter_methods(node) # arguments: [{(sym :table)(str "table")} ...] table_node = node.arguments[0] return unless table_node.is_a? RuboCop::AST::BasicLiteralNode + message = format(MSG_FOR_ALTER_METHODS, table: table_node.value) add_offense(node, message: message) end diff --git a/lib/rubocop/cop/rails/create_table_with_timestamps.rb b/lib/rubocop/cop/rails/create_table_with_timestamps.rb index 899ba298ad07..497510e0b82d 100644 --- a/lib/rubocop/cop/rails/create_table_with_timestamps.rb +++ b/lib/rubocop/cop/rails/create_table_with_timestamps.rb @@ -66,6 +66,7 @@ class CreateTableWithTimestamps < Cop def on_send(node) return unless node.command?(:create_table) + parent = node.parent if create_table_with_block?(parent) diff --git a/lib/rubocop/cop/rails/dynamic_find_by.rb b/lib/rubocop/cop/rails/dynamic_find_by.rb index 9feda5e44db3..97153412ba6f 100644 --- a/lib/rubocop/cop/rails/dynamic_find_by.rb +++ b/lib/rubocop/cop/rails/dynamic_find_by.rb @@ -74,6 +74,7 @@ def column_keywords(method) def static_method_name(method_name) match = METHOD_PATTERN.match(method_name) return nil unless match + match[2] ? 'find_by!' : 'find_by' end end diff --git a/lib/rubocop/cop/rails/http_status.rb b/lib/rubocop/cop/rails/http_status.rb index 136629b96c80..bfece8bb1b31 100644 --- a/lib/rubocop/cop/rails/http_status.rb +++ b/lib/rubocop/cop/rails/http_status.rb @@ -56,8 +56,10 @@ def on_send(node) http_status(node) do |hash_node| status = status_code(hash_node) return unless status + checker = checker_class.new(status) return unless checker.offensive? + add_offense(checker.node, message: checker.message) end end diff --git a/lib/rubocop/cop/rails/inverse_of.rb b/lib/rubocop/cop/rails/inverse_of.rb index a5db9362ec7e..00b6dd9b1226 100644 --- a/lib/rubocop/cop/rails/inverse_of.rb +++ b/lib/rubocop/cop/rails/inverse_of.rb @@ -178,6 +178,7 @@ class InverseOf < Cop def on_send(node) recv, arguments = association_recv_arguments(node) return unless arguments + with_options = with_options_arguments(recv, node) options = arguments.concat(with_options).flat_map do |arg| @@ -189,6 +190,7 @@ def on_send(node) options_requiring_inverse_of?(options) return if options_contain_inverse_of?(options) + add_offense(node, message: message(options), location: :selector) end @@ -203,6 +205,7 @@ def options_requiring_inverse_of?(options) end return required if target_rails_version >= 5.2 + required || options.any? { |opt| as_option?(opt) } end @@ -226,6 +229,7 @@ def with_options_arguments(recv, node) def same_context_in_with_options?(arg, recv) return true if arg.nil? && recv.nil? + arg && recv && arg.children[0] == recv.children[0] end diff --git a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb b/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb index d20b58f56f29..a0347ca24057 100644 --- a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +++ b/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb @@ -92,6 +92,7 @@ def on_send(node) parent = node.each_ancestor(:class, :module).first return unless parent + block = parent.each_child_node(:begin).first return unless block diff --git a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb index 14d74f8bb1f9..ef24d66a1f0c 100644 --- a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +++ b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb @@ -82,6 +82,7 @@ class RedundantReceiverInWithOptions < Cop def on_block(node) with_options?(node) do |arg, body| return unless all_block_nodes_in(body).count.zero? + send_nodes = all_send_nodes_in(body) if send_nodes.all? { |n| same_value?(arg, n.receiver) } diff --git a/lib/rubocop/cop/rails/reversible_migration.rb b/lib/rubocop/cop/rails/reversible_migration.rb index 8e75eb4df3bf..63b2f029afc5 100644 --- a/lib/rubocop/cop/rails/reversible_migration.rb +++ b/lib/rubocop/cop/rails/reversible_migration.rb @@ -249,6 +249,7 @@ def check_change_table_offense(receiver, node) method_name = node.method_name return if receiver != node.receiver && !IRREVERSIBLE_CHANGE_TABLE_CALLS.include?(method_name) + add_offense( node, message: format(MSG, action: "change_table(with #{method_name})") diff --git a/lib/rubocop/cop/rails/save_bang.rb b/lib/rubocop/cop/rails/save_bang.rb index dcdc8127229a..507a864fde15 100644 --- a/lib/rubocop/cop/rails/save_bang.rb +++ b/lib/rubocop/cop/rails/save_bang.rb @@ -172,14 +172,17 @@ def assignable_node(node) def hash_parent(node) pair = node.parent return unless pair && pair.pair_type? + hash = pair.parent return unless hash && hash.hash_type? + hash end def array_parent(node) array = node.parent return unless array && array.array_type? + array end @@ -198,6 +201,7 @@ def conditional?(node) # rubocop:disable Metrics/CyclomaticComplexity condition = node.parent return false unless condition + condition.if_type? || condition.case_type? || condition.or_type? || condition.and_type? || single_negative?(condition) @@ -205,9 +209,11 @@ def conditional?(node) # rubocop:disable Metrics/CyclomaticComplexity def implicit_return?(node) return false unless cop_config['AllowImplicitReturn'] + node = assignable_node(node) method = node.parent return unless method && (method.def_type? || method.block_type?) + method.children.size == node.sibling_index + 1 end diff --git a/lib/rubocop/cop/rails/time_zone.rb b/lib/rubocop/cop/rails/time_zone.rb index c51269e779ed..5c1d87d05b72 100644 --- a/lib/rubocop/cop/rails/time_zone.rb +++ b/lib/rubocop/cop/rails/time_zone.rb @@ -165,6 +165,7 @@ def check_localtime(node) while node && node.send_type? break if extract_method(node) == :localtime + node = node.parent end diff --git a/lib/rubocop/cop/security/eval.rb b/lib/rubocop/cop/security/eval.rb index 6868f591723e..0bb3b1deefb6 100644 --- a/lib/rubocop/cop/security/eval.rb +++ b/lib/rubocop/cop/security/eval.rb @@ -21,6 +21,7 @@ class Eval < Cop def on_send(node) eval?(node) do |code| return if code.dstr_type? && code.recursive_literal? + add_offense(node, location: :selector) end end diff --git a/lib/rubocop/cop/security/open.rb b/lib/rubocop/cop/security/open.rb index 055f2cdacc86..64ed6bd6cf7f 100644 --- a/lib/rubocop/cop/security/open.rb +++ b/lib/rubocop/cop/security/open.rb @@ -41,6 +41,7 @@ def safe?(node) def on_send(node) open?(node) do |code| return if safe?(code) + add_offense(node, location: :selector) end end diff --git a/lib/rubocop/cop/severity.rb b/lib/rubocop/cop/severity.rb index 7dbac4ba0c39..2e17351504b3 100644 --- a/lib/rubocop/cop/severity.rb +++ b/lib/rubocop/cop/severity.rb @@ -34,6 +34,7 @@ def initialize(name_or_code) unless NAMES.include?(name) raise ArgumentError, "Unknown severity: #{name}" end + @name = name.freeze freeze end diff --git a/lib/rubocop/cop/style/ascii_comments.rb b/lib/rubocop/cop/style/ascii_comments.rb index 42dc4349b7e3..f18c3cf6dcfd 100644 --- a/lib/rubocop/cop/style/ascii_comments.rb +++ b/lib/rubocop/cop/style/ascii_comments.rb @@ -24,6 +24,7 @@ def investigate(processed_source) processed_source.each_comment do |comment| next if comment.text.ascii_only? next if only_allowed_non_ascii_chars?(comment.text) + add_offense(comment, location: first_offense_range(comment)) end end diff --git a/lib/rubocop/cop/style/block_delimiters.rb b/lib/rubocop/cop/style/block_delimiters.rb index 7eb6f2119025..c530e08971b9 100644 --- a/lib/rubocop/cop/style/block_delimiters.rb +++ b/lib/rubocop/cop/style/block_delimiters.rb @@ -190,6 +190,7 @@ def get_blocks(node, &block) # In that case, one of the K/V pairs could contain a block node # which could change in meaning if do...end replaced {...} return if node.braces? + node.each_child_node { |child| get_blocks(child, &block) } when :pair node.each_child_node { |child| get_blocks(child, &block) } diff --git a/lib/rubocop/cop/style/class_and_module_children.rb b/lib/rubocop/cop/style/class_and_module_children.rb index b72072d012a1..17ac9f081b32 100644 --- a/lib/rubocop/cop/style/class_and_module_children.rb +++ b/lib/rubocop/cop/style/class_and_module_children.rb @@ -33,6 +33,7 @@ class ClassAndModuleChildren < Cop def on_class(node) _name, superclass, body = *node return if superclass && style != :nested + check_style(node, body) end @@ -129,11 +130,13 @@ def check_style(node, body) def check_nested_style(node) return unless compact_node_name?(node) + add_offense(node, location: :name, message: NESTED_MSG) end def check_compact_style(node, body) return unless one_child?(body) && !compact_node_name?(node) + add_offense(node, location: :name, message: COMPACT_MSG) end diff --git a/lib/rubocop/cop/style/colon_method_definition.rb b/lib/rubocop/cop/style/colon_method_definition.rb index 6ee8bb8e36a9..5bcc046a69ad 100644 --- a/lib/rubocop/cop/style/colon_method_definition.rb +++ b/lib/rubocop/cop/style/colon_method_definition.rb @@ -24,6 +24,7 @@ class ColonMethodDefinition < Cop def on_defs(node) return unless node.loc.operator.source == '::' + add_offense(node, location: :operator) end diff --git a/lib/rubocop/cop/style/commented_keyword.rb b/lib/rubocop/cop/style/commented_keyword.rb index 1f8b939bb3be..5bcc0af1cb63 100644 --- a/lib/rubocop/cop/style/commented_keyword.rb +++ b/lib/rubocop/cop/style/commented_keyword.rb @@ -47,6 +47,7 @@ def investigate(processed_source) line = processed_source.lines[line_position - 1] next if heredoc_lines.any? { |r| r.include?(line_position) } next unless offensive?(line) + range = source_range(processed_source.buffer, line_position, (location.column)...(location.last_column)) @@ -74,6 +75,7 @@ def message(node) def extract_heredoc_lines(ast) return [] unless ast + ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node| body = node.location.heredoc_body (body.first_line...body.last_line) diff --git a/lib/rubocop/cop/style/conditional_assignment.rb b/lib/rubocop/cop/style/conditional_assignment.rb index ee2d061600be..d7b1f8973905 100644 --- a/lib/rubocop/cop/style/conditional_assignment.rb +++ b/lib/rubocop/cop/style/conditional_assignment.rb @@ -69,6 +69,7 @@ def end_with_eq?(sym) def expand_elsif(node, elsif_branches = []) return [] if node.nil? || !node.if_type? + _condition, elsif_branch, else_branch = *node elsif_branches << elsif_branch if else_branch && else_branch.if_type? @@ -100,6 +101,7 @@ def setter_method?(method_name) def assignment_rhs_exist?(node) parent = node.parent return true unless parent + !(parent.mlhs_type? || parent.resbody_type?) end end diff --git a/lib/rubocop/cop/style/copyright.rb b/lib/rubocop/cop/style/copyright.rb index ed562d0081a5..db3a3d60a207 100644 --- a/lib/rubocop/cop/style/copyright.rb +++ b/lib/rubocop/cop/style/copyright.rb @@ -26,6 +26,7 @@ class Copyright < Cop def investigate(processed_source) return if notice.empty? return if notice_found?(processed_source) + range = source_range(processed_source.buffer, 1, 0) add_offense(insert_notice_before(processed_source), location: range, message: format(MSG, notice: notice)) @@ -33,6 +34,7 @@ def investigate(processed_source) def autocorrect(token) raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty? + regex = Regexp.new(notice) unless autocorrect_notice =~ regex raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \ @@ -64,12 +66,14 @@ def insert_notice_before(processed_source) def shebang_token?(processed_source, token_index) return false if token_index >= processed_source.tokens.size + token = processed_source.tokens[token_index] token.comment? && token.text =~ /^#!.*$/ end def encoding_token?(processed_source, token_index) return false if token_index >= processed_source.tokens.size + token = processed_source.tokens[token_index] token.comment? && token.text =~ /^#.*coding\s?[:=]\s?(?:UTF|utf)-8/ end @@ -79,6 +83,7 @@ def notice_found?(processed_source) notice_regexp = Regexp.new(notice) processed_source.each_token do |token| break unless token.comment? + notice_found = !(token.text =~ notice_regexp).nil? break if notice_found end diff --git a/lib/rubocop/cop/style/empty_case_condition.rb b/lib/rubocop/cop/style/empty_case_condition.rb index c3bfa62435c9..54207ffbb26e 100644 --- a/lib/rubocop/cop/style/empty_case_condition.rb +++ b/lib/rubocop/cop/style/empty_case_condition.rb @@ -46,6 +46,7 @@ def on_case(case_node) return if case_node.when_branches.any? do |when_branch| when_branch.each_descendant.any?(&:return_type?) end + if (else_branch = case_node.else_branch) return if else_branch.return_type? || else_branch.each_descendant.any?(&:return_type?) diff --git a/lib/rubocop/cop/style/empty_else.rb b/lib/rubocop/cop/style/empty_else.rb index 3e10971ea2c6..18c95774af30 100644 --- a/lib/rubocop/cop/style/empty_else.rb +++ b/lib/rubocop/cop/style/empty_else.rb @@ -148,12 +148,14 @@ def comment_in_else?(node) def else_line_range(loc) return 0..0 if loc.else.nil? || loc.end.nil? + loc.else.first_line..loc.end.first_line end def base_node(node) return node if node.case_type? return node unless node.elsif? + node.each_ancestor(:if, :case, :when).find(-> { node }) do |parent| parent.loc.end end diff --git a/lib/rubocop/cop/style/empty_lambda_parameter.rb b/lib/rubocop/cop/style/empty_lambda_parameter.rb index 1bb95272ffe1..45b15364ecff 100644 --- a/lib/rubocop/cop/style/empty_lambda_parameter.rb +++ b/lib/rubocop/cop/style/empty_lambda_parameter.rb @@ -25,6 +25,7 @@ class EmptyLambdaParameter < Cop def on_block(node) send_node = node.send_node return unless send_node.send_type? + check(node) if node.send_node.stabby_lambda? end diff --git a/lib/rubocop/cop/style/eval_with_location.rb b/lib/rubocop/cop/style/eval_with_location.rb index 87b65e51f517..a05955baaf66 100644 --- a/lib/rubocop/cop/style/eval_with_location.rb +++ b/lib/rubocop/cop/style/eval_with_location.rb @@ -124,6 +124,7 @@ def string_first_line(str_node) def add_offense_for_same_line(node, line_node) return if special_line_keyword?(line_node) + add_offense( node, location: line_node.loc.expression, @@ -134,6 +135,7 @@ def add_offense_for_same_line(node, line_node) def add_offense_for_different_line(node, line_node, line_diff) sign = line_diff > 0 ? :+ : :- return if line_with_offset?(line_node, sign, line_diff.abs) + add_offense( node, location: line_node.loc.expression, diff --git a/lib/rubocop/cop/style/if_with_semicolon.rb b/lib/rubocop/cop/style/if_with_semicolon.rb index 04b06b39bc14..324f1d9cd68a 100644 --- a/lib/rubocop/cop/style/if_with_semicolon.rb +++ b/lib/rubocop/cop/style/if_with_semicolon.rb @@ -21,6 +21,7 @@ class IfWithSemicolon < Cop def on_normal_if_unless(node) beginning = node.loc.begin return unless beginning && beginning.is?(';') + add_offense(node) end end diff --git a/lib/rubocop/cop/style/inverse_methods.rb b/lib/rubocop/cop/style/inverse_methods.rb index 7709cd126a01..b642e10123a6 100644 --- a/lib/rubocop/cop/style/inverse_methods.rb +++ b/lib/rubocop/cop/style/inverse_methods.rb @@ -55,6 +55,7 @@ class InverseMethods < Cop def on_send(node) return if part_of_ignored_node?(node) + inverse_candidate?(node) do |_method_call, lhs, method, rhs| return unless inverse_methods.key?(method) return if possible_class_hierarchy_check?(lhs, rhs, method) diff --git a/lib/rubocop/cop/style/lambda.rb b/lib/rubocop/cop/style/lambda.rb index f3febf27b902..f63bf5752adc 100644 --- a/lib/rubocop/cop/style/lambda.rb +++ b/lib/rubocop/cop/style/lambda.rb @@ -129,6 +129,7 @@ def autocorrect_literal_to_method(corrector, node) corrector.replace(block_method.source_range, 'lambda') corrector.remove(args.source_range) if args.source_range return if args.children.empty? + arg_str = " |#{lambda_arg_string(args)}|" corrector.insert_after(node.loc.begin, arg_str) end diff --git a/lib/rubocop/cop/style/missing_else.rb b/lib/rubocop/cop/style/missing_else.rb index 93fbcda102a0..3109c9dd1eae 100644 --- a/lib/rubocop/cop/style/missing_else.rb +++ b/lib/rubocop/cop/style/missing_else.rb @@ -167,6 +167,7 @@ def empty_else_cop_enabled? def empty_else_style return unless empty_else_config.key?('EnforcedStyle') + empty_else_config['EnforcedStyle'].to_sym end diff --git a/lib/rubocop/cop/style/multiline_memoization.rb b/lib/rubocop/cop/style/multiline_memoization.rb index 28df5963844d..2a64a12e06b0 100644 --- a/lib/rubocop/cop/style/multiline_memoization.rb +++ b/lib/rubocop/cop/style/multiline_memoization.rb @@ -58,6 +58,7 @@ def autocorrect(node) def bad_rhs?(rhs) return false unless rhs.multiline? + if style == :keyword rhs.begin_type? else diff --git a/lib/rubocop/cop/style/multiple_comparison.rb b/lib/rubocop/cop/style/multiple_comparison.rb index 7dbfc9db2442..59f031cae361 100644 --- a/lib/rubocop/cop/style/multiple_comparison.rb +++ b/lib/rubocop/cop/style/multiple_comparison.rb @@ -37,6 +37,7 @@ def on_or(node) def nested_variable_comparison?(node) return false unless nested_comparison?(node) + variables_in_node(node).count == 1 end diff --git a/lib/rubocop/cop/style/or_assignment.rb b/lib/rubocop/cop/style/or_assignment.rb index fdc7ad3e359f..e669b67e1672 100644 --- a/lib/rubocop/cop/style/or_assignment.rb +++ b/lib/rubocop/cop/style/or_assignment.rb @@ -46,11 +46,13 @@ class OrAssignment < Cop def on_if(node) return unless unless_assignment?(node) + add_offense(node) end def on_lvasgn(node) return unless ternary_assignment?(node) + add_offense(node) end diff --git a/lib/rubocop/cop/style/random_with_offset.rb b/lib/rubocop/cop/style/random_with_offset.rb index 56f7c4c1bdfe..6c802a8138a9 100644 --- a/lib/rubocop/cop/style/random_with_offset.rb +++ b/lib/rubocop/cop/style/random_with_offset.rb @@ -59,6 +59,7 @@ def on_send(node) return unless integer_op_rand?(node) || rand_op_integer?(node) || rand_modified?(node) + add_offense(node) end diff --git a/lib/rubocop/cop/style/redundant_conditional.rb b/lib/rubocop/cop/style/redundant_conditional.rb index 7ce1cfe85d18..41011ecfdc37 100644 --- a/lib/rubocop/cop/style/redundant_conditional.rb +++ b/lib/rubocop/cop/style/redundant_conditional.rb @@ -63,6 +63,7 @@ def message(node) def offense?(node) return if node.modifier_form? + redundant_condition?(node) || redundant_condition_inverted?(node) end diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index d7a135efd12a..c41b92a680ff 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -28,6 +28,7 @@ class RedundantParentheses < Cop def on_begin(node) return if !parentheses?(node) || parens_allowed?(node) + check(node) end @@ -63,8 +64,10 @@ def allowed_method_call?(node) def allowed_multiple_expression?(node) return false if node.children.one? + ancestor = node.ancestors.first return false unless ancestor + !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type? end @@ -89,6 +92,7 @@ def check(begin_node) end return offense(begin_node, 'a variable') if node.variable? return offense(begin_node, 'a constant') if node.const_type? + check_send(begin_node, node) if node.send_type? end @@ -157,6 +161,7 @@ def raised_to_power_negative_numeric?(begin_node, node) siblings = begin_node.parent && begin_node.parent.children return false if siblings.nil? + next_sibling = siblings[begin_node.sibling_index + 1] base_value = node.children.first diff --git a/lib/rubocop/cop/style/redundant_return.rb b/lib/rubocop/cop/style/redundant_return.rb index dfbdb66f9a53..110f6a6ea90d 100644 --- a/lib/rubocop/cop/style/redundant_return.rb +++ b/lib/rubocop/cop/style/redundant_return.rb @@ -118,6 +118,7 @@ def check_case_node(node) def check_when_node(node) return unless node + _cond, body = *node check_branch(body) if body end diff --git a/lib/rubocop/cop/style/rescue_modifier.rb b/lib/rubocop/cop/style/rescue_modifier.rb index a617f8f42eb4..fdd56ce043d3 100644 --- a/lib/rubocop/cop/style/rescue_modifier.rb +++ b/lib/rubocop/cop/style/rescue_modifier.rb @@ -23,6 +23,7 @@ class RescueModifier < Cop def on_resbody(node) return unless rescue_modifier?(node) + add_offense(node.parent) end diff --git a/lib/rubocop/cop/style/rescue_standard_error.rb b/lib/rubocop/cop/style/rescue_standard_error.rb index c74d51a91b1b..a4a55b76102f 100644 --- a/lib/rubocop/cop/style/rescue_standard_error.rb +++ b/lib/rubocop/cop/style/rescue_standard_error.rb @@ -90,6 +90,7 @@ class RescueStandardError < Cop def on_resbody(node) return if rescue_modifier?(node) + case style when :implicit rescue_standard_error?(node) do |error| diff --git a/lib/rubocop/cop/style/safe_navigation.rb b/lib/rubocop/cop/style/safe_navigation.rb index ed6677ab7739..09f99b445f8f 100644 --- a/lib/rubocop/cop/style/safe_navigation.rb +++ b/lib/rubocop/cop/style/safe_navigation.rb @@ -88,6 +88,7 @@ class SafeNavigation < Cop def on_if(node) return if allowed_if_condition?(node) + check_node(node) end @@ -97,6 +98,7 @@ def on_and(node) def check_node(node) return if target_ruby_version < 2.3 + checked_variable, receiver, method_chain, method = extract_parts(node) return unless receiver == checked_variable # method is already a method call so this is actually checking for a @@ -176,12 +178,14 @@ def find_matching_receiver_invocation(method_chain, checked_variable) end return receiver if receiver == checked_variable + find_matching_receiver_invocation(receiver, checked_variable) end def chain_size(method_chain, method) method.each_ancestor(:send).inject(0) do |total, ancestor| break total + 1 if ancestor == method_chain + total + 1 end end diff --git a/lib/rubocop/cop/style/semicolon.rb b/lib/rubocop/cop/style/semicolon.rb index 0dcad8547b5b..1b0fd3252393 100644 --- a/lib/rubocop/cop/style/semicolon.rb +++ b/lib/rubocop/cop/style/semicolon.rb @@ -22,6 +22,7 @@ class Semicolon < Cop def investigate(processed_source) return if processed_source.blank? + @processed_source = processed_source check_for_line_terminator_or_opener @@ -29,6 +30,7 @@ def investigate(processed_source) def on_begin(node) return if cop_config['AllowAsExpressionSeparator'] + exprs = node.children return if exprs.size < 2 @@ -40,6 +42,7 @@ def on_begin(node) # every line with more than 1 expression on it is an offense lines.each do |line, expr_on_line| next unless expr_on_line.size > 1 + # TODO: Find the correct position of the semicolon. We don't know # if the first semicolon on the line is a separator of # expressions. It's just a guess. @@ -50,6 +53,7 @@ def on_begin(node) def autocorrect(range) return unless range + ->(corrector) { corrector.remove(range) } end diff --git a/lib/rubocop/cop/style/signal_exception.rb b/lib/rubocop/cop/style/signal_exception.rb index a8b76b4e7bf7..49b42b021527 100644 --- a/lib/rubocop/cop/style/signal_exception.rb +++ b/lib/rubocop/cop/style/signal_exception.rb @@ -138,6 +138,7 @@ def on_send(node) check_send(:raise, node) unless ignored_node?(node) when :only_raise return if @custom_fail_defined + check_send(:fail, node) when :only_fail check_send(:raise, node) diff --git a/lib/rubocop/cop/style/string_hash_keys.rb b/lib/rubocop/cop/style/string_hash_keys.rb index 8eabf9912cee..21273ff53572 100644 --- a/lib/rubocop/cop/style/string_hash_keys.rb +++ b/lib/rubocop/cop/style/string_hash_keys.rb @@ -33,6 +33,7 @@ class StringHashKeys < Cop def on_pair(node) return unless string_hash_key?(node) return if receive_environments_method?(node) + add_offense(node.key) end diff --git a/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb b/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb index c3f78db43243..b80229439513 100644 --- a/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +++ b/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb @@ -45,6 +45,7 @@ class TrailingCommaInArrayLiteral < Cop def on_array(node) return unless node.square_brackets? + check_literal(node, 'item of %
s array') end diff --git a/lib/rubocop/cop/style/trailing_method_end_statement.rb b/lib/rubocop/cop/style/trailing_method_end_statement.rb index d56e14c6dd62..375842dbc9c5 100644 --- a/lib/rubocop/cop/style/trailing_method_end_statement.rb +++ b/lib/rubocop/cop/style/trailing_method_end_statement.rb @@ -84,6 +84,7 @@ def break_line_before_end(node, corrector) def remove_semicolon(node, corrector) return unless token_before_end(node).semicolon? + corrector.remove(token_before_end(node).pos) end end diff --git a/lib/rubocop/cop/style/trailing_underscore_variable.rb b/lib/rubocop/cop/style/trailing_underscore_variable.rb index 6e4f127903e2..3b5ff640fe8a 100644 --- a/lib/rubocop/cop/style/trailing_underscore_variable.rb +++ b/lib/rubocop/cop/style/trailing_underscore_variable.rb @@ -66,6 +66,7 @@ def find_first_offense(variables) def find_first_possible_offense(variables) variables.reduce(nil) do |offense, variable| break offense unless %i[lvasgn splat].include?(variable.type) + var, = *variable var, = *var if allow_named_underscore_variables diff --git a/lib/rubocop/cop/style/unneeded_condition.rb b/lib/rubocop/cop/style/unneeded_condition.rb index eefea9051861..5c8e92dcd1a9 100644 --- a/lib/rubocop/cop/style/unneeded_condition.rb +++ b/lib/rubocop/cop/style/unneeded_condition.rb @@ -69,6 +69,7 @@ def message(node) def range_of_offense(node) return :expression unless node.ternary? + range_between(node.loc.question.begin_pos, node.loc.colon.end_pos) end diff --git a/lib/rubocop/cop/style/unneeded_percent_q.rb b/lib/rubocop/cop/style/unneeded_percent_q.rb index ddb67a43ed95..ea1d45f30080 100644 --- a/lib/rubocop/cop/style/unneeded_percent_q.rb +++ b/lib/rubocop/cop/style/unneeded_percent_q.rb @@ -32,6 +32,7 @@ class UnneededPercentQ < Cop def on_dstr(node) return unless string_literal?(node) + check(node) end @@ -40,6 +41,7 @@ def on_str(node) # will call `on_dstr` for the entire string and `on_str` for the # non interpolated portion of the string return unless string_literal?(node) + check(node) end diff --git a/lib/rubocop/cop/team.rb b/lib/rubocop/cop/team.rb index 7a2a4c30988d..4175ff19803f 100644 --- a/lib/rubocop/cop/team.rb +++ b/lib/rubocop/cop/team.rb @@ -59,6 +59,7 @@ def forces_for(cops) Force.all.each_with_object([]) do |force_class, forces| joining_cops = cops.select { |cop| cop.join_force?(force_class) } next if joining_cops.empty? + forces << force_class.new(joining_cops) end end diff --git a/lib/rubocop/cop/variable_force.rb b/lib/rubocop/cop/variable_force.rb index e5c12a08f4d5..c9096c257705 100644 --- a/lib/rubocop/cop/variable_force.rb +++ b/lib/rubocop/cop/variable_force.rb @@ -99,6 +99,7 @@ def inspect_variables_in_scope(scope_node) def process_children(origin_node) origin_node.each_child_node do |child_node| next if scanned_node?(child_node) + process_node(child_node) end end @@ -174,6 +175,7 @@ def process_regexp_named_captures(node) variable_names.each do |name| next if variable_table.variable_exist?(name) + variable_table.declare_variable(name, node) end @@ -272,6 +274,7 @@ def process_rescue(node) def process_zero_arity_super(node) variable_table.accessible_variables.each do |variable| next unless variable.method_argument? + variable.reference!(node) end end @@ -315,10 +318,12 @@ def mark_assignments_as_referenced_in_loop(node) # Non related references which are caught in the above scan # would be skipped here. next unless variable + variable.assignments.each do |assignment| next if assignment_nodes_in_loop.none? do |assignment_node| assignment_node.equal?(assignment.node) end + assignment.reference!(node) end end diff --git a/lib/rubocop/cop/variable_force/assignment.rb b/lib/rubocop/cop/variable_force/assignment.rb index c360ea32c27e..e2c70500300b 100644 --- a/lib/rubocop/cop/variable_force/assignment.rb +++ b/lib/rubocop/cop/variable_force/assignment.rb @@ -48,11 +48,13 @@ def regexp_named_capture? def operator_assignment? return false unless meta_assignment_node + OPERATOR_ASSIGNMENT_TYPES.include?(meta_assignment_node.type) end def multiple_assignment? return false unless meta_assignment_node + meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE end @@ -76,6 +78,7 @@ def operator_assignment_node return nil unless node.parent return nil unless OPERATOR_ASSIGNMENT_TYPES.include?(node.parent.type) return nil unless node.sibling_index.zero? + node.parent end @@ -84,6 +87,7 @@ def multiple_assignment_node return nil unless grandparent_node return nil unless grandparent_node.type == MULTIPLE_ASSIGNMENT_TYPE return nil unless node.parent.type == MULTIPLE_LEFT_HAND_SIDE_TYPE + grandparent_node end end diff --git a/lib/rubocop/cop/variable_force/branch.rb b/lib/rubocop/cop/variable_force/branch.rb index c7d2f9505e8f..88ea7dcc16e5 100644 --- a/lib/rubocop/cop/variable_force/branch.rb +++ b/lib/rubocop/cop/variable_force/branch.rb @@ -9,8 +9,10 @@ def self.of(target_node, scope: nil) ([target_node] + target_node.ancestors).each do |node| return nil unless node.parent return nil unless scope.include?(node) + klass = CLASSES_BY_TYPE[node.parent.type] next unless klass + branch = klass.new(node, scope) return branch if branch.branched? end @@ -74,6 +76,7 @@ def control_node def parent return @parent if instance_variable_defined?(:@parent) + @branch = Branch.of(control_node, scope: scope) end @@ -122,6 +125,7 @@ def exclusive_with?(other) def ==(other) return false unless other + control_node.equal?(other.control_node) && child_node.equal?(other.child_node) end diff --git a/lib/rubocop/cop/variable_force/branchable.rb b/lib/rubocop/cop/variable_force/branchable.rb index 43985422f7db..34ef705b2237 100644 --- a/lib/rubocop/cop/variable_force/branchable.rb +++ b/lib/rubocop/cop/variable_force/branchable.rb @@ -8,11 +8,13 @@ class VariableForce module Branchable def branch return @branch if instance_variable_defined?(:@branch) + @branch = Branch.of(node, scope: scope) end def run_exclusively_with?(other) return false if !branch || !other.branch + branch.exclusive_with?(other.branch) end end diff --git a/lib/rubocop/cop/variable_force/scope.rb b/lib/rubocop/cop/variable_force/scope.rb index a6b6de4b1672..812b3e59f325 100644 --- a/lib/rubocop/cop/variable_force/scope.rb +++ b/lib/rubocop/cop/variable_force/scope.rb @@ -61,6 +61,7 @@ def include?(target_node) def each_node(&block) return to_enum(__method__) unless block_given? + yield node if naked_top_level? scan_node(node, &block) end @@ -70,6 +71,7 @@ def each_node(&block) def scan_node(node, &block) node.each_child_node do |child_node| next unless include?(child_node) + yield child_node scan_node(child_node, &block) end @@ -79,16 +81,20 @@ def belong_to_outer_scope?(target_node) return true if !naked_top_level? && target_node.equal?(node) return true if ancestor_node?(target_node) return false unless target_node.parent.equal?(node) + indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type] return false unless indices + indices.include?(target_node.sibling_index) end def belong_to_inner_scope?(target_node) return false if !target_node.parent || target_node.parent.equal?(node) return false unless SCOPE_TYPES.include?(target_node.parent.type) + indices = OUTER_SCOPE_CHILD_INDICES[target_node.parent.type] return true unless indices + !indices.include?(target_node.sibling_index) end diff --git a/lib/rubocop/cop/variable_force/variable_table.rb b/lib/rubocop/cop/variable_force/variable_table.rb index 5f2b3c11c0a5..b32313aa8e51 100644 --- a/lib/rubocop/cop/variable_force/variable_table.rb +++ b/lib/rubocop/cop/variable_force/variable_table.rb @@ -120,6 +120,7 @@ def accessible_variables def mark_variable_as_captured_by_block_if_so(variable) return unless current_scope.node.block_type? return if variable.scope == current_scope + variable.capture_with_block! end end diff --git a/lib/rubocop/file_finder.rb b/lib/rubocop/file_finder.rb index 4bcf666c8a29..743573e23382 100644 --- a/lib/rubocop/file_finder.rb +++ b/lib/rubocop/file_finder.rb @@ -33,11 +33,13 @@ def find_files_upwards(filename, start_dir, use_home: false) def traverse_files_upwards(filename, start_dir, use_home) Pathname.new(start_dir).expand_path.ascend do |dir| break if FileFinder.root_level?(dir) + file = dir + filename yield(file.to_s) if file.exist? end return unless use_home && ENV.key?('HOME') + file = File.join(Dir.home, filename) yield(file) if File.exist?(file) end diff --git a/lib/rubocop/formatter/disabled_config_formatter.rb b/lib/rubocop/formatter/disabled_config_formatter.rb index eb572412e423..85b01df44e5b 100644 --- a/lib/rubocop/formatter/disabled_config_formatter.rb +++ b/lib/rubocop/formatter/disabled_config_formatter.rb @@ -128,10 +128,10 @@ def output_cop_param_comments(output_buffer, params, default_cfg) params.each do |param| value = default_cfg[param] - if value.is_a?(Array) - next if value.empty? - output_buffer.puts "# #{param}: #{value.join(', ')}" - end + next unless value.is_a?(Array) + next if value.empty? + + output_buffer.puts "# #{param}: #{value.join(', ')}" end end diff --git a/lib/rubocop/formatter/file_list_formatter.rb b/lib/rubocop/formatter/file_list_formatter.rb index fef256e398e7..4ad67e71e63a 100644 --- a/lib/rubocop/formatter/file_list_formatter.rb +++ b/lib/rubocop/formatter/file_list_formatter.rb @@ -12,6 +12,7 @@ module Formatter class FileListFormatter < BaseFormatter def file_finished(file, offenses) return if offenses.empty? + output.printf("%s\n", file) end end diff --git a/lib/rubocop/formatter/simple_text_formatter.rb b/lib/rubocop/formatter/simple_text_formatter.rb index 97993c9383c6..6888437b0337 100644 --- a/lib/rubocop/formatter/simple_text_formatter.rb +++ b/lib/rubocop/formatter/simple_text_formatter.rb @@ -27,6 +27,7 @@ def started(_target_files) def file_finished(file, offenses) return if offenses.empty? + count_stats(offenses) report_file(file, offenses) end diff --git a/lib/rubocop/options.rb b/lib/rubocop/options.rb index 507a7f76a183..a51a432b0c47 100644 --- a/lib/rubocop/options.rb +++ b/lib/rubocop/options.rb @@ -29,6 +29,7 @@ def parse(command_line_args) if args.any? raise OptionArgumentError, '-s/--stdin requires exactly one path.' end + # We want the STDIN contents in @options[:stdin] and the file name in # args to simplify the rest of the processing. args = [@options[:stdin]] @@ -261,6 +262,7 @@ def validate_compatibility # rubocop:disable Metrics/MethodLength validate_parallel return if incompatible_options.size <= 1 + raise OptionArgumentError, 'Incompatible cli options: ' \ "#{incompatible_options.inspect}" end @@ -329,6 +331,7 @@ def incompatible_options def validate_exclude_limit_option return if @options[:exclude_limit] =~ /^\d+$/ + # Emulate OptionParser's behavior to make failures consistent regardless # of option order. raise OptionParser::MissingArgument diff --git a/lib/rubocop/path_util.rb b/lib/rubocop/path_util.rb index 0fb644054efe..93e8108e7af8 100644 --- a/lib/rubocop/path_util.rb +++ b/lib/rubocop/path_util.rb @@ -43,6 +43,7 @@ def match_path?(pattern, path) path =~ pattern rescue ArgumentError => e return false if e.message.start_with?('invalid byte sequence') + raise e end end diff --git a/lib/rubocop/processed_source.rb b/lib/rubocop/processed_source.rb index 854ef2d04708..b8dac06c2c7a 100644 --- a/lib/rubocop/processed_source.rb +++ b/lib/rubocop/processed_source.rb @@ -46,6 +46,7 @@ def disabled_line_ranges def ast_with_comments return if !ast || !comments + @ast_with_comments ||= Parser::Source::Comment.associate(ast, comments) end @@ -58,6 +59,7 @@ def lines result = [] all_lines.each_with_index do |line, ix| break if ix >= last_token_line && line == '__END__' + result << line end result @@ -70,6 +72,7 @@ def [](*args) def valid_syntax? return false if @parser_error + @diagnostics.none? { |d| %i[error fatal].include?(d.level) } end @@ -112,6 +115,7 @@ def comments_before_line(line) def start_with?(string) return false if self[0].nil? + self[0].start_with?(string) end diff --git a/lib/rubocop/remote_config.rb b/lib/rubocop/remote_config.rb index 5d0453e8e193..b0c6bd7b77c7 100644 --- a/lib/rubocop/remote_config.rb +++ b/lib/rubocop/remote_config.rb @@ -21,6 +21,7 @@ def file request do |response| next if response.is_a?(Net::HTTPNotModified) next if response.is_a?(SocketError) + File.open cache_path, 'w' do |io| io.write response.body end diff --git a/lib/rubocop/result_cache.rb b/lib/rubocop/result_cache.rb index 72e81a6bac82..972272dd8f2f 100644 --- a/lib/rubocop/result_cache.rb +++ b/lib/rubocop/result_cache.rb @@ -18,6 +18,7 @@ class ResultCache # there's parallel execution and the cache is shared. def self.cleanup(config_store, verbose, cache_root = nil) return if inhibit_cleanup # OPTIMIZE: For faster testing + cache_root ||= cache_root(config_store) return unless File.exist?(cache_root) diff --git a/lib/rubocop/rspec/cop_helper.rb b/lib/rubocop/rspec/cop_helper.rb index 0194a4b22d82..a69bee5f1793 100644 --- a/lib/rubocop/rspec/cop_helper.rb +++ b/lib/rubocop/rspec/cop_helper.rb @@ -19,6 +19,7 @@ def inspect_source(source, file = nil) RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} processed_source = parse_source(source, file) raise 'Error parsing example code' unless processed_source.valid_syntax? + _investigate(cop, processed_source) end @@ -51,6 +52,7 @@ def autocorrect_source_with_loop(source, file = nil) cop.instance_variable_set(:@corrections, []) new_source = autocorrect_source(source, file) return new_source if new_source == source + source = new_source end end @@ -58,6 +60,7 @@ def autocorrect_source_with_loop(source, file = nil) def _investigate(cop, processed_source) forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances| next unless cop.join_force?(klass) + instances << klass.new([cop]) end diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index b0a9bc0ea3b5..79e14aa95106 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -80,6 +80,7 @@ def each_inspected_file(files) if offenses.any? { |o| considered_failure?(o) } break false if @options[:fail_fast] + next false end diff --git a/lib/rubocop/target_finder.rb b/lib/rubocop/target_finder.rb index 8c2fc719e536..2655ac71ce64 100644 --- a/lib/rubocop/target_finder.rb +++ b/lib/rubocop/target_finder.rb @@ -74,6 +74,7 @@ def target_files_in_dir(base_dir = Dir.pwd) def to_inspect?(file, hidden_files, base_dir_config) return false if base_dir_config.file_to_exclude?(file) return true if !hidden_files.include?(file) && ruby_file?(file) + base_dir_config.file_to_include?(file) end @@ -141,6 +142,7 @@ def all_cops_include def ruby_executable?(file) return false unless File.extname(file).empty? && File.exist?(file) + first_line = File.open(file, &:readline) !(first_line =~ /#!.*(#{ruby_interpreters(file).join('|')})/).nil? rescue EOFError, ArgumentError => e diff --git a/manual/cops_layout.md b/manual/cops_layout.md index dfd08b6cf92b..f8908342a9a9 100644 --- a/manual/cops_layout.md +++ b/manual/cops_layout.md @@ -1060,7 +1060,7 @@ AllowMarginComment | `true` | Boolean Enabled by default | Supports autocorrection --- | --- -Disabled | Yes +Enabled | Yes This cop enforces empty line after guard clause diff --git a/spec/rubocop/cli/cli_auto_gen_config_spec.rb b/spec/rubocop/cli/cli_auto_gen_config_spec.rb index bbd5aabfcf60..37b417129c9b 100644 --- a/spec/rubocop/cli/cli_auto_gen_config_spec.rb +++ b/spec/rubocop/cli/cli_auto_gen_config_spec.rb @@ -37,6 +37,7 @@ def f if #{'a' * 80} return y end + z end RUBY @@ -136,6 +137,7 @@ def f if #{'a' * 80} return y end + z end RUBY diff --git a/spec/rubocop/cop/corrector_spec.rb b/spec/rubocop/cop/corrector_spec.rb index 83add27685b2..55b5b0a56289 100644 --- a/spec/rubocop/cop/corrector_spec.rb +++ b/spec/rubocop/cop/corrector_spec.rb @@ -17,6 +17,7 @@ def do_rewrite(corrections = nil, &block) unless corrections.all? { |c| c.is_a?(Proc) } raise 'Corrections should be a proc, block or an array of procs' end + described_class.new(processed_source.buffer, corrections).rewrite end diff --git a/spec/rubocop/options_spec.rb b/spec/rubocop/options_spec.rb index fb1312c72a11..751cedc349f8 100644 --- a/spec/rubocop/options_spec.rb +++ b/spec/rubocop/options_spec.rb @@ -130,6 +130,7 @@ def abs(path) formatter_keys = format_section.reduce([]) do |keys, line| match = line.match(/^[ ]{39}(\[[a-z\]]+)/) next keys unless match + keys << match.captures.first.gsub(/\[|\]/, '') end.sort diff --git a/spec/rubocop/result_cache_spec.rb b/spec/rubocop/result_cache_spec.rb index 8b5ef74ad95d..be6c62223f8a 100644 --- a/spec/rubocop/result_cache_spec.rb +++ b/spec/rubocop/result_cache_spec.rb @@ -105,6 +105,7 @@ def abs(path) cache.save(offenses) Find.find(cache_root) do |path| next unless File.basename(path) == '_' + FileUtils.rm_rf(path) FileUtils.ln_s(attack_target_dir, path) end