erb_lint 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: b98507494f8af2a33e0e6fcbf28c48a2d91c78f42273a3cc6c1206578f3a55b0
4
- data.tar.gz: c090a633a9d041a6d4d71bc292b1525b3a74dccd61311e0224bc1cbb0d8fea33
3
+ metadata.gz: 97583caf4e48c7c5b8a6d2e1bf4c17add7bac9693ef6032be0fb22f9d46f4fa4
4
+ data.tar.gz: 3d1dba73d37680a0339936a12a6c2a048b473a7c9077518f54edd1aa037c4ce5
5
5
  SHA512:
6
- metadata.gz: b2ac77d88b7be36010d69c072e3ee72c370edefa1fc3b68135593b5b494da8b0f9eac79e9b5408d10155a007d94948ae6a4b3b279de2541be66f0576abc964a8
7
- data.tar.gz: f0be6a75c5060b49a8b7c5e5a5d19adb3222fb580a11c2e2e488e5b83ada00db0c4daee25e99ba5538057c749f5937b80d25a96bda1ed967e6d1baeddf787965
6
+ metadata.gz: a043d8859ef059100dd809112f8768908f2ec8df59a52991c09ea995d2181aeabf06c105b3d12eef13ddbbed15d220edbdd88941fe6a55a23fd5c600aa55623d
7
+ data.tar.gz: 805102e771aac93e6a75017dde3f88deee018dceff3c8c57f2ddcdea88f97f241980efc17885999989733952e771e0538a75eaf67566499e61534c9e64476f75
data/lib/erb_lint/cli.rb CHANGED
@@ -50,6 +50,7 @@ module ERBLint
50
50
  @options[:fail_level] ||= severity_level_for_name(:refactor)
51
51
  @stats.files = lint_files.size
52
52
  @stats.linters = enabled_linter_classes.size
53
+ @stats.autocorrectable_linters = enabled_linter_classes.count(&:support_autocorrect?)
53
54
 
54
55
  reporter = Reporter.create_reporter(@options[:format], @stats, autocorrect?)
55
56
  reporter.preview
@@ -232,11 +233,7 @@ module ERBLint
232
233
 
233
234
  def enabled_linter_classes
234
235
  @enabled_linter_classes ||= ERBLint::LinterRegistry.linters
235
- .select { |klass| linter_can_run?(klass) && enabled_linter_names.include?(klass.simple_name.underscore) }
236
- end
237
-
238
- def linter_can_run?(klass)
239
- !autocorrect? || klass.support_autocorrect?
236
+ .select { |klass| enabled_linter_names.include?(klass.simple_name.underscore) }
240
237
  end
241
238
 
242
239
  def relative_filename(filename)
@@ -325,7 +322,7 @@ module ERBLint
325
322
 
326
323
  def format_options_help
327
324
  "Report offenses in the given format: "\
328
- "(#{Reporter.available_formats.join(", ")}) (default: multiline)"
325
+ "(#{Reporter.available_formats.join(", ")}) (default: multiline)"
329
326
  end
330
327
 
331
328
  def invalid_format_error_message(given_format)
@@ -29,6 +29,7 @@ module ERBLint
29
29
  if (extra_keys = given_keys - allowed_keys).any?
30
30
  raise Error, "Given key is not allowed: #{extra_keys.join(", ")}"
31
31
  end
32
+
32
33
  super(config)
33
34
  rescue SmartProperties::InitializationError => e
34
35
  raise Error, "The following properties are required to be set: #{e.properties}"
@@ -40,6 +41,7 @@ module ERBLint
40
41
  unless self.class.properties.key?(name)
41
42
  raise Error, "No such property: #{name}"
42
43
  end
44
+
43
45
  super
44
46
  end
45
47
 
@@ -31,7 +31,7 @@ module ERBLint
31
31
  add_offense(
32
32
  name_node.loc,
33
33
  "Avoid using inline `<script>` tags altogether. "\
34
- "Instead, move javascript code into a static file."
34
+ "Instead, move javascript code into a static file."
35
35
  )
36
36
  next
