code_analyzer 0.4.8 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b18a16f794a38af603dcc6036257fb4718bcaf7f
4
- data.tar.gz: 194f1c7fd4eeda9b5feb90af63a5c0e695144a06
2
+ SHA256:
3
+ metadata.gz: 6c252e6214745ff2bcf507702a49553d0ca90c75a12614ee3e5c197c1994656c
4
+ data.tar.gz: 85bb0e3b8ea846132ba4a159f4477749751cb54a94611a7402d241a2e296f1c6
5
5
  SHA512:
6
- metadata.gz: 10840f679ab73b8e69f324be82c95de6eb11d2002ac984528c224923daf06f0c829590b4d47345cf4fe9086d5258c5e66822ed99bb8d52767729fb8920bc8368
7
- data.tar.gz: a7222ce5efb0197e63e61bc20e86bbc730275494cd315a890602a0cafccae8abf9582d14578e35da962d5cdc7d2ea8e94f026b86c11f3517cf921a2679480f3c
6
+ metadata.gz: 73fc0d1d771feafb646ff218837ae19e8de9556acd25b4fb3a3bad93b76899d950037145c603b1b5e48a38815b1be086102f65c3e77ad2bc2898931858988e6c
7
+ data.tar.gz: 3fd7c957fc9c2ceeb6714cf4a6a4f1db7b00f7ea6d4a9e73553b460c59f0bd42a4f5d4538e7bf00bc2f39d441f29bed8891a3d200a65b0653830d30c735b7dc0
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  cache: bundler
3
- sudo: false
4
3
  rvm:
5
- - 2.3.1
4
+ - 2.6.4
@@ -1,12 +1,12 @@
1
1
  # encoding: utf-8
2
- require "ripper"
3
- require "code_analyzer/version"
4
- require "code_analyzer/nil"
5
- require "code_analyzer/sexp"
2
+ require 'ripper'
3
+ require 'code_analyzer/version'
4
+ require 'code_analyzer/nil'
5
+ require 'code_analyzer/sexp'
6
6
 
7
7
  module CodeAnalyzer
8
- autoload :AnalyzerException, "code_analyzer/analyzer_exception"
9
- autoload :Checker, "code_analyzer/checker"
10
- autoload :CheckingVisitor, "code_analyzer/checking_visitor"
11
- autoload :Warning, "code_analyzer/warning"
8
+ autoload :AnalyzerException, 'code_analyzer/analyzer_exception'
9
+ autoload :Checker, 'code_analyzer/checker'
10
+ autoload :CheckingVisitor, 'code_analyzer/checking_visitor'
11
+ autoload :Warning, 'code_analyzer/warning'
12
12
  end
@@ -28,9 +28,8 @@ module CodeAnalyzer
28
28
  # @param [Sexp] node
29
29
  def node_start(node)
30
30
  @node = node
31
- self.class.get_callbacks("start_#{node.sexp_type}".to_sym).each do |block|
32
- self.instance_exec(node, &block)
33
- end
31
+ self.class.get_callbacks("start_#{node.sexp_type}".to_sym)
32
+ .each { |block| self.instance_exec(node, &block) }
34
33
  end
35
34
 
36
35
  # delegate to end_### according to the sexp_type, like
@@ -51,8 +50,13 @@ module CodeAnalyzer
51
50
  # @param [String] message, is the warning message
52
51
  # @param [String] filename, is the filename of source code
53
52
  # @param [Integer] line_number, is the line number of the source code which is reviewing
54
- def add_warning(message, filename = @node.file, line_number = @node.line_number)
55
- warnings << Warning.new(filename: filename, line_number: line_number, message: message)
53
+ def add_warning(
54
+ message, filename = @node.file, line_number = @node.line_number
55
+ )
56
+ warnings <<
57
+ Warning.new(
58
+ filename: filename, line_number: line_number, message: message
59
+ )
56
60
  end
57
61
 
58
62
  # all warnings.
@@ -60,7 +64,7 @@ module CodeAnalyzer
60
64
  @warnings ||= []
61
65
  end
62
66
 
63
- class <<self
67
+ class << self
64
68
  def interesting_nodes(*nodes)
65
69
  @interesting_nodes ||= []
66
70
  @interesting_nodes += nodes
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
  module CodeAnalyzer
3
3
  module CheckingVisitor
4
- autoload :Base, "code_analyzer/checking_visitor/base"
5
- autoload :Plain, "code_analyzer/checking_visitor/plain"
6
- autoload :Default, "code_analyzer/checking_visitor/default"
4
+ autoload :Base, 'code_analyzer/checking_visitor/base'
5
+ autoload :Plain, 'code_analyzer/checking_visitor/plain'
6
+ autoload :Default, 'code_analyzer/checking_visitor/default'
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@
2
2
  module CodeAnalyzer::CheckingVisitor
