Class: Tailor::Rulers::SpacesBeforeRbraceRuler
- Inherits:
-
Tailor::Ruler
- Object
- Tailor::Ruler
- Tailor::Rulers::SpacesBeforeRbraceRuler
- Defined in:
- lib/tailor/rulers/spaces_before_rbrace_ruler.rb
Overview
Checks for spaces before a } as given by @config. It skips checking when:
-
it’s the first char in the line.
-
it’s the first char in the line, preceded by spaces.
-
it’s directly preceded by a {.
Instance Attribute Summary
Attributes inherited from Tailor::Ruler
Instance Method Summary collapse
-
#count_spaces(lexed_line, column) ⇒ Fixnum
The number of spaces before the rbrace.
- #embexpr_beg_update(lexed_line, lineno, column) ⇒ Object
-
#initialize(config, options) ⇒ SpacesBeforeRbraceRuler
constructor
A new instance of SpacesBeforeRbraceRuler.
- #lbrace_update(lexed_line, lineno, column) ⇒ Object
-
#measure(actual_spaces, lineno, column) ⇒ Object
Checks to see if the number of spaces before an rbrace equals the value at @config.
-
#rbrace_update(lexed_line, lineno, column) ⇒ Object
For Ruby versions < 2.0.0-p0, this has to keep track of {‘s and only follow through with the check if the { was an lbrace because Ripper doesn’t scan the } of an embedded expression (embexpr_end) as such..
Methods inherited from Tailor::Ruler
#add_child_ruler, #problem_type, #problems
Methods included from Logger::Mixin
Constructor Details
#initialize(config, options) ⇒ SpacesBeforeRbraceRuler
12 13 14 15 16 17 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 12 def initialize(config, ) super(config, ) add_lexer_observers :embexpr_beg, :lbrace, :rbrace @lbrace_nesting = [] @embexpr_nesting = [] end |
Instance Method Details
#count_spaces(lexed_line, column) ⇒ Fixnum
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 22 def count_spaces(lexed_line, column) current_index = lexed_line.event_index(column) log "Current event index: #{current_index}" previous_event = lexed_line.at(current_index - 1) log "Previous event: #{previous_event}" if column.zero? || previous_event.nil? log 'rbrace is at the beginning of the line.' @do_measurement = false return 0 end if previous_event[1] == :on_lbrace log "rbrace comes after a '{'" @do_measurement = false return 0 end return 0 if previous_event[1] != :on_sp if current_index - 2 < 0 log 'rbrace is at the beginning of an indented line. Moving on.' @do_measurement = false return previous_event.last.size end previous_event.last.size end |
#embexpr_beg_update(lexed_line, lineno, column) ⇒ Object
51 52 53 54 55 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 51 def embexpr_beg_update(lexed_line, lineno, column) if RUBY_VERSION < '2.0.0' @lbrace_nesting << :embexpr_beg end end |
#lbrace_update(lexed_line, lineno, column) ⇒ Object
57 58 59 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 57 def lbrace_update(lexed_line, lineno, column) @lbrace_nesting << :lbrace end |
#measure(actual_spaces, lineno, column) ⇒ Object
Checks to see if the number of spaces before an rbrace equals the value at @config.
67 68 69 70 71 72 73 74 75 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 67 def measure(actual_spaces, lineno, column) if actual_spaces != @config msg = "Line has #{actual_spaces} space(s) before a }, " msg << "but should have #{@config}." @problems << Problem.new(problem_type, lineno, column, msg, [:level]) end end |
#rbrace_update(lexed_line, lineno, column) ⇒ Object
For Ruby versions < 2.0.0-p0, this has to keep track of {‘s and only follow through with the check if the { was an lbrace because Ripper doesn’t scan the } of an embedded expression (embexpr_end) as such.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 84 def rbrace_update(lexed_line, lineno, column) if RUBY_VERSION < '2.0.0' && @lbrace_nesting.last == :embexpr_beg @lbrace_nesting.pop return end @lbrace_nesting.pop count = count_spaces(lexed_line, column) log "Found #{count} space(s) before rbrace." if !@do_measurement log 'Skipping measurement.' else measure(count, lineno, column) end @do_measurement = true end |