Class: JSInstrument::Instrumenter
- Inherits:
-
Object
- Object
- JSInstrument::Instrumenter
- Defined in:
- lib/jsinstrument/instrumenter.rb
Instance Attribute Summary collapse
-
#batch_text ⇒ Object
Returns the value of attribute batch_text.
-
#commit_hash ⇒ Object
Returns the value of attribute commit_hash.
-
#data_compress_method ⇒ Object
Returns the value of attribute data_compress_method.
-
#js_object_name ⇒ Object
Returns the value of attribute js_object_name.
-
#project_name ⇒ Object
Returns the value of attribute project_name.
-
#src_filename ⇒ Object
Returns the value of attribute src_filename.
-
#upload_url ⇒ Object
Returns the value of attribute upload_url.
Instance Method Summary collapse
Instance Attribute Details
#batch_text ⇒ Object
Returns the value of attribute batch_text.
10 11 12 |
# File 'lib/jsinstrument/instrumenter.rb', line 10 def batch_text @batch_text end |
#commit_hash ⇒ Object
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_method ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
Returns the value of attribute project_name.
10 11 12 |
# File 'lib/jsinstrument/instrumenter.rb', line 10 def project_name @project_name end |
#src_filename ⇒ Object
Returns the value of attribute src_filename.
10 11 12 |
# File 'lib/jsinstrument/instrumenter.rb', line 10 def src_filename @src_filename end |
#upload_url ⇒ Object
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 |
# 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 = 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 } (inserting % bindings) + instrumented_src end |
#instrument_ast(ast) ⇒ Object
45 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 |
# File 'lib/jsinstrument/instrumenter.rb', line 45 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 ast.each do |node| parent = node.parent line = node.line node_classname = node.class.name.split(/::/)[-1].sub('Node', '') parent_classname = parent.class.name.split(/::/)[-1].sub('Node', '') # 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.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 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 end [ast, instrumented_lines.sort, instrumented_func_lines.sort] end |