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 Dec 24, 2018
1 parent 6662d80 commit a771f4c
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -11,6 +11,7 @@ module I18n::Tasks::Data::Tree
# siblings' keys are unique
class Siblings < Nodes # rubocop:disable Metrics/ClassLength
include ::I18n::Tasks::SplitKey
include ::I18n::Tasks::PluralKeys

attr_reader :parent, :key_to_node

Expand Down Expand Up @@ -100,12 +101,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 +188,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 +229,13 @@ 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 plural_forms?(children)

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
15 changes: 15 additions & 0 deletions spec/locale_tree/siblings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@
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 leaves' 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 '#merge warns about conflict with Unicode CLDR category internal nodes' do
a = build_tree(a: 1)
b = build_tree(a: { one: { foo: 1, bar: 2 } })
expect(capture_stderr { a.merge(b) })
.to include("[WARN] 'a' was a leaf, now has children (value <- scope conflict)")
end

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

0 comments on commit a771f4c

Please sign in to comment.