erb_lint 0.0.37 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/exe/erblint +1 -1
  3. data/lib/erb_lint/all.rb +26 -0
  4. data/lib/erb_lint/cli.rb +75 -29
  5. data/lib/erb_lint/corrector.rb +1 -1
  6. data/lib/erb_lint/linter.rb +5 -5
  7. data/lib/erb_lint/linter_config.rb +5 -3
  8. data/lib/erb_lint/linter_registry.rb +2 -2
  9. data/lib/erb_lint/linters/allowed_script_type.rb +9 -8
  10. data/lib/erb_lint/linters/closing_erb_tag_indent.rb +2 -2
  11. data/lib/erb_lint/linters/deprecated_classes.rb +8 -7
  12. data/lib/erb_lint/linters/erb_safety.rb +2 -2
  13. data/lib/erb_lint/linters/extra_newline.rb +1 -1
  14. data/lib/erb_lint/linters/final_newline.rb +2 -2
  15. data/lib/erb_lint/linters/hard_coded_string.rb +39 -16
  16. data/lib/erb_lint/linters/no_javascript_tag_helper.rb +11 -9
  17. data/lib/erb_lint/linters/partial_instance_variable.rb +23 -0
  18. data/lib/erb_lint/linters/require_input_autocomplete.rb +121 -0
  19. data/lib/erb_lint/linters/require_script_nonce.rb +92 -0
  20. data/lib/erb_lint/linters/right_trim.rb +1 -1
  21. data/lib/erb_lint/linters/rubocop.rb +13 -11
  22. data/lib/erb_lint/linters/rubocop_text.rb +1 -1
  23. data/lib/erb_lint/linters/self_closing_tag.rb +6 -7
  24. data/lib/erb_lint/linters/space_around_erb_tag.rb +8 -7
  25. data/lib/erb_lint/linters/space_in_html_tag.rb +7 -6
  26. data/lib/erb_lint/linters/space_indentation.rb +1 -1
  27. data/lib/erb_lint/linters/trailing_whitespace.rb +1 -1
  28. data/lib/erb_lint/offense.rb +8 -4
  29. data/lib/erb_lint/reporter.rb +4 -2
  30. data/lib/erb_lint/reporters/compact_reporter.rb +17 -4
  31. data/lib/erb_lint/reporters/json_reporter.rb +72 -0
  32. data/lib/erb_lint/reporters/multiline_reporter.rb +2 -1
  33. data/lib/erb_lint/runner.rb +3 -2
  34. data/lib/erb_lint/runner_config.rb +9 -7
  35. data/lib/erb_lint/runner_config_resolver.rb +5 -4
  36. data/lib/erb_lint/stats.rb +13 -6
  37. data/lib/erb_lint/utils/block_map.rb +3 -2
  38. data/lib/erb_lint/utils/offset_corrector.rb +2 -2
  39. data/lib/erb_lint/utils/ruby_to_erb.rb +6 -5
  40. data/lib/erb_lint/utils/severity_levels.rb +16 -0
  41. data/lib/erb_lint/version.rb +1 -1
  42. data/lib/erb_lint.rb +1 -24
  43. metadata +23 -17
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module ERBLint
6
+ module Reporters
7
+ class JsonReporter < Reporter
8
+ def preview; end
9
+
10
+ def show
11
+ puts formatted_data
12
+ end
13
+
14
+ private
15
+
16
+ def formatted_data
17
+ {
18
+ metadata: metadata,
19
+ files: formatted_files,
20
+ summary: summary,
21
+ }.to_json
22
+ end
23
+
24
+ def metadata
25
+ {
26
+ erb_lint_version: ERBLint::VERSION,
27
+ ruby_engine: RUBY_ENGINE,
28
+ ruby_version: RUBY_VERSION,
29
+ ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
30
+ ruby_platform: RUBY_PLATFORM,
31
+ }
32
+ end
33
+
34
+ def summary
35
+ {
36
+ offenses: stats.found,
37
+ inspected_files: stats.processed_files.size,
38
+ corrected: stats.corrected,
39
+ }
40
+ end
41
+
42
+ def formatted_files
43
+ processed_files.map do |filename, offenses|
44
+ {
45
+ path: filename,
46
+ offenses: formatted_offenses(offenses),
47
+ }
48
+ end
49
+ end
50
+
51
+ def formatted_offenses(offenses)
52
+ offenses.map do |offense|
53
+ format_offense(offense)
54
+ end
55
+ end
56
+
57
+ def format_offense(offense)
58
+ {
59
+ linter: offense.linter.class.simple_name,
60
+ message: offense.message.to_s,
61
+ location: {
62
+ start_line: offense.line_number,
63
+ start_column: offense.column,
64
+ last_line: offense.source_range.last_line,
65
+ last_column: offense.source_range.last_column,
66
+ length: offense.source_range.length,
67
+ },
68
+ }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "compact_reporter"
3
4
 
