diff --git a/lib/i18n/tasks/data/tree/node.rb b/lib/i18n/tasks/data/tree/node.rb index 2c26c6f6..5cf86d2a 100644 --- a/lib/i18n/tasks/data/tree/node.rb +++ b/lib/i18n/tasks/data/tree/node.rb @@ -10,14 +10,17 @@ class Node # rubocop:disable Metrics/ClassLength attr_accessor :value attr_reader :key, :children, :parent - def initialize(key:, value: nil, data: nil, parent: nil, children: nil) + # rubocop:disable Metrics/ParameterLists + def initialize(key:, value: nil, data: nil, parent: nil, children: nil, warn_about_add_children_to_leaf: true) @key = key @key = @key.to_s.freeze if @key @value = value @data = data @parent = parent + @warn_about_add_children_to_leaf = warn_about_add_children_to_leaf self.children = (children if children) end + # rubocop:enable Metrics/ParameterLists def attributes { key: @key, value: @value, data: @data.try(:clone), parent: @parent, children: @children } @@ -34,7 +37,11 @@ def children=(children) when NilClass nil else - Siblings.new(nodes: children, parent: self) + Siblings.new( + nodes: children, + parent: self, + warn_about_add_children_to_leaf: @warn_about_add_children_to_leaf + ) end dirty! end diff --git a/lib/i18n/tasks/data/tree/siblings.rb b/lib/i18n/tasks/data/tree/siblings.rb index 6bb8e1a3..e5e31e95 100644 --- a/lib/i18n/tasks/data/tree/siblings.rb +++ b/lib/i18n/tasks/data/tree/siblings.rb @@ -18,6 +18,7 @@ def initialize(opts = {}) @parent = opts[:parent] || first.try(:parent) @list.map! { |node| node.parent == @parent ? node : node.derive(parent: @parent) } @key_to_node = @list.each_with_object({}) { |node, h| h[node.key] = node } + @warn_about_add_children_to_leaf = opts.fetch(:warn_about_add_children_to_leaf, true) end def attributes @@ -104,11 +105,16 @@ def set(full_key, node) if rest unless child - child = Node.new(key: key_part, parent: parent, children: []) + child = Node.new( + key: key_part, + parent: parent, + children: [], + warn_about_add_children_to_leaf: @warn_add_children_to_leaf + ) append! child end unless child.children - warn_add_children_to_leaf child + warn_add_children_to_leaf child if @warn_about_add_children_to_leaf child.children = [] end child.children.set rest, node @@ -254,7 +260,7 @@ def build_forest(opts = {}, &block) # @param key_occurrences [I18n::Tasks::Scanners::KeyOccurrences] # @return [Siblings] def from_key_occurrences(key_occurrences) - build_forest do |forest| + build_forest(warn_about_add_children_to_leaf: false) do |forest| key_occurrences.each do |key_occurrence| forest[key_occurrence.key] = ::I18n::Tasks::Data::Tree::Node.new( key: split_key(key_occurrence.key).last, diff --git a/spec/used_keys_spec.rb b/spec/used_keys_spec.rb index 5854557e..07439d7d 100644 --- a/spec/used_keys_spec.rb +++ b/spec/used_keys_spec.rb @@ -9,6 +9,8 @@ div = t 'a' p = t 'a' h1 = t 'b' +h2 = t 'c.layer' +h3 = t 'c.layer.underneath_c' SLIM end @@ -20,9 +22,11 @@ end it '#used_keys' do + allow(I18n::Tasks::Logging).to receive(:log_warn).exactly(0).times + used = task.used_tree leaves = used.leaves.to_a - expect(leaves.size).to eq 2 + expect(leaves.size).to eq 3 expect_node_key_data( leaves[0], 'a', @@ -42,6 +46,8 @@ end it '#used_keys(key_filter: "b*")' do + allow(I18n::Tasks::Logging).to receive(:log_warn).exactly(0).times + used_keys = task.used_tree(key_filter: 'b*') expect(used_keys.size).to eq 1 expect_node_key_data(