Skip to content

Commit

Permalink
Silence warnings for common leaf -> tree expansion scenario.
Browse files Browse the repository at this point in the history
Closes #242

Signed-off-by: Aleksandrs Ļedovskis <aleksandrs@ledovskis.lv>
  • Loading branch information
aleksandrs-ledovskis committed Nov 4, 2018
1 parent e2ffb33 commit 774c3d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/i18n/tasks/data/tree/siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def set(full_key, node)
key: key_part,
parent: parent,
children: [],
warn_about_add_children_to_leaf: @warn_add_children_to_leaf
warn_about_add_children_to_leaf: @warn_about_add_children_to_leaf
)
append! child
end
unless child.children
warn_add_children_to_leaf child if @warn_about_add_children_to_leaf
conditionally_warn_add_children_to_leaf(child, [])
child.children = []
end
child.children.set rest, node
Expand Down Expand Up @@ -187,7 +187,7 @@ def merge_node!(node, on_leaves_merge: nil) # rubocop:disable Metrics/AbcSize,Me
if our.children
our.children.merge!(node.children)
else
warn_add_children_to_leaf our
conditionally_warn_add_children_to_leaf(our, node.children)
our.children = node.children
end
elsif on_leaves_merge
Expand Down Expand Up @@ -228,6 +228,14 @@ def add_ancestors_that_only_contain_nodes!(nodes)
end
end

def conditionally_warn_add_children_to_leaf(node, children)
return unless @warn_about_add_children_to_leaf
return if !children.empty? &&
(children.map(&:key) - I18n::Tasks::PluralKeys::CLDR_CATEGORY_KEYS).empty?

warn_add_children_to_leaf(node)
end

def warn_add_children_to_leaf(node)
::I18n::Tasks::Logging.log_warn "'#{node.full_key}' was a leaf, now has children (value <- scope conflict)"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/i18n/tasks/plural_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

require 'set'
module I18n::Tasks::PluralKeys
PLURAL_KEY_SUFFIXES = Set.new %w[zero one two few many other]
PLURAL_KEY_RE = /\.(?:#{PLURAL_KEY_SUFFIXES.to_a * '|'})$/
# Ref: http://cldr.unicode.org/index/cldr-spec/plural-rules
CLDR_CATEGORY_KEYS = %w[zero one two few many other].freeze
PLURAL_KEY_SUFFIXES = Set.new CLDR_CATEGORY_KEYS
PLURAL_KEY_RE = /\.(?:#{CLDR_CATEGORY_KEYS * '|'})$/

def collapse_plural_nodes!(tree)
tree.leaves.map(&:parent).compact.uniq.each do |node|
Expand Down
8 changes: 8 additions & 0 deletions spec/locale_tree/siblings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
a = build_tree(a: 1)
b = build_tree(a: { b: 1 })
expect { silence_stderr { a.merge(b) } }.to_not raise_error
expect(capture_stderr { a.merge(b) })
.to include("[WARN] 'a' was a leaf, now has children (value <- scope conflict)")
end

it '#merge does not warn about conflict with Unicode CLDR category keys' do
a = build_tree(a: 1)
b = build_tree(a: { zero: 0, one: 1, two: 2, few: 7, many: 88, other: 'ok' })
expect(capture_stderr { a.merge(b) }).to be_empty
end

it '#set conflict value <- scope' do
Expand Down

0 comments on commit 774c3d6

Please sign in to comment.