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.
Constant Summary collapse
- MSG =
'Block body expression is on the same line as the block start.'
Constants included from Util
Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offenses, #processed_source
Instance Method Summary collapse
Methods inherited from Cop
#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, cop_name_with_namespace, cop_type, #debug?, #display_cop_names?, inherited, #initialize, #join_force?, lint?, #message, non_rails, qualified_cop_name, rails?, #relevant_file?, #support_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
begins_its_line?, block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, numeric_range_size, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes, within_node?
Methods included from PathUtil
issue_deprecation_warning, match_path?, relative_path
Constructor Details
This class inherits a constructor from RuboCop::Cop::Cop
Instance Method Details
#autocorrect(node) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 55 def autocorrect(node) @corrections << lambda do |corrector| _method, _args, block_body = *node first_node = if block_body.type == :begin block_body.children.first else block_body end block_start_col = node.loc.expression.column corrector.insert_before(first_node.loc.expression, "\n #{' ' * block_start_col}") end end |
#on_block(node) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubocop/cop/style/multiline_block_layout.rb', line 34 def on_block(node) end_loc = node.loc.end do_loc = node.loc.begin # Actually it's either do or {. return if do_loc.line == end_loc.line # One-liner, no newline needed. # A block node has three children: the block start, # the arguments, and the expression. We care if the block start # and the expression start on the same line. last_expression = node.children.last return unless last_expression expression_loc = last_expression.loc return unless do_loc.line == expression_loc.line expression = last_expression.loc.expression range = Parser::Source::Range.new(expression.source_buffer, expression.begin_pos, expression.end_pos) add_offense(node, range) end |