Class: Condenser::JSAnalyzer

Inherits:
Object
  • Object
show all
Includes:
ParseHelpers
Defined in:
lib/condenser/processors/js_analyzer.rb

Instance Attribute Summary

Attributes included from ParseHelpers

#matched

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseHelpers

#current_line, #cursor, #eos?, #forward, #gobble, #next_word, #pre_match, #rewind, #scan_until, #seek

Class Method Details

.call(environment, input) ⇒ Object



8
9
10
# File 'lib/condenser/processors/js_analyzer.rb', line 8

def self.call(environment, input)
  new.call(environment, input)
end

.setup(env) ⇒ Object



5
6
# File 'lib/condenser/processors/js_analyzer.rb', line 5

def self.setup(env)
end

Instance Method Details

#call(environment, input) ⇒ Object



12
13
14
15
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
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
138
139
140
141
142
143
144
145
# File 'lib/condenser/processors/js_analyzer.rb', line 12

def call(environment, input)
  seek(0)
  @sourcefile = input[:source_file]
  @source = input[:source]
  @stack =  [:main]

  input[:export_dependencies] ||= Set.new

  scan_until(/\A(\/\/[^\n]*(\n|\z))*/)
  if matched
    directives = matched.split(/\n/).map { |l| l.delete_prefix("//").strip }
    directives.each do |directive|
      if directive.start_with?('depends_on')
        input[:process_dependencies] << directive.sub(/\Adepends_on\s+/, '')
      end
    end
  end
  
  last_postion = nil
  last_stack = nil
  while !eos?
    case @stack.last

    when :tick_value
      scan_until(/(\$\{|\`)/)
      case matched
      when '`'
        @stack.pop
      when '${'
        @stack << :tick_statment
      end

    when :import
      scan_until(/[\"\'\`]/)
      input[:export_dependencies] << case matched
      when "\""
        double_quoted_value
      when "'"
        single_quoted_value
      when '`'
        tick_quoted_value
      end
      scan_until(/(;|\n)/)
      @stack.pop

    when :export
      input[:exports] = true;
      input[:default_export] = true if gobble(/\s+default/)
      gobble(/\s+/)

      if gobble(/\{/)
        @stack << :brackets
      elsif gobble(/\*/)
        @stack << :export_from
      else
        @stack.pop
      end

    when :export_from
      if gobble(/\s+from\s+/)
        scan_until(/\"|\'/)
        input[:export_dependencies] <<  case matched
        when '"'
          double_quoted_value
        when "'"
          single_quoted_value
        end
      end
      @stack.pop
      @stack.pop
      
    else
      scan_until(/(\/\/|\/\*|\/|\(|\)|\{|\}|\"|\'|\`|export|import|\z)/)

      case matched
      when '//'
        scan_until(/(\n|\z)/)
      when '/*'
        scan_until(/\*\//)
      when '"'
        double_quoted_value
      when "'"
        single_quoted_value
      when '`'
        @stack << :tick_value
      when '/'
        if match_index = @source.rindex(/(\w+|\)|\])\s*\//, @index)
          match = @source.match(/(\w+|\)|\])\s*\//, match_index)
          if match[0].length + match_index != @index
            regex_value
          end
        else
          regex_value
        end
      when '('
        @stack.push :parenthesis
      when ')'
        raise unexptected_token(")") if @stack.last != :parenthesis
        @stack.pop
      when '{'
        @stack.push :brackets
      when '}'
        case @stack.last
        when :tick_statment
          @stack.pop
        when :brackets
          @stack.pop
          @stack << :export_from if @stack.last == :export
        else
          raise unexptected_token("}")
        end
      when 'export'
        if @stack.last == :main
          @stack << :export
        end
      when 'import'
        if @stack.last == :main
          @stack << :import
        end
      else
        @stack.pop
      end
    end

    if last_postion == @index && last_stack == @stack.last
      syntax_error = Condenser::SyntaxError.new("Error parsing JS file with JSAnalyzer")
      syntax_error.instance_variable_set(:@path, @sourcefile)
      raise Condenser::SyntaxError, "Error parsing JS file with JSAnalyzer"
    else
      last_postion = @index
      last_stack = @stack.last
    end
  end
end

#double_quoted_valueObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/condenser/processors/js_analyzer.rb', line 162

def double_quoted_value
  ret_value = ""

  while scan_until(/[\"\n]/)
    if matched == "\n"
      raise unexptected_token("\\n")
    elsif matched == "\""
      if pre_match[-1] != "\\"
        ret_value << pre_match
        return ret_value
      else
        ret_value << pre_match << "\\\""
      end

      
    else
      ret_value << match
    end
  end
end

#regex_valueObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/condenser/processors/js_analyzer.rb', line 211

def regex_value
  ret_value = ""

  while scan_until(/\//)
    if matched == "/" && pre_match[-1] != "\\"
      ret_value << pre_match
      return ret_value
    else
      ret_value << pre_match
    end
  end
end

#single_quoted_valueObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/condenser/processors/js_analyzer.rb', line 183

def single_quoted_value
  ret_value = ""

  while scan_until(/[\'\n]/)
    if matched == "\n"
      raise unexptected_token("\\n")
    elsif matched == "\'" && pre_match[-1] != "\\"
      ret_value << pre_match
      return ret_value
    else
      ret_value << pre_match
    end
  end
end

#tick_quoted_valueObject



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/condenser/processors/js_analyzer.rb', line 198

def tick_quoted_value
  ret_value = ""

  while scan_until(/[\`]/)
    if matched == "\`" && pre_match[-1] != "\\"
      ret_value << pre_match
      return ret_value
    else
      ret_value << pre_match
    end
  end
end

#unexptected_token(token) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/condenser/processors/js_analyzer.rb', line 147

def unexptected_token(token)
  start = (@source.rindex("\n", @old_index) || 0) + 1
  uptop = @source.index("\n", @index) || (@old_index + @matched.length)
  lineno = @source[0..start].count("\n") + 1

  message = "Unexpected token #{token} #{@sourcefile} #{lineno.to_s.rjust(4)}:#{(@index-start)}"
  message << "\n#{lineno.to_s.rjust(4)}: " << @source[start..uptop]
  message << "\n      #{'-'* (@index-1-start)}#{'^'*(@matched.length)}"
  message << "\n"
  
  syntax_error = Condenser::SyntaxError.new(message)
  syntax_error.instance_variable_set(:@path, @sourcefile)
  syntax_error
end