Class: RuboCop::Cop::Elegant::PairedBrackets

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/elegant/paired_brackets.rb

Overview

Enforces the "paired brackets" notation: every round, square, or curly bracket must either be paired with its matching counterpart on the same line, or end its own line (when opening) or start its own line (when closing). Brackets stranded in the middle of a multi-line expression are forbidden because they hide the structure of the code.

Auto-correct relocates the offending bracket onto its own line: an opener that is not at end-of-line is followed by a newline and the opener-line indent plus two spaces; a closer that is not at start-of-line is preceded by a newline and the opener-line indent. The whitespace already sitting next to the bracket is consumed by the rewrite rather than left in place, so the moved code lands on the intended column instead of drifting right by that much. Surrounding indentation may still need a follow-up layout pass, but the brackets themselves end up paired.

See https://www.yegor256.com/2014/10/23/paired-brackets-notation.html

Constant Summary collapse

MSG =
'Bracket %<text>s must be paired on the same line, or start/end its line'

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



38
39
40
41
# File 'lib/rubocop/cop/elegant/paired_brackets.rb', line 38

def on_new_investigation
  super
  pair.each { |duo| check(duo) }
end