i18n-tasks 1.0.2 → 1.0.5

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
  SHA256:
3
- metadata.gz: 66781586bda6c38de2ca767c265c7e1fbaf6e11184997f00a77c215e2270a7d7
4
- data.tar.gz: 3a94d73244c34887758d208cdc807a930467ac6cde95545dea6acdbed5563c36
3
+ metadata.gz: 4d600c7425116c27f5cadffdc23d6e29de6137f90cea588e1a046ca1ec5970f5
4
+ data.tar.gz: 91ea505ba06486d962e7be61eb52ca6af18bc8d481fdeb6a193f715b3c2f55a2
5
5
  SHA512:
6
- metadata.gz: b51f3bf32fc208a9845b79f9a074ed2bcd2f0ff648309b93dbf7431df7154103a63c298bdb197cfae611012423d315df1552ad600a20135dfd76592df5d853d8
7
- data.tar.gz: 4cc284d8c283c4159333310f4deb2e78ec064f60c024ccdadd4007d883955ca08d1e765b69529744dff2f953973d3cab6ff46138919c5cc41c3c4f986ea2a88a
6
+ metadata.gz: 0e74a51969f4a82f8fa479dc31cb9236cc9de5d6255fe99d6de9eb392fb85e8771f4b21cbaed35f8c114258ada6c88086fb6fae0f1e0d97d8eb1f2d317c4f834
7
+ data.tar.gz: 4a23c041ddef63f1b798f1455af637f77f369dcfb64e93f72c9592caf2105d2e9b15531679b30d7404746674596ee70078bba3525cbc2eacdce00d9d0ebc7054
data/README.md CHANGED
@@ -24,7 +24,7 @@ i18n-tasks can be used with any project using the ruby [i18n gem][i18n-gem] (def
24
24
  Add i18n-tasks to the Gemfile:
25
25
 
26
26
  ```ruby
27
- gem 'i18n-tasks', '~> 1.0.2'
27
+ gem 'i18n-tasks', '~> 1.0.5'
28
28
  ```
29
29
 
30
30
  Copy the default [configuration file](#configuration):
@@ -223,7 +223,7 @@ See the full list of tasks with `i18n-tasks --help`.
223
223
 
224
224
  ### Features and limitations
225
225
 
226
- `i18n-tasks` uses an AST scanner for `.rb` files, and a regexp-based scanner for other files, such as `.haml`.
226
+ `i18n-tasks` uses an AST scanner for `.rb` and `.html.erb` files, and a regexp-based scanner for other files, such as `.haml`.
227
227
 
228
228
  #### Relative keys
229
229
 
@@ -2,22 +2,26 @@
2
2
 
3
3
  module I18n::Tasks
4
4
  module Interpolations
5
- VARIABLE_REGEX = /%{[^}]+}/.freeze
5
+ class << self
6
+ attr_accessor :variable_regex
7
+ end
8
+ @variable_regex = /%{[^}]+}/.freeze
6
9
 
7
10
  def inconsistent_interpolations(locales: nil, base_locale: nil) # rubocop:disable Metrics/AbcSize
8
11
  locales ||= self.locales
9
12
  base_locale ||= self.base_locale
10
13
  result = empty_forest
14
+ variable_regex = I18n::Tasks::Interpolations.variable_regex
11
15
 
12
16
  data[base_locale].key_values.each do |key, value|
13
17
  next if !value.is_a?(String) || ignore_key?(key, :inconsistent_interpolations)
14
18
 
15
- base_vars = Set.new(value.scan(VARIABLE_REGEX))
19
+ base_vars = Set.new(value.scan(variable_regex))
16
20
  (locales - [base_locale]).each do |current_locale|
17
21
  node = data[current_locale].first.children[key]
18
22
  next unless node&.value.is_a?(String)
19
23
 
20
- if base_vars != Set.new(node.value.scan(VARIABLE_REGEX))
24
+ if base_vars != Set.new(node.value.scan(variable_regex))
21
25
  result.merge!(node.walk_to_root.reduce(nil) { |c, p| [p.derive(children: c)] })
22
26
  end
23
27
  end
@@ -35,7 +35,10 @@ module I18n::Tasks::Scanners
35
35
  node
36
36
  end
37
37
 
38
+ # @param node [::Parser::AST::Node]
39
+ # @return [::Parser::AST::Node]
38
40
  def handler_missing(node)
41
+ node = transform_misparsed_comment(node)
39
42
  node.updated(
40
43
  nil,
41
44
  node.children.map { |child| node?(child) ? process(child) : child }
@@ -44,6 +47,42 @@ module I18n::Tasks::Scanners
44
47
 
45
48
  private
46
49
 
50
+ # Works around incorrect handling of comments of the form:
51
+ # <%# ... #>
52
+ # (no space between % and #)
53
+ #
54
+ # With a space the AST is:
55
+ #
56
+ # s(:erb, nil, nil,
57
+ # s(:code, " # this should not fail: ' "), nil)
58
+ #
59
+ # Without a space the AST is:
60
+ #
61
+ # s(:erb,
62
+ # s(:indicator, "#"), nil,
63
+ # s(:code, " this should not fail: ' "), nil)
64
+ # @param node [::Parser::AST::Node]
65
+ # @return [::Parser::AST::Node]
66
+ def transform_misparsed_comment(node)
67
+ return node unless node.type == :erb && node.children.size == 4 &&
68
+ node.children[0]&.type == :indicator && node.children[0].children[0] == "#" &&
69
+ node.children[1].nil? &&
70
+ node.children[2]&.type == :code &&
71
+ node.children[3].nil?
72
+ code_node = node.children[2]
73
+
74
+ # Prepend # to each line to make it a valid Ruby comment.
75
+ code = code_node.children[0].split("\n").map do |line|
76
+ next line if line =~ /^\s*#/
77
+ "##{line}"
78
+ end.join("\n")
79
+
80
+ node.updated(
81
+ nil,
82
+ [nil, nil, code_node.updated(nil, [code]), nil]
83
+ )
84
+ end
85
+
47
86
  def node?(node)
48
87
  node.is_a?(::Parser::AST::Node)
49
88
  end
@@ -52,6 +52,8 @@ module I18n::Tasks::Scanners
52
52
  # @param local_location {Parser::Source::Map} Local location in the parsed string
53
53
  # @return {Parser::Source::Map}
54
54
  def updated_location(global_location, local_location)
55
+ return global_location if local_location.expression.nil?
56
+
55
57
  range = ::Parser::Source::Range.new(
56
58
  global_location.expression.source_buffer,
57
59
  global_location.expression.to_range.begin + local_location.expression.to_range.begin,
@@ -48,7 +48,7 @@ module I18n::Tasks
48
48
  # rubocop:enable Metrics/ParameterLists
49
49
 
50
50
  def inspect
51
- "Occurrence(path: #{@path}, line_num: #{@line_num}, line_pos: #{@line_pos}, pos: #{@pos}, raw_key: #{@raw_key}, default_arg: #{@default_arg}, line: #{@line})"
51
+ "Occurrence(#{@path}:#{@line_num}, line_pos: #{@line_pos}, pos: #{@pos}, raw_key: #{@raw_key}, default_arg: #{@default_arg}, line: #{@line})"
52
52
  end
53
53
 
54
54
  def ==(other)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module Tasks
5
- VERSION = '1.0.2'
5
+ VERSION = '1.0.5'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-24 00:00:00.000000000 Z
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport