code_analyzer 0.5.5 → 0.7.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.
@@ -1,912 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'sexp'
4
-
5
- class Sexp
6
- # check current node.
7
- #
8
- # @param [CodeAnalyzer::CheckingVisitor::Default] visitor the visitor to check current node
9
- def check(visitor)
10
- visitor.check_node(self)
11
- end
12
-
13
- # return the line number of a sexp node.
14
- #
15
- # s(:@ident, "test", s(2, 12)
16
- # => 2
17
-
18
- def line_number
19
- case sexp_type
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
- self[1].line_number
24
- when :assoclist_from_args, :bare_assoc_hash
25
- self[1][0].line_number
26
- when :string_add, :opassign, :unary, :stmts_add
27
- self[2].line_number
28
- when :array
29
- array_values.first.line_number
30
- when :mlhs_add
31
- self.last.line_number
32
- when :params
33
- self[1][0].line_number if self[1].is_a? Array
34
- else
35
- self.last.first if self.last.is_a? Array
36
- end
37
- end
38
-
39
- # return child nodes of a sexp node.
40
- #
41
- # @return [Array] child nodes.
42
- def children
43
- find_all { |sexp| Sexp === sexp }
44
- end
45
-
46
- # recursively find all child nodes, and yeild each child node.
47
- def recursive_children
48
- children.each do |child|
49
- yield child
50
- child.recursive_children { |c| yield c }
51
- end
52
- end
53
-
54
- # grep all the recursive child nodes with conditions, and yield each match node.
55
- #
56
- # @param [Hash] options grep conditions
57
- #
58
- # options is the grep conditions, like
59
- #
60
- # sexp_type: :call,
61
- # receiver: "Post",
62
- # message: ["find", "new"]
63
- # to_s: "devise"
64
- #
65
- # the condition key is one of :sexp_type, :receiver, :message, :to_s,
66
- # the condition value can be Symbol, Array or Sexp.
67
- def grep_nodes(options)
68
- sexp_type = options[:sexp_type]
69
- receiver = options[:receiver]
70
- message = options[:message]
71
- to_s = options[:to_s]
72
- self.recursive_children do |child|
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))
84
- yield child
85
- end
86
- end
87
- end
88
-
89
- # grep all the recursive child nodes with conditions, and yield the first match node.
90
- #
91
- # @param [Hash] options grep conditions
92
- #
93
- # options is the grep conditions, like
94
- #
95
- # sexp_type: :call,
96
- # receiver: s(:const, Post),
97
- # message: [:find, :new]
98
- #
99
- # the condition key is one of :sexp_type, :receiver, :message, and to_s,
100
- # the condition value can be Symbol, Array or Sexp.
101
- def grep_node(options)
102
- result = CodeAnalyzer::Nil.new
103
- grep_nodes(options) do |node|
104
- result = node
105
- break
106
- end
107
- result
108
- end
109
-
110
- # grep all the recursive child nodes with conditions, and get the count of match nodes.
111
- #
112
- # @param [Hash] options grep conditions
113
- # @return [Integer] the count of metch nodes
114
- def grep_nodes_count(options)
115
- count = 0
116
- grep_nodes(options) { |node| count += 1 }
117
- count
118
- end
119
-
120
- # Get receiver node.
121
- #
122
- # s(:call,
123
- # s(:var_ref,
124
- # s(:@ident, "user", s(1, 0))
125
- # ),
126
- # :".",
127
- # s(:@ident, "name", s(1, 5))
128
- # )
129
- # => s(:var_ref,
130
- # s(:@ident, "user", s(1, 0))
131
- # )
132
- #
133
- # @return [Sexp] receiver node
134
- def receiver
135
- case sexp_type
136
- when :assign, :field, :call, :binary, :command_call
137
- self[1]
138
- when :method_add_arg, :method_add_block
139
- self[1].receiver
140
- end
141
- end
142
-
143
- # Get the module name of the module node.
144
- #
145
- # s(:module,
146
- # s(:const_ref, s(:@const, "Admin", s(1, 7))),
147
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
148
- # )
149
- # => s(:const_ref, s(:@const, "Admin", s(1, 7))),
150
- #
151
- # @return [Sexp] module name node
152
- def module_name
153
- self[1] if :module == sexp_type
154
- end
155
-
156
- # Get the class name of the class node.
157
- #
158
- # s(:class,
159
- # s(:const_ref, s(:@const, "User", s(1, 6))),
160
- # nil,
161
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
162
- # )
163
- # => s(:const_ref, s(:@const, "User", s(1, 6))),
164
- #
165
- # @return [Sexp] class name node
166
- def class_name
167
- self[1] if :class == sexp_type
168
- end
169
-
170
- # Get the base class of the class node.
171
- #
172
- # s(:class,
173
- # s(:const_ref, s(:@const, "User", s(1, 6))),
174
- # s(:const_path_ref, s(:var_ref, s(:@const, "ActiveRecord", s(1, 13))), s(:@const, "Base", s(1, 27))),
175
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
176
- # )
177
- # => s(:const_path_ref, s(:var_ref, s(:@const, "ActiveRecord", s(1, 13))), s(:@const, "Base", s(1, 27))),
178
- #
179
- # @return [Sexp] base class of class node
180
- def base_class
181
- self[2] if :class == sexp_type
182
- end
183
-
184
- # Get the left value of the assign node.
185
- #
186
- # s(:assign,
187
- # s(:var_field, s(:@ident, "user", s(1, 0))),
188
- # s(:var_ref, s(:@ident, "current_user", s(1, 7)))
189
- # )
190
- # => s(:var_field, s(:@ident, "user", s(1, 0))),
191
- #
192
- # @return [Symbol] left value of lasgn or iasgn node
193
- def left_value
194
- self[1] if :assign == sexp_type
195
- end
196
-
197
- # Get the right value of assign node.
198
- #
199
- # s(:assign,
200
- # s(:var_field, s(:@ident, "user", s(1, 0))),
201
- # s(:var_ref, s(:@ident, "current_user", s(1, 7)))
202
- # )
203
- # => s(:var_ref, s(:@ident, "current_user", s(1, 7)))
204
- #
205
- # @return [Sexp] right value of assign node
206
- def right_value
207
- self[2] if :assign == sexp_type
208
- end
209
-
210
- # Get the message node.
211
- #
212
- # s(:command,
213
- # s(:@ident, "has_many", s(1, 0)),
214
- # s(:args_add_block,
215
- # s(:args_add, s(:args_new),
216
- # s(:symbol_literal, s(:symbol, s(:@ident, "projects", s(1, 10))))
217
- # ),
218
- # false
219
- # )
220
- # )
221
- # => s(:@ident, "has_many", s(1, 0)),
222
- #
223
- # @return [Symbol] message node
224
- def message
225
- case sexp_type
226
- when :command, :fcall
227
- self[1]
228
- when :binary
229
- self[2]
230
- when :command_call, :field, :call
231
- self[3]
232
- when :method_add_arg, :method_add_block
233
- self[1].message
234
- end
235
- end
236
-
237
- # Get arguments node.
238
- #
239
- # s(:command,
240
- # s(:@ident, "resources", s(1, 0)),
241
- # s(:args_add_block,
242
- # s(:args_add, s(:args_new),
243
- # s(:symbol_literal, s(:symbol, s(:@ident, "posts", s(1, 11))))
244
- # ), false
245
- # )
246
- # )
247
- # => s(:args_add_block,
248
- # s(:args_add, s(:args_new),
249
- # s(:symbol_literal, s(:symbol, s(:@ident, "posts", s(1, 11))))
250
- # ), false
251
- # )
252
- #
253
- # @return [Sexp] arguments node
254
- def arguments
255
- case sexp_type
256
- when :command
257
- self[2]
258
- when :command_call
259
- self[4]
260
- when :method_add_arg
261
- self[2].arguments
262
- when :method_add_block
263
- self[1].arguments
264
- when :arg_paren
265
- self[1]
266
- when :array
267
- self
268
- end
269
- end
270
-
271
- # Get only argument for binary.
272
- #
273
- # s(:binary,
274
- # s(:var_ref, s(:@ident, "user", s(1, 0))),
275
- # :==,
276
- # s(:var_ref, s(:@ident, "current_user", s(1, 8)))
277
- # )
278
- # => s(:var_ref, s(:@ident, "current_user", s(1, 8)))
279
- #
280
- # @return [Sexp] argument node
281
- def argument
282
- self[3] if :binary == sexp_type
283
- end
284
-
285
- # Get all arguments.
286
- #
287
- # s(:args_add_block,
288
- # s(:args_add,
289
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "hello", s(1, 6))))),
290
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "world", s(1, 15))))
291
- # ), false
292
- # )
293
- # => [
294
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "hello", s(1, 6))))),
295
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "world", s(1, 15))))
296
- # ]
297
- #
298
- # @return [Array] all arguments
299
- def all
300
- nodes = []
301
- case sexp_type
302
- when :args_add_block, :array
303
- if :args_new == self[1].sexp_type
304
- nodes << self[2]
305
- else
306
- node = self[1]
307
- while true
308
- if %i[args_add args_add_star].include? node.sexp_type
309
- nodes.unshift node[2]
310
- node = node[1]
311
- elsif :args_new == node.sexp_type
312
- break
313
- end
314
- end
315
- end
316
- when :args_add
317
- nodes.unshift self[2]
318
- end
319
- nodes
320
- end
321
-
322
- # Get the conditional statement of if node.
323
- #
324
- # s(:if,
325
- # s(:var_ref, s(:@kw, "true", s(1, 3))),
326
- # s(:stmts_add, s(:stmts_new), s(:void_stmt)),
327
- # nil
328
- # )
329
- # => s(:var_ref, s(:@kw, "true", s(1, 3))),
330
- #
331
- # @return [Sexp] conditional statement of if node
332
- def conditional_statement
333
- self[1] if %i[if unless elsif ifop if_mod unless_mod].include? sexp_type
334
- end
335
-
336
- # Get all condition nodes.
337
- #
338
- # s(:binary,
339
- # s(:binary,
340
- # s(:var_ref, s(:@ident, "user", s(1, 0))),
341
- # :==,
342
- # s(:var_ref, s(:@ident, "current_user", s(1, 8)))
343
- # ),
344
- # :"&&",
345
- # s(:call,
346
- # s(:var_ref, s(:@ident, "user", s(1, 24))),
347
- # :".",
348
- # s(:@ident, "valid?", s(1, 29))
349
- # )
350
- # )
351
- # => [
352
- # s(:binary,
353
- # s(:var_ref, s(:@ident, "user", s(1, 0))),
354
- # :==,
355
- # s(:var_ref, s(:@ident, "current_user", s(1, 8)))
356
- # ),
357
- # s(:call,
358
- # s(:var_ref, s(:@ident, "user", s(1, 24))),
359
- # :".",
360
- # s(:@ident, "valid?", s(1, 29))
361
- # )
362
- # ]
363
- #
364
- # @return [Array] all condition nodes
365
- def all_conditions
366
- nodes = []
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)
369
- nodes += self[1].all_conditions
370
- else
371
- nodes << self[1]
372
- end
373
- if :binary == self[3].sexp_type && %w[&& || and or].include?(self[3][2].to_s)
374
- nodes += self[3].all_conditions
375
- else
376
- nodes << self[3]
377
- end
378
- else
379
- self
380
- end
381
- end
382
-
383
- # Get the method name of def node.
384
- #
385
- # s(:def,
386
- # s(:@ident, "show", s(1, 4)),
387
- # s(:params, nil, nil, nil, nil, nil),
388
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
389
- # )
390
- # => s(:@ident, "show", s(1, 4)),
391
- #
392
- # @return [Sexp] method name node
393
-
394
- def method_name
395
- case sexp_type
396
- when :def
397
- self[1]
398
- when :defs
399
- self[3]
400
- else
401
-
402
- end
403
- end
404
-
405
- # Get body node.
406
- #
407
- # s(:class,
408
- # s(:const_ref, s(:@const, "User", s(1, 6))),
409
- # nil,
410
- # s(:bodystmt,
411
- # s(:stmts_add, s(:stmts_new),
412
- # s(:def,
413
- # s(:@ident, "login", s(1, 16)),
414
- # s(:params, nil, nil, nil, nil, nil),
415
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
416
- # )
417
- # ), nil, nil, nil
418
- # )
419
- # )
420
- # => s(:bodystmt,
421
- # s(:stmts_add, s(:stmts_new),
422
- # s(:def,
423
- # s(:@ident, "login", s(1, 16)),
424
- # s(:params, nil, nil, nil, nil, nil),
425
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
426
- # )
427
- # ), nil, nil, nil
428
- # )
429
- #
430
- # @return [Sexp] body node
431
-
432
- def body
433
- case sexp_type
434
- when :else
435
- self[1]
436
- when :module, :if, :elsif, :unless, :if_mod, :unless_mod, :ifop
437
- self[2]
438
- when :class, :def
439
- self[3]
440
- when :defs
441
- self[5]
442
- end
443
- end
444
-
445
- # Get block node.
446
- #
447
- # s(:method_add_block,
448
- # s(:command,
449
- # s(:@ident, "resources", s(1, 0)),
450
- # s(:args_add_block, s(:args_add, s(:args_new), s(:symbol_literal, s(:symbol, s(:@ident, "posts", s(1, 11))))), false)
451
- # ),
452
- # s(:do_block, nil,
453
- # s(:stmts_add, s(:stmts_add, s(:stmts_new), s(:void_stmt)),
454
- # s(:command,
455
- # s(:@ident, "resources", s(1, 21)),
456
- # s(:args_add_block, s(:args_add, s(:args_new), s(:symbol_literal, s(:symbol, s(:@ident, "comments", s(1, 32))))), false))
457
- # )
458
- # )
459
- # )
460
- # => s(:do_block, nil,
461
- # s(:stmts_add, s(:stmts_add, s(:stmts_new), s(:void_stmt)),
462
- # s(:command,
463
- # s(:@ident, "resources", s(1, 21)),
464
- # s(:args_add_block, s(:args_add, s(:args_new), s(:symbol_literal, s(:symbol, s(:@ident, "comments", s(1, 32))))), false))
465
- # )
466
- # )
467
- #
468
- # @return [Sexp] body node
469
- def block_node
470
- case sexp_type
471
- when :method_add_block
472
- self[2]
473
- end
474
- end
475
-
476
- # Get all statements nodes.
477
- #
478
- # s(:bodystmt,
479
- # s(:stmts_add,
480
- # s(:stmts_add, s(:stmts_new),
481
- # s(:def,
482
- # s(:@ident, "login?", s(1, 16)),
483
- # s(:params, nil, nil, nil, nil, nil),
484
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
485
- # )
486
- # ),
487
- # s(:def,
488
- # s(:@ident, "admin?", s(1, 33)),
489
- # s(:params, nil, nil, nil, nil, nil),
490
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
491
- # )
492
- # ), nil, nil, nil
493
- # )
494
- # => [
495
- # s(:def,
496
- # s(:@ident, "login?", s(1, 16)),
497
- # s(:params, nil, nil, nil, nil, nil),
498
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
499
- # ),
500
- # s(:def,
501
- # s(:@ident, "admin?", s(1, 33)),
502
- # s(:params, nil, nil, nil, nil, nil),
503
- # s(:bodystmt, s(:stmts_add, s(:stmts_new), s(:void_stmt)), nil, nil, nil)
504
- # )
505
- # ]
506
- #
507
- # @return [Array] all statements
508
- def statements
509
- stmts = []
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
519
- if node
520
- while true
521
- if :stmts_add == node.sexp_type && s(:void_stmt) != node[2]
522
- stmts.unshift node[2]
523
- node = node[1]
524
- else
525
- break
526
- end
527
- end
528
- end
529
- stmts
530
- end
531
-
532
- # Get expcetion class of rescue node.
533
- #
534
- # s(:rescue,
535
- # s(
536
- # s(:var_ref,
537
- # s(:@const, "CustomException", s(1, 17))
538
- # )
539
- # ),
540
- # nil,
541
- # s(:stmts_add, s(:stmts_new), s(:void_stmt)),
542
- # nil
543
- # )
544
- # => s(s(:var_ref, s(:@const, "CustomException", s(1, 17))))
545
- def exception_classes
546
- if :rescue == sexp_type
547
- return [] unless self[1]
548
-
549
- if :mrhs_add == self[1].sexp_type
550
- exceptions = Array.new(self[1][2])
551
- arg_nodes = self[1][1][1]
552
- while :args_add == arg_nodes.sexp_type
553
- exceptions.unshift arg_nodes[2]
554
- arg_nodes = arg_nodes[1]
555
- end
556
- exceptions
557
- else
558
- self[1]
559
- end
560
- end
561
- end
562
-
563
- # Get exception variable of rescue node.
564
- #
565
- # s(:rescue,
566
- # nil,
567
- # s(:var_field, s(:@ident, "e", s(1, 20))),
568
- # s(:stmts_add, s(:stmts_new), s(:void_stmt)),
569
- # nil
570
- # )
571
- # => s(:var_field, s(:@ident, "e", s(1, 20)))
572
- def exception_variable
573
- self[2] if :rescue == sexp_type
574
- end
575
-
576
- # Get hash value node.
577
- #
578
- # s(:hash,
579
- # s(:assoclist_from_args,
580
- # s(
581
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))),
582
- # s(:assoc_new, s(:@label, "last_name:", s(1, 24)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Huang", s(1, 36)))))
583
- # )
584
- # )
585
- # )
586
- # => s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))
587
- #
588
- # @return [Sexp] hash value node
589
- def hash_value(key)
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
-
598
- end
599
-
600
- pair_nodes.size.times { |i| return pair_nodes[i][2] if key == pair_nodes[i][1].to_s } if pair_nodes
601
- CodeAnalyzer::Nil.new
602
- end
603
-
604
- # Get hash size.
605
- #
606
- # s(:hash,
607
- # s(:assoclist_from_args,
608
- # s(
609
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))),
610
- # s(:assoc_new, s(:@label, "last_name:", s(1, 24)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Huang", s(1, 36)))))
611
- # )
612
- # )
613
- # )
614
- # => 2
615
- #
616
- # @return [Integer] hash size
617
- def hash_size
618
- case sexp_type
619
- when :hash
620
- self[1].hash_size
621
- when :assoclist_from_args
622
- self[1].size
623
- when :bare_assoc_hash
624
- self[1].size
625
- end
626
- end
627
-
628
- # Get the hash keys.
629
- #
630
- # s(:hash,
631
- # s(:assoclist_from_args,
632
- # s(
633
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))),
634
- # s(:assoc_new, s(:@label, "last_name:", s(1, 24)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Huang", s(1, 36)))))
635
- # )
636
- # )
637
- # )
638
- # => ["first_name", "last_name"]
639
- #
640
- # @return [Array] hash keys
641
- def hash_keys
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
651
- if pair_nodes
652
- keys = []
653
- pair_nodes.size.times { |i| keys << pair_nodes[i][1].to_s }
654
- keys
655
- end
656
- end
657
-
658
- # Get the hash values.
659
- #
660
- # s(:hash,
661
- # s(:assoclist_from_args,
662
- # s(
663
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))),
664
- # s(:assoc_new, s(:@label, "last_name:", s(1, 24)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Huang", s(1, 36)))))
665
- # )
666
- # )
667
- # )
668
- # => [
669
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14)))),
670
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Huang", s(1, 36))))
671
- # ]
672
- #
673
- # @return [Array] hash values
674
- def hash_values
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
684
- if pair_nodes
685
- values = []
686
- pair_nodes.size.times { |i| values << pair_nodes[i][2] }
687
- values
688
- end
689
- end
690
-
691
- # Get the hash key
692
- #
693
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14)))))
694
- # =>
695
- # s(:@label, "first_name:", s(1, 1))
696
- def key
697
- if :assoc_new == sexp_type
698
- self[1].to_s
699
- end
700
- end
701
-
702
- # Get the hash value
703
- #
704
- # s(:assoc_new, s(:@label, "first_name:", s(1, 1)), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14)))))
705
- # =>
706
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "Richard", s(1, 14))))
707
- def value
708
- if :assoc_new == sexp_type
709
- return nil if self[2].nil?
710
- return self[2] if :array == self[2].sexp_type
711
-
712
- self[2].to_s
713
- end
714
- end
715
-
716
- # Get the array size.
717
- #
718
- # s(:array,
719
- # s(:args_add,
720
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
721
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
722
- # )
723
- # )
724
- # => 2
725
- #
726
- # @return [Integer] array size
727
- def array_size
728
- if :array == sexp_type
729
- first_node = self[1]
730
- array_size = 0
731
- if first_node
732
- while true
733
- array_size += 1
734
- first_node = s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
735
- if :args_add != first_node.sexp_type
736
- array_size += first_node.array_size if :array == first_node.sexp_type
737
- break
738
- end
739
- end
740
- end
741
- array_size
742
- end
743
- end
744
-
745
- # Get the array values.
746
- #
747
- # s(:array,
748
- # s(:args_add,
749
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
750
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
751
- # )
752
- # )
753
- # => [
754
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2)))),
755
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
756
- # ]
757
- #
758
- # @return [Array] array values
759
- def array_values
760
- case sexp_type
761
- when :array
762
- if nil == self[1] || %i[words_new qwords_new symbols_new qsymbols_new].include?(self[1].sexp_type)
763
- []
764
- elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1].sexp_type
765
- self[1].array_values
766
- else
767
- arguments.all
768
- end
769
- when :words_add, :qwords_add, :symbols_add, :qsymbols_add
770
- values = []
771
- node = self
772
- while true
773
- if %i[words_add qwords_add symbols_add qsymbols_add].include? node.sexp_type
774
- values.unshift node[2]
775
- node = node[1]
776
- elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node.sexp_type
777
- break
778
- end
779
- end
780
- values
781
- else
782
- []
783
- end
784
- end
785
-
786
- # old method for alias node.
787
- #
788
- # s(:alias,
789
- # s(:symbol_literal, s(:@ident, "new", s(1, 6))),
790
- # s(:symbol_literal, s(:@ident, "old", s(1, 10)))
791
- # )
792
- # => s(:symbol_literal, s(:@ident, "old", s(1, 10))),
793
- def old_method
794
- self[2]
795
- end
796
-
797
- # new method for alias node.
798
- #
799
- # s(:alias,
800
- # s(:symbol_literal, s(:@ident, "new", s(1, 6))),
801
- # s(:symbol_literal, s(:@ident, "old", s(1, 10)))
802
- # )
803
- # => s(:symbol_literal, s(:@ident, "new", s(1, 6))),
804
- def new_method
805
- self[1]
806
- end
807
-
808
- # To object.
809
- #
810
- # s(:array,
811
- # s(:args_add,
812
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
813
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
814
- # )
815
- # )
816
- # => ["first_name", "last_name"]
817
- #
818
- # @return [Object]
819
- def to_object
820
- case sexp_type
821
- when :array
822
- array_values.map(&:to_s)
823
- else
824
- to_s
825
- end
826
- end
827
-
828
- # to_s.
829
- #
830
- # @return [String] to_s
831
- def to_s
832
- case sexp_type
833
- when :string_literal, :xstring_literal, :string_content, :const_ref, :symbol_literal, :symbol, :args_add_block, :var_ref,
834
- :vcall, :var_field, :@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar, :@period
835
- self[1].to_s
836
- when :string_add
837
- s(:string_content) == self[1] ? self[2].to_s : self[1].to_s
838
- when :args_add
839
- s(:args_new) == self[1] ? self[2].to_s : self[1].to_s
840
- when :qwords_add
841
- self[2].to_s
842
- when :word_add
843
- self[2].to_s
844
- when :const_path_ref
845
- "#{self[1]}::#{self[2]}"
846
- when :@label
847
- self[1].to_s[0..-2]
848
- when :aref
849
- "#{self[1]}[#{self[2]}]"
850
- when :call, :field
851
- "#{self.receiver}.#{self.message}"
852
- when :top_const_ref
853
- "::#{self[1]}"
854
- else
855
- ''
856
- end
857
- end
858
-
859
- # check if the self node is a const.
860
- def const?
861
- :@const == self.sexp_type || (%i[var_ref vcall].include?(self.sexp_type) && :@const == self[1].sexp_type)
862
- end
863
-
864
- # true
865
- def present?
866
- true
867
- end
868
-
869
- # false
870
- def blank?
871
- false
872
- end
873
-
874
- # remove the line and column info from sexp.
875
- def remove_line_and_column
876
- node = self.clone
877
- last_node = node.last
878
- if Sexp === last_node && last_node.size == 2 && last_node.first.is_a?(Integer) && last_node.last.is_a?(Integer)
879
- node.delete_at(-1)
880
- end
881
- node.sexp_body.each_with_index { |child, index| node[index + 1] = child.remove_line_and_column if Sexp === child }
882
- node
883
- end
884
-
885
- # if the return value of these methods is nil, then return CodeAnalyzer::Nil.new instead
886
- %i[
887
- sexp_type
888
- receiver
889
- message
890
- arguments
891
- argument
892
- class_name
893
- base_class
894
- method_name
895
- body
896
- block_node
897
- conditional_statement
898
- left_value
899
- right_value
900
- ].each do |method|
901
- class_eval <<-EOS
902
- alias_method :origin_#{method}, :#{method}
903
-
904
- def #{method}
905
- ret = origin_#{
906
- method
907
- }
908
- ret.nil? ? CodeAnalyzer::Nil.new : ret
909
- end
910
- EOS
911
- end
912
- end