Class: Rubocop::Cop::SpaceAroundOperators
- Inherits:
-
Cop
- Object
- Parser::AST::Processor
- Cop
- Rubocop::Cop::SpaceAroundOperators
show all
- Includes:
- SurroundingSpace
- Defined in:
- lib/rubocop/cop/surrounding_space.rb
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
#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, tokens, sexp, comments) ⇒ Object
#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, #on_comment
Instance Method Details
#check_missing_space(token_before, token, token_after) ⇒ Object
147
148
149
150
151
152
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 147
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.line, MSG_MISSING % text)
end
end
|
#do_not_check_block_arg_pipes(sexp, positions_not_to_check) ⇒ Object
87
88
89
90
91
92
93
94
95
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 87
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
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 107
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 118
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
97
98
99
100
101
102
103
104
105
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 97
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
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 135
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.
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 76
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
154
155
156
157
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 154
def has_space?(token_before, token, token_after)
space_between?(token_before, token) && space_between?(token,
token_after)
end
|
#inspect(source, tokens, sexp, comments) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/rubocop/cop/surrounding_space.rb', line 54
def inspect(source, tokens, 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 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.line, MSG_DETECTED)
end
when *BINARY_OPERATORS
check_missing_space(token_before, token, token_after)
end
end
end
|