erb_lint 0.0.26 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82be0f658ce3baab1198821bebab48ed006b10be
4
- data.tar.gz: b7362a45f4b3a1c33f2fcc5be16c147fce73e04d
3
+ metadata.gz: '068550383b58968cb644751f43444a984065ac9c'
4
+ data.tar.gz: 78c31c8ce1a65a13a8b8a168437fa4142191d132
5
5
  SHA512:
6
- metadata.gz: bb77c4fb3259e5bf91f6881cd573c328959c79c931ac5e8b1a589050a74d8b3167e651df93d40340f2080532f1121d65eddf4d6d8ef481a93d92e61d744e76d8
7
- data.tar.gz: efabfee67363da1cd91591d7acd37098c06ae51da74e1d12a653e0d02965c82c5b996b839f2ba3c823830b7063ea6ee512ed16c73b6c063f3a98639f9f90f646
6
+ metadata.gz: 01530ffea26264a299ee8b0460887927e738479fd1f23dfda161877d2b391b0050cc6e7f880382becdba1b4e3f7310174e449886fd6d2047c59507e05a5cb7c1
7
+ data.tar.gz: 67c4dba8e9214bc5cbc11beed301d9fc5eaf09bbf6b4046038fcdc15173879b1165c82af351d9e4915c08a88dd209cc093e2e58ee8dcf074039c751b1fd7bb37
@@ -6,7 +6,7 @@ require 'active_support/inflector'
6
6
  require 'optparse'
7
7
  require 'psych'
8
8
  require 'yaml'
9
- require 'colorize'
9
+ require 'rainbow'
10
10
 
11
11
  module ERBLint
12
12
  class CLI
@@ -37,7 +37,9 @@ module ERBLint
37
37
 
38
38
  load_config
39
39
 
40
- if lint_files.empty?
40
+ if !@files.empty? && lint_files.empty?
41
+ success!("no files found...\n")
42
+ elsif lint_files.empty?
41
43
  success!("no files given...\n#{option_parser}")
42
44
  end
43
45
 
@@ -57,7 +59,7 @@ module ERBLint
57
59
  rescue => e
58
60
  puts "Exception occured when processing: #{relative_filename(filename)}"
59
61
  puts e.message
60
- puts e.backtrace.join("\n").red
62
+ puts Rainbow(e.backtrace.join("\n")).red
61
63
  puts
62
64
  end
63
65
  end
@@ -65,25 +67,27 @@ module ERBLint
65
67
  if @stats.corrected > 0
66
68
  corrected_found_diff = @stats.found - @stats.corrected
67
69
  if corrected_found_diff > 0
68
- warn "#{@stats.corrected} error(s) corrected and #{corrected_found_diff} error(s) remaining in ERB files".red
70
+ warn Rainbow(
71
+ "#{@stats.corrected} error(s) corrected and #{corrected_found_diff} error(s) remaining in ERB files"
72
+ ).red
69
73
  else
70
- puts "#{@stats.corrected} error(s) corrected in ERB files".green
74
+ puts Rainbow("#{@stats.corrected} error(s) corrected in ERB files").green
71
75
  end
72
76
  elsif @stats.found > 0
73
- warn "#{@stats.found} error(s) were found in ERB files".red
77
+ warn Rainbow("#{@stats.found} error(s) were found in ERB files").red
74
78
  else
75
- puts "No errors were found in ERB files".green
79
+ puts Rainbow("No errors were found in ERB files").green
76
80
  end
77
81
 
78
82
  @stats.found == 0
79
83
  rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, ExitWithFailure => e
80
- warn e.message.red
84
+ warn Rainbow(e.message).red
81
85
  false
82
86
  rescue ExitWithSuccess => e
83
87
  puts e.message
84
88
  true
85
89
  rescue => e
86
- warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}".red
90
+ warn Rainbow("#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}").red
87
91
  false
88
92
  end
89
93
 
@@ -120,7 +124,7 @@ module ERBLint
120
124
  @stats.found += runner.offenses.size
121
125
  runner.offenses.each do |offense|
122
126
  puts <<~EOF
123
- #{offense.message}#{' (not autocorrected)'.red if autocorrect?}
127
+ #{offense.message}#{Rainbow(' (not autocorrected)').red if autocorrect?}
124
128
  In file: #{relative_filename(filename)}:#{offense.line_range.begin}
125
129
 
126
130
  EOF
@@ -139,10 +143,10 @@ module ERBLint
139
143
 
140
144
  def load_config
141
145
  if File.exist?(config_filename)
142
- config = RunnerConfig.new(file_loader.yaml(config_filename))
146
+ config = RunnerConfig.new(file_loader.yaml(config_filename), file_loader)
143
147
  @config = RunnerConfig.default.merge(config)
144
148
  else
145
- warn "#{config_filename} not found: using default config".yellow
149
+ warn Rainbow("#{config_filename} not found: using default config").yellow
146
150
  @config = RunnerConfig.default
