Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: relative_exclude_method_name_paths #454

Merged
merged 1 commit into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/i18n/tasks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def file_config
warn_deprecated 'Please move relative_roots under search in config/i18n-tasks.yml.'
c[:search][:relative_roots] = c.delete(:relative_roots)
end

if c.dig(:search, :exclude_method_name_paths)
warn_deprecated(
'Please rename exclude_method_name_paths to relative_exclude_method_name_paths in config/i18n-tasks.yml.'
)
c[:search][:relative_exclude_method_name_paths] = c[:search].delete(:exclude_method_name_paths)
end
end
else
{}.with_indifferent_access
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/scanners/relative_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module RelativeKeys
# @param calling_method [#call, Symbol, String, false, nil]
# @return [String] absolute version of the key
def absolute_key(key, path, roots: config[:relative_roots],
exclude_method_name_paths: config[:exclude_method_name_paths],
exclude_method_name_paths: config[:relative_exclude_method_name_paths],
calling_method: nil)
return key unless key.start_with?(DOT)
fail 'roots argument is required' unless roots.present?
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/used_keys/app/components/event_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class EventComponent < ViewComponent::Base
def whatever
t(".key")
t("absolute_key")
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/used_keys/app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class EventsController < ApplicationController
def create
t(".relative_key")
t("absolute_key")
I18n.t("very_absolute_key")
end
end
96 changes: 95 additions & 1 deletion spec/used_keys_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
ast_matchers.each do |matcher|
I18n::Tasks.add_ast_matcher(matcher)
end
task.config[:search] = { paths: paths, strict: strict }
task.config[:search] = { paths: paths, strict: strict }.merge(extra_search_config)
TestCodebase.in_test_app_dir(directory: 'spec/fixtures/used_keys') { ex.run }
end

let(:extra_search_config) { {} }

let(:paths) do
%w[a.rb]
end
Expand Down Expand Up @@ -133,4 +135,96 @@
expect(leaves.size).to(eq(3))
end
end

describe 'relative_roots' do
let(:paths) { %w[app/components/event_component.rb app/controllers/events_controller.rb] }
let(:extra_search_config) do
{
relative_roots: %w[app/components app/controllers],
relative_exclude_method_name_paths: %w[app/components]
}
end

it '#used_keys' do
used_keys = task.used_tree
expect(used_keys.size).to eq(1)
leaves = used_keys.leaves.to_a
expect(leaves.size).to(eq(4))

expect_node_key_data(
leaves[0],
'event_component.key',
occurrences: make_occurrences(
[
{
path: 'app/components/event_component.rb',
pos: 62,
line_num: 3,
line_pos: 4,
line: ' t(".key")',
raw_key: '.key'
}
]
)
)
expect_node_key_data(
leaves[1],
'absolute_key',
occurrences: make_occurrences(
[
{
path: 'app/components/event_component.rb',
pos: 76,
line_num: 4,
line_pos: 4,
line: ' t("absolute_key")',
raw_key: 'absolute_key'
},
{
path: 'app/controllers/events_controller.rb',
pos: 87,
line_num: 4,
line_pos: 4,
line: ' t("absolute_key")',
raw_key: 'absolute_key'
}
]
)
)

expect_node_key_data(
leaves[2],
'events.create.relative_key',
occurrences: make_occurrences(
[
{
path: 'app/controllers/events_controller.rb',
pos: 64,
line_num: 3,
line_pos: 4,
line: ' t(".relative_key")',
raw_key: '.relative_key'
}
]
)
)

expect_node_key_data(
leaves[3],
'very_absolute_key',
occurrences: make_occurrences(
[
{
path: 'app/controllers/events_controller.rb',
pos: 109,
line_num: 5,
line_pos: 4,
line: ' I18n.t("very_absolute_key")',
raw_key: 'very_absolute_key'
}
]
)
)
end
end
end