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 =
"1.14.0"

Class Method Summary collapse

Class Method Details

.colorize(str, color_code) ⇒ Object



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

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

.contextObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coffeelint.rb', line 34

def self.context
  coffeescriptSource = File.read(CoffeeScript::Source.path)
  bootstrap = <<-EOF
  window = {
    CoffeeScript: CoffeeScript,
    coffeelint: {}
  };
  EOF
  coffeelintSource = File.read(Coffeelint.path)
  ExecJS.compile(coffeescriptSource + bootstrap + coffeelintSource)
end

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



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

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'

  failure_count = 0
  if errors.length == 0
    puts "  #{good} " + Coffeelint.green(name, pretty_output)
  else
    if errors.any? {|e| e["level"] == "error"}
      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
        failure_count += 1
        print bad + " "
        print Coffeelint.red(disp, pretty_output)
      end
      puts ": #{error["message"]}. #{error["context"]}."
    end
  end
  return failure_count
end

.filter(directory) ⇒ Object



67
68
69
# File 'lib/coffeelint.rb', line 67

def self.filter(directory)
  Dir.glob("#{directory}/**/*.coffee") - Dir.glob("#{directory}/**/{#{ignored_paths}}")
end

.green(str, pretty_output = true) ⇒ Object



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

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

.ignored_pathsObject



71
72
73
74
75
76
77
# File 'lib/coffeelint.rb', line 71

def self.ignored_paths
  if File.exist?(".coffeelintignore")
    File.read(".coffeelintignore").lines.map(&:chomp).join(",")
  else
    []
  end
end

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



46
47
48
49
50
# File 'lib/coffeelint.rb', line 46

def self.lint(script, config = {}, context = Coffeelint.context)
  fname = config.fetch(:config_file, CoffeeLint::Config.locate)
  config.merge!(CoffeeLint::Config.parse(fname)) unless fname.nil?
  context.call('window.coffeelint.lint', script, config)
end

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/coffeelint.rb', line 56

def self.lint_dir(directory, config = {}, context = Coffeelint.context)
  retval = {}
  filtered_path = filter(directory)

  Dir.glob(filtered_path) do |name|
    retval[name] = Coffeelint.lint_file(name, config, context)
    yield name, retval[name] if block_given?
  end
  retval
end

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



52
53
54
# File 'lib/coffeelint.rb', line 52

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

.pathObject



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

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

.red(str, pretty_output = true) ⇒ Object



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

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

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



115
116
117
118
119
# File 'lib/coffeelint.rb', line 115

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

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



121
122
123
124
125
126
# File 'lib/coffeelint.rb', line 121

def self.run_test_suite(directory, config = {})
  pretty_output = config.has_key?(:pretty_output) ? config.delete(:pretty_output) : true
  Coffeelint.lint_dir(directory, config).map do |name, errors|
    Coffeelint.display_test_results(name, errors, pretty_output)
  end.inject(0, :+)
end

.set_path(custom_path) ⇒ Object



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

def self.set_path(custom_path)
  @path = custom_path
end

.yellow(str, pretty_output = true) ⇒ Object



30
31
32
# File 'lib/coffeelint.rb', line 30

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