Class: Rubocop::Cop::Style::SpaceAroundBlockBraces

Inherits:
Cop
  • Object
show all
Includes:
SurroundingSpace
Defined in:
lib/rubocop/cop/style/space_around_block_braces.rb

Overview

Checks that block braces have or don't have surrounding space depending on configuration. For blocks taking parameters, it checks that the left brace has or doesn't have trailing space depending on configuration.

Constant Summary

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods included from SurroundingSpace

#index_of_first_token, #index_of_last_token, #space_between?, #token_table

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#autocorrect(range) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 137

def autocorrect(range)
  @corrections << lambda do |corrector|
    case range.source
    when '}', '|' then corrector.insert_before(range, ' ')
    when ' '      then corrector.remove(range)
    else               corrector.insert_after(range, ' ')
    end
  end
end

#check(t1, t2) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 60

def check(t1, t2)
  if t2.text == '{'
    check_space_outside_left_brace(t1, t2)
  elsif t1.text == '{' && t2.text == '}'
    check_empty_braces(t1, t2)
  elsif cop_config['EnforcedStyle'] == 'space_inside_braces'
    check_space_inside_braces(t1, t2)
  else
    check_no_space_inside_braces(t1, t2)
  end
end

#check_empty_braces(t1, t2) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 72

def check_empty_braces(t1, t2)
  if cop_config['EnforcedStyleForEmptyBraces'] == 'space'
    check_space_inside_braces(t1, t2)
  else
    check_no_space_inside_braces(t1, t2)
  end
end

#check_no_space_inside_braces(t1, t2) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 87

def check_no_space_inside_braces(t1, t2)
  if space_between?(t1, t2)
    token, what = problem_details(t1, t2)
    s = space_range(token)
    convention(s, s, "Space inside #{what} detected.")
  end
end

#check_pipe(t1, t2) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 112

def check_pipe(t1, t2)
  if cop_config['SpaceBeforeBlockParameters']
    unless space_between?(t1, t2)
      convention(t2.pos, t1.pos, 'Space between { and | missing.')
    end
  elsif space_between?(t1, t2)
    s = space_range(t1)
    convention(s, s, 'Space between { and | detected.')
  end
end

#check_space_inside_braces(t1, t2) ⇒ Object



80
81
82
83
84
85
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 80

def check_space_inside_braces(t1, t2)
  unless space_between?(t1, t2)
    token, what = problem_details(t1, t2)
    convention(token.pos, token.pos, "Space missing inside #{what}.")
  end
end

#check_space_outside_left_brace(t1, t2) ⇒ Object



106
107
108
109
110
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 106

def check_space_outside_left_brace(t1, t2)
  if t2.text == '{' && !space_between?(t1, t2)
    convention(t1.pos, t2.pos, 'Space missing to the left of {.')
  end
end

#investigate(processed_source) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 14

def investigate(processed_source)
  return unless processed_source.ast
  @processed_source = processed_source

  processed_source.tokens.each_cons(2) do |t1, t2|
    next if ([t1.pos, t2.pos] - positions_not_to_check).size < 2

    type1, type2 = t1.type, t2.type
    if [:tLCURLY, :tRCURLY].include?(type2)
      check(t1, t2)
    elsif type1 == :tLCURLY
      if type2 == :tPIPE
        check_pipe(t1, t2)
      else
        check(t1, t2)
      end
    end
  end
end

#positions_not_to_checkObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 34

def positions_not_to_check
  @positions_not_to_check ||= begin
    positions = []
    ast = @processed_source.ast
    tokens = @processed_source.tokens

    on_node(:hash, ast) do |hash|
      b_ix = index_of_first_token(hash)
      e_ix = index_of_last_token(hash)
      positions << tokens[b_ix].pos << tokens[e_ix].pos
    end

    # TODO: Check braces inside string/symbol/regexp/xstr
    #   interpolation.
    on_node([:dstr, :dsym, :regexp, :xstr], ast) do |s|
      b_ix = index_of_first_token(s)
      e_ix = index_of_last_token(s)
      tokens[b_ix..e_ix].each do |t|
        positions << t.pos if t.type == :tRCURLY
      end
    end

    positions
  end
end

#problem_details(t1, t2) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 95

def problem_details(t1, t2)
  if t1.text == '{'
    token = t1
    what = t2.text == '}' ? 'empty braces' : '{'
  else
    token = t2
    what = '}'
  end
  [token, what]
end

#space_range(token) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubocop/cop/style/space_around_block_braces.rb', line 123

def space_range(token)
  src = @processed_source.buffer.source
  if token.text == '{'
    b = token.pos.begin_pos + 1
    e = b + 1
    e += 1 while src[e] =~ /\s/
  else
    e = token.pos.begin_pos
    b = e - 1
    b -= 1 while src[b - 1] =~ /\s/
  end
  Parser::Source::Range.new(@processed_source.buffer, b, e)
end