Class: JSInstrument::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/jsinstrument/instrumenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_textObject

Returns the value of attribute batch_text.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def batch_text
  @batch_text
end

#commit_hashObject

Returns the value of attribute commit_hash.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def commit_hash
  @commit_hash
end

#data_compress_methodObject

Returns the value of attribute data_compress_method.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def data_compress_method
  @data_compress_method
end

#js_object_nameObject

Returns the value of attribute js_object_name.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def js_object_name
  @js_object_name
end

#project_nameObject

Returns the value of attribute project_name.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def project_name
  @project_name
end

#src_filenameObject

Returns the value of attribute src_filename.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def src_filename
  @src_filename
end

#upload_urlObject

Returns the value of attribute upload_url.



10
11
12
# File 'lib/jsinstrument/instrumenter.rb', line 10

def upload_url
  @upload_url
end

Instance Method Details

#instrument(src, filename) ⇒ Object



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
# File 'lib/jsinstrument/instrumenter.rb', line 20

def instrument src, filename
  raise 'Filename not set' unless filename
  self.src_filename = filename
  ast = RKelly::Parser.new.parse src
  raise 'Parse source failed.' unless ast
  ast, instrumented_lines, instrumented_func_lines, instrumented_branch_lines = instrument_ast ast
  instrumented_src = ast.to_ecma

  inserting = File.open(File.dirname(__FILE__) + '/inserting.js').read

  bindings = {
    js_object_name: self.js_object_name,
    upload_url: self.upload_url,
    src_filename: self.src_filename,
    project_name: self.project_name,
    commit_hash: self.commit_hash,
    batch_text: self.batch_text,
    data_compress_method: self.data_compress_method,
    instrumented_lines: instrumented_lines,
    instrumented_func_lines: instrumented_func_lines,
    instrumented_branch_lines: instrumented_branch_lines
  }

  (inserting % bindings) + instrumented_src
end

#instrument_ast(ast) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jsinstrument/instrumenter.rb', line 46

def instrument_ast ast
  # build parent attr for all nodes in ast
  #
  ast = RKelly::Visitors::ParentBuilder.new.build ast
  instrumented_lines = Set.new
  instrumented_func_lines = Set.new
  instrumented_branch_lines = Set.new
  ast.each do |node|
    parent = node.parent
    line = node.line
    node_classname = get_node_classname node
    parent_classname = get_node_classname parent

    # instrument only if we can get the line no. and we haven't instrumented it
    
    # line coverage
    #
    if line and not instrumented_lines.member? line
      if ['ExpressionStatement', 'EmptyStatement', 'DoWhile', 'While', 'For', 'ForIn', 'Continue',
         'VarDecl', 'Switch', 'Break', 'Throw', 'Return', 'If', 'VarStatement'].index node_classname

        if 'DoWhile' == parent_classname
          if parent.left.eql? node
            instrumented_lines.add line
            parent.left = get_hitline_call_block node
          end
        # in RKelly, ConditionalNode extends IfNode
        # which is so annoying
        # I mean why they mix a expression and a statement?
        elsif ['If', 'While', 'For', 'ForIn', 'With'].index parent_classname
          if parent.value.eql? node
            instrumented_lines.add line
            parent.value = get_hitline_call_block node
          elsif (parent.respond_to? :else) and parent.else.eql? node
            instrumented_lines.add line
            parent.else = get_hitline_call_block node
          end
        elsif ['CaseBlock', 'Label'].index parent_classname
          # do nothing here
        elsif 'SourceElements' == parent_classname
          if insert_hitline_before node
            instrumented_lines.add line
          end
        end
      elsif ['With', 'Try'].index node_classname
        if 'SourceElements' == parent_classname
          if insert_hitline_before node
            instrumented_lines.add line
          end
        end
      end
    end

    # function coverage
    #
    if line and not instrumented_func_lines.member? line
      # function declaration and anonymous function
      if ['FunctionDecl', 'FunctionExpr'].index node_classname
        if node.function_body
          insert_hitfunc_in node.function_body, line
          instrumented_func_lines.add line
        end
      end
    end

    # branch coverage
    #
    if line and not instrumented_branch_lines.member? line
      # if else
      if 'Block' == node_classname
        if 'If' == parent_classname
          if parent.value.eql? node
            instrumented_branch_lines.add line
            insert_hitbranch_in node
          elsif parent.else.eql? node
            instrumented_branch_lines.add line
            insert_hitbranch_in node
          end
        end
      # switch case
      # CaseClause node has the same structure with BlockNode
      # so we can share the instrument logic
      elsif 'CaseClause' == node_classname
        if node.value
          instrumented_branch_lines.add line
          insert_hitbranch_in node
        end
      end
    end
  end
  [ast, instrumented_lines.sort, instrumented_func_lines.sort, instrumented_branch_lines.sort]
end