4
5
  module ERBLint
@@ -9,7 +10,7 @@ module ERBLint
9
10
  def format_offense(filename, offense)
10
11
  <<~EOF
11
12
 
12
- #{offense.message}#{Rainbow(' (not autocorrected)').red if autocorrect}
13
+ #{offense.message}#{Rainbow(" (not autocorrected)").red if autocorrect}
13
14
  In file: #{filename}:#{offense.line_number}
14
15
  EOF
15
16
  end
@@ -8,7 +8,7 @@ module ERBLint
8
8
  def initialize(file_loader, config)
9
9
  @file_loader = file_loader
10
10
  @config = config || RunnerConfig.default
11
- raise ArgumentError, 'expect `config` to be a RunnerConfig instance' unless @config.is_a?(RunnerConfig)
11
+ raise ArgumentError, "expect `config` to be a RunnerConfig instance" unless @config.is_a?(RunnerConfig)
12
12
 
13
13
  linter_classes = LinterRegistry.linters.select { |klass| @config.for_linter(klass).enabled? }
14
14
  @linters = linter_classes.map do |linter_class|
@@ -18,8 +18,9 @@ module ERBLint
18
18
  end
19
19
 
20
20
  def run(processed_source)
21
+ relative_filename = processed_source.filename.delete_prefix("#{@file_loader.base_path}/")
21
22
  @linters
22
- .reject { |linter| linter.excludes_file?(processed_source.filename) }
23
+ .reject { |linter| linter.excludes_file?(relative_filename) }
23
24
  .each do |linter|
24
25
  linter.run(processed_source)
25
26
  @offenses.concat(linter.offenses)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'erb_lint/runner_config_resolver'
3
+ require "erb_lint/runner_config_resolver"
4
4
 
5
5
  module ERBLint
6
6
  class RunnerConfig
@@ -9,7 +9,7 @@ module ERBLint
9
9
  def initialize(config = nil, file_loader = nil)
10
10
  @config = (config || {}).dup.deep_stringify_keys
11
11
 
12
- resolver.resolve_inheritance_from_gems(@config, @config.delete('inherit_gem'))
12
+ resolver.resolve_inheritance_from_gems(@config, @config.delete("inherit_gem"))
13
13
  resolver.resolve_inheritance(@config, file_loader) if file_loader
14
14
  @config.delete("inherit_from")
15
15
  end
@@ -24,15 +24,16 @@ module ERBLint
24
24
  elsif klass.is_a?(Class) && klass <= ERBLint::Linter
25
25
  klass.simple_name
26
26
  else
27
- raise ArgumentError, 'expected String or linter class'
27
+ raise ArgumentError, "expected String or linter class"
28
28
  end
29
29
  linter_klass = LinterRegistry.find_by_name(klass_name)
30
30
  raise Error, "#{klass_name}: linter not found (is it loaded?)" unless linter_klass
31
+
31
32
  linter_klass.config_schema.new(config_hash_for_linter(klass_name))
32
33
  end
33
34
 
34
35
  def global_exclude
35
- @config['exclude'] || []
36
+ @config["exclude"] || []
36
37
  end
37
38
 
38
39
  def merge(other_config)
@@ -61,6 +62,7 @@ module ERBLint
61
62
  SpaceIndentation: { enabled: default_enabled },
62
63
  SpaceInHtmlTag: { enabled: default_enabled },
63
64
  TrailingWhitespace: { enabled: default_enabled },
65
+ RequireInputAutocomplete: { enabled: default_enabled },
64
66
  },
65
67
  )
66
68
  end
@@ -74,13 +76,13 @@ module ERBLint
74
76
  private
75
77
 
76
78
  def linters_config
77
- @config['linters'] || {}
79
+ @config["linters"] || {}
78
80
  end
79
81
 
80
82
  def config_hash_for_linter(klass_name)
81
83
  config_hash = linters_config[klass_name] || {}
82
- config_hash['exclude'] ||= []
83
- config_hash['exclude'].concat(global_exclude) if config_hash['exclude'].is_a?(Array)
84
+ config_hash["exclude"] ||= []
85
+ config_hash["exclude"].concat(global_exclude) if config_hash["exclude"].is_a?(Array)
84
86
  config_hash
85
87
  end
86
88
 
@@ -24,10 +24,11 @@
24
24
  module ERBLint
25
25
  class RunnerConfigResolver