37
37
  end
@@ -50,8 +50,8 @@ module ERBLint
50
50
  add_offense(
51
51
  type_attribute.loc,
52
52
  "Avoid using #{type_attribute.value.inspect} as type for `<script>` tag. "\
53
- "Must be one of: #{@config.allowed_types.join(", ")}"\
54
- "#{" (or no type attribute)" if @config.allow_blank?}."
53
+ "Must be one of: #{@config.allowed_types.join(", ")}"\
54
+ "#{" (or no type attribute)" if @config.allow_blank?}."
55
55
  )
56
56
  end
57
57
  end
@@ -59,6 +59,7 @@ module ERBLint
59
59
 
60
60
  def autocorrect(_processed_source, offense)
61
61
  return unless offense.context
62
+
62
63
  lambda do |corrector|
63
64
  type_attribute, = *offense.context
64
65
  if type_attribute.nil?
@@ -59,6 +59,7 @@ module ERBLint
59
59
  tags(processed_source).each do |tag|
60
60
  class_value = tag.attributes["class"]&.value
61
61
  next unless class_value
62
+
62
63
  class_value.split(" ").each do |class_name|
63
64
  yielder.yield(class_name, tag.loc)
64
65
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "set"
3
4
  require "better_html/tree/tag"
4
5
  require "active_support/core_ext/string/inflections"
@@ -83,6 +84,7 @@ module ERBLint
83
84
  string = offense.source_range.source
84
85
  return unless (klass = load_corrector)
85
86
  return unless string.strip.length > 1
87
+
86
88
  node = ::RuboCop::AST::StrNode.new(:str, [string])
87
89
  corrector = klass.new(node, processed_source.filename, corrector_i18n_load_path, offense.source_range)
88
90
  corrector.autocorrect(tag_start: "<%= ", tag_end: " %>")
@@ -100,6 +102,7 @@ module ERBLint
100
102
  def load_corrector
101
103
  corrector_name = @config["corrector"].fetch("name") { raise MissingCorrector }
102
104
  raise ForbiddenCorrector unless ALLOWED_CORRECTORS.include?(corrector_name)
105
+
103
106
  require @config["corrector"].fetch("path") { raise MissingCorrector }
104
107
 
105
108
  corrector_name.safe_constantize
@@ -22,6 +22,7 @@ module ERBLint
22
22
  indicator_node, _, code_node, _ = *erb_node
23
23
  indicator = indicator_node&.loc&.source
24
24
  next if indicator == "#"
25
+
25
26
  source = code_node.loc.source
26
27
 
27
28
  ruby_node =
@@ -31,13 +32,14 @@ module ERBLint
31
32
  nil
32
33
  end
33
34
  next unless ruby_node
35
+
34
36
  send_node = ruby_node.descendants(:send).first
35
37
  next unless send_node&.method_name?(:javascript_tag)
36
38
 
37
39
  add_offense(
38
40
  erb_node.loc,
39
41
  "Avoid using 'javascript_tag do' as it confuses tests "\
40
- "that validate html, use inline <script> instead",
42
+ "that validate html, use inline <script> instead",
41
43
  [erb_node, send_node]
42
44
  )
43
45
  end
@@ -8,7 +8,7 @@ module ERBLint
8
8
 
9
9
  def run(processed_source)
10
10
  instance_variable_regex = /\s@\w+/
11
- return unless processed_source.filename.match?(/.*_.*.erb\z/) &&
11
+ return unless processed_source.filename.match?(%r{(\A|.*/)_[^/\s]*\.html\.erb\z}) &&
12
12
  processed_source.file_content.match?(instance_variable_regex)
13
13
 
