Class: Sexp

Inherits:
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, :cdecl, :or, :and, :colon2]

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

Raises:

  • (NoMethodError)


9
10
11
12
13
14
15
16
# File 'lib/ruby_parser/bm_sexp.rb', line 9

def method_missing name, *args
  #Brakeman does not use this functionality,
  #so overriding it to raise a NoMethodError.
  #
  #The original functionality calls find_node and optionally
  #deletes the node if found.
  raise NoMethodError.new("No method '#{name}' for Sexp", name, args)
end

Instance Attribute Details

#or_depthObject

Returns the value of attribute or_depth.



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

def or_depth
  @or_depth
end

#original_lineObject

Returns the value of attribute original_line.



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

def original_line
  @original_line
end

#parenObject (readonly)

Returns the value of attribute paren.



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

def paren
  @paren
end

Instance Method Details

#<<(arg) ⇒ Object



89
90
91
92
# File 'lib/ruby_parser/bm_sexp.rb', line 89

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 ------------^


197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ruby_parser/bm_sexp.rb', line 197

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

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

#arglist=(exp) ⇒ Object

Sets the arglist in a method call.



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ruby_parser/bm_sexp.rb', line 173

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

  if exp.is_a? Sexp and exp.node_type == :arglist
    exp = exp[1..-1]
  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--------^


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

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

  case self.node_type
  when :call, :attrasgn
    if self[3]
      self[3..-1]
    else
      Sexp.new
    end
  when :super, :zsuper
    if self[1]
      self[1..-1]
    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 --------------------------^


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/ruby_parser/bm_sexp.rb', line 365

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

  expect :iter, :call_with_block, :scope, :resbody

  case self.node_type
  when :iter, :call_with_block
    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))))


389
390
391
392
393
394
395
396
# File 'lib/ruby_parser/bm_sexp.rb', line 389

def block_args
  expect :iter, :call_with_block
  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))))


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

def block_call
  expect :iter, :call_with_block
  self[1]
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.



482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/ruby_parser/bm_sexp.rb', line 482

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

  case self.node_type
  when :defn, :methdef, :class
    self[3..-1]
  when :defs, :selfdef
    self[4..-1]
  when :module
    self[2..-1]
  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.



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/ruby_parser/bm_sexp.rb', line 458

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

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

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

  #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.



497
498
499
# File 'lib/ruby_parser/bm_sexp.rb', line 497

def body_list
  self.body.unshift :rlist
end

#callObject

Returns the call Sexp in a result returned from FindCall



519
520
521
522
523
# File 'lib/ruby_parser/bm_sexp.rb', line 519

def call
  expect :result

  self.last
end

#class_nameObject Also known as: module_name



506
507
508
509
# File 'lib/ruby_parser/bm_sexp.rb', line 506

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.



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

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



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

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)))


318
319
320
321
# File 'lib/ruby_parser/bm_sexp.rb', line 318

def condition
  expect :if
  self[1]
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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_parser/bm_sexp.rb', line 20

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)
  end

  s
end

#each_arg(replace = false) ⇒ Object



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

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

  case self.node_type
  when :call, :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



260
261
262
263
# File 'lib/ruby_parser/bm_sexp.rb', line 260

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

#each_sexpObject

Iterates over the Sexps in an Sexp, skipping values that are not an Sexp.



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

def each_sexp
  self.each do |e|
    yield e if Sexp === e
  end
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---^


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

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.



127
128
129
130
131
# File 'lib/ruby_parser/bm_sexp.rb', line 127

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



107
108
109
110
# File 'lib/ruby_parser/bm_sexp.rb', line 107

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

#find_node(*args) ⇒ Object



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

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

#first_argObject

Returns first argument of a method call.



266
267
268
269
# File 'lib/ruby_parser/bm_sexp.rb', line 266

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

#first_arg=(exp) ⇒ Object

Sets first argument of a method call.



272
273
274
275
276
# File 'lib/ruby_parser/bm_sexp.rb', line 272

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

#first_paramObject



398
399
400
401
# File 'lib/ruby_parser/bm_sexp.rb', line 398

def first_param
  expect :args
  self[1]
end

#formal_argsObject



445
446
447
448
449
450
451
452
453
454
# File 'lib/ruby_parser/bm_sexp.rb', line 445

def formal_args
  expect :defn, :defs, :methdef, :selfdef

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

#hashObject



94
95
96
97
98
99
100
# File 'lib/ruby_parser/bm_sexp.rb', line 94

