erb_lint 0.0.20 → 0.0.21

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: ab337195cd313e04726f3c3ca31cc6fd77e661ce
4
- data.tar.gz: 52de035725a4a39e533552a056f17e2f6d87e980
3
+ metadata.gz: 7c5c19ac1eafb41b42b89239504fccaf8f1f52a0
4
+ data.tar.gz: 3afb969caac48e9c7960cddbec2f55630322580d
5
5
  SHA512:
6
- metadata.gz: 35dc5da94ef03e07d04a933ac5ded2951fde3437c20410d333e28b5c0fde51a6fe7df2b743f80cc0e13840c73b18b2309fdcd3a15c8ae9ecb0de30bcaad5270d
7
- data.tar.gz: f1a74b863008b9ed36f56d7ecbf12533b9ee8b3bb89aca0da8c118306013ea0e54c9d1ea84def71b49b912d41c3347163fa284029cab98c36a3ec6132ccc35c9
6
+ metadata.gz: 3716dfb6a37ffe9f3c4b27804cccc597f3da89a46c2075c0bdbb54c9b7840c952f391ff8d8a78bcc2bd07ee56d2baa5972084455671c79cd87a7f588080f1372
7
+ data.tar.gz: 7582a2c11435135fa681c27b5e7bcc904a3a1e67d01c8dad930e1d89b8d8d7e9959e4b632701971f14eac3374dd2789f1b68f8c9ebaf05bd7c988f536a57dc10
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'better_html/tree/tag'
4
+ require 'active_support/core_ext/string/inflections'
4
5
 
5
6
  module ERBLint
6
7
  module Linters
@@ -8,25 +9,74 @@ module ERBLint
8
9
  class HardCodedString < Linter
9
10
  include LinterRegistry
10
11
 
12
+ ForbiddenCorrector = Class.new(StandardError)
13
+ MissingCorrector = Class.new(StandardError)
14
+
15
+ ALLOWED_CORRECTORS = %w(
16
+ I18nCorrector
17
+ RuboCop::I18nCorrector
18
+ )
19
+
20
+ class ConfigSchema < LinterConfig
21
+ property :corrector, accepts: Hash, required: false, default: {}
22
+ end
23
+ self.config_schema = ConfigSchema
24
+
11
25
  def offenses(processed_source)
12
26
  hardcoded_strings = processed_source.ast.descendants(:text).each_with_object([]) do |text_node, to_check|
13
27
  next if javascript?(processed_source, text_node)
14
28
 
15
- offended_str = text_node.to_a.find { |node| relevant_node(node) }
16
- to_check << [text_node, offended_str] if offended_str
29
+ offended_strings = text_node.to_a.select { |node| relevant_node(node) }
30
+ offended_strings.each do |offended_string|
31
+ offended_string.split("\n").each do |str|
32
+ to_check << [text_node, str] if str.length > 1
33
+ end
34
+ end
17
35
  end
18
36
 
19
37
  hardcoded_strings.compact.map do |text_node, offended_str|
38
+ range_begin, range_stop = find_range(text_node, offended_str)
39
+ source_range = processed_source.to_source_range(range_begin, range_stop)
40
+
20
41
  Offense.new(
21
42
  self,
22
- processed_source.to_source_range(text_node.loc.start, text_node.loc.stop),
23
- message(offended_str)
43
+ source_range,
44
+ message(source_range.source)
24
45
  )
25
46
  end
26
47
  end
27
48
 
49
+ def find_range(node, str)
50
+ match = node.loc.source.match(Regexp.new(Regexp.quote(str.strip)))
51
+ return unless match
52
+
53
+ range_begin = match.begin(0) + node.loc.start
54
+ range_end = match.end(0) + node.loc.start - 1
55
+ [range_begin, range_end]
56
+ end
57
+
58
+ def autocorrect(processed_source, offense)
59
+ string = offense.source_range.source
60
+ return unless klass = load_corrector
61
+ return unless string.strip.length > 1
62
+
63
+ corrector = klass.new(processed_source.filename, offense.source_range)
64
+ node = RuboCop::AST::StrNode.new(:str, [string])
65
+ corrector.autocorrect(node, tag_start: '<%= ', tag_end: ' %>')
66
+ rescue MissingCorrector
67
+ nil
68
+ end
69
+
28
70
  private
29
71
 
72
+ def load_corrector
73
+ corrector_name = @config['corrector'].fetch('name') { raise MissingCorrector }
74
+ raise ForbiddenCorrector unless ALLOWED_CORRECTORS.include?(corrector_name)
75
+ require @config['corrector'].fetch('path') { raise MissingCorrector }
76
+
77
+ corrector_name.safe_constantize
78
+ end
79
+
30
80
  def javascript?(processed_source, text_node)
31
81
  ast = processed_source.parser.ast.to_a
32
82
  index = ast.find_index(text_node)
@@ -51,11 +101,7 @@ module ERBLint
51
101
  def message(string)
52
102
  stripped_string = string.strip
53
103
 
54
- if stripped_string.length > 1
55
- "String not translated: #{stripped_string}"
56
- else
57
- "Consider using Rails helpers to move out the single character `#{stripped_string}` from the html."
58
- end
104
+ "String not translated: #{stripped_string}"
59
105
  end
60
106
  end
61
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ERBLint
4
- VERSION = '0.0.20'
4
+ VERSION = '0.0.21'
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.20
4
+ version: 0.0.21
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-01-29 00:00:00.000000000 Z
11
+ date: 2018-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html