14
14
  add_offense(
@@ -63,7 +63,7 @@ module ERBLint
63
63
  add_offense(
64
64
  tag_node.to_a[1].loc,
65
65
  "Input tag is missing an autocomplete attribute. If no "\
66
- "autocomplete behaviour is desired, use the value `off` or `nope`.",
66
+ "autocomplete behaviour is desired, use the value `off` or `nope`.",
67
67
  [autocomplete_attribute]
68
68
  )
69
69
  end
@@ -97,7 +97,7 @@ module ERBLint
97
97
  add_offense(
98
98
  erb_node.loc,
99
99
  "Input field helper is missing an autocomplete attribute. If no "\
100
- "autocomplete behaviour is desired, use the value `off` or `nope`.",
100
+ "autocomplete behaviour is desired, use the value `off` or `nope`.",
101
101
  [erb_node, send_node]
102
102
  )
103
103
  end
@@ -74,8 +74,8 @@ module ERBLint
74
74
 
75
75
  def tag_helper?(send_node)
76
76
  send_node&.method_name?(:javascript_tag) ||
77
- send_node&.method_name?(:javascript_include_tag) ||
78
- send_node&.method_name?(:javascript_pack_tag)
77
+ send_node&.method_name?(:javascript_include_tag) ||
78
+ send_node&.method_name?(:javascript_pack_tag)
79
79
  end
80
80
 
81
81
  def code_comment?(indicator_node)
@@ -37,6 +37,7 @@ module ERBLint
37
37
  if ::RuboCop::Version::STRING.to_f >= 0.87
38
38
  def autocorrect(_processed_source, offense)
39
39
  return unless offense.context
40
+
40
41
  rubocop_correction = offense.context[:rubocop_correction]
41
42
  return unless rubocop_correction
42
43
 
@@ -150,6 +151,7 @@ module ERBLint
150
151
  @rubocop_config,
151
152
  extra_details: true,
152
153
  display_cop_names: true,
154
+ autocorrect: true,
153
155
  auto_correct: true,
154
156
  stdin: "",
155
157
  )
@@ -37,6 +37,7 @@ module ERBLint
37
37
  end
38
38
 
39
39
  next unless @config.enforced_style == :never && tag.self_closing?
40
+
40
41
  end_solidus = tag_node.children.last
