eslint-rails 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a72fcfdebdae83dcc61832c1d38fdf48dd6bf68
4
- data.tar.gz: 2cd33b71cb583ab5a2c3662ddda5c768c0115e05
3
+ metadata.gz: a225cfc475435f30b32752520bc9d542e92d4734
4
+ data.tar.gz: 59b7cf5158a98e762febff32834c91f4b3e6a2fe
5
5
  SHA512:
6
- metadata.gz: 0d773e32beb13c5a3daf0001a354ff4218b0920ed1fcc6e1c6854898e844620879d4f8e160b02e3e77a0823ed3aa2f8d269a25fb588765b5dbad09efd070931f
7
- data.tar.gz: 2dacc23af60766cb8d7d255a212417833d0bf11d033d4dfc6b701952b576b9552fde4a4d82ef570b56ae4bc05fbd833cb6dd5904f3b351bbac7c04c2577f3ea9
6
+ metadata.gz: 3aeb4cd0968fa89ae1430d065f5de8fcb1f490d6f0f9e76e22f376331f7b1d49caee5717676fdd1c7eaef70a6cc1be91edd303f84f3352f30a878e8d6ce82307
7
+ data.tar.gz: 1df9381c4a7c723b0a111f744a36726dd897c571f7a4e8f680142d18c28ee1d384a2dca276cf08c3e8fbf39fc0b341112afbec12d3766cc23fe1f7f20f3bd534
data/README.md CHANGED
@@ -36,17 +36,25 @@ in your browser. To force retrieval of the default conguration, use
36
36
 
37
37
  ![rake-eslint-rails-run][]
38
38
 
39
+ This will analyze all of the javascript files in the project:
40
+
41
+ ```sh
42
+ rake eslint:run_all
43
+ ```
44
+
45
+ Or you can run it on a single file. This will analyze `application.js`:
46
+
39
47
  ```sh
40
48
  rake eslint:run
41
49
  ```
42
50
 
43
- This will analyze `application.js`. Optionally, you can supply a filename to the
44
- task. To analyze `components/woop.js` and `utilities.js.coffee.erb` you would
45
- run (respectively):
51
+ Or, you can supply a filename to the task, using several different formats, and it will lint just that file. For example, to analyze `app/assets/javascripts/components/utilities.js`, you can run any of the following:
46
52
 
47
53
  ```sh
48
- rake eslint:run[components/woop]
49
- rake eslint:run[utilities]
54
+ rake eslint:run[components/utilities]
55
+ rake eslint:run[components/utilities.js]
56
+ rake eslint:run[app/assets/javascripts/components/utilities]
57
+ rake eslint:run[app/assets/javascripts/components/utilities.js]
50
58
  ```
51
59
 
52
60
  ### Web interface
@@ -1,24 +1,83 @@
1
+ require 'colorize'
2
+
1
3
  module ESLintRails
2
4
  class Runner
3
5
  include ActionView::Helpers::JavaScriptHelper
4
6
 
5
- def initialize(filename)
6
- @filename = filename || 'application'
7
+ def initialize(file)
8
+ @file = normalize_infile(file)
7
9
  end
8
10
 
9
11
  def run