3
3
  # Base class for checking visitor.
4
4
  class Base
5
- def initialize(options={})
5
+ def initialize(options = {})
6
6
  @checkers = options[:checkers]
7
7
  end
8
8
 
@@ -2,7 +2,7 @@
2
2
  module CodeAnalyzer::CheckingVisitor
3
3
  # This is the default checking visitor to check ruby sexp nodes.
4
4
  class Default < Base
5
- def initialize(options={})
5
+ def initialize(options = {})
6
6
  super
7
7
  @checks = {}
8
8
  @checkers.each do |checker|
@@ -28,9 +28,7 @@ module CodeAnalyzer::CheckingVisitor
28
28
  def after_check
29
29
  @checkers.each do |checker|
30
30
  after_check_callbacks = checker.class.get_callbacks(:after_check)
31
- after_check_callbacks.each do |block|
32
- checker.instance_exec &block
33
- end
31
+ after_check_callbacks.each { |block| checker.instance_exec &block }
34
32
  end
35
33
  end
36
34
 
@@ -41,7 +39,9 @@ module CodeAnalyzer::CheckingVisitor
41
39
  def parse(filename, content)
42
40
  Sexp.from_array(Ripper::SexpBuilder.new(content).parse)
43
41
  rescue Exception
44
- raise AnalyzerException.new("#{filename} looks like it's not a valid Ruby file. Skipping...")
42
+ raise AnalyzerException.new(
43
+ "#{filename} looks like it's not a valid Ruby file. Skipping..."
44
+ )
45
45
  end
46
46
 
47
47
  # recursively check ruby sexp node.
@@ -52,14 +52,18 @@ module CodeAnalyzer::CheckingVisitor
52
52
  def check_node(node)
53
53
  checkers = @checks[node.sexp_type]
54
54
  if checkers
55
- checkers.each { |checker| checker.node_start(node) if checker.parse_file?(node.file) }
55
+ checkers.each do |checker|
56
+ checker.node_start(node) if checker.parse_file?(node.file)
57
+ end
56
58
  end
57
- node.children.each { |child_node|
59
+ node.children.each do |child_node|
58
60
  child_node.file = node.file
59
61
  check_node(child_node)
60
- }
62
+ end
61
63
  if checkers
62
- checkers.each { |checker| checker.node_end(node) if checker.parse_file?(node.file) }
64
+ checkers.each do |checker|
65
+ checker.node_end(node) if checker.parse_file?(node.file)
66
+ end
63
67
  end
64
68
  end
65
69
  end
@@ -16,10 +16,9 @@ class Sexp
16
16
  def line_number
17
17
  case sexp_type
18
18
  when :def, :defs, :command, :command_call, :call, :fcall, :method_add_arg, :method_add_block,
19
- :var_ref, :vcall, :const_ref, :const_path_ref, :class, :module,
20
- :if, :unless, :elsif, :ifop, :if_mod, :unless_mod, :binary,
21
- :alias, :symbol_literal, :symbol, :aref, :hash, :assoc_new, :string_literal,
22
- :massign
19
+ :var_ref, :vcall, :const_ref, :const_path_ref, :class, :module, :if, :unless,
20
+ :elsif, :ifop, :if_mod, :unless_mod, :binary, :alias, :symbol_literal, :symbol,
21
+ :aref, :hash, :assoc_new, :string_literal, :massign, :var_field
23
22
  self[1].line_number
24
23
  when :assoclist_from_args, :bare_assoc_hash
25
24
  self[1][0].line_number
@@ -38,7 +37,7 @@ class Sexp
38
37
  #
39
38
  # @return [Array] child nodes.
40
39
  def children
41
- find_all { | sexp | Sexp === sexp }
40
+ find_all { |sexp| Sexp === sexp }
42
41
  end
43
42
 
44
43
  # recursively find all child nodes, and yeild each child node.
@@ -68,10 +67,46 @@ class Sexp
68
67
  message = options[:message]
69
68
  to_s = options[:to_s]
70
69
  self.recursive_children do |child|
