i18n-tasks 0.9.31 → 0.9.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -5
- data/i18n-tasks.gemspec +4 -4
- data/lib/i18n/tasks/cli.rb +12 -14
- data/lib/i18n/tasks/command/commands/data.rb +8 -6
- data/lib/i18n/tasks/command/commands/eq_base.rb +2 -2
- data/lib/i18n/tasks/command/commands/health.rb +3 -2
- data/lib/i18n/tasks/command/commands/interpolations.rb +1 -1
- data/lib/i18n/tasks/command/commands/meta.rb +1 -1
- data/lib/i18n/tasks/command/commands/missing.rb +7 -6
- data/lib/i18n/tasks/command/commands/tree.rb +8 -6
- data/lib/i18n/tasks/command/commands/usages.rb +5 -4
- data/lib/i18n/tasks/command/dsl.rb +4 -4
- data/lib/i18n/tasks/command/option_parsers/enum.rb +2 -0
- data/lib/i18n/tasks/command/option_parsers/locale.rb +2 -1
- data/lib/i18n/tasks/command/options/data.rb +1 -0
- data/lib/i18n/tasks/command/options/locales.rb +5 -5
- data/lib/i18n/tasks/concurrent/cached_value.rb +2 -0
- data/lib/i18n/tasks/configuration.rb +4 -4
- data/lib/i18n/tasks/console_context.rb +1 -1
- data/lib/i18n/tasks/data/file_formats.rb +2 -0
- data/lib/i18n/tasks/data/file_system_base.rb +7 -6
- data/lib/i18n/tasks/data/router/conservative_router.rb +2 -1
- data/lib/i18n/tasks/data/router/pattern_router.rb +2 -0
- data/lib/i18n/tasks/data/tree/node.rb +5 -2
- data/lib/i18n/tasks/data/tree/nodes.rb +1 -0
- data/lib/i18n/tasks/data/tree/siblings.rb +9 -2
- data/lib/i18n/tasks/data/tree/traversal.rb +11 -2
- data/lib/i18n/tasks/html_keys.rb +2 -4
- data/lib/i18n/tasks/ignore_keys.rb +4 -3
- data/lib/i18n/tasks/interpolations.rb +4 -2
- data/lib/i18n/tasks/key_pattern_matching.rb +3 -2
- data/lib/i18n/tasks/missing_keys.rb +33 -7
- data/lib/i18n/tasks/plural_keys.rb +6 -1
- data/lib/i18n/tasks/references.rb +2 -0
- data/lib/i18n/tasks/reports/base.rb +3 -2
- data/lib/i18n/tasks/reports/terminal.rb +6 -4
- data/lib/i18n/tasks/scanners/file_scanner.rb +4 -3
- data/lib/i18n/tasks/scanners/files/caching_file_finder.rb +0 -3
- data/lib/i18n/tasks/scanners/files/file_finder.rb +3 -2
- data/lib/i18n/tasks/scanners/occurrence_from_position.rb +3 -3
- data/lib/i18n/tasks/scanners/pattern_scanner.rb +7 -4
- data/lib/i18n/tasks/scanners/pattern_with_scope_scanner.rb +4 -2
- data/lib/i18n/tasks/scanners/relative_keys.rb +1 -0
- data/lib/i18n/tasks/scanners/ruby_ast_scanner.rb +16 -13
- data/lib/i18n/tasks/scanners/ruby_key_literals.rb +3 -3
- data/lib/i18n/tasks/scanners/scanner_multiplexer.rb +2 -0
- data/lib/i18n/tasks/split_key.rb +2 -0
- data/lib/i18n/tasks/string_interpolation.rb +1 -0
- data/lib/i18n/tasks/translation.rb +3 -3
- data/lib/i18n/tasks/translators/base_translator.rb +3 -1
- data/lib/i18n/tasks/translators/deepl_translator.rb +9 -2
- data/lib/i18n/tasks/translators/google_translator.rb +2 -0
- data/lib/i18n/tasks/translators/yandex_translator.rb +2 -0
- data/lib/i18n/tasks/used_keys.rb +15 -11
- data/lib/i18n/tasks/version.rb +1 -1
- data/templates/config/i18n-tasks.yml +1 -1
- metadata +10 -7
@@ -60,11 +60,13 @@ module I18n::Tasks
|
|
60
60
|
locales.each_with_object(empty_forest) do |locale, forest|
|
61
61
|
required_keys = required_plural_keys_for_locale(locale)
|
62
62
|
next if required_keys.empty?
|
63
|
+
|
63
64
|
tree = empty_forest
|
64
65
|
plural_nodes data[locale] do |node|
|
65
66
|
children = node.children
|
66
67
|
present_keys = Set.new(children.map { |c| c.key.to_sym })
|
67
68
|
next if present_keys.superset?(required_keys)
|
69
|
+
|
68
70
|
tree[node.full_key] = node.derive(
|
69
71
|
value: children.to_hash,
|
70
72
|
children: nil,
|
@@ -79,12 +81,8 @@ module I18n::Tasks
|
|
79
81
|
def required_plural_keys_for_locale(locale)
|
80
82
|
@plural_keys_for_locale ||= {}
|
81
83
|
return @plural_keys_for_locale[locale] if @plural_keys_for_locale.key?(locale)
|
82
|
-
|
83
|
-
|
84
|
-
Set.new(load_rails_i18n_pluralization!(locale)[locale.to_sym][:i18n][:plural][:keys])
|
85
|
-
rescue SystemCallError, IOError
|
86
|
-
Set.new
|
87
|
-
end
|
84
|
+
|
85
|
+
@plural_keys_for_locale[locale] = plural_keys_for_locale(locale)
|
88
86
|
end
|
89
87
|
|
90
88
|
# Loads rails-i18n pluralization config for the given locale.
|
@@ -123,7 +121,7 @@ module I18n::Tasks
|
|
123
121
|
end
|
124
122
|
|
125
123
|
def locale_key_missing?(locale, key)
|
126
|
-
!key_value?(key, locale) && !external_key?(key) && !ignore_key?(key, :missing)
|
124
|
+
!key_value?(key, locale) && !external_key?(key, locale) && !ignore_key?(key, :missing)
|
127
125
|
end
|
128
126
|
|
129
127
|
# @param [::I18n::Tasks::Data::Tree::Siblings] forest
|
@@ -136,6 +134,7 @@ module I18n::Tasks
|
|
136
134
|
locale = root.key
|
137
135
|
root.keys do |key, node|
|
138
136
|
next unless yield node
|
137
|
+
|
139
138
|
if locales_and_node_by_key.key?(key)
|
140
139
|
locales_and_node_by_key[key][0] << locale
|
141
140
|
else
|
@@ -154,5 +153,32 @@ module I18n::Tasks
|
|
154
153
|
end
|
155
154
|
forest
|
156
155
|
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def plural_keys_for_locale(locale)
|
160
|
+
configuration = load_rails_i18n_pluralization!(locale)
|
161
|
+
if configuration[locale.to_sym].nil?
|
162
|
+
alternate_locale = alternate_locale_from(locale)
|
163
|
+
return Set.new if configuration[alternate_locale.to_sym].nil?
|
164
|
+
|
165
|
+
return set_from_rails_i18n_pluralization(configuration, alternate_locale)
|
166
|
+
end
|
167
|
+
set_from_rails_i18n_pluralization(configuration, locale)
|
168
|
+
rescue SystemCallError, IOError
|
169
|
+
Set.new
|
170
|
+
end
|
171
|
+
|
172
|
+
def alternate_locale_from(locale)
|
173
|
+
re = /(\w{2})-*(\w{2,3})*/
|
174
|
+
match = locale.match(re)
|
175
|
+
language_code = match[1]
|
176
|
+
country_code = match[2]
|
177
|
+
"#{language_code}-#{country_code.upcase}"
|
178
|
+
end
|
179
|
+
|
180
|
+
def set_from_rails_i18n_pluralization(configuration, locale)
|
181
|
+
Set.new(configuration[locale.to_sym][:i18n][:plural][:keys])
|
182
|
+
end
|
157
183
|
end
|
158
184
|
end
|
@@ -5,12 +5,13 @@ module I18n::Tasks::PluralKeys
|
|
5
5
|
# Ref: http://cldr.unicode.org/index/cldr-spec/plural-rules
|
6
6
|
CLDR_CATEGORY_KEYS = %w[zero one two few many other].freeze
|
7
7
|
PLURAL_KEY_SUFFIXES = Set.new CLDR_CATEGORY_KEYS
|
8
|
-
PLURAL_KEY_RE = /\.(?:#{CLDR_CATEGORY_KEYS * '|'})
|
8
|
+
PLURAL_KEY_RE = /\.(?:#{CLDR_CATEGORY_KEYS * '|'})$/.freeze
|
9
9
|
|
10
10
|
def collapse_plural_nodes!(tree)
|
11
11
|
tree.leaves.map(&:parent).compact.uniq.each do |node|
|
12
12
|
children = node.children
|
13
13
|
next unless plural_forms?(children)
|
14
|
+
|
14
15
|
node.value = children.to_hash
|
15
16
|
node.children = nil
|
16
17
|
node.data.merge! children.first.data
|
@@ -23,6 +24,7 @@ module I18n::Tasks::PluralKeys
|
|
23
24
|
# @return [String] the base form if the key is a specific plural form (e.g. apple for apple.many), the key otherwise.
|
24
25
|
def depluralize_key(key, locale = base_locale)
|
25
26
|
return key if key !~ PLURAL_KEY_RE
|
27
|
+
|
26
28
|
key_name = last_key_part(key)
|
27
29
|
parent_key = key[0..- (key_name.length + 2)]
|
28
30
|
nodes = tree("#{locale}.#{parent_key}").presence || (locale != base_locale && tree("#{base_locale}.#{parent_key}"))
|
@@ -37,10 +39,12 @@ module I18n::Tasks::PluralKeys
|
|
37
39
|
# @yieldparam node [::I18n::Tasks::Data::Tree::Node] plural node
|
38
40
|
def plural_nodes(tree)
|
39
41
|
return to_enum(:plural_nodes, tree) unless block_given?
|
42
|
+
|
40
43
|
visited = Set.new
|
41
44
|
tree.leaves do |node|
|
42
45
|
parent = node.parent
|
43
46
|
next if !parent || visited.include?(parent)
|
47
|
+
|
44
48
|
yield parent if plural_forms?(parent.children)
|
45
49
|
visited.add(parent)
|
46
50
|
end
|
@@ -49,6 +53,7 @@ module I18n::Tasks::PluralKeys
|
|
49
53
|
|
50
54
|
def plural_forms?(s)
|
51
55
|
return false if non_plural_other?(s)
|
56
|
+
|
52
57
|
s.present? && s.all? { |node| node.leaf? && plural_suffix?(node.key) }
|
53
58
|
end
|
54
59
|
|
@@ -10,12 +10,14 @@ module I18n::Tasks
|
|
10
10
|
data_refs = merge_reference_trees(data_forest.select_keys { |_, node| node.reference? }))
|
11
11
|
fail ArgumentError, 'usages must be a Data::Tree::Instance' unless usages.is_a?(Data::Tree::Siblings)
|
12
12
|
fail ArgumentError, 'all_references must be a Data::Tree::Instance' unless data_refs.is_a?(Data::Tree::Siblings)
|
13
|
+
|
13
14
|
raw_refs = empty_forest
|
14
15
|
resolved_refs = empty_forest
|
15
16
|
refs = empty_forest
|
16
17
|
data_refs.key_to_node.each do |ref_key_part, ref_node|
|
17
18
|
usages.each do |usage_node|
|
18
19
|
next unless usage_node.key == ref_key_part
|
20
|
+
|
19
21
|
if ref_node.leaf?
|
20
22
|
process_leaf!(ref_node, usage_node, raw_refs, resolved_refs, refs)
|
21
23
|
else
|
@@ -9,6 +9,7 @@ module I18n::Tasks::Reports
|
|
9
9
|
end
|
10
10
|
|
11
11
|
attr_reader :task
|
12
|
+
|
12
13
|
delegate :base_locale, :locales, to: :task
|
13
14
|
|
14
15
|
protected
|
@@ -58,6 +59,7 @@ module I18n::Tasks::Reports
|
|
58
59
|
|
59
60
|
def format_locale(locale)
|
60
61
|
return '' unless locale
|
62
|
+
|
61
63
|
if locale.split('+') == task.locales.sort
|
62
64
|
'all'
|
63
65
|
else
|
@@ -67,8 +69,7 @@ module I18n::Tasks::Reports
|
|
67
69
|
|
68
70
|
def collapse_missing_tree!(forest)
|
69
71
|
forest = task.collapse_plural_nodes!(forest)
|
70
|
-
|
71
|
-
forest
|
72
|
+
task.collapse_same_key_in_locales!(forest) { |node| node.data[:type] == :missing_used }
|
72
73
|
end
|
73
74
|
end
|
74
75
|
end
|
@@ -106,9 +106,10 @@ module I18n
|
|
106
106
|
private
|
107
107
|
|
108
108
|
def missing_key_info(leaf)
|
109
|
-
|
109
|
+
case leaf[:type]
|
110
|
+
when :missing_used
|
110
111
|
first_occurrence leaf
|
111
|
-
|
112
|
+
when :missing_plural
|
112
113
|
leaf[:data][:missing_keys].join(', ')
|
113
114
|
else
|
114
115
|
"#{Rainbow(leaf[:data][:missing_diff_locale]).cyan} "\
|
@@ -134,6 +135,7 @@ module I18n
|
|
134
135
|
|
135
136
|
def format_reference_desc(node_data)
|
136
137
|
return nil unless node_data
|
138
|
+
|
137
139
|
case node_data[:ref_type]
|
138
140
|
when :reference_usage
|
139
141
|
Rainbow('(ref)').yellow.bright
|
@@ -159,9 +161,9 @@ module I18n
|
|
159
161
|
print_table headings: [Rainbow(I18n.t('i18n_tasks.common.locale')).cyan.bright,
|
160
162
|
Rainbow(I18n.t('i18n_tasks.common.key')).cyan.bright,
|
161
163
|
I18n.t('i18n_tasks.common.value')] do |t|
|
162
|
-
t.rows = locale_key_value_datas.map
|
164
|
+
t.rows = locale_key_value_datas.map do |(locale, k, v, data)|
|
163
165
|
[{ value: Rainbow(locale).cyan, alignment: :center }, format_key(k, data), format_value(v)]
|
164
|
-
|
166
|
+
end
|
165
167
|
end
|
166
168
|
else
|
167
169
|
puts 'ø'
|
@@ -11,10 +11,11 @@ module I18n::Tasks::Scanners
|
|
11
11
|
attr_reader :config
|
12
12
|
|
13
13
|
def initialize(
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
config: {},
|
15
|
+
file_finder_provider: Files::CachingFileFinderProvider.new,
|
16
|
+
file_reader: Files::CachingFileReader.new
|
17
17
|
)
|
18
|
+
super()
|
18
19
|
@config = config
|
19
20
|
@file_reader = file_reader
|
20
21
|
@file_finder = file_finder_provider.get(**config.slice(:paths, :only, :exclude))
|
@@ -22,9 +22,6 @@ module I18n::Tasks::Scanners::Files
|
|
22
22
|
# @param (see FileFinder#traverse_files)
|
23
23
|
# @yieldparam (see FileFinder#traverse_files)
|
24
24
|
# @return (see FileFinder#traverse_files)
|
25
|
-
def traverse_files
|
26
|
-
super
|
27
|
-
end
|
28
25
|
|
29
26
|
alias uncached_find_files find_files
|
30
27
|
private :uncached_find_files
|
@@ -15,6 +15,7 @@ module I18n::Tasks::Scanners::Files
|
|
15
15
|
# Files matching any of the exclusion patterns will be excluded even if they match an inclusion pattern.
|
16
16
|
def initialize(paths: ['.'], only: nil, exclude: [])
|
17
17
|
fail 'paths argument is required' if paths.nil?
|
18
|
+
|
18
19
|
@paths = paths
|
19
20
|
@include = only
|
20
21
|
@exclude = exclude || []
|
@@ -25,8 +26,8 @@ module I18n::Tasks::Scanners::Files
|
|
25
26
|
# @yield [path]
|
26
27
|
# @yieldparam path [String] the path of the found file.
|
27
28
|
# @return [Array<of block results>]
|
28
|
-
def traverse_files
|
29
|
-
find_files.map
|
29
|
+
def traverse_files(&block)
|
30
|
+
find_files.map(&block)
|
30
31
|
end
|
31
32
|
|
32
33
|
# @return [Array<String>] found files
|
@@ -15,11 +15,11 @@ module I18n
|
|
15
15
|
line_begin = contents.rindex(/^/, position - 1)
|
16
16
|
line_end = contents.index(/.(?=\r?\n|$)/, position)
|
17
17
|
Results::Occurrence.new(
|
18
|
-
path:
|
19
|
-
pos:
|
18
|
+
path: path,
|
19
|
+
pos: position,
|
20
20
|
line_num: contents[0..position].count("\n") + 1,
|
21
21
|
line_pos: position - line_begin + 1,
|
22
|
-
line:
|
22
|
+
line: contents[line_begin..line_end],
|
23
23
|
raw_key: raw_key
|
24
24
|
)
|
25
25
|
end
|
@@ -12,7 +12,7 @@ module I18n::Tasks::Scanners
|
|
12
12
|
include OccurrenceFromPosition
|
13
13
|
include RubyKeyLiterals
|
14
14
|
|
15
|
-
TRANSLATE_CALL_RE = /(?<=^|[^\w'\-.]|[^\w'\-]I18n\.|I18n\.)t(?:!|ranslate!?)
|
15
|
+
TRANSLATE_CALL_RE = /(?<=^|[^\w'\-.]|[^\w'\-]I18n\.|I18n\.)t(?:!|ranslate!?)?/.freeze
|
16
16
|
IGNORE_LINES = {
|
17
17
|
'coffee' => /^\s*#(?!\si18n-tasks-use)/,
|
18
18
|
'erb' => /^\s*<%\s*#(?!\si18n-tasks-use)/,
|
@@ -43,10 +43,13 @@ module I18n::Tasks::Scanners
|
|
43
43
|
src_pos = Regexp.last_match.offset(0).first
|
44
44
|
location = occurrence_from_position(path, text, src_pos, raw_key: strip_literal(match[0]))
|
45
45
|
next if exclude_line?(location.line, path)
|
46
|
+
|
46
47
|
key = match_to_key(match, path, location)
|
47
48
|
next unless key
|
49
|
+
|
48
50
|
key += ':' if key.end_with?('.')
|
49
51
|
next unless valid_key?(key)
|
52
|
+
|
50
53
|
keys << [key, location]
|
51
54
|
end
|
52
55
|
keys
|
@@ -67,7 +70,7 @@ module I18n::Tasks::Scanners
|
|
67
70
|
re && re =~ line
|
68
71
|
end
|
69
72
|
|
70
|
-
VALID_KEY_RE_DYNAMIC = /^(#{VALID_KEY_CHARS}|[:\#{@}\[\]])
|
73
|
+
VALID_KEY_RE_DYNAMIC = /^(#{VALID_KEY_CHARS}|[:\#{@}\[\]])+$/.freeze
|
71
74
|
|
72
75
|
def valid_key?(key)
|
73
76
|
if @config[:strict]
|
@@ -84,7 +87,7 @@ module I18n::Tasks::Scanners
|
|
84
87
|
def closest_method(occurrence)
|
85
88
|
method = File.readlines(occurrence.path, encoding: 'UTF-8')
|
86
89
|
.first(occurrence.line_num - 1).reverse_each.find { |x| x =~ /\bdef\b/ }
|
87
|
-
method && method.strip.sub(/^def\s*/, '').sub(/[
|
90
|
+
method && method.strip.sub(/^def\s*/, '').sub(/[(\s;].*$/, '')
|
88
91
|
end
|
89
92
|
|
90
93
|
# This method only exists for backwards compatibility with monkey-patches and plugins
|
@@ -93,7 +96,7 @@ module I18n::Tasks::Scanners
|
|
93
96
|
def default_pattern
|
94
97
|
# capture only the first argument
|
95
98
|
/
|
96
|
-
#{translate_call_re} [
|
99
|
+
#{translate_call_re} [( ] \s* (?# fn call begin )
|
97
100
|
(#{first_argument_re}) (?# capture the first argument)
|
98
101
|
/x
|
99
102
|
end
|
@@ -26,6 +26,7 @@ module I18n::Tasks::Scanners
|
|
26
26
|
if scope
|
27
27
|
scope_parts = extract_literal_or_array_of_literals(scope)
|
28
28
|
return nil if scope_parts.nil? || scope_parts.empty?
|
29
|
+
|
29
30
|
"#{scope_parts.join('.')}.#{key}"
|
30
31
|
else
|
31
32
|
key unless match[0] =~ /\A\w/
|
@@ -61,7 +62,7 @@ module I18n::Tasks::Scanners
|
|
61
62
|
|
62
63
|
# extract literal or array of literals
|
63
64
|
# returns nil on any other input
|
64
|
-
# rubocop:disable Metrics/
|
65
|
+
# rubocop:disable Metrics/MethodLength,Metrics/PerceivedComplexity
|
65
66
|
def extract_literal_or_array_of_literals(s)
|
66
67
|
literals = []
|
67
68
|
braces_stack = []
|
@@ -78,6 +79,7 @@ module I18n::Tasks::Scanners
|
|
78
79
|
s.each_char.with_index do |c, i|
|
79
80
|
if c == '['
|
80
81
|
return nil unless braces_stack.empty?
|
82
|
+
|
81
83
|
braces_stack.push(i)
|
82
84
|
elsif c == ']'
|
83
85
|
break
|
@@ -93,6 +95,6 @@ module I18n::Tasks::Scanners
|
|
93
95
|
consume_literal.call unless acc.empty?
|
94
96
|
literals
|
95
97
|
end
|
96
|
-
# rubocop:enable Metrics/
|
98
|
+
# rubocop:enable Metrics/MethodLength,Metrics/PerceivedComplexity
|
97
99
|
end
|
98
100
|
end
|
@@ -12,6 +12,7 @@ module I18n
|
|
12
12
|
def absolute_key(key, path, roots: config[:relative_roots], calling_method: nil)
|
13
13
|
return key unless key.start_with?(DOT)
|
14
14
|
fail 'roots argument is required' unless roots.present?
|
15
|
+
|
15
16
|
normalized_path = File.expand_path(path)
|
16
17
|
(root = path_root(normalized_path, roots)) ||
|
17
18
|
fail(CommandError, "Cannot resolve relative key \"#{key}\".\n" \
|
@@ -5,7 +5,7 @@ require 'i18n/tasks/scanners/relative_keys'
|
|
5
5
|
require 'i18n/tasks/scanners/ruby_ast_call_finder'
|
6
6
|
require 'parser/current'
|
7
7
|
|
8
|
-
# rubocop:disable Metrics/AbcSize,Metrics/BlockNesting,Metrics/
|
8
|
+
# rubocop:disable Metrics/AbcSize,Metrics/BlockNesting,Metrics/PerceivedComplexity
|
9
9
|
# TODO: make this class more readable.
|
10
10
|
|
11
11
|
module I18n::Tasks::Scanners
|
@@ -14,7 +14,7 @@ module I18n::Tasks::Scanners
|
|
14
14
|
include RelativeKeys
|
15
15
|
include AST::Sexp
|
16
16
|
|
17
|
-
MAGIC_COMMENT_PREFIX = /\A.\s*i18n-tasks-use\s
|
17
|
+
MAGIC_COMMENT_PREFIX = /\A.\s*i18n-tasks-use\s+/.freeze
|
18
18
|
RECEIVER_MESSAGES = [nil, AST::Node.new(:const, [nil, :I18n])].product(%i[t t! translate translate!])
|
19
19
|
|
20
20
|
def initialize(**args)
|
@@ -71,6 +71,7 @@ module I18n::Tasks::Scanners
|
|
71
71
|
scope = extract_string(scope_node.children[1],
|
72
72
|
array_join_with: '.', array_flatten: true, array_reject_blank: true)
|
73
73
|
return nil if scope.nil? && scope_node.type != :nil
|
74
|
+
|
74
75
|
key = [scope, key].join('.') unless scope == ''
|
75
76
|
end
|
76
77
|
default_arg = if (default_arg_node = extract_hash_pair(second_arg_node, 'default'))
|
@@ -95,6 +96,7 @@ module I18n::Tasks::Scanners
|
|
95
96
|
def extract_hash_pair(node, key)
|
96
97
|
node.children.detect do |child|
|
97
98
|
next unless child.type == :pair
|
99
|
+
|
98
100
|
key_node = child.children[0]
|
99
101
|
%i[sym str].include?(key_node.type) && key_node.children[0].to_s == key
|
100
102
|
end
|
@@ -115,15 +117,15 @@ module I18n::Tasks::Scanners
|
|
115
117
|
def extract_string(node, array_join_with: nil, array_flatten: false, array_reject_blank: false)
|
116
118
|
if %i[sym str int].include?(node.type)
|
117
119
|
node.children[0].to_s
|
118
|
-
elsif %i[true false].include?(node.type)
|
120
|
+
elsif %i[true false].include?(node.type)
|
119
121
|
node.type.to_s
|
120
122
|
elsif node.type == :nil
|
121
123
|
''
|
122
124
|
elsif node.type == :array && array_join_with
|
123
125
|
extract_array_as_string(
|
124
126
|
node,
|
125
|
-
array_join_with:
|
126
|
-
array_flatten:
|
127
|
+
array_join_with: array_join_with,
|
128
|
+
array_flatten: array_flatten,
|
127
129
|
array_reject_blank: array_reject_blank
|
128
130
|
).tap do |str|
|
129
131
|
# `nil` is returned when a dynamic value is encountered in strict mode. Propagate:
|
@@ -149,11 +151,12 @@ module I18n::Tasks::Scanners
|
|
149
151
|
# @return [String, nil] `nil` is returned only when a dynamic value is encountered in strict mode.
|
150
152
|
def extract_array_as_string(node, array_join_with:, array_flatten: false, array_reject_blank: false)
|
151
153
|
children_strings = node.children.map do |child|
|
152
|
-
if %i[sym str int true false].include?(child.type)
|
154
|
+
if %i[sym str int true false].include?(child.type)
|
153
155
|
extract_string child
|
154
156
|
else
|
155
157
|
# ignore dynamic argument in strict mode
|
156
158
|
return nil if config[:strict]
|
159
|
+
|
157
160
|
if %i[dsym dstr].include?(child.type) || (child.type == :array && array_flatten)
|
158
161
|
extract_string(child, array_join_with: array_join_with)
|
159
162
|
else
|
@@ -180,12 +183,12 @@ module I18n::Tasks::Scanners
|
|
180
183
|
# @return [Results::Occurrence]
|
181
184
|
def range_to_occurrence(raw_key, range, default_arg: nil)
|
182
185
|
Results::Occurrence.new(
|
183
|
-
path:
|
184
|
-
pos:
|
185
|
-
line_num:
|
186
|
-
line_pos:
|
187
|
-
line:
|
188
|
-
raw_key:
|
186
|
+
path: range.source_buffer.name,
|
187
|
+
pos: range.begin_pos,
|
188
|
+
line_num: range.line,
|
189
|
+
line_pos: range.column,
|
190
|
+
line: range.source_line,
|
191
|
+
raw_key: raw_key,
|
189
192
|
default_arg: default_arg
|
190
193
|
)
|
191
194
|
end
|
@@ -203,4 +206,4 @@ module I18n::Tasks::Scanners
|
|
203
206
|
end
|
204
207
|
end
|
205
208
|
end
|
206
|
-
# rubocop:enable Metrics/AbcSize,Metrics/BlockNesting,Metrics/
|
209
|
+
# rubocop:enable Metrics/AbcSize,Metrics/BlockNesting,Metrics/PerceivedComplexity
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module I18n::Tasks::Scanners
|
4
4
|
module RubyKeyLiterals
|
5
|
-
LITERAL_RE = /:?".+?"|:?'.+?'|:\w
|
5
|
+
LITERAL_RE = /:?".+?"|:?'.+?'|:\w+/.freeze
|
6
6
|
|
7
7
|
# Match literals:
|
8
8
|
# * String: '', "#{}"
|
@@ -20,8 +20,8 @@ module I18n::Tasks::Scanners
|
|
20
20
|
literal
|
21
21
|
end
|
22
22
|
|
23
|
-
VALID_KEY_CHARS = /(?:[[:word:]]|[-.?!:;À-ž])
|
24
|
-
VALID_KEY_RE = /^#{VALID_KEY_CHARS}
|
23
|
+
VALID_KEY_CHARS = /(?:[[:word:]]|[-.?!:;À-ž])/.freeze
|
24
|
+
VALID_KEY_RE = /^#{VALID_KEY_CHARS}+$/.freeze
|
25
25
|
|
26
26
|
def valid_key?(key)
|
27
27
|
key =~ VALID_KEY_RE && !key.end_with?('.')
|