147
151
  end
148
152
  @config.merge!(runner_config_override)
@@ -164,6 +168,7 @@ module ERBLint
164
168
  Dir[pattern].select { |filename| !excluded?(filename) }
165
169
  else
166
170
  @files
171
+ .map { |f| Dir.exist?(f) ? Dir[File.join(f, DEFAULT_LINT_ALL_GLOB)] : f }
167
172
  .map { |f| f.include?('*') ? Dir[f] : f }
168
173
  .flatten
169
174
  .map { |f| File.expand_path(f, Dir.pwd) }
@@ -10,19 +10,14 @@ module ERBLint
10
10
  end
11
11
 
12
12
  def yaml(filename)
13
- YAML.safe_load(read_content(filename), [Regexp], [], false, filename) || {}
14
- rescue Psych::SyntaxError
15
- {}
13
+ YAML.safe_load(read_content(filename), [Regexp, Symbol], [], false, filename) || {}
16
14
  end
17
15
 
18
16
  private
19
17
 
20
- def join(filename)
21
- File.join(base_path, filename)
22
- end
23
-
24
18
  def read_content(filename)
25
- File.read(join(filename))
19
+ path = File.expand_path(filename, base_path)
20
+ File.read(path)
26
21
  end
27
22
  end
28
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
-
2
+ require "set"
3
3
  require 'better_html/tree/tag'
4
4
  require 'active_support/core_ext/string/inflections'
5
5
 
@@ -11,20 +11,25 @@ module ERBLint
11
11
 
12
12
  ForbiddenCorrector = Class.new(StandardError)
13
13
  MissingCorrector = Class.new(StandardError)
14
+ MissingI18nLoadPath = Class.new(StandardError)
14
15
 
15
16
  ALLOWED_CORRECTORS = %w(
16
17
  I18nCorrector
17
- RuboCop::I18nCorrector
18
+ RuboCop::Corrector::I18n::HardCodedString
18
19
  )
19
20
 
21
+ NON_TEXT_TAGS = Set.new(%w(script style xmp iframe noembed noframes listing))
22
+ BLACK_LISTED_TEXT = Set.new(%w(&nbsp; &ensp; &emsp; &thinsp;))
23
+
20
24
  class ConfigSchema < LinterConfig
21
25
  property :corrector, accepts: Hash, required: false, default: {}
26
+ property :i18n_load_path, accepts: String, required: false, default: ''
22
27
  end
23
28
  self.config_schema = ConfigSchema
24
29
 
25
30
  def run(processed_source)
26
31
  hardcoded_strings = processed_source.ast.descendants(:text).each_with_object([]) do |text_node, to_check|
27
- next if javascript?(processed_source, text_node)
32
+ next if non_text_tag?(processed_source, text_node)
28
33
 
29
34
  offended_strings = text_node.to_a.select { |node| relevant_node(node) }
30
35
  offended_strings.each do |offended_string|
@@ -58,11 +63,10 @@ module ERBLint
58
63
  string = offense.source_range.source
59
64
  return unless klass = load_corrector
60
65
  return unless string.strip.length > 1
61
-
62
- corrector = klass.new(processed_source.filename, offense.source_range)
66
+ corrector = klass.new(processed_source.filename, corrector_i18n_load_path, offense.source_range)
63
67
  node = RuboCop::AST::StrNode.new(:str, [string])
64
68
  corrector.autocorrect(node, tag_start: '<%= ', tag_end: ' %>')
65
- rescue MissingCorrector
69
+ rescue MissingCorrector, MissingI18nLoadPath
66
70
  nil
67
71
  end
68
72
 
@@ -70,7 +74,7 @@ module ERBLint
70
74
 
71
75
  def check_string?(str)
72
76
  string = str.gsub(/\s*/, '')
73
- string.length > 1 && !%w(&nbsp;).include?(string)
77
+ string.length > 1 && !BLACK_LISTED_TEXT.include?(string)
74
78
  end
75
79
 
76
80
  def load_corrector
@@ -81,7 +85,11 @@ module ERBLint
81
85
  corrector_name.safe_constantize
82
86
  end
83
87
 
84
- def javascript?(processed_source, text_node)
88
+ def corrector_i18n_load_path
89
+ @config['corrector'].fetch('i18n_load_path') { raise MissingI18nLoadPath }
90
+ end
91
+
92
+ def non_text_tag?(processed_source, text_node)
85
93
  ast = processed_source.parser.ast.to_a
86
94
  index = ast.find_index(text_node)
87
95
 
@@ -90,7 +98,7 @@ module ERBLint
90
98
  if previous_node.type == :tag
91
99
  tag = BetterHtml::Tree::Tag.from_node(previous_node)
92
100
 
93
- tag.name == "script" && !tag.closing?
101
+ NON_TEXT_TAGS.include?(tag.name) && !tag.closing?
94
102
  end