71
- if (!sexp_type || (sexp_type.is_a?(Array) ? sexp_type.include?(child.sexp_type) : sexp_type == child.sexp_type)) &&
72
- (!receiver || (receiver.is_a?(Array) ? receiver.include?(child.receiver.to_s) : receiver == child.receiver.to_s)) &&
73
- (!message || (message.is_a?(Array) ? message.include?(child.message.to_s) : message == child.message.to_s)) &&
74
- (!to_s || (to_s.is_a?(Array) ? to_s.include?(child.to_s) : to_s == child.to_s))
70
+ if (
71
+ !sexp_type ||
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
+ )
99
+ ) &&
100
+ (
101
+ !to_s ||
102
+ (
103
+ if to_s.is_a?(Array)
104
+ to_s.include?(child.to_s)
105
+ else
106
+ to_s == child.to_s
107
+ end
108
+ )
109
+ )
75
110
  yield child
76
111
  end
77
112
  end
@@ -91,7 +126,10 @@ class Sexp
91
126
  # the condition value can be Symbol, Array or Sexp.
92
127
  def grep_node(options)
93
128
  result = CodeAnalyzer::Nil.new
94
- grep_nodes(options) { |node| result = node; break; }
129
+ grep_nodes(options) do |node|
130
+ result = node
131
+ break
132
+ end
95
133
  result
96
134
  end
97
135
 
@@ -138,9 +176,7 @@ class Sexp
138
176
  #
139
177
  # @return [Sexp] module name node
140
178
  def module_name
141
- if :module == sexp_type
142
- self[1]
143
- end
179
+ self[1] if :module == sexp_type
144
180
  end
145
181
 
146
182
  # Get the class name of the class node.
@@ -154,9 +190,7 @@ class Sexp
154
190
  #
155
191
  # @return [Sexp] class name node
156
192
  def class_name
157
- if :class == sexp_type
158
- self[1]
159
- end
193
+ self[1] if :class == sexp_type
160
194
  end
161
195
 
162
196
  # Get the base class of the class node.
@@ -170,9 +204,7 @@ class Sexp
170
204
  #
171
205
  # @return [Sexp] base class of class node
172
206
  def base_class
173
- if :class == sexp_type
174
- self[2]
175
- end
207
+ self[2] if :class == sexp_type
176
208
  end
177
209
 
178
210
  # Get the left value of the assign node.
@@ -185,9 +217,7 @@ class Sexp
185
217
  #
186
218
  # @return [Symbol] left value of lasgn or iasgn node
187
219
  def left_value
188
- if :assign == sexp_type
189
- self[1]
190
- end
220
+ self[1] if :assign == sexp_type
191
221
  end
192
222
 
193
223
  # Get the right value of assign node.
@@ -200,9 +230,7 @@ class Sexp
200
230
  #
201
231
  # @return [Sexp] right value of assign node
202
232
  def right_value
203
- if :assign == sexp_type
204
- self[2]
205
- end
233
+ self[2] if :assign == sexp_type
206
234
  end
207
235
 
208
236
  # Get the message node.
@@ -277,9 +305,7 @@ class Sexp
277
305
  #
278
306
  # @return [Sexp] argument node
279
307
  def argument
280
- if :binary == sexp_type
281
- self[3]
282
- end
308
+ self[3] if :binary == sexp_type
283
309
  end
284
310
 
285
311
  # Get all arguments.
@@ -305,7 +331,7 @@ class Sexp
305
331
  else
306
332
  node = self[1]
307
333
  while true
308
- if [:args_add, :args_add_star].include? node.sexp_type
334
+ if %i[args_add args_add_star].include? node.sexp_type
309
335
  nodes.unshift node[2]
310
336
  node = node[1]
311
337
  elsif :args_new == node.sexp_type
@@ -330,9 +356,7 @@ class Sexp
330
356
  #
331
357
  # @return [Sexp] conditional statement of if node
332
358
  def conditional_statement
333
- if [:if, :unless, :elsif, :ifop, :if_mod, :unless_mod].include? sexp_type
334
- self[1]
335
- end
359
+ self[1] if %i[if unless elsif ifop if_mod unless_mod].include? sexp_type
336
360
  end
337
361
 
338
362
  # Get all condition nodes.
@@ -366,13 +390,15 @@ class Sexp
366
390
  # @return [Array] all condition nodes
367
391
  def all_conditions
368
392
  nodes = []
369
- if :binary == sexp_type && %w(&& || and or).include?(self[2].to_s)
370
- if :binary == self[1].sexp_type && %w(&& || and or).include?(self[1][2].to_s)
393
+ 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)
371
396
  nodes += self[1].all_conditions
372
397
  else
373
398
  nodes << self[1]
374
399
  end
375
- if :binary == self[3].sexp_type && %w(&& || and or).include?(self[3][2].to_s)
400
+ if :binary == self[3].sexp_type &&
401
+ %w[&& || and or].include?(self[3][2].to_s)
376
402
  nodes += self[3].all_conditions
