coffeelint 1.8.1 → 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/coffeelint.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require "coffeelint/version"
2
+ require 'coffeelint/config'
2
3
  require 'coffeelint/cmd'
3
4
  require 'execjs'
4
5
  require 'coffee-script'
5
- require 'json'
6
6
 
7
7
  module Coffeelint
8
8
  require 'coffeelint/railtie' if defined?(Rails::Railtie)
@@ -44,10 +44,8 @@ module Coffeelint
44
44
  end
45
45
 
46
46
  def self.lint(script, config = {})
47
- if !config[:config_file].nil?
48
- fname = config.delete(:config_file)
49
- config.merge!(JSON.parse(File.read(fname)))
50
- end
47
+ fname = config.fetch(:config_file, CoffeeLint::Config.locate)
48
+ config.merge!(CoffeeLint::Config.parse(fname)) unless fname.nil?
51
49
  Coffeelint.context.call('window.coffeelint.lint', script, config)
52
50
  end
53
51
 
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+
3
+ module CoffeeLint
4
+ class Config
5
+ # Looks for existing config files and returns the first match.
6
+ def self.locate
7
+ locations = default_locations
8
+
9
+ # handle environment variables
10
+ locations.push(ENV['COFFEELINT_CONFIG']) if ENV['COFFEELINT_CONFIG']
11
+ locations.concat(config_files_in_path(ENV['HOME'])) if ENV['HOME']
12
+
13
+ locations.compact.detect { |file| File.exists?(file) }
14
+ end
15
+
16
+ # Parses a given JSON file to a Hash.
17
+ def self.parse(file_name)
18
+ JSON.parse(File.read(file_name))
19
+ end
20
+
21
+ # Config files CoffeeLint will look for.
22
+ def self.default_locations
23
+ config_files + config_files_in_path('config')
24
+ end
25
+ private_class_method :default_locations
26
+
27
+ # Maps config file names in given path/directory.
28
+ def self.config_files_in_path(path)
29
+ config_files.map { |file| File.join([*path, file].compact.reject(&:empty?)) }
30
+ end
31
+ private_class_method :config_files_in_path
32
+
33
+ # Config file names CoffeeLint will look for.
34
+ def self.config_files
35
+ %w(
36
+ coffeelint.json
37
+ .coffeelint.json
38
+ )
39
+ end
40
+ private_class_method :config_files
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module Coffeelint
2
- VERSION = "1.8.1"
2
+ VERSION = "1.9.1"
3
3
  end
@@ -1,17 +1,5 @@
1
1
  desc "lint application javascript"
2
2
  task :coffeelint do
3
- conf = {}
4
-
5
- config_file = [].tap {|files|
6
- files << ENV['COFFEELNT_CONFIG'] if ENV['COFFEELENT_CONFIG']
7
- files << 'config/coffeelint.json'
8
- if ENV['HOME']
9
- files << "#{ENV['HOME']}/coffeelint.json"
10
- files << "#{ENV['HOME']}/.coffeelint.json"
11
- end
12
- }.compact.detect {|file| File.exists?(file) }
13
-
14
- conf[:config_file] = config_file if config_file
15
- success = Coffeelint.run_test_suite('app', conf) and Coffeelint.run_test_suite('spec', conf)
3
+ success = Coffeelint.run_test_suite('app') and Coffeelint.run_test_suite('spec')
16
4
  fail "Lint!" unless success
17
5
  end
@@ -0,0 +1,5 @@
1
+ {
2
+ "max_line_length": {
3
+ "value": 120
4
+ }
5
+ }
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ module CoffeeLint
4
+ describe Config do
5
+ describe '.locate' do
6
+ before(:each) { allow(File).to receive(:exists?) { false } }
7
+
8
+ it 'returns nil if no config file could be located' do
9
+ expect(Config.locate).to eq(nil)
10
+ end
11
+
12
+ context 'default locations' do
13
+ %w(coffeelint.json .coffeelint.json config/coffeelint.json config/.coffeelint.json).each do |config_file|
14
+ it "tries to locate #{config_file}" do
15
+ allow(File).to receive(:exists?).with(config_file).and_return(true)
16
+ expect(Config.locate).to eq(config_file)
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'environment variables' do
22
+ it 'tries to locate ENV[\'COFFEELINT_CONFIG\']' do
23
+ ENV['COFFEELINT_CONFIG'] = 'coffeelint.json'
24
+
25
+ allow(File).to receive(:exists?).with('coffeelint.json').and_return(true)
26
+ expect(Config.locate).to eq('coffeelint.json')
27
+ end
28
+
29
+ it 'tries to locate config files in ENV[\'HOME\']' do
30
+ ENV['HOME'] = '~/coffeescript'
31
+
32
+ allow(File).to receive(:exists?).with('~/coffeescript/.coffeelint.json').and_return(true)
33
+ expect(Config.locate).to eq('~/coffeescript/.coffeelint.json')
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '.parse' do
39
+ it 'should parse a given JSON file' do
40
+ expect(Config.parse(File.join(File.dirname(__FILE__), 'assets/.coffeelint.json'))).
41
+ to eq({"max_line_length" => {"value" => 120}})
42
+ end
43
+ end
44
+
45
+ context 'private class methods' do
46
+ describe '.config_files_in_path' do
47
+ it 'ignores empty path segments' do
48
+ result = %w(coffeelint.json .coffeelint.json)
49
+ expect(Config.send(:config_files_in_path, '')).to eq(result)
50
+ expect(Config.send(:config_files_in_path, [])).to eq(result)
51
+ expect(Config.send(:config_files_in_path, [''])).to eq(result)
52
+ end
53
+
54
+ it 'builds usefull path segements' do
55
+ result = %w(config/coffeelint.json config/.coffeelint.json)
56
+ expect(Config.send(:config_files_in_path, 'config')).to eq(result)
57
+ expect(Config.send(:config_files_in_path, ['config'])).to eq(result)
58
+ expect(Config.send(:config_files_in_path, ['coffeescript', 'config'])).
59
+ to eq(%w(coffeescript/config/coffeelint.json coffeescript/config/.coffeelint.json))
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coffeelint
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-29 00:00:00.000000000 Z
12
+ date: 2015-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: coffee-script
@@ -111,10 +111,13 @@ files:
111
111
  - coffeelint.gemspec
112
112
  - lib/coffeelint.rb
113
113
  - lib/coffeelint/cmd.rb
114
+ - lib/coffeelint/config.rb
114
115
  - lib/coffeelint/railtie.rb
115
116
  - lib/coffeelint/version.rb
116
117
  - lib/tasks/coffeelint.rake
118
+ - spec/assets/.coffeelint.json
117
119
  - spec/coffeelint_spec.rb
120
+ - spec/config_spec.rb
118
121
  - spec/spec_helper.rb
119
122
  - coffeelint/lib/coffeelint.js
120
123
  homepage: https://github.com/zipcodeman/coffeelint-ruby
@@ -143,5 +146,7 @@ signing_key:
143
146
  specification_version: 3
144
147
  summary: Ruby bindings for coffeelint along with railtie to add rake task to rails
145
148
  test_files:
149
+ - spec/assets/.coffeelint.json
146
150
  - spec/coffeelint_spec.rb
151
+ - spec/config_spec.rb
147
152
  - spec/spec_helper.rb