Skip to content

Commit

Permalink
Ignore the "adding children to leaf" warning for keys that are discov…
Browse files Browse the repository at this point in the history
…ered in source. (#228)

Ignore the "adding children to leaf" warning for keys that are discovered in source.
  • Loading branch information
jfly authored and glebm committed Feb 11, 2017
1 parent a483b91 commit 225cee6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/i18n/tasks/data/tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/i18n/tasks/data/tree/siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion spec/used_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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',
Expand All @@ -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(
Expand Down

0 comments on commit 225cee6

Please sign in to comment.