377
403
  else
378
404
  nodes << self[3]
@@ -399,6 +425,7 @@ class Sexp
399
425
  when :defs
400
426
  self[3]
401
427
  else
428
+
402
429
  end
403
430
  end
404
431
 
@@ -506,13 +533,15 @@ class Sexp
506
533
  # @return [Array] all statements
507
534
  def statements
508
535
  stmts = []
509
- node = case sexp_type
510
- when :do_block, :brace_block
511
- self[2]
512
- when :bodystmt
513
- self[1]
514
- else
515
- end
536
+ node =
537
+ case sexp_type
538
+ when :do_block, :brace_block
539
+ self[2][1]
540
+ when :bodystmt
541
+ self[1]
542
+ else
543
+
544
+ end
516
545
  if node
517
546
  while true
518
547
  if :stmts_add == node.sexp_type && s(:void_stmt) != node[2]
@@ -566,9 +595,7 @@ class Sexp
566
595
  # )
567
596
  # => s(:var_field, s(:@ident, "e", s(1, 20)))
568
597
  def exception_variable
569
- if :rescue == sexp_type
570
- self[2]
571
- end
598
+ self[2] if :rescue == sexp_type
572
599
  end
573
600
 
574
601
  # Get hash value node.
@@ -585,18 +612,18 @@ class Sexp
585
612
  #
586
613
  # @return [Sexp] hash value node
587
614
  def hash_value(key)
588
- pair_nodes = case sexp_type
589
- when :bare_assoc_hash
590
- self[1]
591
- when :hash
592
- self[1][1]
593
- else
594
- end
615
+ pair_nodes =
616
+ case sexp_type
617
+ when :bare_assoc_hash
618
+ self[1]
619
+ when :hash
620
+ self[1][1]
621
+ else
622
+
623
+ end
595
624
  if pair_nodes
596
625
  pair_nodes.size.times do |i|
597
- if key == pair_nodes[i][1].to_s
598
- return pair_nodes[i][2]
599
- end
626
+ return pair_nodes[i][2] if key == pair_nodes[i][1].to_s
600
627
  end
601
628
  end
602
629
  CodeAnalyzer::Nil.new
@@ -640,18 +667,18 @@ class Sexp
640
667
  #
641
668
  # @return [Array] hash keys
642
669
  def hash_keys
643
- pair_nodes = case sexp_type
644
- when :bare_assoc_hash
645
- self[1]
646
- when :hash
647
- self[1][1]
648
- else
649
- end
670
+ pair_nodes =
671
+ case sexp_type
672
+ when :bare_assoc_hash
673
+ self[1]
674
+ when :hash
675
+ self[1][1]
676
+ else
677
+
678
+ end
650
679
  if pair_nodes
651
680
  keys = []
652
- pair_nodes.size.times do |i|
653
- keys << pair_nodes[i][1].to_s
654
- end
681
+ pair_nodes.size.times { |i| keys << pair_nodes[i][1].to_s }
655
682
  keys
656
683
  end
657
684
  end
@@ -673,18 +700,18 @@ class Sexp
673
700
  #
674
701
  # @return [Array] hash values
675
702
  def hash_values
676
- pair_nodes = case sexp_type
677
- when :bare_assoc_hash
678
- self[1]
679
- when :hash
680
- self[1][1]
681
- else
682
- end
703
+ pair_nodes =
704
+ case sexp_type
705
+ when :bare_assoc_hash
706
+ self[1]
707
+ when :hash
708
+ self[1][1]
709
+ else
710
+
711
+ end
683
712
  if pair_nodes
684
713
  values = []
685
- pair_nodes.size.times do |i|
686
- values << pair_nodes[i][2]
687
- end
714
+ pair_nodes.size.times { |i| values << pair_nodes[i][2] }
688
715
  values
689
716
  end
690
717
  end
@@ -707,7 +734,8 @@ class Sexp
707
734
  if first_node
708
735
  while true
709
736
  array_size += 1
710
- first_node = s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
737
+ first_node =
738
+ s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
711
739
  if :args_add != first_node.sexp_type
712
740
  if :array == first_node.sexp_type
713
741
  array_size += first_node.array_size
@@ -737,9 +765,13 @@ class Sexp
737
765
  def array_values
738
766
  case sexp_type
739
767
  when :array
740
- if nil == self[1] || [:words_new, :qwords_new, :symbols_new, :qsymbols_new].include?(self[1].sexp_type)
768
+ if nil == self[1] ||
769
+ %i[words_new qwords_new symbols_new qsymbols_new].include?(
770
+ self[1].sexp_type
771
+ )
741
772
  []
