Class: JasmineParser::NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jasmine-parser/node_factory.rb

Overview

TODO: Add some tests for node factory

Class Method Summary collapse

Class Method Details

.create_node(args) ⇒ Object



35
36
37
38
39
40
41
42
43
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
# File 'lib/jasmine-parser/node_factory.rb', line 35

def self.create_node(args)
  self.verify(args)

  node = Node.send(args[:type], args)

  node.js_object.each do |js_node|
    begin
      child_node_line = JavascriptObjectWrapper.get_node_line js_node
      child_node_type = JavascriptObjectWrapper.get_node_type js_node
      child_node_name = JavascriptObjectWrapper.get_node_name js_node
    rescue => e
      #I know that blank rescue is the root of all evil
      #However, this is still early stages, and we have not run into every possibility to account for
      #So it would be bad to fail someone's test run because their file contains something we don't expect
      #This way, we just throw a warning that we had an issue, and update the parser to account for that scenario
      #In the future, remove this rescue once all of the types of nodes have been accounted for

      Announcer.warning("Had an issue parsing one of the children nodes, check jasmine-parser-error.log for details")
      File.open("jasmine-parser-error.log", "a") do |file|
        file.write "\n\n\n"
        file.write "#{Time.now}"
        file.write "Encountered error trying to parse one of the children nodes for #{node.filename} starting on line #{node.line}"
        file.write e
      end

    end


    if JavascriptObjectWrapper.lines_already_covered.include? child_node_line
      next
    else
      JavascriptObjectWrapper.lines_already_covered << child_node_line  
    end


    child_node_js_object = JavascriptObjectWrapper.get_expression_statement_nodes js_node
    child_node_js_object.shift #Delete the very first item in array, because it contains the whole self node in it.


    

    if Node.respond_to? child_node_type
      node.children << self.create_node({
       :name        => child_node_name,
       :line  => child_node_line,
       :type        => child_node_type,
       :js_object   => child_node_js_object,
       :filename    => node.filename,
       :parent      => node
      })
    end
  end

  node

end

.parse_file(filename) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jasmine-parser/node_factory.rb', line 98

def self.parse_file(filename)

  full_page_js_object = JavascriptObjectWrapper.parse_file filename
  js_object  = JavascriptObjectWrapper.get_expression_statement_nodes full_page_js_object

  root_node = self.create_node({
   :name =>     "",
   :filename    => filename,
   :line  => 0,
   :type        => :root,
   :js_object   => js_object

  })

  root_node

end

.verify(args) ⇒ Object



92
93
94
95
# File 'lib/jasmine-parser/node_factory.rb', line 92

def self.verify(args)
  raise NodeTypeCannotBeBlankOrNilError if (args[:type].nil? or args[:type] == "")
  raise NodeStartLineHasToBeIntegerOrNilError unless (args[:line].nil? or args[:line].kind_of?(Fixnum))
end