code_analyzer 0.4.6 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +2 -2
- data/code_analyzer.gemspec +2 -1
- data/lib/code_analyzer.rb +10 -9
- data/lib/code_analyzer/analyzer_exception.rb +2 -1
- data/lib/code_analyzer/checker.rb +5 -8
- data/lib/code_analyzer/checking_visitor.rb +5 -4
- data/lib/code_analyzer/checking_visitor/base.rb +3 -2
- data/lib/code_analyzer/checking_visitor/default.rb +9 -12
- data/lib/code_analyzer/checking_visitor/plain.rb +3 -4
- data/lib/code_analyzer/nil.rb +2 -1
- data/lib/code_analyzer/sexp.rb +111 -112
- data/lib/code_analyzer/version.rb +3 -2
- data/lib/code_analyzer/warning.rb +3 -2
- data/spec/code_analyzer/checker_spec.rb +19 -17
- data/spec/code_analyzer/checking_visitor/base_spec.rb +2 -0
- data/spec/code_analyzer/checking_visitor/default_spec.rb +14 -12
- data/spec/code_analyzer/checking_visitor/plain_spec.rb +6 -4
- data/spec/code_analyzer/nil_spec.rb +12 -10
- data/spec/code_analyzer/sexp_spec.rb +463 -328
- data/spec/code_analyzer/warning_spec.rb +6 -6
- data/spec/spec_helper.rb +3 -1
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5a68561649e1e345495d4a494530dc7abb9ca256dc5a999d1aa71b1c567b0e66
|
4
|
+
data.tar.gz: d21414e33aa72bbb23571985b9eaa00e177057f7781cbb3f1398476119f38c69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99369336e083dd38d76e7436ee9d824565108760339d3da59b057649341917da4649e68c78f01a29ba45c09c95a2ca71c1b2392d9c2a252826cb81abb2580131
|
7
|
+
data.tar.gz: cbba861e5caa35dcd4a0e887159ae0a14bb166a21f2b0bb8b5e33f188c346f9b71b6b11bbee253e9ee2b18b4fc7be039801d438168d43d00b4f007b0541698d8
|
data/.travis.yml
CHANGED
data/code_analyzer.gemspec
CHANGED
data/lib/code_analyzer.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ripper'
|
4
|
+
require 'code_analyzer/version'
|
5
|
+
require 'code_analyzer/nil'
|
6
|
+
require 'code_analyzer/sexp'
|
6
7
|
|
7
8
|
module CodeAnalyzer
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
autoload :AnalyzerException, 'code_analyzer/analyzer_exception'
|
10
|
+
autoload :Checker, 'code_analyzer/checker'
|
11
|
+
autoload :CheckingVisitor, 'code_analyzer/checking_visitor'
|
12
|
+
autoload :Warning, 'code_analyzer/warning'
|
12
13
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CodeAnalyzer
|
3
4
|
# A checker class that takes charge of checking the sexp.
|
4
5
|
class Checker
|
@@ -28,9 +29,7 @@ module CodeAnalyzer
|
|
28
29
|
# @param [Sexp] node
|
29
30
|
def node_start(node)
|
30
31
|
@node = node
|
31
|
-
self.class.get_callbacks("start_#{node.sexp_type}".to_sym).each
|
32
|
-
self.instance_exec(node, &block)
|
33
|
-
end
|
32
|
+
self.class.get_callbacks("start_#{node.sexp_type}".to_sym).each { |block| self.instance_exec(node, &block) }
|
34
33
|
end
|
35
34
|
|
36
35
|
# delegate to end_### according to the sexp_type, like
|
@@ -41,9 +40,7 @@ module CodeAnalyzer
|
|
41
40
|
# @param [Sexp] node
|
42
41
|
def node_end(node)
|
43
42
|
@node = node
|
44
|
-
self.class.get_callbacks("end_#{node.sexp_type}".to_sym).each
|
45
|
-
self.instance_exec(node, &block)
|
46
|
-
end
|
43
|
+
self.class.get_callbacks("end_#{node.sexp_type}".to_sym).each { |block| self.instance_exec(node, &block) }
|
47
44
|
end
|
48
45
|
|
49
46
|
# add an warning.
|
@@ -60,7 +57,7 @@ module CodeAnalyzer
|
|
60
57
|
@warnings ||= []
|
61
58
|
end
|
62
59
|
|
63
|
-
class <<self
|
60
|
+
class << self
|
64
61
|
def interesting_nodes(*nodes)
|
65
62
|
@interesting_nodes ||= []
|
66
63
|
@interesting_nodes += nodes
|
@@ -1,8 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CodeAnalyzer
|
3
4
|
module CheckingVisitor
|
4
|
-
autoload :Base,
|
5
|
-
autoload :Plain,
|
6
|
-
autoload :Default,
|
5
|
+
autoload :Base, 'code_analyzer/checking_visitor/base'
|
6
|
+
autoload :Plain, 'code_analyzer/checking_visitor/plain'
|
7
|
+
autoload :Default, 'code_analyzer/checking_visitor/default'
|
7
8
|
end
|
8
9
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CodeAnalyzer::CheckingVisitor
|
3
4
|
# This is the default checking visitor to check ruby sexp nodes.
|
4
5
|
class Default < Base
|
5
|
-
def initialize(options={})
|
6
|
+
def initialize(options = {})
|
6
7
|
super
|
7
8
|
@checks = {}
|
8
9
|
@checkers.each do |checker|
|
@@ -28,9 +29,7 @@ module CodeAnalyzer::CheckingVisitor
|
|
28
29
|
def after_check
|
29
30
|
@checkers.each do |checker|
|
30
31
|
after_check_callbacks = checker.class.get_callbacks(:after_check)
|
31
|
-
after_check_callbacks.each
|
32
|
-
checker.instance_exec &block
|
33
|
-
end
|
32
|
+
after_check_callbacks.each { |block| checker.instance_exec &block }
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
@@ -51,16 +50,14 @@ module CodeAnalyzer::CheckingVisitor
|
|
51
50
|
# 3. it triggers the interesting checkers' end callbacks.
|
52
51
|
def check_node(node)
|
53
52
|
checkers = @checks[node.sexp_type]
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
node.children.each { |child_node|
|
53
|
+
|
54
|
+
checkers.each { |checker| checker.node_start(node) if checker.parse_file?(node.file) } if checkers
|
55
|
+
node.children.each do |child_node|
|
58
56
|
child_node.file = node.file
|
59
57
|
check_node(child_node)
|
60
|
-
}
|
61
|
-
if checkers
|
62
|
-
checkers.each { |checker| checker.node_end(node) if checker.parse_file?(node.file) }
|
63
58
|
end
|
59
|
+
|
60
|
+
checkers.each { |checker| checker.node_end(node) if checker.parse_file?(node.file) } if checkers
|
64
61
|
end
|
65
62
|
end
|
66
63
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CodeAnalyzer::CheckingVisitor
|
3
4
|
# This is the checking visitor to check ruby plain code.
|
4
5
|
class Plain < Base
|
@@ -7,9 +8,7 @@ module CodeAnalyzer::CheckingVisitor
|
|
7
8
|
# @param [String] filename is the filename of ruby code.
|
8
9
|
# @param [String] content is the content of ruby file.
|
9
10
|
def check(filename, content)
|
10
|
-
@checkers.each
|
11
|
-
checker.check(filename, content) if checker.parse_file?(filename)
|
12
|
-
end
|
11
|
+
@checkers.each { |checker| checker.check(filename, content) if checker.parse_file?(filename) }
|
13
12
|
end
|
14
13
|
end
|
15
14
|
end
|
data/lib/code_analyzer/nil.rb
CHANGED
data/lib/code_analyzer/sexp.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'sexp'
|
3
4
|
|
4
5
|
class Sexp
|
@@ -13,22 +14,23 @@ class Sexp
|
|
13
14
|
#
|
14
15
|
# s(:@ident, "test", s(2, 12)
|
15
16
|
# => 2
|
17
|
+
|
16
18
|
def line_number
|
17
19
|
case sexp_type
|
18
|
-
when :def, :defs, :command, :command_call, :call, :fcall, :method_add_arg, :method_add_block,
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:alias, :symbol_literal, :symbol, :aref, :hash, :assoc_new, :string_literal,
|
22
|
-
:massign
|
20
|
+
when :def, :defs, :command, :command_call, :call, :fcall, :method_add_arg, :method_add_block, :var_ref, :vcall,
|
21
|
+
:const_ref, :top_const_ref, :const_path_ref, :class, :module, :if, :unless, :elsif, :ifop, :if_mod, :unless_mod, :binary, :alias,
|
22
|
+
:symbol_literal, :symbol, :aref, :hash, :assoc_new, :string_literal, :massign, :var_field, :assign, :paren, :dot2, :dot3
|
23
23
|
self[1].line_number
|
24
24
|
when :assoclist_from_args, :bare_assoc_hash
|
25
25
|
self[1][0].line_number
|
26
|
-
when :string_add, :opassign
|
26
|
+
when :string_add, :opassign, :unary, :stmts_add
|
27
27
|
self[2].line_number
|
28
28
|
when :array
|
29
29
|
array_values.first.line_number
|
30
30
|
when :mlhs_add
|
31
31
|
self.last.line_number
|
32
|
+
when :params
|
33
|
+
self[1][0].line_number if self[1].is_a? Array
|
32
34
|
else
|
33
35
|
self.last.first if self.last.is_a? Array
|
34
36
|
end
|
@@ -38,7 +40,7 @@ class Sexp
|
|
38
40
|
#
|
39
41
|
# @return [Array] child nodes.
|
40
42
|
def children
|
41
|
-
find_all { |
|
43
|
+
find_all { |sexp| Sexp === sexp }
|
42
44
|
end
|
43
45
|
|
44
46
|
# recursively find all child nodes, and yeild each child node.
|
@@ -68,10 +70,17 @@ class Sexp
|
|
68
70
|
message = options[:message]
|
69
71
|
to_s = options[:to_s]
|
70
72
|
self.recursive_children do |child|
|
71
|
-
if (
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
if (
|
74
|
+
!sexp_type || (sexp_type.is_a?(Array) ? sexp_type.include?(child.sexp_type) : sexp_type == child.sexp_type)
|
75
|
+
) &&
|
76
|
+
(
|
77
|
+
!receiver ||
|
78
|
+
(receiver.is_a?(Array) ? receiver.include?(child.receiver.to_s) : receiver == child.receiver.to_s)
|
79
|
+
) &&
|
80
|
+
(
|
81
|
+
!message || (message.is_a?(Array) ? message.include?(child.message.to_s) : message == child.message.to_s)
|
82
|
+
) &&
|
83
|
+
(!to_s || (to_s.is_a?(Array) ? to_s.include?(child.to_s) : to_s == child.to_s))
|
75
84
|
yield child
|
76
85
|
end
|
77
86
|
end
|
@@ -91,7 +100,10 @@ class Sexp
|
|
91
100
|
# the condition value can be Symbol, Array or Sexp.
|
92
101
|
def grep_node(options)
|
93
102
|
result = CodeAnalyzer::Nil.new
|
94
|
-
grep_nodes(options)
|
103
|
+
grep_nodes(options) do |node|
|
104
|
+
result = node
|
105
|
+
break
|
106
|
+
end
|
95
107
|
result
|
96
108
|
end
|
97
109
|
|
@@ -138,9 +150,7 @@ class Sexp
|
|
138
150
|
#
|
139
151
|
# @return [Sexp] module name node
|
140
152
|
def module_name
|
141
|
-
if :module == sexp_type
|
142
|
-
self[1]
|
143
|
-
end
|
153
|
+
self[1] if :module == sexp_type
|
144
154
|
end
|
145
155
|
|
146
156
|
# Get the class name of the class node.
|
@@ -154,9 +164,7 @@ class Sexp
|
|
154
164
|
#
|
155
165
|
# @return [Sexp] class name node
|
156
166
|
def class_name
|
157
|
-
if :class == sexp_type
|
158
|
-
self[1]
|
159
|
-
end
|
167
|
+
self[1] if :class == sexp_type
|
160
168
|
end
|
161
169
|
|
162
170
|
# Get the base class of the class node.
|
@@ -170,9 +178,7 @@ class Sexp
|
|
170
178
|
#
|
171
179
|
# @return [Sexp] base class of class node
|
172
180
|
def base_class
|
173
|
-
if :class == sexp_type
|
174
|
-
self[2]
|
175
|
-
end
|
181
|
+
self[2] if :class == sexp_type
|
176
182
|
end
|
177
183
|
|
178
184
|
# Get the left value of the assign node.
|
@@ -185,9 +191,7 @@ class Sexp
|
|
185
191
|
#
|
186
192
|
# @return [Symbol] left value of lasgn or iasgn node
|
187
193
|
def left_value
|
188
|
-
if :assign == sexp_type
|
189
|
-
self[1]
|
190
|
-
end
|
194
|
+
self[1] if :assign == sexp_type
|
191
195
|
end
|
192
196
|
|
193
197
|
# Get the right value of assign node.
|
@@ -200,9 +204,7 @@ class Sexp
|
|
200
204
|
#
|
201
205
|
# @return [Sexp] right value of assign node
|
202
206
|
def right_value
|
203
|
-
if :assign == sexp_type
|
204
|
-
self[2]
|
205
|
-
end
|
207
|
+
self[2] if :assign == sexp_type
|
206
208
|
end
|
207
209
|
|
208
210
|
# Get the message node.
|
@@ -277,9 +279,7 @@ class Sexp
|
|
277
279
|
#
|
278
280
|
# @return [Sexp] argument node
|
279
281
|
def argument
|
280
|
-
if :binary == sexp_type
|
281
|
-
self[3]
|
282
|
-
end
|
282
|
+
self[3] if :binary == sexp_type
|
283
283
|
end
|
284
284
|
|
285
285
|
# Get all arguments.
|
@@ -305,7 +305,7 @@ class Sexp
|
|
305
305
|
else
|
306
306
|
node = self[1]
|
307
307
|
while true
|
308
|
-
if [
|
308
|
+
if %i[args_add args_add_star].include? node.sexp_type
|
309
309
|
nodes.unshift node[2]
|
310
310
|
node = node[1]
|
311
311
|
elsif :args_new == node.sexp_type
|
@@ -330,9 +330,7 @@ class Sexp
|
|
330
330
|
#
|
331
331
|
# @return [Sexp] conditional statement of if node
|
332
332
|
def conditional_statement
|
333
|
-
if [
|
334
|
-
self[1]
|
335
|
-
end
|
333
|
+
self[1] if %i[if unless elsif ifop if_mod unless_mod].include? sexp_type
|
336
334
|
end
|
337
335
|
|
338
336
|
# Get all condition nodes.
|
@@ -366,13 +364,13 @@ class Sexp
|
|
366
364
|
# @return [Array] all condition nodes
|
367
365
|
def all_conditions
|
368
366
|
nodes = []
|
369
|
-
if :binary == sexp_type && %w
|
370
|
-
if :binary == self[1].sexp_type && %w
|
367
|
+
if :binary == sexp_type && %w[&& || and or].include?(self[2].to_s)
|
368
|
+
if :binary == self[1].sexp_type && %w[&& || and or].include?(self[1][2].to_s)
|
371
369
|
nodes += self[1].all_conditions
|
372
370
|
else
|
373
371
|
nodes << self[1]
|
374
372
|
end
|
375
|
-
if :binary == self[3].sexp_type && %w
|
373
|
+
if :binary == self[3].sexp_type && %w[&& || and or].include?(self[3][2].to_s)
|
376
374
|
nodes += self[3].all_conditions
|
377
375
|
else
|
378
376
|
nodes << self[3]
|
@@ -392,6 +390,7 @@ class Sexp
|
|
392
390
|
# => s(:@ident, "show", s(1, 4)),
|
393
391
|
#
|
394
392
|
# @return [Sexp] method name node
|
393
|
+
|
395
394
|
def method_name
|
396
395
|
case sexp_type
|
397
396
|
when :def
|
@@ -399,6 +398,7 @@ class Sexp
|
|
399
398
|
when :defs
|
400
399
|
self[3]
|
401
400
|
else
|
401
|
+
|
402
402
|
end
|
403
403
|
end
|
404
404
|
|
@@ -428,6 +428,7 @@ class Sexp
|
|
428
428
|
# )
|
429
429
|
#
|
430
430
|
# @return [Sexp] body node
|
431
|
+
|
431
432
|
def body
|
432
433
|
case sexp_type
|
433
434
|
when :else
|
@@ -506,13 +507,15 @@ class Sexp
|
|
506
507
|
# @return [Array] all statements
|
507
508
|
def statements
|
508
509
|
stmts = []
|
509
|
-
node =
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
510
|
+
node =
|
511
|
+
case sexp_type
|
512
|
+
when :do_block, :brace_block
|
513
|
+
self[2][1]
|
514
|
+
when :bodystmt
|
515
|
+
self[1]
|
516
|
+
else
|
517
|
+
|
518
|
+
end
|
516
519
|
if node
|
517
520
|
while true
|
518
521
|
if :stmts_add == node.sexp_type && s(:void_stmt) != node[2]
|
@@ -542,6 +545,7 @@ class Sexp
|
|
542
545
|
def exception_classes
|
543
546
|
if :rescue == sexp_type
|
544
547
|
return [] unless self[1]
|
548
|
+
|
545
549
|
if :mrhs_add == self[1].sexp_type
|
546
550
|
exceptions = Array.new(self[1][2])
|
547
551
|
arg_nodes = self[1][1][1]
|
@@ -566,9 +570,7 @@ class Sexp
|
|
566
570
|
# )
|
567
571
|
# => s(:var_field, s(:@ident, "e", s(1, 20)))
|
568
572
|
def exception_variable
|
569
|
-
if :rescue == sexp_type
|
570
|
-
self[2]
|
571
|
-
end
|
573
|
+
self[2] if :rescue == sexp_type
|
572
574
|
end
|
573
575
|
|
574
576
|
# Get hash value node.
|
@@ -585,20 +587,17 @@ class Sexp
|
|
585
587
|
#
|
586
588
|
# @return [Sexp] hash value node
|
587
589
|
def hash_value(key)
|
588
|
-
pair_nodes =
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
pair_nodes.size.times do |i|
|
597
|
-
if key == pair_nodes[i][1].to_s
|
598
|
-
return pair_nodes[i][2]
|
599
|
-
end
|
590
|
+
pair_nodes =
|
591
|
+
case sexp_type
|
592
|
+
when :bare_assoc_hash
|
593
|
+
self[1]
|
594
|
+
when :hash
|
595
|
+
self[1][1]
|
596
|
+
else
|
597
|
+
|
600
598
|
end
|
601
|
-
|
599
|
+
|
600
|
+
pair_nodes.size.times { |i| return pair_nodes[i][2] if key == pair_nodes[i][1].to_s } if pair_nodes
|
602
601
|
CodeAnalyzer::Nil.new
|
603
602
|
end
|
604
603
|
|
@@ -640,18 +639,18 @@ class Sexp
|
|
640
639
|
#
|
641
640
|
# @return [Array] hash keys
|
642
641
|
def hash_keys
|
643
|
-
pair_nodes =
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
642
|
+
pair_nodes =
|
643
|
+
case sexp_type
|
644
|
+
when :bare_assoc_hash
|
645
|
+
self[1]
|
646
|
+
when :hash
|
647
|
+
self[1][1]
|
648
|
+
else
|
649
|
+
|
650
|
+
end
|
650
651
|
if pair_nodes
|
651
652
|
keys = []
|
652
|
-
pair_nodes.size.times
|
653
|
-
keys << pair_nodes[i][1].to_s
|
654
|
-
end
|
653
|
+
pair_nodes.size.times { |i| keys << pair_nodes[i][1].to_s }
|
655
654
|
keys
|
656
655
|
end
|
657
656
|
end
|
@@ -673,18 +672,18 @@ class Sexp
|
|
673
672
|
#
|
674
673
|
# @return [Array] hash values
|
675
674
|
def hash_values
|
676
|
-
pair_nodes =
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
675
|
+
pair_nodes =
|
676
|
+
case sexp_type
|
677
|
+
when :bare_assoc_hash
|
678
|
+
self[1]
|
679
|
+
when :hash
|
680
|
+
self[1][1]
|
681
|
+
else
|
682
|
+
|
683
|
+
end
|
683
684
|
if pair_nodes
|
684
685
|
values = []
|
685
|
-
pair_nodes.size.times
|
686
|
-
values << pair_nodes[i][2]
|
687
|
-
end
|
686
|
+
pair_nodes.size.times { |i| values << pair_nodes[i][2] }
|
688
687
|
values
|
689
688
|
end
|
690
689
|
end
|
@@ -709,9 +708,7 @@ class Sexp
|
|
709
708
|
array_size += 1
|
710
709
|
first_node = s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
|
711
710
|
if :args_add != first_node.sexp_type
|
712
|
-
if :array == first_node.sexp_type
|
713
|
-
array_size += first_node.array_size
|
714
|
-
end
|
711
|
+
array_size += first_node.array_size if :array == first_node.sexp_type
|
715
712
|
break
|
716
713
|
end
|
717
714
|
end
|
@@ -737,21 +734,21 @@ class Sexp
|
|
737
734
|
def array_values
|
738
735
|
case sexp_type
|
739
736
|
when :array
|
740
|
-
if nil == self[1]
|
737
|
+
if nil == self[1] || %i[words_new qwords_new symbols_new qsymbols_new].include?(self[1].sexp_type)
|
741
738
|
[]
|
742
|
-
elsif [
|
739
|
+
elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1].sexp_type
|
743
740
|
self[1].array_values
|
744
741
|
else
|
745
742
|
arguments.all
|
746
743
|
end
|
747
|
-
when :words_add, :qwords_add, :qsymbols_add
|
744
|
+
when :words_add, :qwords_add, :symbols_add, :qsymbols_add
|
748
745
|
values = []
|
749
746
|
node = self
|
750
747
|
while true
|
751
|
-
if [
|
748
|
+
if %i[words_add qwords_add symbols_add qsymbols_add].include? node.sexp_type
|
752
749
|
values.unshift node[2]
|
753
750
|
node = node[1]
|
754
|
-
elsif [
|
751
|
+
elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node.sexp_type
|
755
752
|
break
|
756
753
|
end
|
757
754
|
end
|
@@ -808,22 +805,13 @@ class Sexp
|
|
808
805
|
# @return [String] to_s
|
809
806
|
def to_s
|
810
807
|
case sexp_type
|
811
|
-
when :string_literal, :xstring_literal, :string_content, :const_ref, :symbol_literal, :symbol,
|
812
|
-
:
|
813
|
-
:@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar
|
808
|
+
when :string_literal, :xstring_literal, :string_content, :const_ref, :symbol_literal, :symbol, :args_add_block, :var_ref,
|
809
|
+
:vcall, :var_field, :@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar, :@period
|
814
810
|
self[1].to_s
|
815
811
|
when :string_add
|
816
|
-
|
817
|
-
self[2].to_s
|
818
|
-
else
|
819
|
-
self[1].to_s
|
820
|
-
end
|
812
|
+
s(:string_content) == self[1] ? self[2].to_s : self[1].to_s
|
821
813
|
when :args_add
|
822
|
-
|
823
|
-
self[2].to_s
|
824
|
-
else
|
825
|
-
self[1].to_s
|
826
|
-
end
|
814
|
+
s(:args_new) == self[1] ? self[2].to_s : self[1].to_s
|
827
815
|
when :qwords_add
|
828
816
|
self[2].to_s
|
829
817
|
when :word_add
|
@@ -839,13 +827,13 @@ class Sexp
|
|
839
827
|
when :top_const_ref
|
840
828
|
"::#{self[1]}"
|
841
829
|
else
|
842
|
-
|
830
|
+
''
|
843
831
|
end
|
844
832
|
end
|
845
833
|
|
846
834
|
# check if the self node is a const.
|
847
835
|
def const?
|
848
|
-
:@const == self.sexp_type || ([
|
836
|
+
:@const == self.sexp_type || (%i[var_ref vcall].include?(self.sexp_type) && :@const == self[1].sexp_type)
|
849
837
|
end
|
850
838
|
|
851
839
|
# true
|
@@ -865,22 +853,33 @@ class Sexp
|
|
865
853
|
if Sexp === last_node && last_node.size == 2 && last_node.first.is_a?(Integer) && last_node.last.is_a?(Integer)
|
866
854
|
node.delete_at(-1)
|
867
855
|
end
|
868
|
-
node.sexp_body.each_with_index
|
869
|
-
if Sexp === child
|
870
|
-
node[index+1] = child.remove_line_and_column
|
871
|
-
end
|
872
|
-
end
|
856
|
+
node.sexp_body.each_with_index { |child, index| node[index + 1] = child.remove_line_and_column if Sexp === child }
|
873
857
|
node
|
874
858
|
end
|
875
859
|
|
876
860
|
# if the return value of these methods is nil, then return CodeAnalyzer::Nil.new instead
|
877
|
-
[
|
878
|
-
|
861
|
+
%i[
|
862
|
+
sexp_type
|
863
|
+
receiver
|
864
|
+
message
|
865
|
+
arguments
|
866
|
+
argument
|
867
|
+
class_name
|
868
|
+
base_class
|
869
|
+
method_name
|
870
|
+
body
|
871
|
+
block_node
|
872
|
+
conditional_statement
|
873
|
+
left_value
|
874
|
+
right_value
|
875
|
+
].each do |method|
|
879
876
|
class_eval <<-EOS
|
880
877
|
alias_method :origin_#{method}, :#{method}
|
881
878
|
|
882
879
|
def #{method}
|
883
|
-
ret = origin_#{
|
880
|
+
ret = origin_#{
|
881
|
+
method
|
882
|
+
}
|
884
883
|
ret.nil? ? CodeAnalyzer::Nil.new : ret
|
885
884
|
end
|
886
885
|
EOS
|