742
- elsif [:words_add, :qwords_add, :symbols_add, :qsymbols_add].include? self[1].sexp_type
773
+ elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1]
774
+ .sexp_type
743
775
  self[1].array_values
744
776
  else
745
777
  arguments.all
@@ -748,10 +780,12 @@ class Sexp
748
780
  values = []
749
781
  node = self
750
782
  while true
751
- if [:words_add, :qwords_add, :symbols_add, :qsymbols_add].include? node.sexp_type
783
+ if %i[words_add qwords_add symbols_add qsymbols_add].include? node
784
+ .sexp_type
752
785
  values.unshift node[2]
753
786
  node = node[1]
754
- elsif [:words_new, :qwords_new, :symbols_new, :qsymbols_new].include? node.sexp_type
787
+ elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node
788
+ .sexp_type
755
789
  break
756
790
  end
757
791
  end
@@ -808,22 +842,14 @@ class Sexp
808
842
  # @return [String] to_s
809
843
  def to_s
810
844
  case sexp_type
811
- when :string_literal, :xstring_literal, :string_content, :const_ref, :symbol_literal, :symbol,
812
- :args_add_block, :var_ref, :vcall, :var_field,
845
+ when :string_literal, :xstring_literal, :string_content, :const_ref,
846
+ :symbol_literal, :symbol, :args_add_block, :var_ref, :vcall, :var_field,
813
847
  :@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar
814
848
  self[1].to_s
815
849
  when :string_add
816
- if s(:string_content) == self[1]
817
- self[2].to_s
818
- else
819
- self[1].to_s
820
- end
850
+ s(:string_content) == self[1] ? self[2].to_s : self[1].to_s
821
851
  when :args_add
822
- if s(:args_new) == self[1]
823
- self[2].to_s
824
- else
825
- self[1].to_s
826
- end
852
+ s(:args_new) == self[1] ? self[2].to_s : self[1].to_s
827
853
  when :qwords_add
828
854
  self[2].to_s
829
855
  when :word_add
@@ -839,13 +865,17 @@ class Sexp
839
865
  when :top_const_ref
840
866
  "::#{self[1]}"
841
867
  else
842
- ""
868
+ ''
843
869
  end
844
870
  end
845
871
 
846
872
  # check if the self node is a const.
847
873
  def const?
848
- :@const == self.sexp_type || ([:var_ref, :vcall].include?(self.sexp_type) && :@const == self[1].sexp_type)
874
+ :@const == self.sexp_type ||
875
+ (
876
+ %i[var_ref vcall].include?(self.sexp_type) &&
877
+ :@const == self[1].sexp_type
878
+ )
849
879
  end
850
880
 
851
881
  # true
@@ -862,25 +892,42 @@ class Sexp
862
892
  def remove_line_and_column
863
893
  node = self.clone
864
894
  last_node = node.last
865
- if Sexp === last_node && last_node.size == 2 && last_node.first.is_a?(Integer) && last_node.last.is_a?(Integer)
895
+ if Sexp === last_node && last_node.size == 2 &&
896
+ last_node.first.is_a?(Integer) &&
897
+ last_node.last.is_a?(Integer)
866
898
  node.delete_at(-1)
867
899
  end
868
900
  node.sexp_body.each_with_index do |child, index|
869
- if Sexp === child
870
- node[index+1] = child.remove_line_and_column
871
- end
901
+ node[index + 1] = child.remove_line_and_column if Sexp === child
872
902
  end
873
903
  node
874
904
  end
875
905
 
876
906
  # if the return value of these methods is nil, then return CodeAnalyzer::Nil.new instead
877
- [:sexp_type, :receiver, :message, :arguments, :argument, :class_name, :base_class, :method_name,
878
- :body, :block_node, :conditional_statement, :left_value, :right_value].each do |method|
907
+ %i[
908
+ sexp_type
909
+ receiver
910
+ message
911
+ arguments
912
+ argument
913
+ class_name
914
+ base_class
915
+ method_name
916
+ body
917
+ block_node
918
+ conditional_statement
919
+ left_value
920
+ right_value
921
+ ].each do |method|
879
922
  class_eval <<-EOS
880
923
  alias_method :origin_#{method}, :#{method}
881
924
 
882
- def #{method}
883
- ret = origin_#{method}
925
+ def #{
926
+ method
927
+ }
928
+ ret = origin_#{
929
+ method
930
+ }
884
931
  ret.nil? ? CodeAnalyzer::Nil.new : ret
885
932
  end
886
933
  EOS