Module: RuboCop::Cop::MultilineExpressionIndentation
- Defined in:
- lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Overview
Common functionality for checking multiline method calls and binary operations.
Constant Summary collapse
- KEYWORD_ANCESTOR_TYPES =
[:for, :return, *Util::MODIFIER_NODES].freeze
- UNALIGNED_RHS_TYPES =
%i[if while until for return array kwbegin].freeze
- ASSIGNMENT_RHS_TYPES =
[:send, *Util::ASGN_NODES].freeze
- DEFAULT_MESSAGE_TAIL =
'an expression'.freeze
- ASSIGNMENT_MESSAGE_TAIL =
'an expression in an assignment'.freeze
- KEYWORD_MESSAGE_TAIL =
'a %s in %s `%s` statement'.freeze
Instance Method Summary collapse
- #argument_in_method_call(node, kind) ⇒ Object
- #assignment_rhs(node) ⇒ Object
- #check(range, node, lhs, rhs) ⇒ Object
- #correct_indentation(node) ⇒ Object
- #disqualified_rhs?(candidate, ancestor) ⇒ Boolean
- #grouped_expression?(node) ⇒ Boolean
- #incorrect_style_detected(range, node, lhs, rhs) ⇒ Object
- #indentation(node) ⇒ Object
- #indented_keyword_expression(node) ⇒ Object
- #inside_arg_list_parentheses?(node, ancestor) ⇒ Boolean
- #keyword_message_tail(node) ⇒ Object
- #kw_node_with_special_indentation(node) ⇒ Object
-
#left_hand_side(lhs) ⇒ Object
In a chain of method calls, we regard the top send node as the base for indentation of all lines following the first.
- #not_for_this_cop?(node) ⇒ Boolean
- #on_send(node) ⇒ Object
- #operation_description(node, rhs) ⇒ Object
- #part_of_assignment_rhs(node, candidate) ⇒ Object
- #part_of_block_body?(candidate, block_node) ⇒ Boolean
- #regular_method_right_hand_side(send_node) ⇒ Object
- #right_hand_side(send_node) ⇒ Object
-
#valid_method_rhs_candidate?(candidate, node) ⇒ Boolean
The []= operator and setters (a.b = c) are parsed as :send nodes.
- #valid_rhs?(candidate, ancestor) ⇒ Boolean
- #valid_rhs_candidate?(candidate, node) ⇒ Boolean
Instance Method Details
#argument_in_method_call(node, kind) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 132 def argument_in_method_call(node, kind) node.each_ancestor(:send, :block).find do |a| # If the node is inside a block, it makes no difference if that block # is an argument in a method call. It doesn't count. break false if a.block_type? next if a.setter_method? a.arguments.any? do |arg| within_node?(node, arg) && (kind == :with_or_without_parentheses || kind == :with_parentheses && parentheses?(node.parent)) end end end |
#assignment_rhs(node) ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 191 def assignment_rhs(node) case node.type when :casgn then _scope, _lhs, rhs = *node when :op_asgn then _lhs, _op, rhs = *node when :send then rhs = node.last_argument else _lhs, rhs = *node end rhs end |
#check(range, node, lhs, rhs) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 66 def check(range, node, lhs, rhs) if range incorrect_style_detected(range, node, lhs, rhs) else correct_style_detected end end |
#correct_indentation(node) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 56 def correct_indentation(node) if kw_node_with_special_indentation(node) # This cop could have its own IndentationWidth configuration configured_indentation_width + @config.for_cop('Layout/IndentationWidth')['Width'] else configured_indentation_width end end |
#disqualified_rhs?(candidate, ancestor) ⇒ Boolean
162 163 164 165 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 162 def disqualified_rhs?(candidate, ancestor) UNALIGNED_RHS_TYPES.include?(ancestor.type) || ancestor.block_type? && part_of_block_body?(candidate, ancestor) end |
#grouped_expression?(node) ⇒ Boolean
208 209 210 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 208 def grouped_expression?(node) node.begin_type? && node.loc.respond_to?(:begin) && node.loc.begin end |
#incorrect_style_detected(range, node, lhs, rhs) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 74 def incorrect_style_detected(range, node, lhs, rhs) add_offense(range, range, (node, lhs, rhs)) do if supported_styles.size > 2 || offending_range(node, lhs, rhs, alternative_style) unrecognized_style_detected else opposite_style_detected end end end |
#indentation(node) ⇒ Object
85 86 87 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 85 def indentation(node) node.source_range.source_line =~ /\S/ end |
#indented_keyword_expression(node) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 122 def indented_keyword_expression(node) if node.for_type? expression = node.collection else expression, = *node end expression end |
#inside_arg_list_parentheses?(node, ancestor) ⇒ Boolean
212 213 214 215 216 217 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 212 def inside_arg_list_parentheses?(node, ancestor) return false unless ancestor.send_type? && ancestor.parenthesized? node.source_range.begin_pos > ancestor.loc.begin.begin_pos && node.source_range.end_pos < ancestor.loc.end.end_pos end |
#keyword_message_tail(node) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 101 def (node) keyword = node.loc.keyword.source kind = keyword == 'for' ? 'collection' : 'condition' article = keyword =~ /^[iu]/ ? 'an' : 'a' format(KEYWORD_MESSAGE_TAIL, kind, article, keyword) end |
#kw_node_with_special_indentation(node) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 109 def kw_node_with_special_indentation(node) keyword_node = node.each_ancestor(*KEYWORD_ANCESTOR_TYPES).find do |ancestor| within_node?(node, indented_keyword_expression(ancestor)) end if keyword_node && block_given? yield keyword_node else keyword_node end end |
#left_hand_side(lhs) ⇒ Object
In a chain of method calls, we regard the top send node as the base for indentation of all lines following the first. For example: a.
b c { block }. <-- b is indented relative to a
d <-- d is indented relative to a
31 32 33 34 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 31 def left_hand_side(lhs) lhs = lhs.parent while lhs.parent && lhs.parent.send_type? lhs end |
#not_for_this_cop?(node) ⇒ Boolean
201 202 203 204 205 206 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 201 def not_for_this_cop?(node) node.ancestors.any? do |ancestor| grouped_expression?(ancestor) || inside_arg_list_parentheses?(node, ancestor) end end |
#on_send(node) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 16 def on_send(node) return if !node.receiver || node.method?(:[]) return unless relevant_node?(node) lhs = left_hand_side(node.receiver) rhs = right_hand_side(node) range = offending_range(node, lhs, rhs, style) check(range, node, lhs, rhs) end |
#operation_description(node, rhs) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 89 def operation_description(node, rhs) kw_node_with_special_indentation(node) do |ancestor| return (ancestor) end part_of_assignment_rhs(node, rhs) do |_node| return ASSIGNMENT_MESSAGE_TAIL end DEFAULT_MESSAGE_TAIL end |
#part_of_assignment_rhs(node, candidate) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 148 def part_of_assignment_rhs(node, candidate) rhs_node = node.each_ancestor.find do |ancestor| break if disqualified_rhs?(candidate, ancestor) valid_rhs?(candidate, ancestor) end if rhs_node && block_given? yield rhs_node else rhs_node end end |
#part_of_block_body?(candidate, block_node) ⇒ Boolean
187 188 189 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 187 def part_of_block_body?(candidate, block_node) block_node.body && within_node?(candidate, block_node.body) end |
#regular_method_right_hand_side(send_node) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 44 def regular_method_right_hand_side(send_node) dot = send_node.loc.dot selector = send_node.loc.selector if send_node.dot? && selector && dot.line == selector.line dot.join(selector) elsif selector selector elsif send_node.implicit_call? dot.join(send_node.loc.begin) end end |
#right_hand_side(send_node) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 36 def right_hand_side(send_node) if send_node.operator_method? && send_node.arguments? send_node.first_argument.source_range # not used for method calls else regular_method_right_hand_side(send_node) end end |
#valid_method_rhs_candidate?(candidate, node) ⇒ Boolean
The []= operator and setters (a.b = c) are parsed as :send nodes.
178 179 180 181 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 178 def valid_method_rhs_candidate?(candidate, node) node.setter_method? && valid_rhs_candidate?(candidate, node.last_argument) end |
#valid_rhs?(candidate, ancestor) ⇒ Boolean
167 168 169 170 171 172 173 174 175 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 167 def valid_rhs?(candidate, ancestor) if ancestor.send_type? valid_method_rhs_candidate?(candidate, ancestor) elsif Util::ASGN_NODES.include?(ancestor.type) valid_rhs_candidate?(candidate, assignment_rhs(ancestor)) else false end end |
#valid_rhs_candidate?(candidate, node) ⇒ Boolean
183 184 185 |
# File 'lib/rubocop/cop/mixin/multiline_expression_indentation.rb', line 183 def valid_rhs_candidate?(candidate, node) !candidate || within_node?(candidate, node) end |