code_analyzer 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/code_analyzer.gemspec +2 -1
- data/lib/code_analyzer.rb +2 -1
- data/lib/code_analyzer/analyzer_exception.rb +2 -1
- data/lib/code_analyzer/checker.rb +6 -13
- data/lib/code_analyzer/checking_visitor.rb +2 -1
- data/lib/code_analyzer/checking_visitor/base.rb +2 -1
- data/lib/code_analyzer/checking_visitor/default.rb +7 -14
- data/lib/code_analyzer/checking_visitor/plain.rb +3 -4
- data/lib/code_analyzer/nil.rb +2 -1
- data/lib/code_analyzer/sexp.rb +37 -85
- data/lib/code_analyzer/version.rb +3 -2
- data/lib/code_analyzer/warning.rb +2 -1
- data/spec/code_analyzer/checker_spec.rb +4 -6
- data/spec/code_analyzer/checking_visitor/base_spec.rb +2 -0
- data/spec/code_analyzer/checking_visitor/default_spec.rb +2 -0
- data/spec/code_analyzer/checking_visitor/plain_spec.rb +2 -0
- data/spec/code_analyzer/nil_spec.rb +2 -0
- data/spec/code_analyzer/sexp_spec.rb +152 -189
- data/spec/code_analyzer/warning_spec.rb +3 -6
- data/spec/spec_helper.rb +2 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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,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,8 +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)
|
32
|
-
.each { |block| self.instance_exec(node, &block) }
|
32
|
+
self.class.get_callbacks("start_#{node.sexp_type}".to_sym).each { |block| self.instance_exec(node, &block) }
|
33
33
|
end
|
34
34
|
|
35
35
|
# delegate to end_### according to the sexp_type, like
|
@@ -40,9 +40,7 @@ module CodeAnalyzer
|
|
40
40
|
# @param [Sexp] node
|
41
41
|
def node_end(node)
|
42
42
|
@node = node
|
43
|
-
self.class.get_callbacks("end_#{node.sexp_type}".to_sym).each
|
44
|
-
self.instance_exec(node, &block)
|
45
|
-
end
|
43
|
+
self.class.get_callbacks("end_#{node.sexp_type}".to_sym).each { |block| self.instance_exec(node, &block) }
|
46
44
|
end
|
47
45
|
|
48
46
|
# add an warning.
|
@@ -50,13 +48,8 @@ module CodeAnalyzer
|
|
50
48
|
# @param [String] message, is the warning message
|
51
49
|
# @param [String] filename, is the filename of source code
|
52
50
|
# @param [Integer] line_number, is the line number of the source code which is reviewing
|
53
|
-
def add_warning(
|
54
|
-
|
55
|
-
)
|
56
|
-
warnings <<
|
57
|
-
Warning.new(
|
58
|
-
filename: filename, line_number: line_number, message: message
|
59
|
-
)
|
51
|
+
def add_warning(message, filename = @node.file, line_number = @node.line_number)
|
52
|
+
warnings << Warning.new(filename: filename, line_number: line_number, message: message)
|
60
53
|
end
|
61
54
|
|
62
55
|
# all warnings.
|
@@ -1,4 +1,5 @@
|
|
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
|
@@ -39,9 +40,7 @@ module CodeAnalyzer::CheckingVisitor
|
|
39
40
|
def parse(filename, content)
|
40
41
|
Sexp.from_array(Ripper::SexpBuilder.new(content).parse)
|
41
42
|
rescue Exception
|
42
|
-
raise AnalyzerException.new(
|
43
|
-
"#{filename} looks like it's not a valid Ruby file. Skipping..."
|
44
|
-
)
|
43
|
+
raise AnalyzerException.new("#{filename} looks like it's not a valid Ruby file. Skipping...")
|
45
44
|
end
|
46
45
|
|
47
46
|
# recursively check ruby sexp node.
|
@@ -51,20 +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
|
-
checker.node_start(node) if checker.parse_file?(node.file)
|
57
|
-
end
|
58
|
-
end
|
53
|
+
|
54
|
+
checkers.each { |checker| checker.node_start(node) if checker.parse_file?(node.file) } if checkers
|
59
55
|
node.children.each do |child_node|
|
60
56
|
child_node.file = node.file
|
61
57
|
check_node(child_node)
|
62
58
|
end
|
63
|
-
|
64
|
-
|
65
|
-
checker.node_end(node) if checker.parse_file?(node.file)
|
66
|
-
end
|
67
|
-
end
|
59
|
+
|
60
|
+
checkers.each { |checker| checker.node_end(node) if checker.parse_file?(node.file) } if checkers
|
68
61
|
end
|
69
62
|
end
|
70
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,21 +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
|
-
:aref, :hash, :assoc_new, :string_literal, :massign, :var_field
|
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
|
22
23
|
self[1].line_number
|
23
24
|
when :assoclist_from_args, :bare_assoc_hash
|
24
25
|
self[1][0].line_number
|
25
|
-
when :string_add, :opassign
|
26
|
+
when :string_add, :opassign, :unary, :stmts_add
|
26
27
|
self[2].line_number
|
27
28
|
when :array
|
28
29
|
array_values.first.line_number
|
29
30
|
when :mlhs_add
|
30
31
|
self.last.line_number
|
32
|
+
when :params
|
33
|
+
self[1][0].line_number if self[1].is_a? Array
|
31
34
|
else
|
32
35
|
self.last.first if self.last.is_a? Array
|
33
36
|
end
|
@@ -68,45 +71,16 @@ class Sexp
|
|
68
71
|
to_s = options[:to_s]
|
69
72
|
self.recursive_children do |child|
|
70
73
|
if (
|
71
|
-
|
72
|
-
(
|
73
|
-
if sexp_type.is_a?(Array)
|
74
|
-
sexp_type.include?(child.sexp_type)
|
75
|
-
else
|
76
|
-
sexp_type == child.sexp_type
|
77
|
-
end
|
78
|
-
)
|
79
|
-
) &&
|
80
|
-
(
|
81
|
-
!receiver ||
|
82
|
-
(
|
83
|
-
if receiver.is_a?(Array)
|
84
|
-
receiver.include?(child.receiver.to_s)
|
85
|
-
else
|
86
|
-
receiver == child.receiver.to_s
|
87
|
-
end
|
88
|
-
)
|
89
|
-
) &&
|
90
|
-
(
|
91
|
-
!message ||
|
92
|
-
(
|
93
|
-
if message.is_a?(Array)
|
94
|
-
message.include?(child.message.to_s)
|
95
|
-
else
|
96
|
-
message == child.message.to_s
|
97
|
-
end
|
98
|
-
)
|
74
|
+
!sexp_type || (sexp_type.is_a?(Array) ? sexp_type.include?(child.sexp_type) : sexp_type == child.sexp_type)
|
99
75
|
) &&
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
)
|
109
|
-
)
|
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))
|
110
84
|
yield child
|
111
85
|
end
|
112
86
|
end
|
@@ -391,14 +365,12 @@ class Sexp
|
|
391
365
|
def all_conditions
|
392
366
|
nodes = []
|
393
367
|
if :binary == sexp_type && %w[&& || and or].include?(self[2].to_s)
|
394
|
-
if :binary == self[1].sexp_type &&
|
395
|
-
%w[&& || and or].include?(self[1][2].to_s)
|
368
|
+
if :binary == self[1].sexp_type && %w[&& || and or].include?(self[1][2].to_s)
|
396
369
|
nodes += self[1].all_conditions
|
397
370
|
else
|
398
371
|
nodes << self[1]
|
399
372
|
end
|
400
|
-
if :binary == self[3].sexp_type &&
|
401
|
-
%w[&& || and or].include?(self[3][2].to_s)
|
373
|
+
if :binary == self[3].sexp_type && %w[&& || and or].include?(self[3][2].to_s)
|
402
374
|
nodes += self[3].all_conditions
|
403
375
|
else
|
404
376
|
nodes << self[3]
|
@@ -418,6 +390,7 @@ class Sexp
|
|
418
390
|
# => s(:@ident, "show", s(1, 4)),
|
419
391
|
#
|
420
392
|
# @return [Sexp] method name node
|
393
|
+
|
421
394
|
def method_name
|
422
395
|
case sexp_type
|
423
396
|
when :def
|
@@ -455,6 +428,7 @@ class Sexp
|
|
455
428
|
# )
|
456
429
|
#
|
457
430
|
# @return [Sexp] body node
|
431
|
+
|
458
432
|
def body
|
459
433
|
case sexp_type
|
460
434
|
when :else
|
@@ -571,6 +545,7 @@ class Sexp
|
|
571
545
|
def exception_classes
|
572
546
|
if :rescue == sexp_type
|
573
547
|
return [] unless self[1]
|
548
|
+
|
574
549
|
if :mrhs_add == self[1].sexp_type
|
575
550
|
exceptions = Array.new(self[1][2])
|
576
551
|
arg_nodes = self[1][1][1]
|
@@ -621,11 +596,8 @@ class Sexp
|
|
621
596
|
else
|
622
597
|
|
623
598
|
end
|
624
|
-
|
625
|
-
|
626
|
-
return pair_nodes[i][2] if key == pair_nodes[i][1].to_s
|
627
|
-
end
|
628
|
-
end
|
599
|
+
|
600
|
+
pair_nodes.size.times { |i| return pair_nodes[i][2] if key == pair_nodes[i][1].to_s } if pair_nodes
|
629
601
|
CodeAnalyzer::Nil.new
|
630
602
|
end
|
631
603
|
|
@@ -734,12 +706,9 @@ class Sexp
|
|
734
706
|
if first_node
|
735
707
|
while true
|
736
708
|
array_size += 1
|
737
|
-
first_node =
|
738
|
-
s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
|
709
|
+
first_node = s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
|
739
710
|
if :args_add != first_node.sexp_type
|
740
|
-
if :array == first_node.sexp_type
|
741
|
-
array_size += first_node.array_size
|
742
|
-
end
|
711
|
+
array_size += first_node.array_size if :array == first_node.sexp_type
|
743
712
|
break
|
744
713
|
end
|
745
714
|
end
|
@@ -765,13 +734,9 @@ class Sexp
|
|
765
734
|
def array_values
|
766
735
|
case sexp_type
|
767
736
|
when :array
|
768
|
-
if nil == self[1] ||
|
769
|
-
%i[words_new qwords_new symbols_new qsymbols_new].include?(
|
770
|
-
self[1].sexp_type
|
771
|
-
)
|
737
|
+
if nil == self[1] || %i[words_new qwords_new symbols_new qsymbols_new].include?(self[1].sexp_type)
|
772
738
|
[]
|
773
|
-
elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1]
|
774
|
-
.sexp_type
|
739
|
+
elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1].sexp_type
|
775
740
|
self[1].array_values
|
776
741
|
else
|
777
742
|
arguments.all
|
@@ -780,12 +745,10 @@ class Sexp
|
|
780
745
|
values = []
|
781
746
|
node = self
|
782
747
|
while true
|
783
|
-
if %i[words_add qwords_add symbols_add qsymbols_add].include? node
|
784
|
-
.sexp_type
|
748
|
+
if %i[words_add qwords_add symbols_add qsymbols_add].include? node.sexp_type
|
785
749
|
values.unshift node[2]
|
786
750
|
node = node[1]
|
787
|
-
elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node
|
788
|
-
.sexp_type
|
751
|
+
elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node.sexp_type
|
789
752
|
break
|
790
753
|
end
|
791
754
|
end
|
@@ -842,9 +805,8 @@ class Sexp
|
|
842
805
|
# @return [String] to_s
|
843
806
|
def to_s
|
844
807
|
case sexp_type
|
845
|
-
when :string_literal, :xstring_literal, :string_content, :const_ref,
|
846
|
-
:
|
847
|
-
:@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar, :@period
|
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
|
848
810
|
self[1].to_s
|
849
811
|
when :string_add
|
850
812
|
s(:string_content) == self[1] ? self[2].to_s : self[1].to_s
|
@@ -871,11 +833,7 @@ class Sexp
|
|
871
833
|
|
872
834
|
# check if the self node is a const.
|
873
835
|
def const?
|
874
|
-
:@const == self.sexp_type ||
|
875
|
-
(
|
876
|
-
%i[var_ref vcall].include?(self.sexp_type) &&
|
877
|
-
:@const == self[1].sexp_type
|
878
|
-
)
|
836
|
+
:@const == self.sexp_type || (%i[var_ref vcall].include?(self.sexp_type) && :@const == self[1].sexp_type)
|
879
837
|
end
|
880
838
|
|
881
839
|
# true
|
@@ -892,14 +850,10 @@ class Sexp
|
|
892
850
|
def remove_line_and_column
|
893
851
|
node = self.clone
|
894
852
|
last_node = node.last
|
895
|
-
if Sexp === last_node && last_node.size == 2 &&
|
896
|
-
last_node.first.is_a?(Integer) &&
|
897
|
-
last_node.last.is_a?(Integer)
|
853
|
+
if Sexp === last_node && last_node.size == 2 && last_node.first.is_a?(Integer) && last_node.last.is_a?(Integer)
|
898
854
|
node.delete_at(-1)
|
899
855
|
end
|
900
|
-
node.sexp_body.each_with_index
|
901
|
-
node[index + 1] = child.remove_line_and_column if Sexp === child
|
902
|
-
end
|
856
|
+
node.sexp_body.each_with_index { |child, index| node[index + 1] = child.remove_line_and_column if Sexp === child }
|
903
857
|
node
|
904
858
|
end
|
905
859
|
|
@@ -922,9 +876,7 @@ class Sexp
|
|
922
876
|
class_eval <<-EOS
|
923
877
|
alias_method :origin_#{method}, :#{method}
|
924
878
|
|
925
|
-
def #{
|
926
|
-
method
|
927
|
-
}
|
879
|
+
def #{method}
|
928
880
|
ret = origin_#{
|
929
881
|
method
|
930
882
|
}
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer
|
@@ -28,16 +30,12 @@ module CodeAnalyzer
|
|
28
30
|
|
29
31
|
context '#parse_file?' do
|
30
32
|
it 'should return true if node_file matches pattern' do
|
31
|
-
allow(checker).to receive(:interesting_files).and_return(
|
32
|
-
[%r{spec\/.*\.rb}, %r{lib\/.*\.rb}]
|
33
|
-
)
|
33
|
+
allow(checker).to receive(:interesting_files).and_return([%r{spec\/.*\.rb}, %r{lib\/.*\.rb}])
|
34
34
|
expect(checker.parse_file?('lib/code_analyzer.rb')).to be true
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should return false if node_file doesn't match pattern" do
|
38
|
-
allow(checker).to receive(:interesting_files).and_return(
|
39
|
-
[%r{spec\/.*\.rb}]
|
40
|
-
)
|
38
|
+
allow(checker).to receive(:interesting_files).and_return([%r{spec\/.*\.rb}])
|
41
39
|
expect(checker.parse_file?('lib/code_analyzer.rb')).to be false
|
42
40
|
end
|
43
41
|
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Sexp do
|
4
|
-
describe '
|
6
|
+
describe 'line_number' do
|
5
7
|
before :each do
|
6
8
|
content = <<-EOF
|
7
9
|
class Demo
|
@@ -18,9 +20,12 @@ describe Sexp do
|
|
18
20
|
end
|
19
21
|
def condition
|
20
22
|
if success?
|
21
|
-
puts "unknown" if output?
|
23
|
+
puts "unknown" if !output?
|
22
24
|
elsif fail?
|
25
|
+
1..2
|
26
|
+
3...4
|
23
27
|
end
|
28
|
+
pp ::Rails.application
|
24
29
|
end
|
25
30
|
end
|
26
31
|
EOF
|
@@ -39,6 +44,10 @@ describe Sexp do
|
|
39
44
|
expect(@node.grep_node(sexp_type: :const_ref).line_number).to eq 1
|
40
45
|
end
|
41
46
|
|
47
|
+
it 'should return top const line' do
|
48
|
+
expect(@node.grep_node(sexp_type: :top_const_ref).line_number).to eq 20
|
49
|
+
end
|
50
|
+
|
42
51
|
it 'should return const path line' do
|
43
52
|
expect(@node.grep_node(sexp_type: :const_path_ref).line_number).to eq 3
|
44
53
|
end
|
@@ -70,6 +79,79 @@ describe Sexp do
|
|
70
79
|
it 'should return if_mod line' do
|
71
80
|
expect(@node.grep_node(sexp_type: :if_mod).line_number).to eq 15
|
72
81
|
end
|
82
|
+
|
83
|
+
it 'should return unary line' do
|
84
|
+
expect(@node.grep_node(sexp_type: :unary).line_number).to eq 15
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should return assign line' do
|
88
|
+
expect(@node.grep_node(sexp_type: :assign).line_number).to eq 6
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should return paren line' do
|
92
|
+
expect(@node.grep_node(sexp_type: :paren).line_number).to eq 10
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should return dot2 line' do
|
96
|
+
expect(@node.grep_node(sexp_type: :dot2).line_number).to eq 17
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should return dot3 line' do
|
100
|
+
expect(@node.grep_node(sexp_type: :dot3).line_number).to eq 18
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should return params line' do
|
104
|
+
expect(@node.grep_node(sexp_type: :params).line_number).to be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should return params line if not empty' do
|
108
|
+
@node = parse_content(<<~CODE)
|
109
|
+
# @see Foo
|
110
|
+
def foo(a, b)
|
111
|
+
end
|
112
|
+
CODE
|
113
|
+
expect(@node.grep_node(sexp_type: :params).line_number).to eq 2
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should return stmts_add line' do
|
117
|
+
expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 13
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when a complex code is given' do
|
121
|
+
before :each do
|
122
|
+
@node = parse_content(<<~CODE)
|
123
|
+
def foo(num)
|
124
|
+
unless (num == 0 ? :zero, :other) || !@opts.right?
|
125
|
+
@bar = {}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
CODE
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should return unless line' do
|
132
|
+
expect(@node.grep_node(sexp_type: :unless).line_number).to eq 2
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should return paren line' do
|
136
|
+
expect(@node.grep_node(sexp_type: :paren).line_number).to eq 1
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should return stmts_add line' do
|
140
|
+
expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 2
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should return binary line' do
|
144
|
+
expect(@node.grep_node(sexp_type: :binary).line_number).to eq 2
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should return unary line' do
|
148
|
+
expect(@node.grep_node(sexp_type: :unary).line_number).to eq 2
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should return assign line' do
|
152
|
+
expect(@node.grep_node(sexp_type: :assign).line_number).to eq 3
|
153
|
+
end
|
154
|
+
end
|
73
155
|
end
|
74
156
|
|
75
157
|
describe 'grep_nodes' do
|
@@ -84,14 +166,12 @@ describe Sexp do
|
|
84
166
|
|
85
167
|
it 'should get the call nodes with receiver current_user' do
|
86
168
|
nodes = []
|
87
|
-
@node.grep_nodes(sexp_type: :call, receiver: 'current_user')
|
88
|
-
nodes << node
|
89
|
-
end
|
169
|
+
@node.grep_nodes(sexp_type: :call, receiver: 'current_user') { |node| nodes << node }
|
90
170
|
expect(nodes).to eq [
|
91
171
|
s(
|
92
172
|
:call,
|
93
173
|
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
94
|
-
s(:@period,
|
174
|
+
s(:@period, '.', s(2, 20)),
|
95
175
|
s(:@ident, 'posts', s(2, 21))
|
96
176
|
)
|
97
177
|
]
|
@@ -99,25 +179,23 @@ describe Sexp do
|
|
99
179
|
|
100
180
|
it 'should get the call nodes with different messages' do
|
101
181
|
nodes = []
|
102
|
-
@node.grep_nodes(sexp_type: :call, message: %w[posts find])
|
103
|
-
nodes << node
|
104
|
-
end
|
182
|
+
@node.grep_nodes(sexp_type: :call, message: %w[posts find]) { |node| nodes << node }
|
105
183
|
expect(nodes).to eq [
|
106
184
|
s(
|
107
185
|
:call,
|
108
186
|
s(
|
109
187
|
:call,
|
110
188
|
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
111
|
-
s(:@period,
|
189
|
+
s(:@period, '.', s(2, 20)),
|
112
190
|
s(:@ident, 'posts', s(2, 21))
|
113
191
|
),
|
114
|
-
s(:@period,
|
192
|
+
s(:@period, '.', s(2, 26)),
|
115
193
|
s(:@ident, 'find', s(2, 27))
|
116
194
|
),
|
117
195
|
s(
|
118
196
|
:call,
|
119
197
|
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
120
|
-
s(:@period,
|
198
|
+
s(:@period, '.', s(2, 20)),
|
121
199
|
s(:@ident, 'posts', s(2, 21))
|
122
200
|
)
|
123
201
|
]
|
@@ -125,9 +203,7 @@ describe Sexp do
|
|
125
203
|
|
126
204
|
it 'should get the vcall node with to_s' do
|
127
205
|
nodes = []
|
128
|
-
@node.grep_nodes(sexp_type: :vcall, to_s: 'current_user')
|
129
|
-
nodes << node
|
130
|
-
end
|
206
|
+
@node.grep_nodes(sexp_type: :vcall, to_s: 'current_user') { |node| nodes << node }
|
131
207
|
expect(nodes).to eq [s(:vcall, s(:@ident, 'current_user', s(2, 8)))]
|
132
208
|
end
|
133
209
|
end
|
@@ -147,7 +223,7 @@ describe Sexp do
|
|
147
223
|
expect(node).to eq s(
|
148
224
|
:call,
|
149
225
|
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
150
|
-
s(:@period,
|
226
|
+
s(:@period, '.', s(2, 20)),
|
151
227
|
s(:@ident, 'posts', s(2, 21))
|
152
228
|
)
|
153
229
|
end
|
@@ -170,8 +246,7 @@ describe Sexp do
|
|
170
246
|
|
171
247
|
describe 'receiver' do
|
172
248
|
it 'should get receiver of assign node' do
|
173
|
-
node =
|
174
|
-
parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
|
249
|
+
node = parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
|
175
250
|
receiver = node.receiver
|
176
251
|
expect(receiver.sexp_type).to eq :field
|
177
252
|
expect(receiver.receiver.to_s).to eq 'user'
|
@@ -179,8 +254,7 @@ describe Sexp do
|
|
179
254
|
end
|
180
255
|
|
181
256
|
it 'should get receiver of field node' do
|
182
|
-
node =
|
183
|
-
parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
|
257
|
+
node = parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
|
184
258
|
expect(node.receiver.to_s).to eq 'user'
|
185
259
|
end
|
186
260
|
|
@@ -204,16 +278,12 @@ describe Sexp do
|
|
204
278
|
end
|
205
279
|
|
206
280
|
it 'should get receiver of method_add_arg' do
|
207
|
-
node =
|
208
|
-
parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
|
281
|
+
node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
|
209
282
|
expect(node.receiver.to_s).to eq 'Post'
|
210
283
|
end
|
211
284
|
|
212
285
|
it 'should get receiver of method_add_block' do
|
213
|
-
node =
|
214
|
-
parse_content('Post.save do; end').grep_node(
|
215
|
-
sexp_type: :method_add_block
|
216
|
-
)
|
286
|
+
node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
|
217
287
|
expect(node.receiver.to_s).to eq 'Post'
|
218
288
|
end
|
219
289
|
end
|
@@ -234,10 +304,7 @@ describe Sexp do
|
|
234
304
|
|
235
305
|
describe 'base_class' do
|
236
306
|
it 'should get base class of class node' do
|
237
|
-
node =
|
238
|
-
parse_content('class User < ActiveRecord::Base; end').grep_node(
|
239
|
-
sexp_type: :class
|
240
|
-
)
|
307
|
+
node = parse_content('class User < ActiveRecord::Base; end').grep_node(sexp_type: :class)
|
241
308
|
expect(node.base_class.to_s).to eq 'ActiveRecord::Base'
|
242
309
|
end
|
243
310
|
end
|
@@ -263,10 +330,7 @@ describe Sexp do
|
|
263
330
|
end
|
264
331
|
|
265
332
|
it 'should get the message of command_call' do
|
266
|
-
node =
|
267
|
-
parse_content('map.resources :posts do; end').grep_node(
|
268
|
-
sexp_type: :command_call
|
269
|
-
)
|
333
|
+
node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
|
270
334
|
expect(node.message.to_s).to eq 'resources'
|
271
335
|
end
|
272
336
|
|
@@ -291,46 +355,34 @@ describe Sexp do
|
|
291
355
|
end
|
292
356
|
|
293
357
|
it 'should get the message of method_add_arg' do
|
294
|
-
node =
|
295
|
-
parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
|
358
|
+
node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
|
296
359
|
expect(node.message.to_s).to eq 'find'
|
297
360
|
end
|
298
361
|
|
299
362
|
it 'should get the message of method_add_block' do
|
300
|
-
node =
|
301
|
-
parse_content('Post.save do; end').grep_node(
|
302
|
-
sexp_type: :method_add_block
|
303
|
-
)
|
363
|
+
node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
|
304
364
|
expect(node.message.to_s).to eq 'save'
|
305
365
|
end
|
306
366
|
end
|
307
367
|
|
308
368
|
describe 'arguments' do
|
309
369
|
it 'should get the arguments of command' do
|
310
|
-
node =
|
311
|
-
parse_content('resources :posts do; end').grep_node(sexp_type: :command)
|
370
|
+
node = parse_content('resources :posts do; end').grep_node(sexp_type: :command)
|
312
371
|
expect(node.arguments.sexp_type).to eq :args_add_block
|
313
372
|
end
|
314
373
|
|
315
374
|
it 'should get the arguments of command_call' do
|
316
|
-
node =
|
317
|
-
parse_content('map.resources :posts do; end').grep_node(
|
318
|
-
sexp_type: :command_call
|
319
|
-
)
|
375
|
+
node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
|
320
376
|
expect(node.arguments.sexp_type).to eq :args_add_block
|
321
377
|
end
|
322
378
|
|
323
379
|
it 'should get the arguments of method_add_arg' do
|
324
|
-
node =
|
325
|
-
parse_content('User.find(:all)').grep_node(sexp_type: :method_add_arg)
|
380
|
+
node = parse_content('User.find(:all)').grep_node(sexp_type: :method_add_arg)
|
326
381
|
expect(node.arguments.sexp_type).to eq :args_add_block
|
327
382
|
end
|
328
383
|
|
329
384
|
it 'should get the arguments of method_add_block' do
|
330
|
-
node =
|
331
|
-
parse_content('Post.save(false) do; end').grep_node(
|
332
|
-
sexp_type: :method_add_block
|
333
|
-
)
|
385
|
+
node = parse_content('Post.save(false) do; end').grep_node(sexp_type: :method_add_block)
|
334
386
|
expect(node.arguments.sexp_type).to eq :args_add_block
|
335
387
|
end
|
336
388
|
end
|
@@ -344,49 +396,30 @@ describe Sexp do
|
|
344
396
|
|
345
397
|
describe 'all' do
|
346
398
|
it 'should get all arguments' do
|
347
|
-
node =
|
348
|
-
parse_content("puts 'hello', 'world'").grep_node(
|
349
|
-
sexp_type: :args_add_block
|
350
|
-
)
|
399
|
+
node = parse_content("puts 'hello', 'world'").grep_node(sexp_type: :args_add_block)
|
351
400
|
expect(node.all.map(&:to_s)).to eq %w[hello world]
|
352
401
|
end
|
353
402
|
|
354
403
|
it 'should get all arguments with &:' do
|
355
|
-
node =
|
356
|
-
parse_content('user.posts.map(&:title)').grep_node(
|
357
|
-
sexp_type: :args_add_block
|
358
|
-
)
|
404
|
+
node = parse_content('user.posts.map(&:title)').grep_node(sexp_type: :args_add_block)
|
359
405
|
expect(node.all.map(&:to_s)).to eq %w[title]
|
360
406
|
end
|
361
407
|
|
362
408
|
it 'should get all arguments with command_call node' do
|
363
|
-
node =
|
364
|
-
parse_content('options_for_select(Account.get_business current_user)')
|
365
|
-
.grep_node(sexp_type: :args_add)
|
409
|
+
node = parse_content('options_for_select(Account.get_business current_user)').grep_node(sexp_type: :args_add)
|
366
410
|
expect(node.all).to eq [
|
367
411
|
s(
|
368
412
|
:command_call,
|
369
413
|
s(:var_ref, s(:@const, 'Account', s(1, 19))),
|
370
|
-
s(:@period,
|
414
|
+
s(:@period, '.', s(1, 26)),
|
371
415
|
s(:@ident, 'get_business', s(1, 27)),
|
372
|
-
s(
|
373
|
-
:args_add_block,
|
374
|
-
s(
|
375
|
-
:args_add,
|
376
|
-
s(:args_new),
|
377
|
-
s(:vcall, s(:@ident, 'current_user', s(1, 40)))
|
378
|
-
),
|
379
|
-
false
|
380
|
-
)
|
416
|
+
s(:args_add_block, s(:args_add, s(:args_new), s(:vcall, s(:@ident, 'current_user', s(1, 40)))), false)
|
381
417
|
)
|
382
418
|
]
|
383
419
|
end
|
384
420
|
|
385
421
|
it 'no error for args_add_star' do
|
386
|
-
node =
|
387
|
-
parse_content("send(:\"\#{route}_url\", *args)").grep_node(
|
388
|
-
sexp_type: :args_add_block
|
389
|
-
)
|
422
|
+
node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(sexp_type: :args_add_block)
|
390
423
|
expect { node.all }.not_to raise_error
|
391
424
|
end
|
392
425
|
end
|
@@ -403,8 +436,7 @@ describe Sexp do
|
|
403
436
|
end
|
404
437
|
|
405
438
|
it 'should get conditional statement of elsif' do
|
406
|
-
node =
|
407
|
-
parse_content('if true; elsif false; end').grep_node(sexp_type: :elsif)
|
439
|
+
node = parse_content('if true; elsif false; end').grep_node(sexp_type: :elsif)
|
408
440
|
expect(node.conditional_statement.to_s).to eq 'false'
|
409
441
|
end
|
410
442
|
|
@@ -414,8 +446,7 @@ describe Sexp do
|
|
414
446
|
end
|
415
447
|
|
416
448
|
it 'should get conditional statement of unless_mod' do
|
417
|
-
node =
|
418
|
-
parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
|
449
|
+
node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
|
419
450
|
expect(node.conditional_statement.to_s).to eq 'false'
|
420
451
|
end
|
421
452
|
|
@@ -427,9 +458,7 @@ describe Sexp do
|
|
427
458
|
|
428
459
|
describe 'all_conditions' do
|
429
460
|
it 'should get all conditions' do
|
430
|
-
node =
|
431
|
-
parse_content('user == current_user && user.valid? || user.admin?')
|
432
|
-
.grep_node(sexp_type: :binary)
|
461
|
+
node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(sexp_type: :binary)
|
433
462
|
expect(node.all_conditions.size).to eq 3
|
434
463
|
end
|
435
464
|
end
|
@@ -463,8 +492,7 @@ describe Sexp do
|
|
463
492
|
end
|
464
493
|
|
465
494
|
it 'should get body of module' do
|
466
|
-
node =
|
467
|
-
parse_content('module Enumerable; end').grep_node(sexp_type: :module)
|
495
|
+
node = parse_content('module Enumerable; end').grep_node(sexp_type: :module)
|
468
496
|
expect(node.body.sexp_type).to eq :bodystmt
|
469
497
|
end
|
470
498
|
|
@@ -474,22 +502,17 @@ describe Sexp do
|
|
474
502
|
end
|
475
503
|
|
476
504
|
it 'should get body of elsif' do
|
477
|
-
node =
|
478
|
-
parse_content("if true; elsif true; 'OK'; end").grep_node(
|
479
|
-
sexp_type: :elsif
|
480
|
-
)
|
505
|
+
node = parse_content("if true; elsif true; 'OK'; end").grep_node(sexp_type: :elsif)
|
481
506
|
expect(node.body.sexp_type).to eq :stmts_add
|
482
507
|
end
|
483
508
|
|
484
509
|
it 'should get body of unless' do
|
485
|
-
node =
|
486
|
-
parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
|
510
|
+
node = parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
|
487
511
|
expect(node.body.sexp_type).to eq :stmts_add
|
488
512
|
end
|
489
513
|
|
490
514
|
it 'should get body of else' do
|
491
|
-
node =
|
492
|
-
parse_content("if true; else; 'OK'; end").grep_node(sexp_type: :else)
|
515
|
+
node = parse_content("if true; else; 'OK'; end").grep_node(sexp_type: :else)
|
493
516
|
expect(node.body.sexp_type).to eq :stmts_add
|
494
517
|
end
|
495
518
|
|
@@ -499,8 +522,7 @@ describe Sexp do
|
|
499
522
|
end
|
500
523
|
|
501
524
|
it 'should get body of unless_mod' do
|
502
|
-
node =
|
503
|
-
parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
|
525
|
+
node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
|
504
526
|
expect(node.body.to_s).to eq 'OK'
|
505
527
|
end
|
506
528
|
|
@@ -512,9 +534,7 @@ describe Sexp do
|
|
512
534
|
|
513
535
|
describe 'block' do
|
514
536
|
it 'sould get block of method_add_block node' do
|
515
|
-
node =
|
516
|
-
parse_content('resources :posts do; resources :comments; end')
|
517
|
-
.grep_node(sexp_type: :method_add_block)
|
537
|
+
node = parse_content('resources :posts do; resources :comments; end').grep_node(sexp_type: :method_add_block)
|
518
538
|
expect(node.block_node.sexp_type).to eq :do_block
|
519
539
|
end
|
520
540
|
end
|
@@ -522,40 +542,29 @@ describe Sexp do
|
|
522
542
|
describe 'statements' do
|
523
543
|
it 'should get statements of do_block node' do
|
524
544
|
node =
|
525
|
-
parse_content(
|
526
|
-
'resources :posts do; resources :comments; resources :like; end'
|
527
|
-
)
|
528
|
-
.grep_node(sexp_type: :do_block)
|
545
|
+
parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(sexp_type: :do_block)
|
529
546
|
expect(node.statements.size).to eq 2
|
530
547
|
end
|
531
548
|
|
532
549
|
it 'should get statements of bodystmt node' do
|
533
|
-
node =
|
534
|
-
parse_content('class User; def login?; end; def admin?; end; end')
|
535
|
-
.grep_node(sexp_type: :bodystmt)
|
550
|
+
node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(sexp_type: :bodystmt)
|
536
551
|
expect(node.statements.size).to eq 2
|
537
552
|
end
|
538
553
|
end
|
539
554
|
|
540
555
|
describe 'exception_classes' do
|
541
556
|
it 'should get exception classes of rescue node' do
|
542
|
-
node =
|
543
|
-
parse_content('def test; rescue CustomException; end').grep_node(
|
544
|
-
sexp_type: :rescue
|
545
|
-
)
|
557
|
+
node = parse_content('def test; rescue CustomException; end').grep_node(sexp_type: :rescue)
|
546
558
|
expect(node.exception_classes.first.to_s).to eq 'CustomException'
|
547
559
|
end
|
548
560
|
|
549
561
|
it 'should get empty of empty rescue node' do
|
550
|
-
node =
|
551
|
-
parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
|
562
|
+
node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
|
552
563
|
expect(node.exception_classes.first.to_s).to eq ''
|
553
564
|
end
|
554
565
|
|
555
566
|
it 'should get exception classes of rescue node for multiple exceptions' do
|
556
|
-
node =
|
557
|
-
parse_content('def test; rescue StandardError, CustomException; end')
|
558
|
-
.grep_node(sexp_type: :rescue)
|
567
|
+
node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(sexp_type: :rescue)
|
559
568
|
expect(node.exception_classes.first.to_s).to eq 'StandardError'
|
560
569
|
expect(node.exception_classes.last.to_s).to eq 'CustomException'
|
561
570
|
end
|
@@ -563,36 +572,28 @@ describe Sexp do
|
|
563
572
|
|
564
573
|
describe 'exception_variable' do
|
565
574
|
it 'should get exception varible of rescue node' do
|
566
|
-
node =
|
567
|
-
parse_content('def test; rescue => e; end').grep_node(
|
568
|
-
sexp_type: :rescue
|
569
|
-
)
|
575
|
+
node = parse_content('def test; rescue => e; end').grep_node(sexp_type: :rescue)
|
570
576
|
expect(node.exception_variable.to_s).to eq 'e'
|
571
577
|
end
|
572
578
|
|
573
579
|
it 'should get empty of empty rescue node' do
|
574
|
-
node =
|
575
|
-
parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
|
580
|
+
node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
|
576
581
|
expect(node.exception_variable.to_s).to eq ''
|
577
582
|
end
|
578
583
|
end
|
579
584
|
|
580
585
|
describe 'hash_value' do
|
581
586
|
it 'should get value for hash node' do
|
582
|
-
node =
|
583
|
-
parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
584
|
-
sexp_type: :hash
|
585
|
-
)
|
587
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
|
586
588
|
expect(node.hash_value('first_name').to_s).to eq 'Richard'
|
587
589
|
expect(node.hash_value('last_name').to_s).to eq 'Huang'
|
588
590
|
end
|
589
591
|
|
590
592
|
it 'should get value for bare_assoc_hash' do
|
591
593
|
node =
|
592
|
-
parse_content(
|
593
|
-
|
594
|
+
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
595
|
+
sexp_type: :bare_assoc_hash
|
594
596
|
)
|
595
|
-
.grep_node(sexp_type: :bare_assoc_hash)
|
596
597
|
expect(node.hash_value('first_name').to_s).to eq 'Richard'
|
597
598
|
expect(node.hash_value('last_name').to_s).to eq 'Huang'
|
598
599
|
end
|
@@ -600,67 +601,52 @@ describe Sexp do
|
|
600
601
|
|
601
602
|
describe 'hash_size' do
|
602
603
|
it 'should get value for hash node' do
|
603
|
-
node =
|
604
|
-
parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
605
|
-
sexp_type: :hash
|
606
|
-
)
|
604
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
|
607
605
|
expect(node.hash_size).to eq 2
|
608
606
|
end
|
609
607
|
|
610
608
|
it 'should get value for bare_assoc_hash' do
|
611
609
|
node =
|
612
|
-
parse_content(
|
613
|
-
|
610
|
+
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
611
|
+
sexp_type: :bare_assoc_hash
|
614
612
|
)
|
615
|
-
.grep_node(sexp_type: :bare_assoc_hash)
|
616
613
|
expect(node.hash_size).to eq 2
|
617
614
|
end
|
618
615
|
end
|
619
616
|
|
620
617
|
describe 'hash_keys' do
|
621
618
|
it 'should get hash_keys for hash node' do
|
622
|
-
node =
|
623
|
-
parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
624
|
-
sexp_type: :hash
|
625
|
-
)
|
619
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
|
626
620
|
expect(node.hash_keys).to eq %w[first_name last_name]
|
627
621
|
end
|
628
622
|
|
629
623
|
it 'should get hash_keys for bare_assoc_hash' do
|
630
624
|
node =
|
631
|
-
parse_content(
|
632
|
-
|
625
|
+
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
626
|
+
sexp_type: :bare_assoc_hash
|
633
627
|
)
|
634
|
-
.grep_node(sexp_type: :bare_assoc_hash)
|
635
628
|
expect(node.hash_keys).to eq %w[first_name last_name]
|
636
629
|
end
|
637
630
|
end
|
638
631
|
|
639
632
|
describe 'hash_values' do
|
640
633
|
it 'should get hash_values for hash node' do
|
641
|
-
node =
|
642
|
-
parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
643
|
-
sexp_type: :hash
|
644
|
-
)
|
634
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
|
645
635
|
expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
|
646
636
|
end
|
647
637
|
|
648
638
|
it 'should get hash_values for bare_assoc_hash' do
|
649
639
|
node =
|
650
|
-
parse_content(
|
651
|
-
|
640
|
+
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
641
|
+
sexp_type: :bare_assoc_hash
|
652
642
|
)
|
653
|
-
.grep_node(sexp_type: :bare_assoc_hash)
|
654
643
|
expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
|
655
644
|
end
|
656
645
|
end
|
657
646
|
|
658
647
|
describe 'array_size' do
|
659
648
|
it 'should get array size' do
|
660
|
-
node =
|
661
|
-
parse_content("['first_name', 'last_name']").grep_node(
|
662
|
-
sexp_type: :array
|
663
|
-
)
|
649
|
+
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
664
650
|
expect(node.array_size).to eq 2
|
665
651
|
end
|
666
652
|
|
@@ -672,10 +658,7 @@ describe Sexp do
|
|
672
658
|
|
673
659
|
describe 'array_values' do
|
674
660
|
it 'should get array values' do
|
675
|
-
node =
|
676
|
-
parse_content("['first_name', 'last_name']").grep_node(
|
677
|
-
sexp_type: :array
|
678
|
-
)
|
661
|
+
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
679
662
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
680
663
|
end
|
681
664
|
|
@@ -685,8 +668,7 @@ describe Sexp do
|
|
685
668
|
end
|
686
669
|
|
687
670
|
it 'should get array value with array and words_add' do
|
688
|
-
node =
|
689
|
-
parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
|
671
|
+
node = parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
|
690
672
|
expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
|
691
673
|
end
|
692
674
|
|
@@ -696,8 +678,7 @@ describe Sexp do
|
|
696
678
|
end
|
697
679
|
|
698
680
|
it 'should get array value with array and qwords_add' do
|
699
|
-
node =
|
700
|
-
parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
|
681
|
+
node = parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
|
701
682
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
702
683
|
end
|
703
684
|
|
@@ -708,8 +689,7 @@ describe Sexp do
|
|
708
689
|
|
709
690
|
if RUBY_VERSION.to_i > 1
|
710
691
|
it 'should get array value with array and symbols_add' do
|
711
|
-
node =
|
712
|
-
parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
|
692
|
+
node = parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
|
713
693
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
714
694
|
end
|
715
695
|
|
@@ -719,8 +699,7 @@ describe Sexp do
|
|
719
699
|
end
|
720
700
|
|
721
701
|
it 'should get array value with array and qsymbols_add' do
|
722
|
-
node =
|
723
|
-
parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
|
702
|
+
node = parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
|
724
703
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
725
704
|
end
|
726
705
|
|
@@ -733,9 +712,7 @@ describe Sexp do
|
|
733
712
|
|
734
713
|
describe 'alias' do
|
735
714
|
context 'method' do
|
736
|
-
before
|
737
|
-
@node = parse_content('alias new old').grep_node(sexp_type: :alias)
|
738
|
-
end
|
715
|
+
before { @node = parse_content('alias new old').grep_node(sexp_type: :alias) }
|
739
716
|
|
740
717
|
it 'should get old_method' do
|
741
718
|
expect(@node.old_method.to_s).to eq 'old'
|
@@ -747,9 +724,7 @@ describe Sexp do
|
|
747
724
|
end
|
748
725
|
|
749
726
|
context 'symbol' do
|
750
|
-
before
|
751
|
-
@node = parse_content('alias :new :old').grep_node(sexp_type: :alias)
|
752
|
-
end
|
727
|
+
before { @node = parse_content('alias :new :old').grep_node(sexp_type: :alias) }
|
753
728
|
|
754
729
|
it 'should get old_method' do
|
755
730
|
expect(@node.old_method.to_s).to eq 'old'
|
@@ -763,10 +738,7 @@ describe Sexp do
|
|
763
738
|
|
764
739
|
describe 'to_object' do
|
765
740
|
it 'should to array' do
|
766
|
-
node =
|
767
|
-
parse_content("['first_name', 'last_name']").grep_node(
|
768
|
-
sexp_type: :array
|
769
|
-
)
|
741
|
+
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
770
742
|
expect(node.to_object).to eq %w[first_name last_name]
|
771
743
|
end
|
772
744
|
|
@@ -776,8 +748,7 @@ describe Sexp do
|
|
776
748
|
end
|
777
749
|
|
778
750
|
it 'should to array with symbols' do
|
779
|
-
node =
|
780
|
-
parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
|
751
|
+
node = parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
|
781
752
|
expect(node.to_object).to eq %w[first_name last_name]
|
782
753
|
end
|
783
754
|
|
@@ -819,16 +790,12 @@ describe Sexp do
|
|
819
790
|
end
|
820
791
|
|
821
792
|
it 'should get to_s for class with module' do
|
822
|
-
node =
|
823
|
-
parse_content('ActiveRecord::Base').grep_node(
|
824
|
-
sexp_type: :const_path_ref
|
825
|
-
)
|
793
|
+
node = parse_content('ActiveRecord::Base').grep_node(sexp_type: :const_path_ref)
|
826
794
|
expect(node.to_s).to eq 'ActiveRecord::Base'
|
827
795
|
end
|
828
796
|
|
829
797
|
it 'should get to_s for label' do
|
830
|
-
node =
|
831
|
-
parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
|
798
|
+
node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
|
832
799
|
expect(node.to_s).to eq 'first_name'
|
833
800
|
end
|
834
801
|
|
@@ -876,15 +843,11 @@ describe Sexp do
|
|
876
843
|
|
877
844
|
describe 'remove_line_and_column' do
|
878
845
|
it 'should remove' do
|
879
|
-
s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(
|
880
|
-
:@ident,
|
881
|
-
'test'
|
882
|
-
)
|
846
|
+
s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(:@ident, 'test')
|
883
847
|
end
|
884
848
|
|
885
849
|
it 'should remove child nodes' do
|
886
|
-
s(:const_ref, s(:@const, 'Demo', s(1, 12)))
|
887
|
-
.remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
|
850
|
+
s(:const_ref, s(:@const, 'Demo', s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
|
888
851
|
end
|
889
852
|
end
|
890
853
|
end
|
@@ -1,15 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer
|
4
6
|
describe Warning do
|
5
7
|
it 'should return error with filename, line number and message' do
|
6
8
|
expect(
|
7
|
-
Warning.new(
|
8
|
-
filename: 'app/models/user.rb',
|
9
|
-
line_number: '100',
|
10
|
-
message: 'not good'
|
11
|
-
)
|
12
|
-
.to_s
|
9
|
+
Warning.new(filename: 'app/models/user.rb', line_number: '100', message: 'not good').to_s
|
13
10
|
).to eq 'app/models/user.rb:100 - not good'
|
14
11
|
end
|
15
12
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_analyzer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sexp_processor
|
@@ -91,7 +91,7 @@ homepage: https://github.com/flyerhzm/code_analyzer
|
|
91
91
|
licenses:
|
92
92
|
- MIT
|
93
93
|
metadata: {}
|
94
|
-
post_install_message:
|
94
|
+
post_install_message:
|
95
95
|
rdoc_options: []
|
96
96
|
require_paths:
|
97
97
|
- lib
|
@@ -106,8 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
|
-
rubygems_version: 3.
|
110
|
-
signing_key:
|
109
|
+
rubygems_version: 3.1.2
|
110
|
+
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: a code analyzer helps you build your own code analyzer tool.
|
113
113
|
test_files:
|