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 <[email protected]>
  • Loading branch information
aleksandrs-ledovskis committed Oct 27, 2018
1 parent 4969c5d commit 7131653
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/i18n/tasks/data/tree/siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module I18n::Tasks::Data::Tree
# in case of an empty parent (nil) it represents a forest
# siblings' keys are unique
class Siblings < Nodes # rubocop:disable Metrics/ClassLength
# Ref: http://cldr.unicode.org/index/cldr-spec/plural-rules
CLDR_CATEGORY_KEYS = %w[zero one two few many other].freeze

include ::I18n::Tasks::SplitKey

attr_reader :parent, :key_to_node
Expand Down Expand Up @@ -100,12 +103,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 +190,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 +231,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) - 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
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 7131653

Please sign in to comment.