41
42
  add_offense(
42
43
  end_solidus.loc,
@@ -16,6 +16,7 @@ module ERBLint
16
16
  indicator_node, ltrim, code_node, rtrim = *erb_node
17
17
  indicator = indicator_node&.loc&.source
18
18
  next if indicator == "#" || indicator == "%"
19
+
19
20
  code = code_node.children.first
20
21
 
21
22
  start_spaces = code.match(START_SPACES)&.captures&.first || ""
@@ -23,7 +24,7 @@ module ERBLint
23
24
  add_offense(
24
25
  code_node.loc.resize(start_spaces.size),
25
26
  "Use 1 space after `<%#{indicator}#{ltrim&.loc&.source}` "\
26
- "instead of #{start_spaces.size} space#{"s" if start_spaces.size > 1}.",
27
+ "instead of #{start_spaces.size} space#{"s" if start_spaces.size > 1}.",
27
28
  " "
28
29
  )
29
30
  elsif start_spaces.count("\n") > 1
@@ -31,7 +32,7 @@ module ERBLint
31
32
  add_offense(
32
33
  code_node.loc.resize(start_spaces.size),
33
34
  "Use 1 newline after `<%#{indicator&.loc&.source}#{ltrim&.loc&.source}` "\
34
- "instead of #{start_spaces.count("\n")}.",
35
+ "instead of #{start_spaces.count("\n")}.",
35
36
  "#{lines.first}\n#{lines.last}"
36
37
  )
37
38
  end
@@ -41,7 +42,7 @@ module ERBLint
41
42
  add_offense(
42
43
  code_node.loc.end.adjust(begin_pos: -end_spaces.size),
43
44
  "Use 1 space before `#{rtrim&.loc&.source}%>` "\
44
- "instead of #{end_spaces.size} space#{"s" if start_spaces.size > 1}.",
45
+ "instead of #{end_spaces.size} space#{"s" if start_spaces.size > 1}.",
45
46
  " "
46
47
  )
47
48
  elsif end_spaces.count("\n") > 1
@@ -49,7 +50,7 @@ module ERBLint
49
50
  add_offense(
50
51
  code_node.loc.end.adjust(begin_pos: -end_spaces.size),
51
52
  "Use 1 newline before `#{rtrim&.loc&.source}%>` "\
52
- "instead of #{end_spaces.count("\n")}.",
53
+ "instead of #{end_spaces.count("\n")}.",
53
54
  "#{lines.first}\n#{lines.last}"
54
55
  )
55
56
  end
@@ -98,6 +98,7 @@ module ERBLint
98
98
  no_space(processed_source, equal.loc.end_pos...value.loc.begin_pos) if equal && value
99
99
 
100
100
  next if index >= attributes.children.size - 1
101
+
101
102
  next_attribute = attributes.children[index + 1]
102
103
 
103
104
  single_space_or_newline(
@@ -9,6 +9,7 @@ module ERBLint
9
9
  unless source_range.is_a?(Parser::Source::Range)
10
10
  raise ArgumentError, "expected Parser::Source::Range for arg 2"
11
11
  end
12
+
12
13
  @linter = linter
13
14
  @source_range = source_range
14
15
  @message = message
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "active_support/core_ext/class"
3
4
  require "active_support/core_ext/module/delegation"
4
5
 
@@ -34,6 +35,7 @@ module ERBLint
34
35
  private
35
36
 
36
37
  attr_reader :stats, :autocorrect
38
+
37
39
  delegate :processed_files, to: :stats
38
40
  end
39
41
  end
@@ -4,8 +4,7 @@ module ERBLint
4
4
  module Reporters
5
5
  class CompactReporter < Reporter
6
6
  def preview
7
- puts "Linting #{stats.files} files with "\
8
- "#{stats.linters} #{"autocorrectable " if autocorrect}linters..."
7
+ puts "#{linting} #{stats.files} files with #{linters}..."
9
8
  end
10
9
 
11
10
  def show
@@ -21,6 +20,14 @@ module ERBLint
21
20
 
22
21
  private
23
22
 
23
+ def linting
24
+ "Linting" + (autocorrect ? " and autocorrecting" : "")
25
+ end
26
+
27
+ def linters
28
+ "#{stats.linters} linters" + (autocorrect ? " (#{stats.autocorrectable_linters} autocorrectable)" : "")
29
+ end
30
+
24
31
  def format_offense(filename, offense)
25
32
  [
26
33
  "#{filename}:",
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "compact_reporter"
3
4
 
4
5
  module ERBLint
@@ -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)
@@ -28,6 +28,7 @@ module ERBLint
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
 
@@ -28,6 +28,7 @@ module ERBLint
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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ERBLint
3
4
  class Stats
4
5
  attr_accessor :ignored,
@@ -6,6 +7,7 @@ module ERBLint
6
7
  :corrected,
7
8
  :exceptions,
8
9
  :linters,
10
+ :autocorrectable_linters,
9
11
  :files,
10
12
  :processed_files
11
13
 
@@ -15,6 +17,7 @@ module ERBLint
15
17
  corrected: 0,
16
18
  exceptions: 0,
17
19
  linters: 0,
20
+ autocorrectable_linters: 0,
18
21
  files: 0,
19
22
  processed_files: {}
20
23
  )
@@ -23,6 +26,7 @@ module ERBLint
23
26
  @corrected = corrected
24
27
  @exceptions = exceptions
25
28
  @linters = linters
29
+ @autocorrectable_linters = autocorrectable_linters
26
30
  @files = files
27
31
  @processed_files = processed_files
28
32
  end
@@ -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
@@ -64,7 +64,7 @@ module ERBLint
64
64
  else
65
65
  raise TypeError,
66
66
  "Expected a Parser::Source::Range, Comment or " \
67
- "Rubocop::AST::Node, got #{node_or_range.class}"
67
+ "Rubocop::AST::Node, got #{node_or_range.class}"
68
68
  end
69
69
  end
70
70
  end
@@ -17,6 +17,7 @@ module ERBLint
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ERBLint
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
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.1.1
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-07-28 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
  - - ">="
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
224
  requirements: []
225
- rubygems_version: 3.2.20
225
+ rubygems_version: 3.3.3
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: ERB lint tool