95
103
  end
96
104
 
@@ -23,11 +23,12 @@ module ERBLint
23
23
  next if indicator == '#'
24
24
  source = code_node.loc.source
25
25
 
26
- ruby_node = begin
27
- BetterHtml::TestHelper::RubyNode.parse(source)
28
- rescue ::Parser::SyntaxError
29
- nil
30
- end
26
+ ruby_node =
27
+ begin
28
+ BetterHtml::TestHelper::RubyNode.parse(source)
29
+ rescue ::Parser::SyntaxError
30
+ nil
31
+ end
31
32
  next unless ruby_node
32
33
  send_node = ruby_node.descendants(:send).first
33
34
  next unless send_node&.method_name?(:javascript_tag)
@@ -147,7 +147,7 @@ module ERBLint
147
147
  def base_configs(inherit_from)
148
148
  regex = URI::DEFAULT_PARSER.make_regexp(%w(http https))
149
149
  configs = Array(inherit_from).compact.map do |base_name|
150
- if base_name =~ /\A#{regex}\z/
150
+ if base_name.match?(/\A#{regex}\z/)
151
151
  RuboCop::ConfigLoader.load_file(RuboCop::RemoteConfig.new(base_name, Dir.pwd))
152
152
  else
153
153
  config_from_hash(@file_loader.yaml(base_name))
@@ -1,11 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'erb_lint/runner_config_resolver'
4
+
3
5
  module ERBLint
4
6
  class RunnerConfig
5
7
  class Error < StandardError; end
6
8
 
7
- def initialize(config = nil)
9
+ def initialize(config = nil, file_loader = nil)
8
10
  @config = (config || {}).dup.deep_stringify_keys
11
+
12
+ resolver.resolve_inheritance_from_gems(@config, @config.delete('inherit_gem'))
13
+ resolver.resolve_inheritance(@config, file_loader) if file_loader
14
+ @config.delete("inherit_from")
9
15
  end
10
16
 
11
17
  def to_hash
@@ -71,5 +77,9 @@ module ERBLint
71
77
  config_hash['exclude'].concat(global_exclude) if config_hash['exclude'].is_a?(Array)
72
78
  config_hash
73
79
  end
80
+
81
+ def resolver
82
+ @resolver ||= ERBLint::RunnerConfigResolver.new
83
+ end
74
84
  end
75
85
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2012-18 Bozhidar Batsov
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module ERBLint
25
+ class RunnerConfigResolver
26
+ def resolve_inheritance(hash, file_loader)
27
+ inherited_files = Array(hash['inherit_from'])
28
+ base_configs(file_loader, inherited_files).reverse_each do |base_config|
29
+ base_config.each do |k, v|
30
+ next unless v.is_a?(Hash)
31
+ v = v.deep_merge(hash[k]) if hash.key?(k)
32
+ hash[k] = v
33
+ end
34
+ end
35
+ end
36
+
37
+ def resolve_inheritance_from_gems(hash, gems)
38
+ (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
+
41
+ hash['inherit_from'] = Array(hash['inherit_from'])
42
+ Array(config_path).reverse_each do |path|
43
+ # Put gem configuration first so local configuration overrides it.
44
+ hash['inherit_from'].unshift gem_config_path(gem_name, path)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def gem_config_path(gem_name, relative_config_path)
52
+ spec = Gem::Specification.find_by_name(gem_name)
53
+ File.join(spec.gem_dir, relative_config_path)
54
+ rescue Gem::LoadError => e
55
+ raise Gem::LoadError, "Unable to find gem #{gem_name}; is the gem installed? #{e}"
56
+ end
57
+
58
+ def base_configs(file_loader, inherit_from)
59
+ configs = Array(inherit_from).compact.map do |f|
60
+ inherited_file = File.expand_path(f, file_loader.base_path)
61
+ file_loader.yaml(inherited_file)
62
+ end
63
+ configs.compact
64
+ end
65
+ end
66
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ERBLint
4
- VERSION = '0.0.26'
4
+ VERSION = '0.0.27'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.26
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Chan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-24 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: colorize
84
+ name: rainbow
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -159,6 +159,7 @@ files:
159
159
  - lib/erb_lint/processed_source.rb
160
160
  - lib/erb_lint/runner.rb
161
161
  - lib/erb_lint/runner_config.rb
162
+ - lib/erb_lint/runner_config_resolver.rb
162
163
  - lib/erb_lint/utils/block_map.rb
163
164
  - lib/erb_lint/utils/offset_corrector.rb
164
165
  - lib/erb_lint/utils/ruby_to_erb.rb
@@ -175,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
175
176
  requirements:
176
177
  - - ">="
177
178
  - !ruby/object:Gem::Version
178
- version: '0'
179
+ version: 2.3.0
179
180
  required_rubygems_version: !ruby/object:Gem::Requirement
180
181
  requirements:
181
182
  - - ">="