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 |
# 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 = 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 } (inserting % bindings) + instrumented_src end |
#instrument_ast(ast) ⇒ Object
44 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 |
# File 'lib/jsinstrument/instrumenter.rb', line 44 def instrument_ast ast # build parent attr for all nodes in ast # ast = RKelly::Visitors::ParentBuilder.new.build ast instrumented_lines = Set.new ast.each do |node| parent = node.parent line = node.line # instrument only if we can get the line no. and we haven't instrumented it # if line and not instrumented_lines.member? line if ExpressionStatementNode === node or EmptyStatementNode === node or DoWhileNode === node or WhileNode === node or ForNode === node or ForInNode === node or ContinueNode === node or VarDeclNode === node or SwitchNode === node or BreakNode === node or ThrowNode === node or ReturnNode === node or IfNode === node or VarStatementNode === node if DoWhileNode === parent if parent.left.eql? node instrumented_lines.add line parent.left = get_inserted_block node end # in RKelly, ConditionalNode extends IfNode # which is so annoying # I mean why they mix a expression and a statement? elsif (IfNode === parent and not ConditionalNode === parent) or WhileNode === parent or ForNode === parent or ForInNode === parent or WithNode === parent if parent.value.eql? node instrumented_lines.add line parent.value = get_inserted_block node elsif parent.else.eql? node instrumented_lines.add line parent.else = get_inserted_block node end elsif CaseBlockNode === parent or LabelNode === parent # do nothing here elsif SourceElementsNode === parent if insert_func_before node instrumented_lines.add line end end elsif WithNode === node or TryNode === node if SourceElementsNode === parent if insert_func_before node instrumented_lines.add line end end elsif FunctionDeclNode === node or FunctionExprNode === node if insert_func_before node instrumented_lines.add line end end end end [ast, instrumented_lines.sort] end |