26
26
  def resolve_inheritance(hash, file_loader)
27
- inherited_files = Array(hash['inherit_from'])
27
+ inherited_files = Array(hash["inherit_from"])
28
28
  base_configs(file_loader, inherited_files).reverse_each do |base_config|
29
29
  base_config.each do |k, v|
30
30
  next unless v.is_a?(Hash)
31
+
31
32
  v = v.deep_merge(hash[k]) if hash.key?(k)
32
33
  hash[k] = v
33
34
  end
@@ -36,12 +37,12 @@ module ERBLint
36
37
 
37
38
  def resolve_inheritance_from_gems(hash, gems)
38
39
  (gems || {}).each_pair do |gem_name, config_path|
39
- raise(ArgumentError, "can't inherit configuration from the erb-lint gem") if gem_name == 'erb-lint'
40
+ raise(ArgumentError, "can't inherit configuration from the erb-lint gem") if gem_name == "erb-lint"
40
41
 
41
- hash['inherit_from'] = Array(hash['inherit_from'])
42
+ hash["inherit_from"] = Array(hash["inherit_from"])
42
43
  Array(config_path).reverse_each do |path|
43
44
  # Put gem configuration first so local configuration overrides it.
44
- hash['inherit_from'].unshift(gem_config_path(gem_name, path))
45
+ hash["inherit_from"].unshift(gem_config_path(gem_name, path))
45
46
  end
46
47
  end
47
48
  end
@@ -1,25 +1,32 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ERBLint
3
4
  class Stats
4
- attr_accessor :found,
5
- :corrected,
6
- :exceptions,
7
- :linters,
8
- :files,
9
- :processed_files
5
+ attr_accessor :ignored,
6
+ :found,
7
+ :corrected,
8
+ :exceptions,
9
+ :linters,
10
+ :autocorrectable_linters,
11
+ :files,
12
+ :processed_files
10
13
 
11
14
  def initialize(
15
+ ignored: 0,
12
16
  found: 0,
13
17
  corrected: 0,
14
18
  exceptions: 0,
15
19
  linters: 0,
20
+ autocorrectable_linters: 0,
16
21
  files: 0,
17
22
  processed_files: {}
18
23
  )
24
+ @ignored = ignored
19
25
  @found = found
20
26
  @corrected = corrected
21
27
  @exceptions = exceptions
22
28
  @linters = linters
29
+ @autocorrectable_linters = autocorrectable_linters
23
30
  @files = files
24
31
  @processed_files = processed_files
25
32
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'better_html/ast/node'
4
- require 'better_html/test_helper/ruby_node'
3
+ require "better_html/ast/node"
4
+ require "better_html/test_helper/ruby_node"
5
5
 
6
6
  module ERBLint
7
7
  module Utils
@@ -194,6 +194,7 @@ module ERBLint
194
194
 
195
195
  def find_entry(range)
196
196
  return unless range
197
+
197
198
  @entries.find do |entry|
198
199
  entry.contains_ruby_range?(Range.new(range.begin_pos, range.end_pos))
199
200
  end
@@ -63,8 +63,8 @@ module ERBLint
63
63
  node_or_range
64
64
  else
65
65
  raise TypeError,
66
- 'Expected a Parser::Source::Range, Comment or ' \
67
- "Rubocop::AST::Node, got #{node_or_range.class}"
66
+ "Expected a Parser::Source::Range, Comment or " \
67
+ "Rubocop::AST::Node, got #{node_or_range.class}"
68
68
  end
69
69
  end
70
70
  end
@@ -9,14 +9,15 @@ module ERBLint
9
9
  def html_options_to_tag_attributes(hash_node)
10
10
  hash_node.children.map do |pair_node|
11
11
  key_node, value_node = *pair_node
12
- key = ruby_to_erb(key_node, '=') { |s| s.tr('_', '-') }
13
- value = ruby_to_erb(value_node, '=') { |s| escape_quote(s) }
14
- [key, "\"#{value}\""].join('=')
15
- end.join(' ')
12
+ key = ruby_to_erb(key_node, "=") { |s| s.tr("_", "-") }
13
+ value = ruby_to_erb(value_node, "=") { |s| escape_quote(s) }
14
+ [key, "\"#{value}\""].join("=")
15
+ end.join(" ")
16
16
  end
17
17
 
18
18
  def ruby_to_erb(node, indicator = nil, &block)
19
19
  return node if node.nil? || node.is_a?(String)
20
+
20
21
  case node.type
21
22
  when :str, :sym
22
23
  s = node.children.first.to_s
@@ -43,7 +44,7 @@ module ERBLint
43
44
  end
44
45
 
