Class: Rubocop::Cop::SpaceAroundBraces
- Includes:
- SurroundingSpace
- Defined in:
- lib/rubocop/cop/surrounding_space.rb
Constant Summary collapse
- MSG_LEFT =
"Surrounding space missing for '{'."
- MSG_RIGHT =
"Space missing to the left of '}'."
Instance Attribute Summary
Attributes inherited from Cop
#debug, #disabled_lines, #offences
Instance Method Summary collapse
- #check(t1, t2, msg) ⇒ Object
- #get_positions_not_to_check(tokens, sexp) ⇒ Object
- #inspect(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, cop_name, #has_report?, #ignore_node, inherited, #initialize, #name
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#check(t1, t2, msg) ⇒ Object
200 201 202 203 204 |
# File 'lib/rubocop/cop/surrounding_space.rb', line 200 def check(t1, t2, msg) unless space_between?(t1, t2) add_offence(:convention, t1.pos.line, msg) end end |
#get_positions_not_to_check(tokens, sexp) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rubocop/cop/surrounding_space.rb', line 179 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, tokens, sexp, comments) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rubocop/cop/surrounding_space.rb', line 165 def inspect(source, tokens, sexp, comments) @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 |