Class: Sexp

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_parser/bm_sexp.rb

Overview

Sexp changes from ruby_parser and some changes for caching hash value and tracking ‘original’ line number of a Sexp.

Constant Summary collapse

ASSIGNMENT_BOOL =
[:gasgn, :iasgn, :lasgn, :cvdecl, :cvasgn, :cdecl, :or, :and, :colon2, :op_asgn_or]
CALLS =
[:call, :attrasgn, :safe_call, :safe_attrasgn]

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

silence redefined method warning



9
# File 'lib/ruby_parser/bm_sexp.rb', line 9

alias_method :method_missing, :method_missing

Instance Attribute Details

#or_depthObject

Returns the value of attribute or_depth.



5
6
7
# File 'lib/ruby_parser/bm_sexp.rb', line 5

def or_depth
  @or_depth
end

#original_lineObject

Returns the value of attribute original_line.



5
6
7
# File 'lib/ruby_parser/bm_sexp.rb', line 5

def original_line
  @original_line
end

Instance Method Details

#<<(arg) ⇒ Object



99
100
101
102
# File 'lib/ruby_parser/bm_sexp.rb', line 99

def << arg
  @my_hash_value = nil
  old_push arg
end

#arglistObject

Returns arglist for method call. This differs from Sexp#args, as Sexp#args does not return a ‘real’ Sexp (it does not have a node type) but Sexp#arglist returns a s(:arglist, …)

s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
                                             ^------------ arglist ------------^


200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ruby_parser/bm_sexp.rb', line 200

def arglist
  expect :call, :attrasgn, :safe_call, :safe_attrasgn, :super, :zsuper

  case self.node_type
  when :call, :attrasgn, :safe_call, :safe_attrasgn
    self.sexp_body(3).unshift :arglist
  when :super, :zsuper
    if self[1]
      self.sexp_body.unshift :arglist
    else
      Sexp.new(:arglist)
    end
  end
end

#arglist=(exp) ⇒ Object

Sets the arglist in a method call.



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ruby_parser/bm_sexp.rb', line 176

def arglist= exp
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  @my_hash_value = nil
  start_index = 3

  if exp.is_a? Sexp and exp.node_type == :arglist
    exp = exp.sexp_body
  end

  exp.each_with_index do |e, i|
    self[start_index + i] = e
  end
end

#argsObject

Returns arguments of a method call. This will be an ‘untyped’ Sexp.

s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
                                                         ^--------args--------^


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ruby_parser/bm_sexp.rb', line 219

def args
  expect :call, :attrasgn, :safe_call, :safe_attrasgn, :super, :zsuper

  case self.node_type
  when :call, :attrasgn, :safe_call, :safe_attrasgn
    if self[3]
      self.sexp_body(3)
    else
      Sexp.new
    end
  when :super, :zsuper
    if self[1]
      self.sexp_body
    else
      Sexp.new
    end
  end
end

#block(delete = nil) ⇒ Object

Returns block of a call with a block. Could be a single expression or a block:

s(:iter,
 s(:call, nil, :x, s(:arglist)),
  s(:lasgn, :y),
   s(:block, s(:lvar, :y), s(:call, nil, :z, s(:arglist))))
   ^-------------------- block --------------------------^


394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/ruby_parser/bm_sexp.rb', line 394

def block delete = nil
  unless delete.nil? #this is from RubyParser
    return find_node :block, delete
  end

  expect :iter, :scope, :resbody

  case self.node_type
  when :iter
    self[3]
  when :scope
    self[1]
  when :resbody
    #This is for Ruby2Ruby ONLY
    find_node :block
  end
end

#block_argsObject

Returns parameters for a block

s(:iter,
 s(:call, nil, :x, s(:arglist)),
  s(:lasgn, :y), <- block_args
   s(:call, nil, :p, s(:arglist, s(:lvar, :y))))


418
419
420
421
422
423
424
425
# File 'lib/ruby_parser/bm_sexp.rb', line 418

def block_args
  expect :iter
  if self[2] == 0 # ?! See https://github.com/presidentbeef/brakeman/issues/331
    return Sexp.new(:args)
  else
    self[2]
  end
end

#block_callObject

Method call associated with a block:

s(:iter,
 s(:call, nil, :x, s(:arglist)), <- block_call
  s(:lasgn, :y),
   s(:block, s(:lvar, :y), s(:call, nil, :z, s(:arglist))))


376
377
378
379
380
381
382
383
384
# File 'lib/ruby_parser/bm_sexp.rb', line 376

def block_call
  expect :iter

  if self[1].node_type == :lambda
    s(:call, nil, :lambda).line(self.line)
  else
    self[1]
  end
end

#bodyObject

Returns body of a method definition, class, or module. This will be an untyped Sexp containing a list of Sexps from the body.