45
46
  def escape_quote(str)
46
- str.gsub('"', '&quot;')
47
+ str.gsub('"', "&quot;")
47
48
  end
48
49
  end
49
50
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ERBLint
4
+ module Utils
5
+ module SeverityLevels
6
+ SEVERITY_NAMES = [:info, :refactor, :convention, :warning, :error, :fatal].freeze
7
+
8
+ SEVERITY_CODE_TABLE = { I: :info, R: :refactor, C: :convention,
9
+ W: :warning, E: :error, F: :fatal, }.freeze
10
+
11
+ def severity_level_for_name(name)
12
+ SEVERITY_NAMES.index(name || :error) + 1
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ERBLint
4
- VERSION = '0.0.37'
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/erb_lint.rb CHANGED
@@ -1,26 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubocop'
4
-
5
- require 'erb_lint/corrector'
6
- require 'erb_lint/file_loader'
7
- require 'erb_lint/linter_config'
8
- require 'erb_lint/linter_registry'
9
- require 'erb_lint/linter'
10
- require 'erb_lint/offense'
11
- require 'erb_lint/processed_source'
12
- require 'erb_lint/runner_config'
13
- require 'erb_lint/runner'
14
- require 'erb_lint/version'
15
- require 'erb_lint/stats'
16
- require 'erb_lint/reporter'
17
-
18
- # Load linters
19
- Dir[File.expand_path('erb_lint/linters/**/*.rb', File.dirname(__FILE__))].each do |file|
20
- require file
21
- end
22
-
23
- # Load reporters
24
- Dir[File.expand_path('erb_lint/reporters/**/*.rb', File.dirname(__FILE__))].each do |file|
25
- require file
26
- end
3
+ require "erb_lint/version"
metadata CHANGED
@@ -1,45 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.37
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Chan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-12 00:00:00.000000000 Z
11
+ date: 2022-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: better_html
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.7
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.7
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: html_tokenizer
28
+ name: better_html
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 1.0.7
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 1.0.7
41
41
  - !ruby/object:Gem::Dependency
42
- name: rubocop
42
+ name: html_tokenizer
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.7.1.4
69
69
  - !ruby/object:Gem::Dependency
70
- name: activesupport
70
+ name: rainbow
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: smart_properties
84
+ name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rainbow
98
+ name: smart_properties
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -160,6 +160,7 @@ extra_rdoc_files: []
160
160
  files:
161
161
  - exe/erblint
162
162
  - lib/erb_lint.rb
163
+ - lib/erb_lint/all.rb
163
164
  - lib/erb_lint/cli.rb
164
165
  - lib/erb_lint/corrector.rb
165
166
  - lib/erb_lint/file_loader.rb
@@ -175,6 +176,9 @@ files:
175
176
  - lib/erb_lint/linters/hard_coded_string.rb
176
177
  - lib/erb_lint/linters/no_javascript_tag_helper.rb
177
178
  - lib/erb_lint/linters/parser_errors.rb
179
+ - lib/erb_lint/linters/partial_instance_variable.rb
180
+ - lib/erb_lint/linters/require_input_autocomplete.rb
181
+ - lib/erb_lint/linters/require_script_nonce.rb
178
182
  - lib/erb_lint/linters/right_trim.rb
179
183
  - lib/erb_lint/linters/rubocop.rb
180
184
  - lib/erb_lint/linters/rubocop_text.rb
@@ -187,6 +191,7 @@ files:
187
191
  - lib/erb_lint/processed_source.rb
188
192
  - lib/erb_lint/reporter.rb
189
193
  - lib/erb_lint/reporters/compact_reporter.rb
194
+ - lib/erb_lint/reporters/json_reporter.rb
190
195
  - lib/erb_lint/reporters/multiline_reporter.rb
191
196
  - lib/erb_lint/runner.rb
192
197
  - lib/erb_lint/runner_config.rb
@@ -195,6 +200,7 @@ files:
195
200
  - lib/erb_lint/utils/block_map.rb
196
201
  - lib/erb_lint/utils/offset_corrector.rb
197
202
  - lib/erb_lint/utils/ruby_to_erb.rb
203
+ - lib/erb_lint/utils/severity_levels.rb
198
204
  - lib/erb_lint/version.rb
199
205
  homepage: https://github.com/Shopify/erb-lint
200
206
  licenses:
@@ -216,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
222
  - !ruby/object:Gem::Version
217
223
  version: '0'
218
224
  requirements: []
219
- rubygems_version: 3.0.3
225
+ rubygems_version: 3.3.3
220
226
  signing_key:
221
227
  specification_version: 4
222
228
  summary: ERB lint tool