Skip to content

Commit

Permalink
Enable Layout/EmptyLineAfterGuardClause cop by default
Browse files Browse the repository at this point in the history
This PR enables `Layout/EmptyLineAfterGuardClause` cop by default.

This cop has been introduced by rubocop#5522, with setting that is
disabled by default. After that, I improved this cop by
rubocop#5679, rubocop#5700, rubocop#5720, and rubocop#5760.

I think that this cop can be enabled by default as the background
of the issue rubocop#5376.

And this PR applies auto-correction using this cop.
  • Loading branch information
koic committed Aug 30, 2018
1 parent 6140ba6 commit c4c1ce1
Show file tree
Hide file tree
Showing 163 changed files with 292 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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}"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/ast/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module AST
module Traversal
def walk(node)
return if node.nil?

send(:"on_#{node.type}", node)
nil
end
Expand Down Expand Up @@ -65,6 +66,7 @@ def on_#{type}(node)

def on_const(node)
return unless (child = node.children[0])

send(:"on_#{child.type}", child)
end

Expand All @@ -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

Expand All @@ -85,19 +88,22 @@ def on_class(node)
send(:"on_#{child.type}", child)
end
return unless (child = children[2])

send(:"on_#{child.type}", child)
end

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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/config_loader_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/autocorrect_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/bundler/ordered_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ 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)
next unless case_insensitive_out_of_order?(
gem_name(current),
gem_name(previous)
)

register_offense(previous, current)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/commissioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def correct(node)
@corrected_nodes[node] = true
correction = autocorrect(node)
return :uncorrected unless correction

@corrections << correction
:corrected
end
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 '\
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/correctors/alignment_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/correctors/line_break_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/correctors/space_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/force.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/gemspec/ordered_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading

0 comments on commit c4c1ce1

Please sign in to comment.