531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/ruby_parser/bm_sexp.rb', line 531

def body
  expect :defn, :defs, :class, :module

  case self.node_type
  when :defn, :class
    self.sexp_body(3)
  when :defs
    self.sexp_body(4)
  when :module
    self.sexp_body(2)
  end
end

#body=(exp) ⇒ Object

Sets body, which is now a complicated process because the body is no longer a separate Sexp, but just a list of Sexps.



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/ruby_parser/bm_sexp.rb', line 503

def body= exp
  expect :defn, :defs, :class, :module
  @my_hash_value = nil

  case self.node_type
  when :defn, :class
    index = 3
  when :defs
    index = 4
  when :module
    index = 2
  end

  self.slice!(index..-1) #Remove old body

  if exp.first == :rlist
    exp = exp.sexp_body
  end

  #Insert new body
  exp.each do |e|
    self[index] = e
    index += 1
  end
end

#body_listObject

Like Sexp#body, except the returned Sexp is of type :rlist instead of untyped.



546
547
548
# File 'lib/ruby_parser/bm_sexp.rb', line 546

def body_list
  self.body.unshift :rlist
end

#callObject

Returns the call Sexp in a result returned from FindCall



582
583
584
585
586
# File 'lib/ruby_parser/bm_sexp.rb', line 582

def call
  expect :result

  self.last
end

#call_chainObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/ruby_parser/bm_sexp.rb', line 315

def call_chain
  expect :call, :attrasgn, :safe_call, :safe_attrasgn

  chain = []
  call = self

  while call.class == Sexp and CALLS.include? call.first 
    chain << call.method
    call = call.target
  end

  chain.reverse!
  chain
end

#class_nameObject Also known as: module_name



569
570
571
572
# File 'lib/ruby_parser/bm_sexp.rb', line 569

def class_name
  expect :class, :module
  self[1]
end

#combine(exp, line = nil) ⇒ Object

Join self and exp into an :or Sexp. Sets or_depth. Used for combining “branched” values in AliasProcessor.



83
84
85
86
87
88
89
# File 'lib/ruby_parser/bm_sexp.rb', line 83

def combine exp, line = nil
  combined = Sexp.new(:or, self, exp).line(line || -2)

  combined.or_depth = [self.or_depth, exp.or_depth].compact.reduce(0, :+) + 1

  combined
end

#compactObject



113
114
115
116
# File 'lib/ruby_parser/bm_sexp.rb', line 113

def compact
  @my_hash_value = nil
  old_compact
end

#conditionObject

Returns condition of an if expression:

s(:if,
 s(:lvar, :condition), <-- condition
 s(:lvar, :then_val),
 s(:lvar, :else_val)))


336
337
338
339
# File 'lib/ruby_parser/bm_sexp.rb', line 336

def condition
  expect :if
  self[1]
end

#condition=(exp) ⇒ Object



341
342
343
344
# File 'lib/ruby_parser/bm_sexp.rb', line 341

def condition= exp
  expect :if
  self[1] = exp
end

#deep_clone(line = nil) ⇒ Object

Create clone of Sexp and nested Sexps but not their non-Sexp contents. If a line number is provided, also sets line/original_line on all Sexps.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_parser/bm_sexp.rb', line 28

def deep_clone line = nil
  s = Sexp.new

  self.each do |e|
    if e.is_a? Sexp
      s << e.deep_clone(line)
    else
      s << e
    end
  end

  if line
    s.original_line = self.original_line || self.line
    s.line(line)
  else
    s.original_line = self.original_line
    s.line(self.line) if self.line
  end

  s
end

#each_arg(replace = false) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ruby_parser/bm_sexp.rb', line 238

def each_arg replace = false
  expect :call, :attrasgn, :safe_call, :safe_attrasgn, :super, :zsuper
  range = nil

  case self.node_type
  when :call, :attrasgn, :safe_call, :safe_attrasgn
    if self[3]
      range = (3...self.length)
    end
  when :super, :zsuper
    if self[1]
      range = (1...self.length)
    end
  end

  if range
    range.each do |i|
      res = yield self[i]
      self[i] = res if replace
    end
  end

  self
end

#each_arg!(&block) ⇒ Object



263
264
265
266
# File 'lib/ruby_parser/bm_sexp.rb', line 263

def each_arg! &block
  @my_hash_value = nil
  self.each_arg true, &block
end

#else_clauseObject

Returns ‘else’ clause of an if expression:

s(:if,
 s(:lvar, :condition),
 s(:lvar, :then_val),
 s(:lvar, :else_val)))
 ^---else caluse---^


365
366
367
368
# File 'lib/ruby_parser/bm_sexp.rb', line 365

def else_clause
  expect :if
  self[3]
end

#expect(*types) ⇒ Object

