Class: Epic::Validator::JavaScript

Inherits:
Base
  • Object
show all
Defined in:
lib/epic.rb

Direct Known Subclasses

JSON

Instance Method Summary collapse

Methods inherited from Base

#base_path, #configuration, configuration, configure, #display_path, #tmp_path

Instance Method Details

#jslint_settingsObject



79
80
81
# File 'lib/epic.rb', line 79

def jslint_settings
  configuration.jslint_settings
end

#jslint_settings_countObject



83
84
85
# File 'lib/epic.rb', line 83

def jslint_settings_count
  jslint_settings.to_s.split("\n").size
end

#pre_process(content) ⇒ Object



87
88
89
# File 'lib/epic.rb', line 87

def pre_process(content)
  content
end

#use_jslint_settings?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/epic.rb', line 75

def use_jslint_settings?
  !jslint_settings.blank?
end

#validate(path) ⇒ Object



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
# File 'lib/epic.rb', line 91

def validate(path)
  display = display_path(path)
  $stdout.print "   #{display}  validating . . . "
  output = ""

  File.open(path) do |f|
    output = f.read
  end
  
  output = pre_process(output)
  
  FileUtils.mkdir_p(tmp_path)
  
  js_fragment_path = File.expand_path("#{tmp_path}/#{File.basename(path)}_fragment")
  fragment_display_path = display_path(js_fragment_path)
    
  if File.exists?(js_fragment_path)
    puts "That already exists?"
  else
    File.open(js_fragment_path,'w') do |f|
      f.puts jslint_settings if use_jslint_settings?
      f.puts output
    end

    jslint_path = File.expand_path("#{File.dirname(__FILE__)}/../vendor/ext/jslint.js")
    raise "#{jslint_path} does not exist" unless File.exists?(jslint_path)
    rhino_path = File.expand_path("#{File.dirname(__FILE__)}/../vendor/ext/js.jar")
    raise "#{rhino_path} does not exist" unless File.exists?(rhino_path)
    
    results = F.execute("java -jar #{rhino_path} #{jslint_path} #{js_fragment_path}", :return => true)

    if results =~ /jslint: No problems found/
      $stdout.puts "OK"
    else
      $stdout.puts "errors found!"
      results.split("\n").each do |result|
        if result =~ /line (\d+) character (\d+): (.*)/
          line_number = $1.to_i
          error = "Error at #{fragment_display_path} line #{line_number-jslint_settings_count} character #{$2}: #{$3}"
          error += F.get_line_from_file(js_fragment_path, line_number)
    
          $stdout.puts error
        end
      end
      message = "JavaScript Errors embedded in #{display}"
      g(message)
      raise message
    end
  end
end