Class: Epic::Validator::JavaScript

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

Direct Known Subclasses

JSON

Instance Attribute Summary

Attributes inherited from Base

#errors, #path

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Errors

#display_errors, #errors, #valid?

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Epic::Validator::Base

Instance Method Details

#js_fragment_pathObject



16
17
18
# File 'lib/epic/validator/javascript.rb', line 16

def js_fragment_path
  File.expand_path("#{tmp_path}/#{File.basename(path)}_fragment")
end

#jslint_pathObject



20
21
22
23
24
# File 'lib/epic/validator/javascript.rb', line 20

def jslint_path
  jslint_path = File.expand_path("#{vendor_path}/ext/jslint.js")
  raise "#{jslint_path} does not exist" unless File.exists?(jslint_path)
  jslint_path
end

#jslint_settingsObject



4
5
6
# File 'lib/epic/validator/javascript.rb', line 4

def jslint_settings
  configuration.jslint_settings.to_s
end

#jslint_settings_countObject



8
9
10
# File 'lib/epic/validator/javascript.rb', line 8

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

#pre_process(content) ⇒ Object



12
13
14
# File 'lib/epic/validator/javascript.rb', line 12

def pre_process(content)
  jslint_settings + content
end

#rhino_pathObject



26
27
28
29
30
# File 'lib/epic/validator/javascript.rb', line 26

def rhino_path
  rhino_path = File.expand_path("#{vendor_path}/ext/js.jar")
  raise "#{rhino_path} does not exist" unless File.exists?(rhino_path)
  rhino_path
end

#valid_results?(results) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/epic/validator/javascript.rb', line 36

def valid_results?(results)
  fragment_display_path = display_path(js_fragment_path)
  unless results =~ /jslint: No problems 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)
  
        errors << error
      end
    end
  end
  errors.length <= 0
end

#validate(filename = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/epic/validator/javascript.rb', line 65

def validate(filename=nil)
  @path = filename || path
  $stdout.print "   #{display_path}  validating . . . "
  
  results = validate_path
  
  if valid_results?(results)
    $stdout.puts "OK"
  else
    $stdout.puts "errors found!"
    display_errors
  end
  
  errors.length <= 0
end

#validate_pathObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/epic/validator/javascript.rb', line 52

def validate_path
  raw_output = File.read(path)        
  output = pre_process(raw_output)
  
  FileUtils.mkdir_p(tmp_path)
  
  File.open(js_fragment_path,'w') do |f|
    f.puts output
  end

  F.execute("java -jar #{rhino_path} #{jslint_path} #{js_fragment_path}", :return => true)
end

#vendor_pathObject



32
33
34
# File 'lib/epic/validator/javascript.rb', line 32

def vendor_path
  File.join(File.dirname(__FILE__), "..", "..", "..", "vendor")
end