Class: Rubocop::Cop::Style::SpaceAroundBraces
- Includes:
- SurroundingSpace
- Defined in:
- lib/rubocop/cop/style/surrounding_space.rb
Overview
Checks that block braces have surrounding space.
Constant Summary collapse
- MSG_LEFT =
"Surrounding space missing for '{'."- MSG_RIGHT =
"Space missing to the left of '}'."
Instance Attribute Summary
Attributes inherited from Cop
#autocorrect, #debug, #disabled_lines, #offences
Instance Method Summary collapse
- #check(t1, t2, msg) ⇒ Object
- #get_positions_not_to_check(tokens, sexp) ⇒ Object
- #inspect(source_buffer, source, tokens, sexp, comments) ⇒ Object
Methods included from SurroundingSpace
#build_token_table, #index_of_first_token, #index_of_last_token, #space_between?
Methods inherited from Cop
#add_offence, #autocorrect_action, cop_name, #do_autocorrect, #ignore_node, inherited, #initialize, #name, rails?
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#check(t1, t2, msg) ⇒ Object
211 212 213 214 215 216 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 211 def check(t1, t2, msg) unless space_between?(t1, t2) brace_token = msg == MSG_LEFT ? t1 : t2 add_offence(:convention, brace_token.pos, msg) end end |
#get_positions_not_to_check(tokens, sexp) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 190 def get_positions_not_to_check(tokens, sexp) positions_not_to_check = [] on_node(:hash, sexp) do |hash| b_ix = index_of_first_token(hash, tokens) e_ix = index_of_last_token(hash, tokens) positions_not_to_check << tokens[b_ix].pos << tokens[e_ix].pos end # TODO: Check braces inside string/symbol/regexp/xstr interpolation. on_node([:dstr, :dsym, :regexp, :xstr], sexp) do |s| b_ix = index_of_first_token(s, tokens) e_ix = index_of_last_token(s, tokens) tokens[b_ix..e_ix].each do |t| positions_not_to_check << t.pos if t.type == :tRCURLY end end positions_not_to_check end |
#inspect(source_buffer, source, tokens, sexp, comments) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 175 def inspect(source_buffer, source, tokens, sexp, comments) return unless sexp @source = source positions_not_to_check = get_positions_not_to_check(tokens, sexp) 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 # :tLBRACE in hash literals, :tLCURLY otherwise. next if [:tLCURLY, :tLBRACE].include?(type1) && type2 == :tRCURLY check(t1, t2, MSG_LEFT) if type1 == :tLCURLY || type2 == :tLCURLY check(t1, t2, MSG_RIGHT) if type2 == :tRCURLY end end |