Skip to content

Commit

Permalink
Improve reporting for missing ref keys
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Feb 5, 2016
1 parent c84f300 commit 16b0af8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
10 changes: 9 additions & 1 deletion lib/i18n/tasks/data/tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,20 @@ def inspect(level = 0)
Term::ANSIColor.dark '∅'
else
[Term::ANSIColor.color(1 + level % 15, key),
(": #{Term::ANSIColor.cyan(value.to_s)}" if leaf?),
(": #{format_value_for_inspect(value)}" if leaf?),
(" #{data}" if data?)].compact.join
end
[' ' * level, label, ("\n" + children.map { |c| c.inspect(level + 1) }.join("\n") if children?)].compact.join
end

def format_value_for_inspect(value)
if value.is_a?(Symbol)
"#{Term::ANSIColor.bold(Term::ANSIColor.yellow ' ⮕')}#{Term::ANSIColor.yellow value.to_s}"
else
Term::ANSIColor.cyan(value.to_s)
end
end

protected

def dirty!
Expand Down
17 changes: 10 additions & 7 deletions lib/i18n/tasks/reports/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def missing_keys(forest = task.missing_keys)
cyan(bold I18n.t('i18n_tasks.common.key')),
I18n.t('i18n_tasks.missing.details_title')] do |t|
t.rows = sort_by_attr!(forest_to_attr(forest)).map do |a|
[{value: cyan(format_locale(a[:locale])), alignment: :center}, cyan(a[:key]), missing_key_info(a)]
[{value: cyan(format_locale(a[:locale])), alignment: :center},
[format_reference_desc(a[:data]), cyan(a[:key])].compact.join(' '),
missing_key_info(a)]
end
end
else
Expand Down Expand Up @@ -92,21 +94,22 @@ def format_value(val)
val.is_a?(Symbol) ? "#{bold(yellow('⮕ '))}#{yellow(val.to_s)}" : val.to_s.strip
end

def format_reference_desc(node)
case node.data[:type]
def format_reference_desc(node_data)
return nil unless node_data
case node_data[:ref_type]
when :reference_usage
bold(yellow('(reference)'))
bold(yellow('(ref)'))
when :reference_usage_resolved
bold(yellow('(resolved reference)'))
bold(yellow('(resolved ref)'))
when :reference_usage_key
bold(yellow('(reference key)'))
bold(yellow('(ref key)'))
end
end

def print_occurrences(node, full_key = node.full_key)
occurrences = node.data[:occurrences]
puts [bold("#{full_key}"),
format_reference_desc(node),
format_reference_desc(node.data),
(green(occurrences.size.to_s) if occurrences.size > 1)
].compact.join ' '
occurrences.each do |occurrence|
Expand Down
7 changes: 3 additions & 4 deletions lib/i18n/tasks/used_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ module UsedKeys
# @return [Data::Tree::Siblings]
def used_tree(key_filter: nil, strict: nil, include_raw_references: false)
src_tree = used_in_source_tree(key_filter: key_filter, strict: strict)

raw_refs, resolved_refs, used_refs = process_references(src_tree['used'].children)
raw_refs.leaves { |node| node.data[:type] = :reference_usage }
resolved_refs.leaves { |node| node.data[:type] = :reference_usage_resolved }
used_refs.leaves { |node| node.data[:type] = :reference_usage_key }
raw_refs.leaves { |node| node.data[:ref_type] = :reference_usage }
resolved_refs.leaves { |node| node.data[:ref_type] = :reference_usage_resolved }
used_refs.leaves { |node| node.data[:ref_type] = :reference_usage_key }
src_tree.tap do |result|
tree = result['used'].children
tree.subtract_by_key!(raw_refs)
Expand Down
14 changes: 7 additions & 7 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,19 @@
it 'finds references' do
result = Term::ANSIColor.uncolor(run_cmd 'find', 'reference*')
expect(result).to eq(<<-TXT)
missing_target.a (resolved reference)
missing_target.a (resolved ref)
app/views/index.html.slim:36 = t 'reference-missing-target.a'
reference-missing-target (reference key)
reference-missing-target (ref key)
app/views/index.html.slim:36 = t 'reference-missing-target.a'
reference-missing-target.a (reference)
reference-missing-target.a (ref)
app/views/index.html.slim:36 = t 'reference-missing-target.a'
reference-ok-nested (reference key)
reference-ok-nested (ref key)
app/views/index.html.slim:35 = t 'reference-ok-nested.a'
reference-ok-nested.a (reference)
reference-ok-nested.a (ref)
app/views/index.html.slim:35 = t 'reference-ok-nested.a'
reference-ok-plain (reference key)
reference-ok-plain (ref key)
app/views/index.html.slim:34 = t 'reference-ok-plain'
resolved_reference_target.a (resolved reference)
resolved_reference_target.a (resolved ref)
app/views/index.html.slim:35 = t 'reference-ok-nested.a'
TXT
end
Expand Down
3 changes: 2 additions & 1 deletion spec/support/i18n_tasks_output_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
def locale_re
/^\w{2}\b/
end

def extract_keys(actual)
actual = Term::ANSIColor.uncolor(actual).split("\n").map(&:presence).compact
actual = actual[3..-2]
Expand All @@ -16,6 +16,7 @@ def extract_keys(actual)
actual.map { |row|
key = [row[locale_col], row[key_col]].map(&:presence).compact.join('.')
key = key[0..-2] if key.end_with?(':')
key = key.sub(/\((?:ref|resolved ref|ref key)\) /, '')
key
}.compact
end
Expand Down

0 comments on commit 16b0af8

Please sign in to comment.