Module: Coffeelint

Defined in:
lib/coffeelint.rb,
lib/coffeelint/cmd.rb,
lib/coffeelint/railtie.rb,
lib/coffeelint/version.rb

Defined Under Namespace

Modules: Cmd Classes: Railtie

Constant Summary collapse

VERSION =
"0.2.7"

Class Method Summary collapse

Class Method Details

.colorize(str, color_code) ⇒ Object



14
15
16
# File 'lib/coffeelint.rb', line 14

def self.colorize(str, color_code)
  "\e[#{color_code}m#{str}\e[0m"
end

.contextObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/coffeelint.rb', line 30

def self.context
  coffeescriptSource = File.read(CoffeeScript::Source.path)
  bootstrap = "  window = {\n    CoffeeScript: CoffeeScript,\n    coffeelint: {}\n  };\n  EOF\n  coffeelintSource = File.read(Coffeelint.path)\n  ExecJS.compile(coffeescriptSource + bootstrap + coffeelintSource)\nend\n"

.display_test_results(name, errors, pretty_output = true) ⇒ Object



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

def self.display_test_results(name, errors, pretty_output = true)
  good = pretty_output ? "\u2713" : 'Passed'
  warn = pretty_output ? "\u26A1" : 'Warn'
  bad = pretty_output ? "\u2717" : 'Failed'

  if errors.length == 0
    puts "  #{good} " + Coffeelint.green(name, pretty_output)
    return true
  else
    no_failures = true
    if errors.any? {|e| e["level"] == "error"}
      no_failures = false
      puts "  #{bad} " + Coffeelint.red(name, pretty_output)
    else
      puts "  #{warn} " + Coffeelint.yellow(name, pretty_output)
    end

    errors.each do |error|
      disp = "##{error["lineNumber"]}"
      if error["lineNumberEnd"]
        disp += "-#{error["lineNumberEnd"]}"
      end

      print "     "
      if error["level"] == "warn"
        print warn + " "
        print Coffeelint.yellow(disp, pretty_output)
      else
        print bad + " "
        print Coffeelint.red(disp, pretty_output)
      end
      puts ": #{error["message"]}. #{error["context"]}."
    end
    return no_failures
  end
end

.green(str, pretty_output = true) ⇒ Object



22
23
24
# File 'lib/coffeelint.rb', line 22

def self.green(str, pretty_output = true)
  pretty_output ? Coffeelint.colorize(str, 32) : str
end

.lint(script, config = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/coffeelint.rb', line 42

def self.lint(script, config = {})
  if !config[:config_file].nil?
    fname = config.delete(:config_file)
    config.merge!(JSON.parse(File.read(fname)))
  end
  Coffeelint.context.call('window.coffeelint.lint', script, config)
end

.lint_dir(directory, config = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/coffeelint.rb', line 54

def self.lint_dir(directory, config = {})
  retval = {}
  Dir.glob("#{directory}/**/*.coffee") do |name|
    retval[name] = Coffeelint.lint_file(name, config)
    yield name, retval[name] if block_given?
  end
  retval
end

.lint_file(filename, config = {}) ⇒ Object



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

def self.lint_file(filename, config = {})
  Coffeelint.lint(File.read(filename), config)
end

.pathObject



10
11
12
# File 'lib/coffeelint.rb', line 10

def self.path()
  @path ||= File.expand_path('../../coffeelint/lib/coffeelint.js', __FILE__)
end

.red(str, pretty_output = true) ⇒ Object



18
19
20
# File 'lib/coffeelint.rb', line 18

def self.red(str, pretty_output = true)
  pretty_output ? Coffeelint.colorize(str, 31) : str
end

.run_test(file, config = {}) ⇒ Object



100
101
102
103
104
# File 'lib/coffeelint.rb', line 100

def self.run_test(file, config = {})
  pretty_output = config.has_key?(:pretty_output) ? config.delete(:pretty_output) : true
  result = Coffeelint.lint_file(file, config)
  Coffeelint.display_test_results(file, result, pretty_output)
end

.run_test_suite(directory, config = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/coffeelint.rb', line 106

def self.run_test_suite(directory, config = {})
  pretty_output = config.has_key?(:pretty_output) ? config.delete(:pretty_output) : true
  success = true
  Coffeelint.lint_dir(directory, config) do |name, errors|
    result = Coffeelint.display_test_results(name, errors, pretty_output)
    success = false if not result
  end
  success
end

.yellow(str, pretty_output = true) ⇒ Object



26
27
28
# File 'lib/coffeelint.rb', line 26

def self.yellow(str, pretty_output = true)
  pretty_output ? Coffeelint.colorize(str, 33) : str
end