def hash
  #There still seems to be some instances in which the hash of the
  #Sexp changes, but I have not found what method call is doing it.
  #Of course, Sexp is subclasses from Array, so who knows what might
  #be going on.
  @my_hash_value ||= super
end

#last_argObject



302
303
304
305
306
307
308
309
310
# File 'lib/ruby_parser/bm_sexp.rb', line 302

def last_arg
  expect :call, :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


407
408
409
410
# File 'lib/ruby_parser/bm_sexp.rb', line 407

def lhs
  expect *ASSIGNMENT_BOOL
  self[1]
end

#lhs=(exp) ⇒ Object

Sets the left hand side of assignment or boolean.



413
414
415
416
# File 'lib/ruby_parser/bm_sexp.rb', line 413

def lhs= exp
  expect *ASSIGNMENT_BOOL
  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


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ruby_parser/bm_sexp.rb', line 153

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

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

#method=(name) ⇒ Object



166
167
168
169
170
# File 'lib/ruby_parser/bm_sexp.rb', line 166

def method= name
  expect :call

  self[2] = name
end

#method_nameObject

Returns name of method being defined in a method definition.



434
435
436
437
438
439
440
441
442
443
# File 'lib/ruby_parser/bm_sexp.rb', line 434

def method_name
  expect :defn, :defs, :methdef, :selfdef

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

#moduleObject

Returns the module the call is inside



526
527
528
529
530
# File 'lib/ruby_parser/bm_sexp.rb', line 526

def module
  expect :result

  self[1]
end

#node_type=(type) ⇒ Object



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

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

#old_compactObject



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

alias :old_compact :compact

#old_faraObject



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

alias :old_fara :find_and_replace_all

#old_find_nodeObject



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

alias :old_find_node :find_node

#old_pushObject



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

alias :old_push :<<

#parent_nameObject



513
514
515
516
# File 'lib/ruby_parser/bm_sexp.rb', line 513

def parent_name
  expect :class
  self[2]
end

#render_typeObject



501
502
503
504
# File 'lib/ruby_parser/bm_sexp.rb', line 501

def render_type
  expect :render
  self[1]
end

#result_classObject

Return the class the call is inside



533
534
535
536
537
# File 'lib/ruby_parser/bm_sexp.rb', line 533

def result_class
  expect :result

  self[2]
end

#rhsObject

Returns right side (value) of assignment or boolean:

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


422
423
424
425
# File 'lib/ruby_parser/bm_sexp.rb', line 422

def rhs
  expect *ASSIGNMENT_BOOL
  self[2]
end

#rhs=(exp) ⇒ Object

Sets the right hand side of assignment or boolean.



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

def rhs= exp
  expect *ASSIGNMENT_BOOL
  self[2] = exp
end

#secondObject



57
58
59
# File 'lib/ruby_parser/bm_sexp.rb', line 57

def second
  self[1]
end

#second_argObject

Returns second argument of a method call.



279
280
281
282
# File 'lib/ruby_parser/bm_sexp.rb', line 279

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

#second_arg=(exp) ⇒ Object

Sets second argument of a method call.



285
286
287
288
289
# File 'lib/ruby_parser/bm_sexp.rb', line 285

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

#set_args(*exp) ⇒ Object



187
188
189
# File 'lib/ruby_parser/bm_sexp.rb', line 187

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-----------^


137
138
139
140
# File 'lib/ruby_parser/bm_sexp.rb', line 137

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

#target=(exp) ⇒ Object

Sets the target of a method call:



143
144
145
146
147
# File 'lib/ruby_parser/bm_sexp.rb', line 143

def target= exp
  expect :call, :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)))


329
330
331
332
# File 'lib/ruby_parser/bm_sexp.rb', line 329

def then_clause
  expect :if
  self[2]
end

#third_argObject



291
292
293
294
# File 'lib/ruby_parser/bm_sexp.rb', line 291

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

#third_arg=(exp) ⇒ Object



296
297
298
299
300
# File 'lib/ruby_parser/bm_sexp.rb', line 296

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

#to_symObject



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

def to_sym
  self.value.to_sym
end

#valueObject

Raises:



46
47
48
49
# File 'lib/ruby_parser/bm_sexp.rb', line 46

def value
  raise WrongSexpError, "Sexp#value called on multi-item Sexp", caller[1..-1] if size > 2
  last
end

#value=(exp) ⇒ Object

Raises:



51
52
53
54
55
# File 'lib/ruby_parser/bm_sexp.rb', line 51

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