Class: Contrast::Agent::Assess::Policy::Propagator::Trim

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/agent/assess/policy/propagator/trim.rb

Overview

This class is specifically for String#tr(_s) propagation

Disclaimer: there may be a better way, but we’re in a ‘get it work’ state. hopefully, we’ll be in a ‘get it right’ state soon.

Class Method Summary collapse

Class Method Details

.tr_s_tagger(patcher, preshift, ret, _block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/contrast/agent/assess/policy/propagator/trim.rb', line 60

def tr_s_tagger patcher, preshift, ret, _block
  return unless ret && !ret.empty?

  properties = Contrast::Agent::Assess::Tracker.properties(ret)
  return unless properties

  source = preshift.object
  args = preshift.args
  properties.splat_from(source, ret)
  properties.build_event(
      patcher,
      ret,
      source,
      ret,
      args)
  ret
end

.tr_tagger(patcher, preshift, ret, _block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/contrast/agent/assess/policy/propagator/trim.rb', line 16

def tr_tagger patcher, preshift, ret, _block
  return ret unless ret && !ret.empty?

  properties = Contrast::Agent::Assess::Tracker.properties(ret)
  return unless properties

  source = preshift.object
  args = preshift.args
  properties.copy_from(source, ret)
  replace_string = args[1]
  source_chars = source.chars
  # if the replace string is empty, then there's a bunch of deletes. this
  # functions the same as the Removal propagation.
  if replace_string == Contrast::Utils::ObjectShare::EMPTY_STRING
    Contrast::Agent::Assess::Policy::Propagator::Remove.handle_removal(source_chars, ret)
  else
    remove_ranges = []
    ret_chars = ret.chars
    start = nil
    source_chars.each_with_index do |char, idx|
      if ret_chars[idx] == char
        next unless start

        remove_ranges << (start...idx)
        start = nil
      else
        start ||= idx
      end
    end
    # account for the last char being different
    remove_ranges << (start...source_chars.length) if start
    properties.delete_tags_at_ranges(remove_ranges, false)
  end

  properties.build_event(
      patcher,
      ret,
      source,
      ret,
      args,
      1)
  ret
end