Raise a WrongSexpError if the nodes type does not match one of the expected types.



130
131
132
133
134
# File 'lib/ruby_parser/bm_sexp.rb', line 130

def expect *types
  unless types.include? self.node_type
    raise WrongSexpError, "Expected #{types.join ' or '} but given #{self.inspect}", caller[1..-1]
  end
end

#find_and_replace_all(*args) ⇒ Object



118
119
120
121
# File 'lib/ruby_parser/bm_sexp.rb', line 118

def find_and_replace_all *args
  @my_hash_value = nil
  old_fara(*args)
end

#find_node(*args) ⇒ Object



123
124
125
126
# File 'lib/ruby_parser/bm_sexp.rb', line 123

def find_node *args
  @my_hash_value = nil
  old_find_node(*args)
end

#first_argObject

Returns first argument of a method call.



269
270
271
272
# File 'lib/ruby_parser/bm_sexp.rb', line 269

def first_arg
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  self[3]
end

#first_arg=(exp) ⇒ Object

Sets first argument of a method call.



275
276
277
278
279
# File 'lib/ruby_parser/bm_sexp.rb', line 275

def first_arg= exp
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  @my_hash_value = nil
  self[3] = exp
end

#first_paramObject



427
428
429
430
# File 'lib/ruby_parser/bm_sexp.rb', line 427

def first_param
  expect :args
  self[1]
end

#formal_argsObject



490
491
492
493
494
495
496
497
498
499
# File 'lib/ruby_parser/bm_sexp.rb', line 490

def formal_args
  expect :defn, :defs

  case self.node_type
  when :defn
    self[2]
  when :defs
    self[3]
  end
end

#hashObject

silence redefined method warning



104
# File 'lib/ruby_parser/bm_sexp.rb', line 104

alias_method :hash, :hash

#inspect(seen = Set.new) ⇒ Object



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/ruby_parser/bm_sexp.rb', line 603

def inspect seen = Set.new
  if seen.include? self.object_id
    's(...)'
  else
    seen << self.object_id
    sexp_str = self.map do |x|
      if x.is_a? Sexp
        x.inspect seen
      else
        x.inspect
      end
    end.join(', ')

    "s(#{sexp_str})"
  end
end

#last_argObject



305
306
307
308
309
310
311
312
313
# File 'lib/ruby_parser/bm_sexp.rb', line 305

def last_arg
  expect :call, :attrasgn, :safe_call, :safe_attrasgn

  if self[3]
    self[-1]
  else
    nil
  end
end

#lhsObject

Returns the left hand side of assignment or boolean:

s(:lasgn, :x, s(:lit, 1))
           ^--lhs


436
437
438
439
# File 'lib/ruby_parser/bm_sexp.rb', line 436

def lhs
  expect(*ASSIGNMENT_BOOL)
  self[1]
end

#lhs=(exp) ⇒ Object

Sets the left hand side of assignment or boolean.



442
443
444
445
446
# File 'lib/ruby_parser/bm_sexp.rb', line 442

def lhs= exp
  expect(*ASSIGNMENT_BOOL)
  @my_hash_value = nil
  self[1] = exp
end

#methodObject

Returns method of a method call:

s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1)))

^- method


156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ruby_parser/bm_sexp.rb', line 156

def method
  expect :call, :attrasgn, :safe_call, :safe_attrasgn, :super, :zsuper, :result

  case self.node_type
  when :call, :attrasgn, :safe_call, :safe_attrasgn
    self[2]
  when :super, :zsuper
    :super
  when :result
    self.last
  end
end

#method=(name) ⇒ Object



169
170
171
172
173
# File 'lib/ruby_parser/bm_sexp.rb', line 169

def method= name
  expect :call, :safe_call

  self[2] = name
end

#method_lengthObject

Number of “statements” in a method. This is more efficient than ‘Sexp#body.length` because `Sexp#body` creates a new Sexp.



553
554
555
556
557
558
559
560
561
562
# File 'lib/ruby_parser/bm_sexp.rb', line 553

def method_length
  expect :defn, :defs

  case self.node_type
  when :defn
    self.length - 3
  when :defs
    self.length - 4
  end
end

#method_nameObject

Returns name of method being defined in a method definition.



479
480
481
482
483
484
485
486
487
488
# File 'lib/ruby_parser/bm_sexp.rb', line 479

def method_name
  expect :defn, :defs

  case self.node_type
  when :defn
    self[1]
  when :defs
    self[2]
  end
end

#moduleObject

Returns the module the call is inside



589
590
591
592
593
# File 'lib/ruby_parser/bm_sexp.rb', line 589

def module
  expect :result

  self[1]
end

#node_type=(type) ⇒ Object



75
76
77
78
# File 'lib/ruby_parser/bm_sexp.rb', line 75

