Class: RuboCop::Cop::Style::MultilineBlockLayout
- Defined in:
- lib/rubocop/cop/style/multiline_block_layout.rb
Overview
This cop checks whether the multiline do end blocks have a newline after the start of the block. Additionally, it checks whether the block arguments, if any, are on the same line as the start of the block.
Constant Summary collapse
- MSG =
'Block body expression is on the same line as ' \ 'the block start.'.freeze
- ARG_MSG =
'Block argument expression is not on the same line as the ' \ 'block start.'.freeze
Constants included from Util
Util::ASGN_NODES, Util::BYTE_ORDER_MARK, Util::CONDITIONAL_NODES, Util::EQUALS_ASGN_NODES, Util::LITERAL_REGEX, Util::LOGICAL_OPERATOR_NODES, Util::MODIFIER_NODES, Util::OPERATOR_METHODS, Util::SHORTHAND_ASGN_NODES
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offenses, #processed_source
Instance Method Summary collapse
- #add_offense_for_expression(node, expr, msg) ⇒ Object
- #args_on_different_line?(do_line, args) ⇒ Boolean
- #arguments_on_different_line?(node, args) ⇒ Boolean
- #autocorrect(node) ⇒ Object
- #autocorrect_arguments(corrector, node, args) ⇒ Object
- #autocorrect_body(corrector, node, block_body) ⇒ Object
- #block_arg_string(args) ⇒ Object
- #on_block(node) ⇒ Object
- #oneliner?(node) ⇒ Boolean
Methods inherited from Cop
#add_offense, all, badge, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, #correct, department, #duplicate_location?, #excluded_file?, #find_location, #highlights, inherited, #initialize, #join_force?, lint?, match?, #message, #messages, non_rails, #parse, qualified_cop_name, #relevant_file?, #target_ruby_version
Methods included from AST::Sexp
Methods included from NodePattern::Macros
#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
begins_its_line?, block_length, comment_line?, compatible_external_encoding_for?, directions, double_quotes_acceptable?, double_quotes_required?, effective_column, ends_its_line?, escape_string, first_part_of_call_chain, hard_to_type?, interpret_string_escapes, line_distance, line_range, move_pos, needs_escaping?, numeric_range_size, on_node, operator?, parentheses?, parenthesized_call?, preceed?, range_between, range_with_surrounding_comma, range_with_surrounding_space, same_line?, source_range, strip_quotes, stripped_source_upto, to_string_literal, to_supported_styles, to_symbol_literal, within_node?
Methods included from PathUtil
absolute?, match_path?, relative_path
Constructor Details
This class inherits a constructor from RuboCop::Cop::Cop
Instance Method Details
#add_offense_for_expression(node, expr, msg) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 73 def add_offense_for_expression(node, expr, msg) expression = expr.source_range range = range_between(expression.begin_pos, expression.end_pos) add_offense(node, range, msg) end |
#args_on_different_line?(do_line, args) ⇒ Boolean
67 68 69 70 71 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 67 def args_on_different_line?(do_line, args) return false if args.children.empty? do_line != args.loc.last_line end |
#arguments_on_different_line?(node, args) ⇒ Boolean
97 98 99 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 97 def arguments_on_different_line?(node, args) args.children.empty? || args.loc.last_line == node.loc.line end |
#autocorrect(node) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 80 def autocorrect(node) lambda do |corrector| _method, args, block_body = *node unless arguments_on_different_line?(node, args) autocorrect_arguments(corrector, node, args) expr_before_body = args.source_range.end end return unless block_body expr_before_body ||= node.loc.begin if expr_before_body.line == block_body.loc.line autocorrect_body(corrector, node, block_body) end end end |
#autocorrect_arguments(corrector, node, args) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 101 def autocorrect_arguments(corrector, node, args) end_pos = range_with_surrounding_space(args.source_range, :right, false) .end_pos range = range_between(node.loc.begin.end.begin_pos, end_pos) corrector.replace(range, " |#{block_arg_string(args)}|") end |
#autocorrect_body(corrector, node, block_body) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 109 def autocorrect_body(corrector, node, block_body) first_node = if block_body.begin_type? block_body.children.first else block_body end block_start_col = node.source_range.column corrector.insert_before(first_node.source_range, "\n #{' ' * block_start_col}") end |
#block_arg_string(args) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 122 def block_arg_string(args) args.children.map do |arg| if arg.mlhs_type? "(#{block_arg_string(arg)})" else arg.source end end.join(', ') end |
#on_block(node) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 44 def on_block(node) return if oneliner?(node) # A block node has three children: the block start, # the arguments, and the expression. We care if the block start # with arguments and the expression start on the same line. _block_start, args, last_expression = node.children do_loc = node.loc.begin # Actually it's either do or {. if args_on_different_line?(do_loc.line, args) add_offense_for_expression(node, args, ARG_MSG) end return unless last_expression expression_loc = last_expression.loc return unless do_loc.line == expression_loc.line add_offense_for_expression(node, last_expression, MSG) end |
#oneliner?(node) ⇒ Boolean
63 64 65 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 63 def oneliner?(node) node.loc.begin.line == node.loc.end.line end |