10
- file_content = Rails.application.assets[@filename].to_s
11
-
12
- eslint_js = Rails.application.assets['eslint'].to_s
12
+ warnings = assets.map do |asset|
13
+ generate_warnings(asset).tap { |warnings| output_progress(warnings) }
14
+ end
15
+
16
+ puts
17
+
18
+ warnings.flatten
19
+ end
20
+
21
+ private
22
+
23
+ def normalize_infile(file)
24
+ file = file.to_s.gsub(/^app\/assets\/javascripts\//, '') # Remove beginning of asset path
25
+ file = Pathname.new("#{Dir.pwd}/app/assets/javascripts/#{file}") # Ensure path is absolute
26
+ file = Pathname.new("#{file}.js") if !file.directory? && file.extname.empty? # Make sure it has an extension
27
+ file
28
+ end
29
+
30
+ def assets
31
+ all_js_assets = Rails.application.assets.each_file.to_a.select { |pn| pn.extname == '.js' }
32
+
33
+ assets = all_js_assets.select{|a| is_descendant?(@file, a)}
13
34
 
14
- warning_hashes = ExecJS.eval <<-JS
35
+ assets.reject{|a| a.to_s =~ /eslint.js|vendor|gems|min.js|editorial/ }
36
+ end
37
+
38
+ def eslint_js
39
+ @eslint_js ||= Rails.application.assets['eslint'].to_s
40
+ end
41
+
42
+ def warning_hashes(file_content)
43
+ ExecJS.eval <<-JS
15
44
  function () {
16
45
  window = this;
17
46
  #{eslint_js};
18
- return eslint.verify('#{escape_javascript file_content}', #{Config.read});
47
+ return eslint.verify('#{escape_javascript(file_content)}', #{Config.read});
19
48
  }()
20
49
  JS
21
- warning_hashes.map{|hash| ESLintRails::Warning.new(hash)}
50
+ end
51
+
52
+ def generate_warnings(asset)
53
+ relative_path = asset.relative_path_from(Pathname.new(Dir.pwd))
54
+ file_content = asset.read
55
+
56
+ warning_hashes(file_content).map do |hash|
57
+ ESLintRails::Warning.new(relative_path, hash)
58
+ end
59
+ end
60
+
61
+ def output_progress(warnings)
62
+ print case file_severity(warnings)
63
+ when :high
64
+ 'H'.red
65
+ when :low
66
+ 'L'.yellow
67
+ else
68
+ '.'.green
69
+ end
70
+ end
71
+
72
+ def file_severity(warnings)
73
+ warnings.map(&:severity).uniq.sort.first
74
+ end
75
+
76
+ def is_descendant?(a, b)
77
+ a_list = a.to_s.split('/')
78
+ b_list = b.to_s.split('/')
79
+
80
+ b_list[0..a_list.size-1] == a_list
22
81
  end
23
82
  end
24
83
  end
@@ -4,21 +4,17 @@ require 'colorize'
4
4
  module ESLintRails
5
5
  class TextFormatter
6
6
 
7
- WARNING_TEMPLATE = <<-TEXT.strip_heredoc
8
- <%= line %>,<%= column %> <%= severity %>! [<%= rule_id %>] <%= message %>
9
- TEXT
10
-
11
7
  def initialize(warnings)
12
8
  @warnings = warnings
13
9
  end
14
10
 
15
11
  def format
16
- max_line_column_length = @warnings.map { |warning| (warning.line + warning.column).size }.max
12
+ max_line_column_length = @warnings.map { |warning| warning.location.size }.max
17
13
  max_rule_id_length = @warnings.map { |warning| warning.rule_id.size }.max
18
14
  max_message_length = @warnings.map { |warning| warning.message.size }.max
19
15
  @warnings.each do |warning|
20
16
  message = [
21
- "#{warning.line}:#{warning.column}".ljust(max_line_column_length + 1),
17
+ warning.location.ljust(max_line_column_length + 1),
22
18
  warning.severity.to_s.ljust(6),
23
19
  warning.rule_id.ljust(max_rule_id_length),
24
20
  warning.message.ljust(max_message_length)
@@ -1,3 +1,3 @@
1
1
  module ESLintRails
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -1,11 +1,12 @@
1
1
  module ESLintRails
2
2
  class Warning
3
- attr_reader :rule_id, :message, :line, :column, :node_type
3
+ attr_reader :filename, :rule_id, :message, :line, :column, :node_type
4
4
 
5
5
  SEVERITY = [ :low, :high ].freeze
6
6
  private_constant :SEVERITY
7
7
 
8
- def initialize(warning_hash)
8
+ def initialize(filename, warning_hash)
9
+ @filename = filename
9
10
  @rule_id = warning_hash['ruleId'] || "unexpected error"
10
11
  @severity = warning_hash['severity']
11
12
  @message = warning_hash['message']
@@ -17,5 +18,9 @@ module ESLintRails
17
18
  def severity
18
19
  SEVERITY[@severity-1]
19
20
  end
21
+
22
+ def location
23
+ "#{filename}:#{line}:#{column}"
24
+ end
20
25
  end
21
26
  end
@@ -3,10 +3,8 @@ ENV['EXECJS_RUNTIME'] = 'RubyRacer'
3
3
  require 'eslint-rails'
4
4
 
5
5
  namespace :eslint do
6
-
7
- desc %{Run ESLint against the specified JavaScript file and report warnings (default is 'application')}
8
- task :run, [:filename] => :environment do |_, args|
9
- warnings = ESLintRails::Runner.new(args[:filename]).run
6
+ def run_and_print_results(file)
7
+ warnings = ESLintRails::Runner.new(file).run
10
8
 
11
9
  if warnings.empty?
12
10
  puts 'All good! :)'.green
@@ -18,6 +16,16 @@ namespace :eslint do
18
16
  end
19
17
  end
20
18
 
19
+ desc %{Run ESLint against the specified JavaScript file and report warnings (default is 'application')}
20
+ task :run, [:filename] => :environment do |_, args|
21
+ run_and_print_results(args[:filename] || 'application')
22
+ end
23
+
24
+ desc 'Run ESLint against all project javascript files and report warnings'
25
+ task run_all: :environment do |_, args|
26
+ run_and_print_results(nil) # Run all
27
+ end
28
+
21
29
  desc 'Print the current configuration file (Uses local config/eslint.json if it exists; uses default config/eslint.json if it does not; optionally force default by passing a parameter)'
22
30
  task :print_config, [:force_default] => :environment do |_, args|
23
31
  puts ESLintRails::Config.read(force_default: args[:force_default])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eslint-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Force
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-26 00:00:00.000000000 Z
12
+ date: 2016-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties