eslint-webpacker 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c862f7643f282334fe5b6e3858ac3a1186d561df164245d01931e7e0204bfebd
4
+ data.tar.gz: 2a098fd6df1905629206b729c6675be84985539d5c85bfea2079a605d228c60b
5
+ SHA512:
6
+ metadata.gz: 48d83f8ea6260c95fd6a582fb7d2d4cdcb1829a6aa6be87dcd9ad0ca66b46aadf03c9da63b573f7d58d8f4e1f59a82556c6ac892db766d41b6e071a1f59b9fa6
7
+ data.tar.gz: ec22a9ce7d9f7f1d8cff02e73c751a1ba442a25d34d1dac10b4a812bc4b08295ffd5c9b210d84f211e74e2dc78e60525edb39025ecd35c67d11f53a6903f82bd
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+
7
+ Metrics/BlockLength:
8
+ ExcludedMethods: ['describe', 'context']
9
+
10
+ Layout/LineLength:
11
+ Max: 80
12
+
13
+ Naming/RescuedExceptionsVariableName:
14
+ Enabled: Yes
15
+ PreferredName: error
16
+
17
+ Style/Documentation:
18
+ Enabled: No
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+
10
+
11
+ ## [1.0.1] - 2021-03-22
12
+ ### Fixed
13
+ - Version file loading from gemspec file
14
+
15
+ ## [1.0.0] - 2021-03-22
16
+ ### Fixed
17
+ - Initial import running `eslint` or `eslint_d`
18
+
19
+
20
+ [Unreleased]: https://gitlab.com/zedtux/eslint-webpacker/-/compare/v1.0.1...master
21
+ [1.0.1]: https://gitlab.com/zedtux/eslint-webpacker/-/compare/v1.0.0...v1.0.1
22
+ [1.0.0]: https://gitlab.com/zedtux/eslint-webpacker/-/tags/v1.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eslint-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Guillaume Hain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # eslint-webpacker
2
+
3
+ A Ruby on Rails gem that runs `eslint` (https://eslint.org/) from the `node_modules` folder built with [the webpacker gem](https://github.com/rails/webpacker).
4
+
5
+ I've made this gem in order to use it in my project's `Rakefile` so that it checks the javascript files, like [rubocop](https://github.com/rubocop/rubocop) does for Ruby files.
6
+
7
+ Supported file extensions are:
8
+
9
+ - _.js_
10
+ - _.jsx_
11
+ - _.es6_
12
+
13
+ This repo is a fork of [the eslint-rails gem](https://github.com/appfolio/eslint-rails).
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ group :development do
21
+ gem 'eslint-rails'
22
+ end
23
+ ```
24
+
25
+ If not already done, install the `eslint` package and configure your plugins. Also see bellow if it's too slow.
26
+
27
+ ## Usage
28
+
29
+ ### CLI
30
+
31
+ This gem comes with one Rake task:
32
+
33
+ ```sh
34
+ rake eslint:run
35
+ ```
36
+
37
+ Ran like this, it will scan all the JavaScript files from your project.
38
+
39
+ But you can supply a filename to the task, using several different formats, and it will lint just that file. For example, to analyze `app/javascript/components/utilities.js`, you can run any of the following:
40
+
41
+ ```sh
42
+ rake eslint:run[components/utilities]
43
+ rake eslint:run[components/utilities.js]
44
+ rake eslint:run[app/javascript/components/utilities]
45
+ rake eslint:run[app/javascript/components/utilities.js]
46
+ ```
47
+
48
+ ## But it is really slow!
49
+
50
+ You are using the `eslint` NPM package which requires to boot Node JS for each files.
51
+
52
+ In order to speed up the checks, install [estlint_d](https://github.com/mantoni/eslint_d.js) which runs ESlint in a daemon :
53
+
54
+ ```
55
+ yarn add eslint_d
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on Gitlab at https://gitlab.com/zedtux/eslint-webpacker.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/eslint-webpacker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'eslint-webpacker'
7
+ spec.version = ESLintWebpacker::VERSION
8
+ spec.authors = ['Guillaume Hain']
9
+ spec.email = ['zedtux@zedroot.org']
10
+ spec.summary = 'Runs eslint or eslint_d from Ruby'
11
+ spec.description = 'This gem is useful when added to the Rakefile in ' \
12
+ 'order to scan each of your JavaScript files in a ' \
13
+ 'Ruby On Rails project using the webpacker gem.'
14
+ spec.homepage = 'https://gitlab.com/zedtux/eslint-webpacker'
15
+ spec.license = 'MIT'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = "#{spec.homepage}-/blob/master/CHANGELOG.md"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.required_ruby_version = '>= 2.4'
29
+
30
+ spec.add_dependency 'colorize'
31
+ spec.add_dependency 'railties', '>= 3.2'
32
+ spec.add_dependency 'webpacker'
33
+
34
+ spec.add_development_dependency 'bundler'
35
+ spec.add_development_dependency 'rake'
36
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eslint-webpacker/engine'
4
+ require 'eslint-webpacker/runner'
5
+ require 'eslint-webpacker/text_formatter'
6
+ require 'eslint-webpacker/version'
7
+ require 'eslint-webpacker/warning'
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ESLintRails
4
+ class Engine < Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+
5
+ module ESLintWebpacker
6
+ class Runner
7
+ include ActionView::Helpers::JavaScriptHelper
8
+
9
+ JAVASCRIPT_EXTENSIONS = %w[.js .jsx .es6].freeze
10
+
11
+ def initialize(file)
12
+ @file = normalize_infile(file)
13
+ end
14
+
15
+ def run
16
+ javascripts = javascript_files
17
+
18
+ puts "Inspecting #{javascripts.size} files"
19
+
20
+ warnings = javascripts.map do |javascript|
21
+ generate_warnings(javascript).tap { |warns| output_progress(warns) }
22
+ end
23
+
24
+ puts
25
+
26
+ warnings.flatten
27
+ end
28
+
29
+ private
30
+
31
+ def descendant?(file_a, file_b)
32
+ a_list = file_a.to_s.split('/')
33
+ b_list = file_b.to_s.split('/')
34
+
35
+ b_list[0..a_list.size - 1] == a_list
36
+ end
37
+
38
+ def eslint_d_available?
39
+ File.exist?(Rails.root.join('node_modules', '.bin', 'eslint_d'))
40
+ end
41
+
42
+ def generate_warnings(javascript)
43
+ relative_path = javascript.relative_path_from(Pathname.new(Dir.pwd))
44
+
45
+ warning_hashes(javascript).flat_map do |hash|
46
+ next if hash['errorCount'].zero?
47
+
48
+ hash['messages'].map do |message|
49
+ ESLintWebpacker::Warning.new(relative_path, message)
50
+ end
51
+ end.compact
52
+ end
53
+
54
+ def javascript_files
55
+ all_javascripts = webpacker_files.select do |asset|
56
+ JAVASCRIPT_EXTENSIONS.include?(asset.extname)
57
+ end
58
+
59
+ javascript_files = all_javascripts.select { |a| descendant?(@file, a) }
60
+
61
+ javascript_files.reject do |path|
62
+ path.to_s =~ /eslint.js|vendor|gems|min.js|editorial/
63
+ end
64
+ end
65
+
66
+ def normalize_infile(file)
67
+ # Remove beginning of asset path
68
+ file = file.to_s.gsub(%r{^app/javascript/}, '')
69
+
70
+ # Ensure path is absolute
71
+ file = Pathname.new("#{Dir.pwd}/app/javascript/#{file}")
72
+
73
+ return file if file.directory?
74
+ return file if file.extname.present?
75
+
76
+ # Make sure it has an extension
77
+ Pathname.new("#{file}.js")
78
+ end
79
+
80
+ def output_progress(warnings)
81
+ print case file_severity(warnings)
82
+ when :high
83
+ 'H'.red
84
+ when :low
85
+ 'L'.yellow
86
+ else
87
+ '.'.green
88
+ end
89
+ end
90
+
91
+ def warning_hashes(file_path)
92
+ command = eslint_d_available? ? 'eslint_d' : 'eslint'
93
+
94
+ JSON.parse(`./node_modules/.bin/#{command} --format json #{file_path}`)
95
+ end
96
+
97
+ def webpacker_files
98
+ Dir.glob(Rails.root.join('app', 'javascript', '**/*')).map do |path|
99
+ Pathname.new(path)
100
+ end
101
+ end
102
+
103
+ def file_severity(warnings)
104
+ return :none if warnings.blank?
105
+
106
+ warnings.map(&:severity).uniq.min
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/strip'
4
+ require 'colorize'
5
+
6
+ module ESLintWebpacker
7
+ class TextFormatter
8
+ def initialize(warnings)
9
+ @warnings = warnings
10
+ end
11
+
12
+ def format
13
+ max_line_column_length = max_length_of_attribute(:location)
14
+ max_rule_id_length = max_length_of_attribute(:rule_id)
15
+ max_message_length = max_length_of_attribute(:message)
16
+ @warnings.each do |warning|
17
+ message = [
18
+ warning.location.ljust(max_line_column_length + 1),
19
+ warning.severity.to_s.ljust(6),
20
+ warning.rule_id.ljust(max_rule_id_length),
21
+ warning.message.ljust(max_message_length)
22
+ ].join(' ')
23
+ colorized_message =
24
+ case warning.severity
25
+ when :low
26
+ message.green
27
+ when :high
28
+ message.yellow
29
+ else
30
+ raise 'BULLSHIT'
31
+ end
32
+ puts colorized_message
33
+ end
34
+
35
+ puts "#{@warnings.size} warning(s) found."
36
+ end
37
+
38
+ private
39
+
40
+ def max_length_of_attribute(attr_key)
41
+ @warnings.map { |warning| warning.send(attr_key).size }.max
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ESLintWebpacker
4
+ VERSION = '1.0.1'
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ESLintWebpacker
4
+ class Warning
5
+ attr_reader :filename, :line, :column, :node_type
6
+
7
+ SEVERITY = %i[low high].freeze
8
+ private_constant :SEVERITY
9
+
10
+ def initialize(filename, warning_hash)
11
+ @filename = filename
12
+ @rule_id = warning_hash['ruleId'] || 'unexpected error'
13
+ @severity = warning_hash['severity']
14
+ @message = warning_hash['message']
15
+ @line = warning_hash['line']
16
+ @column = warning_hash['column']
17
+ @node_type = warning_hash['nodeType']
18
+ end
19
+
20
+ def severity
21
+ SEVERITY[@severity - 1]
22
+ end
23
+
24
+ def location
25
+ "#{filename}:#{line}:#{column}"
26
+ end
27
+
28
+ def rule_id
29
+ @rule_id || 'N/A'
30
+ end
31
+
32
+ def message
33
+ @message || 'N/A'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eslint-webpacker'
4
+
5
+ namespace :eslint do
6
+ def run_and_print_results(file)
7
+ puts 'Running Eslint...'
8
+ warnings = ESLintWebpacker::Runner.new(file).run
9
+
10
+ if warnings.empty?
11
+ puts 'All good! :)'.green
12
+ exit 0
13
+ else
14
+ formatter = ESLintWebpacker::TextFormatter.new(warnings)
15
+ formatter.format
16
+ exit 1
17
+ end
18
+ end
19
+
20
+ desc 'Run ESLint against the specified JavaScript file or the entire ' \
21
+ 'project and report warnings'
22
+ task :run, [:filename] => :environment do |_, args|
23
+ run_and_print_results(args[:filename])
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eslint-webpacker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Hain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webpacker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This gem is useful when added to the Rakefile in order to scan each of
84
+ your JavaScript files in a Ruby On Rails project using the webpacker gem.
85
+ email:
86
+ - zedtux@zedroot.org
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - eslint-webpacker.gemspec
99
+ - lib/eslint-webpacker.rb
100
+ - lib/eslint-webpacker/engine.rb
101
+ - lib/eslint-webpacker/runner.rb
102
+ - lib/eslint-webpacker/text_formatter.rb
103
+ - lib/eslint-webpacker/version.rb
104
+ - lib/eslint-webpacker/warning.rb
105
+ - lib/tasks/eslint.rake
106
+ homepage: https://gitlab.com/zedtux/eslint-webpacker
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ allowed_push_host: https://rubygems.org
111
+ homepage_uri: https://gitlab.com/zedtux/eslint-webpacker
112
+ source_code_uri: https://gitlab.com/zedtux/eslint-webpacker
113
+ changelog_uri: https://gitlab.com/zedtux/eslint-webpacker-/blob/master/CHANGELOG.md
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '2.4'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubygems_version: 3.1.4
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Runs eslint or eslint_d from Ruby
133
+ test_files: []