code_analyzer 0.5.4 → 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,911 +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 self[2] if :array == self[2].sexp_type
710
-
711
- self[2].to_s
712
- end
713
- end
714
-
715
- # Get the array size.
716
- #
717
- # s(:array,
718
- # s(:args_add,
719
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
720
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
721
- # )
722
- # )
723
- # => 2
724
- #
725
- # @return [Integer] array size
726
- def array_size
727
- if :array == sexp_type
728
- first_node = self[1]
729
- array_size = 0
730
- if first_node
731
- while true
732
- array_size += 1
733
- first_node = s(:args_new) == first_node[1] ? first_node[2] : first_node[1]
734
- if :args_add != first_node.sexp_type
735
- array_size += first_node.array_size if :array == first_node.sexp_type
736
- break
737
- end
738
- end
739
- end
740
- array_size
741
- end
742
- end
743
-
744
- # Get the array values.
745
- #
746
- # s(:array,
747
- # s(:args_add,
748
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
749
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
750
- # )
751
- # )
752
- # => [
753
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2)))),
754
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
755
- # ]
756
- #
757
- # @return [Array] array values
758
- def array_values
759
- case sexp_type
760
- when :array
761
- if nil == self[1] || %i[words_new qwords_new symbols_new qsymbols_new].include?(self[1].sexp_type)
762
- []
763
- elsif %i[words_add qwords_add symbols_add qsymbols_add].include? self[1].sexp_type
764
- self[1].array_values
765
- else
766
- arguments.all
767
- end
768
- when :words_add, :qwords_add, :symbols_add, :qsymbols_add
769
- values = []
770
- node = self
771
- while true
772
- if %i[words_add qwords_add symbols_add qsymbols_add].include? node.sexp_type
773
- values.unshift node[2]
774
- node = node[1]
775
- elsif %i[words_new qwords_new symbols_new qsymbols_new].include? node.sexp_type
776
- break
777
- end
778
- end
779
- values
780
- else
781
- []
782
- end
783
- end
784
-
785
- # old method for alias node.
786
- #
787
- # s(:alias,
788
- # s(:symbol_literal, s(:@ident, "new", s(1, 6))),
789
- # s(:symbol_literal, s(:@ident, "old", s(1, 10)))
790
- # )
791
- # => s(:symbol_literal, s(:@ident, "old", s(1, 10))),
792
- def old_method
793
- self[2]
794
- end
795
-
796
- # new method for alias node.
797
- #
798
- # s(:alias,
799
- # s(:symbol_literal, s(:@ident, "new", s(1, 6))),
800
- # s(:symbol_literal, s(:@ident, "old", s(1, 10)))
801
- # )
802
- # => s(:symbol_literal, s(:@ident, "new", s(1, 6))),
803
- def new_method
804
- self[1]
805
- end
806
-
807
- # To object.
808
- #
809
- # s(:array,
810
- # s(:args_add,
811
- # s(:args_add, s(:args_new), s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "first_name", s(1, 2))))),
812
- # s(:string_literal, s(:string_add, s(:string_content), s(:@tstring_content, "last_name", s(1, 16))))
813
- # )
814
- # )
815
- # => ["first_name", "last_name"]
816
- #
817
- # @return [Object]
818
- def to_object
819
- case sexp_type
820
- when :array
821
- array_values.map(&:to_s)
822
- else
823
- to_s
824
- end
825
- end
826
-
827
- # to_s.
828
- #
829
- # @return [String] to_s
830
- def to_s
831
- case sexp_type
832
- when :string_literal, :xstring_literal, :string_content, :const_ref, :symbol_literal, :symbol, :args_add_block, :var_ref,
833
- :vcall, :var_field, :@ident, :@tstring_content, :@const, :@ivar, :@kw, :@gvar, :@cvar, :@period
834
- self[1].to_s
835
- when :string_add
836
- s(:string_content) == self[1] ? self[2].to_s : self[1].to_s
837
- when :args_add
838
- s(:args_new) == self[1] ? self[2].to_s : self[1].to_s
839
- when :qwords_add
840
- self[2].to_s
841
- when :word_add
842
- self[2].to_s
843
- when :const_path_ref
844
- "#{self[1]}::#{self[2]}"
845
- when :@label
846
- self[1].to_s[0..-2]
847
- when :aref
848
- "#{self[1]}[#{self[2]}]"
849
- when :call, :field
850
- "#{self.receiver}.#{self.message}"
851
- when :top_const_ref
852
- "::#{self[1]}"
853
- else
854
- ''
855
- end
856
- end
857
-
858
- # check if the self node is a const.
859
- def const?
860
- :@const == self.sexp_type || (%i[var_ref vcall].include?(self.sexp_type) && :@const == self[1].sexp_type)
861
- end
862
-
863
- # true
864
- def present?
865
- true
866
- end
867
-
868
- # false
869
- def blank?
870
- false
871
- end
872
-
873
- # remove the line and column info from sexp.
874
- def remove_line_and_column
875
- node = self.clone
876
- last_node = node.last
877
- if Sexp === last_node && last_node.size == 2 && last_node.first.is_a?(Integer) && last_node.last.is_a?(Integer)
878
- node.delete_at(-1)
879
- end
880
- node.sexp_body.each_with_index { |child, index| node[index + 1] = child.remove_line_and_column if Sexp === child }
881
- node
882
- end
883
-
884
- # if the return value of these methods is nil, then return CodeAnalyzer::Nil.new instead
885
- %i[
886
- sexp_type
887
- receiver
888
- message
889
- arguments
890
- argument
891
- class_name
892
- base_class
893
- method_name
894
- body
895
- block_node
896
- conditional_statement
897
- left_value
898
- right_value
899
- ].each do |method|
900
- class_eval <<-EOS
901
- alias_method :origin_#{method}, :#{method}
902
-
903
- def #{method}
904
- ret = origin_#{
905
- method
906
- }
907
- ret.nil? ? CodeAnalyzer::Nil.new : ret
908
- end
909
- EOS
910
- end
911
- end