Class: Rubocop::Cop::Style::SpaceAroundBlockBraces
- Includes:
- SurroundingSpace
- Defined in:
- lib/rubocop/cop/style/surrounding_space.rb
Overview
Checks that block braces have surrounding space. For blocks taking parameters, it check that the left brace has or doesn't have trailing space depending on configuration.
Constant Summary collapse
- MSG_LEFT =
"Surrounding space missing for '{'."
- MSG_RIGHT =
"Space missing to the left of '}'."
- MSG_PIPE =
'Space between { and | detected.'
Constants inherited from Cop
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offences, #processed_source
Instance Method Summary collapse
- #check(t1, t2, msg) ⇒ Object
- #check_pipe(t1, t2, msg) ⇒ Object
- #investigate(processed_source) ⇒ Object
- #positions_not_to_check ⇒ Object
Methods included from SurroundingSpace
#index_of_first_token, #index_of_last_token, #space_between?, #token_table
Methods inherited from Cop
#add_offence, all, #autocorrect?, #autocorrect_action, #convention, #cop_config, #cop_name, cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, rails?, style?, #warning
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#check(t1, t2, msg) ⇒ Object
232 233 234 235 236 237 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 232 def check(t1, t2, msg) unless space_between?(t1, t2) brace_token = t1.text == '{' ? t1 : t2 convention(nil, brace_token.pos, msg) end end |
#check_pipe(t1, t2, msg) ⇒ Object
239 240 241 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 239 def check_pipe(t1, t2, msg) convention(nil, t1.pos, msg) if space_between?(t1, t2) end |
#investigate(processed_source) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 186 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 check(t1, t2, MSG_LEFT) if type2 == :tLCURLY if type1 == :tLCURLY if type2 == :tPIPE && cop_config['NoSpaceBeforeBlockParameters'] check_pipe(t1, t2, MSG_PIPE) else check(t1, t2, MSG_LEFT) end end check(t1, t2, MSG_RIGHT) if type2 == :tRCURLY end end |
#positions_not_to_check ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 206 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 |