summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-11-20 09:22:14 +0000
committerRémy Coutable <remy@rymai.me>2017-11-20 09:22:14 +0000
commit4f09d099e9fa29659917f87c2dc25f2b9aa05b5e (patch)
treea70a9b3d42c8d3762974d354b31aa611590f55b8 /rubocop
parent135437b70d5bf12fff17d10adeae0c459b779138 (diff)
parent181cd299f9e06223e8338e93b1c318c671ccb1aa (diff)
downloadgitlab-ce-4f09d099e9fa29659917f87c2dc25f2b9aa05b5e.tar.gz
Merge branch '18040-rubocop-line-break-after-guard-clause' into 'master'
Adds Rubocop rule for line break after guard clause Closes #18040 See merge request gitlab-org/gitlab-ce!15188
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/line_break_after_guard_clauses.rb100
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 101 insertions, 0 deletions
diff --git a/rubocop/cop/line_break_after_guard_clauses.rb b/rubocop/cop/line_break_after_guard_clauses.rb
new file mode 100644
index 00000000000..67477f064ab
--- /dev/null
+++ b/rubocop/cop/line_break_after_guard_clauses.rb
@@ -0,0 +1,100 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # Ensures a line break after guard clauses.
+ #
+ # @example
+ # # bad
+ # return unless condition
+ # do_stuff
+ #
+ # # good
+ # return unless condition
+ #
+ # do_stuff
+ #
+ # # bad
+ # raise if condition
+ # do_stuff
+ #
+ # # good
+ # raise if condition
+ #
+ # do_stuff
+ #
+ # Multiple guard clauses are allowed without
+ # line break.
+ #
+ # # good
+ # return unless condition_a
+ # return unless condition_b
+ #
+ # do_stuff
+ #
+ # Guard clauses in case statement are allowed without
+ # line break.
+ #
+ # # good
+ # case model
+ # when condition_a
+ # return true unless condition_b
+ # when
+ # ...
+ # end
+ #
+ # Guard clauses before end are allowed without
+ # line break.
+ #
+ # # good
+ # if condition_a
+ # do_something
+ # else
+ # do_something_else
+ # return unless condition
+ # end
+ #
+ # do_something_more
+ class LineBreakAfterGuardClauses < RuboCop::Cop::Cop
+ MSG = 'Add a line break after guard clauses'
+
+ def_node_matcher :guard_clause_node?, <<-PATTERN
+ [{(send nil? {:raise :fail :throw} ...) return break next} single_line?]
+ PATTERN
+
+ def on_if(node)
+ return unless node.single_line?
+ return unless guard_clause?(node)
+ return if next_line(node).blank? || clause_last_line?(next_line(node)) || guard_clause?(next_sibling(node))
+
+ add_offense(node, :expression, MSG)
+ end
+
+ def autocorrect(node)
+ lambda do |corrector|
+ corrector.insert_after(node.loc.expression, "\n")
+ end
+ end
+
+ private
+
+ def guard_clause?(node)
+ return false unless node.if_type?
+
+ guard_clause_node?(node.if_branch)
+ end
+
+ def next_sibling(node)
+ node.parent.children[node.sibling_index + 1]
+ end
+
+ def next_line(node)
+ processed_source[node.loc.line]
+ end
+
+ def clause_last_line?(line)
+ line =~ /^\s*(?:end|elsif|else|when|rescue|ensure)/
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index a1668749dd9..7621ea50da9 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -3,6 +3,7 @@ require_relative 'cop/active_record_serialize'
require_relative 'cop/custom_error_class'
require_relative 'cop/gem_fetcher'
require_relative 'cop/in_batches'
+require_relative 'cop/line_break_after_guard_clauses'
require_relative 'cop/polymorphic_associations'
require_relative 'cop/project_path_helper'
require_relative 'cop/redirect_with_status'