From a771f4cfc596584eab78bf9cbfb28f63c18ed4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandrs=20=C4=BBedovskis?= Date: Sat, 27 Oct 2018 15:41:15 +0300 Subject: [PATCH] Silence warnings for common leaf -> tree expansion scenario. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes https://github.com/glebm/i18n-tasks/issues/242 Signed-off-by: Aleksandrs Ļedovskis --- lib/i18n/tasks/data/tree/siblings.rb | 14 +++++++++++--- lib/i18n/tasks/plural_keys.rb | 6 ++++-- spec/locale_tree/siblings_spec.rb | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/i18n/tasks/data/tree/siblings.rb b/lib/i18n/tasks/data/tree/siblings.rb index ef9269b4..50d8d195 100644 --- a/lib/i18n/tasks/data/tree/siblings.rb +++ b/lib/i18n/tasks/data/tree/siblings.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/i18n/tasks/plural_keys.rb b/lib/i18n/tasks/plural_keys.rb index bd310fae..f7d2c7c3 100644 --- a/lib/i18n/tasks/plural_keys.rb +++ b/lib/i18n/tasks/plural_keys.rb @@ -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| diff --git a/spec/locale_tree/siblings_spec.rb b/spec/locale_tree/siblings_spec.rb index abe976a1..5562c00f 100644 --- a/spec/locale_tree/siblings_spec.rb +++ b/spec/locale_tree/siblings_spec.rb @@ -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