Class: RuboCop::Cop::Style::SingleLineDoEndBlock
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::SingleLineDoEndBlock
- Extended by:
- AutoCorrector
- Includes:
- CheckSingleLineSuitability
- Defined in:
- lib/rubocop/cop/style/single_line_do_end_block.rb
Overview
Checks for single-line ‘do`…`end` block.
In practice a single line ‘do`…`end` is autocorrected when `EnforcedStyle: semantic` is configured for `Style/BlockDelimiters`. The autocorrection maintains the `do` … `end` syntax to preserve semantics and does not change it to ``…`` block.
NOTE: If ‘InspectBlocks` is set to `true` for `Layout/RedundantLineBreak`, blocks will be autocorrected to be on a single line if possible. This cop respects that configuration by not registering an offense if it would subsequently cause a `Layout/RedundantLineBreak` offense.
Constant Summary collapse
- MSG =
'Prefer multiline `do`...`end` block.'
Instance Method Summary collapse
-
#on_block(node) ⇒ Object
(also: #on_numblock)
rubocop:disable Metrics/AbcSize.
Methods included from AutoCorrector
Methods included from CheckSingleLineSuitability
Instance Method Details
#on_block(node) ⇒ Object Also known as: on_numblock
rubocop:disable Metrics/AbcSize
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/cop/style/single_line_do_end_block.rb', line 40 def on_block(node) return if !node.single_line? || node.braces? return if single_line_blocks_preferred? && suitable_as_single_line?(node) add_offense(node) do |corrector| corrector.insert_after(do_line(node), "\n") node_body = node.body if node_body.respond_to?(:heredoc?) && node_body.heredoc? corrector.remove(node.loc.end) corrector.insert_after(node_body.loc.heredoc_end, "\nend") else corrector.insert_before(node.loc.end, "\n") end end end |