Class: YMDP::Validator::JavaScript

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

Direct Known Subclasses

JSON

Class Method Summary collapse

Methods inherited from Base

#base_path, base_path, #configuration, configuration, configure, #content_variables, display_path, #display_path, #paths, #servers

Class Method Details

.jslint_settingsObject



50
51
52
# File 'lib/ymdp/validator/validator.rb', line 50

def self.jslint_settings
  configuration.jslint_settings
end

.jslint_settings_countObject



54
55
56
# File 'lib/ymdp/validator/validator.rb', line 54

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

.pre_process(content) ⇒ Object



58
59
60
# File 'lib/ymdp/validator/validator.rb', line 58

def self.pre_process(content)
  content
end

.use_jslint_settings?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ymdp/validator/validator.rb', line 46

def self.use_jslint_settings?
  !jslint_settings.blank?
end

.validate(filename) ⇒ Object



42
43
44
# File 'lib/ymdp/validator/validator.rb', line 42

def self.validate(filename)
  validate_javascript(filename)
end

.validate_javascript(path) ⇒ Object



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
# File 'lib/ymdp/validator/validator.rb', line 62

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

  File.open(path) do |f|
    output = f.read
  end
  
  output = pre_process(output)
  
  js_fragment_path = File.expand_path("#{TMP_PATH}/#{File.basename(path)}_fragment")
  fragment_display_path = display_path(js_fragment_path)
    
  unless File.exists?(js_fragment_path)
    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__)}/jslint.js")
    raise "#{jslint_path} does not exist" unless File.exists?(jslint_path)
    results = F.execute("java org.mozilla.javascript.tools.shell.Main #{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