def node_type= type
  @my_hash_value = nil
  self[0] = type
end

#old_compactObject



95
# File 'lib/ruby_parser/bm_sexp.rb', line 95

alias :old_compact :compact

#old_faraObject



96
# File 'lib/ruby_parser/bm_sexp.rb', line 96

alias :old_fara :find_and_replace_all

#old_find_nodeObject



97
# File 'lib/ruby_parser/bm_sexp.rb', line 97

alias :old_find_node :find_node

#old_pushObject



94
# File 'lib/ruby_parser/bm_sexp.rb', line 94

alias :old_push :<<

#parenObject

silence redefined method warning



50
# File 'lib/ruby_parser/bm_sexp.rb', line 50

alias_method :paren, :paren

#parent_nameObject



576
577
578
579
# File 'lib/ruby_parser/bm_sexp.rb', line 576

def parent_name
  expect :class
  self[2]
end

#render_typeObject



564
565
566
567
# File 'lib/ruby_parser/bm_sexp.rb', line 564

def render_type
  expect :render
  self[1]
end

#result_classObject

Return the class the call is inside



596
597
598
599
600
# File 'lib/ruby_parser/bm_sexp.rb', line 596

def result_class
  expect :result

  self[2]
end

#rhsObject

Returns right side (value) of assignment or boolean:

s(:lasgn, :x, s(:lit, 1))
              ^--rhs---^


452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/ruby_parser/bm_sexp.rb', line 452

def rhs
  expect :attrasgn, :safe_attrasgn, *ASSIGNMENT_BOOL

  if self.node_type == :attrasgn or self.node_type == :safe_attrasgn
    if self[2] == :[]=
      self[4]
    else
      self[3]
    end
  else
    self[2]
  end
end

#rhs=(exp) ⇒ Object

Sets the right hand side of assignment or boolean.



467
468
469
470
471
472
473
474
475
476
# File 'lib/ruby_parser/bm_sexp.rb', line 467

def rhs= exp
  expect :attrasgn, :safe_attrasgn, *ASSIGNMENT_BOOL
  @my_hash_value = nil

  if self.node_type == :attrasgn or self.node_type == :safe_attrasgn
    self[3] = exp
  else
    self[2] = exp
  end
end

#secondObject



67
68
69
# File 'lib/ruby_parser/bm_sexp.rb', line 67

def second
  self[1]
end

#second_argObject

Returns second argument of a method call.



282
283
284
285
# File 'lib/ruby_parser/bm_sexp.rb', line 282

def second_arg
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  self[4]
end

#second_arg=(exp) ⇒ Object

Sets second argument of a method call.



288
289
290
291
292
# File 'lib/ruby_parser/bm_sexp.rb', line 288

def second_arg= exp
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  @my_hash_value = nil
  self[4] = exp
end

#set_args(*exp) ⇒ Object



190
191
192
# File 'lib/ruby_parser/bm_sexp.rb', line 190

def set_args *exp
  self.arglist = exp
end

#targetObject

Returns target of a method call:

s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1)))

^-----------target-----------^


140
141
142
143
# File 'lib/ruby_parser/bm_sexp.rb', line 140

def target
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  self[1]
end

#target=(exp) ⇒ Object

Sets the target of a method call:



146
147
148
149
150
# File 'lib/ruby_parser/bm_sexp.rb', line 146

def target= exp
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  @my_hash_value = nil
  self[1] = exp
end

#then_clauseObject

Returns ‘then’ clause of an if expression:

s(:if,
 s(:lvar, :condition),
 s(:lvar, :then_val), <-- then clause
 s(:lvar, :else_val)))


353
354
355
356
# File 'lib/ruby_parser/bm_sexp.rb', line 353

def then_clause
  expect :if
  self[2]
end

#third_argObject



294
295
296
297
# File 'lib/ruby_parser/bm_sexp.rb', line 294

def third_arg
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  self[5]
end

#third_arg=(exp) ⇒ Object



299
300
301
302
303
# File 'lib/ruby_parser/bm_sexp.rb', line 299

def third_arg= exp
  expect :call, :attrasgn, :safe_call, :safe_attrasgn
  @my_hash_value = nil
  self[5] = exp
end

#to_symObject



71
72
73
# File 'lib/ruby_parser/bm_sexp.rb', line 71

def to_sym
  self.value.to_sym
end

#valueObject

silence redefined method warning

Raises:



55
# File 'lib/ruby_parser/bm_sexp.rb', line 55

alias_method :value, :value

#value=(exp) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/ruby_parser/bm_sexp.rb', line 61

def value= exp
  raise WrongSexpError, "Sexp#value= called on multi-item Sexp: `#{self.inspect}`" if size > 2
  @my_hash_value = nil
  self[1] = exp
end