Class: Rubocop::Cop::Style::SpaceAroundOperators
- Inherits:
-
Cop
- Object
- Parser::Rewriter
- Cop
- Rubocop::Cop::Style::SpaceAroundOperators
show all
- Includes:
- SurroundingSpace
- Defined in:
- lib/rubocop/cop/style/surrounding_space.rb
Overview
Checks that operators have space around them, except for **
which should not have surrounding space.
Constant Summary
collapse
- MSG_MISSING =
"Surrounding space missing for operator '%s'."
- MSG_DETECTED =
'Space around operator ** detected.'
- BINARY_OPERATORS =
[:tEQL, :tAMPER2, :tPIPE, :tCARET, :tPLUS, :tMINUS, :tSTAR2,
:tDIVIDE, :tPERCENT, :tEH, :tCOLON, :tANDOP, :tOROP, :tMATCH,
:tNMATCH, :tEQ, :tNEQ, :tGT, :tRSHFT, :tGEQ, :tLT,
:tLSHFT, :tLEQ, :tASSOC, :tEQQ, :tCMP, :tOP_ASGN]
Instance Attribute Summary
Attributes inherited from Cop
#autocorrect, #debug, #disabled_lines, #offences
Instance Method Summary
collapse
-
#check_missing_space(token_before, token, token_after) ⇒ Object
-
#do_not_check_block_arg_pipes(sexp, positions_not_to_check) ⇒ Object
-
#do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check) ⇒ Object
-
#do_not_check_def_things(tokens, sexp, positions_not_to_check) ⇒ Object
-
#do_not_check_param_default(tokens, sexp, positions_not_to_check) ⇒ Object
-
#do_not_check_singleton_operator_defs(tokens, sexp, positions_not_to_check) ⇒ Object
-
#get_positions_not_to_check(tokens, sexp) ⇒ Object
Returns an array of positions marking the tokens that this cop should not check, either because the token is not an operator or because another cop does the check.
-
#has_space?(token_before, token, token_after) ⇒ Boolean
-
#inspect(source_buffer, source, tokens, sexp, comments) ⇒ Object
#build_token_table, #index_of_first_token, #index_of_last_token, #space_between?
Methods inherited from Cop
#add_offence, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?
Instance Method Details
#check_missing_space(token_before, token, token_after) ⇒ Object
156
157
158
159
160
161
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 156
def check_missing_space(token_before, token, token_after)
unless has_space?(token_before, token, token_after)
text = token.text.to_s + (token.type == :tOP_ASGN ? '=' : '')
add_offence(:convention, token.pos, MSG_MISSING % text)
end
end
|
#do_not_check_block_arg_pipes(sexp, positions_not_to_check) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 94
def do_not_check_block_arg_pipes(sexp, positions_not_to_check)
on_node(:block, sexp) do |b|
on_node(:args, b) do |a|
positions_not_to_check << a.loc.begin << a.loc.end if a.loc.begin
end
end
end
|
#do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 114
def do_not_check_class_lshift_self(tokens,
sexp,
positions_not_to_check)
on_node(:sclass, sexp) do |sclass|
ix = index_of_first_token(sclass, tokens)
if tokens[ix, 2].map(&:type) == [:kCLASS, :tLSHFT]
positions_not_to_check << tokens[ix + 1].pos
end
end
end
|
#do_not_check_def_things(tokens, sexp, positions_not_to_check) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 127
def do_not_check_def_things(tokens, sexp, positions_not_to_check)
on_node(:def, sexp) do |def_node|
on_node([:blockarg, :restarg], def_node) do |arg_node|
positions_not_to_check <<
tokens[index_of_first_token(arg_node, tokens)].pos
end
positions_not_to_check <<
tokens[index_of_first_token(def_node, tokens) + 1].pos
end
end
|
#do_not_check_param_default(tokens, sexp, positions_not_to_check) ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 104
def do_not_check_param_default(tokens, sexp, positions_not_to_check)
on_node(:optarg, sexp) do |optarg|
_arg, equals, _value = tokens[index_of_first_token(optarg, tokens),
3]
positions_not_to_check << equals.pos
end
end
|
#do_not_check_singleton_operator_defs(tokens, sexp, positions_not_to_check) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 144
def do_not_check_singleton_operator_defs(tokens, sexp,
positions_not_to_check)
on_node(:defs, sexp) do |defs_node|
_receiver, name, _args = *defs_node
ix = index_of_first_token(defs_node, tokens)
name_token = tokens[ix..-1].find { |t| t.text == name.to_s }
positions_not_to_check << name_token.pos
end
end
|
#get_positions_not_to_check(tokens, sexp) ⇒ Object
Returns an array of positions marking the tokens that this cop
should not check, either because the token is not an operator
or because another cop does the check.
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 83
def get_positions_not_to_check(tokens, sexp)
positions_not_to_check = []
do_not_check_block_arg_pipes(sexp, positions_not_to_check)
do_not_check_param_default(tokens, sexp, positions_not_to_check)
do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check)
do_not_check_def_things(tokens, sexp, positions_not_to_check)
do_not_check_singleton_operator_defs(tokens, sexp,
positions_not_to_check)
positions_not_to_check
end
|
#has_space?(token_before, token, token_after) ⇒ Boolean
163
164
165
166
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 163
def has_space?(token_before, token, token_after)
space_between?(token_before, token) && space_between?(token,
token_after)
end
|
#inspect(source_buffer, source, tokens, sexp, comments) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 58
def inspect(source_buffer, source, tokens, sexp, )
return unless sexp
@source = source
positions_not_to_check = get_positions_not_to_check(tokens, sexp)
tokens.each_cons(3) do |token_before, token, token_after|
next if token_before.type == :kDEF next if token_before.type == :tDOT next if positions_not_to_check.include?(token.pos)
case token.type
when :tPOW
if has_space?(token_before, token, token_after)
add_offence(:convention, token.pos, MSG_DETECTED)
end
when *BINARY_OPERATORS
check_missing_space(token_before, token, token_after)
end
end
end
|