Skip to content

Commit 0688701

Browse files
authored
Prism: Relative keys not supported in partial ERB (#665)
1 parent 3fb1092 commit 0688701

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

lib/i18n/tasks/scanners/prism_scanners/nodes.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ def rails_view?
3333
rails && file_path.present? && file_path.include?("app/views/")
3434
end
3535

36+
def partial_view?
37+
file_path.present? && File.basename(file_path).start_with?("_")
38+
end
39+
3640
def support_relative_keys?
37-
rails_view?
41+
rails_view? && !partial_view?
3842
end
3943

4044
def path
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
I18n.t("this_should_not")
2+
<h2>
3+
<% # i18n-tasks-use t('comment.absolute.attribute') %>
4+
<%= Translate.absolute.attribute %>
5+
<%= MeetingNote.model_name.human(count: 1) %>
6+
<%= AgendaItem.human_attribute_name(:title) %>
7+
</h2>
8+
9+
<%= t(".cannot_resolve_relative") %>

spec/used_keys_erb_prism_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,74 @@
446446
end
447447
end
448448
end
449+
450+
describe "partials" do
451+
let(:paths) { %w[app/views/application/_event.html.erb] }
452+
453+
it "does not allow relative keys in partials" do
454+
used_keys = task.used_tree
455+
expect(used_keys.size).to eq(1)
456+
leaves = used_keys.leaves.to_a
457+
leaves_as_hash = leaves_to_hash(leaves)
458+
expect(leaves_as_hash.keys).to match_array(
459+
%w[
460+
activerecord.attributes.agenda_item.title
461+
activerecord.models.meeting_note.one
462+
comment.absolute.attribute
463+
]
464+
)
465+
466+
expect_node_key_data(
467+
leaves[0],
468+
"comment.absolute.attribute",
469+
occurrences:
470+
make_occurrences(
471+
[
472+
{
473+
path: "app/views/application/_event.html.erb",
474+
pos: 35,
475+
line_num: 3,
476+
line_pos: 5,
477+
line: " <% # i18n-tasks-use t('comment.absolute.attribute') %>",
478+
raw_key: "comment.absolute.attribute"
479+
}
480+
]
481+
)
482+
)
483+
expect_node_key_data(
484+
leaves[1],
485+
"activerecord.models.meeting_note.one",
486+
occurrences:
487+
make_occurrences(
488+
[
489+
{
490+
path: "app/views/application/_event.html.erb",
491+
pos: 132,
492+
line_num: 5,
493+
line_pos: 7,
494+
line: " <%= MeetingNote.model_name.human(count: 1) %>",
495+
raw_key: "activerecord.models.meeting_note.one"
496+
}
497+
]
498+
)
499+
)
500+
expect_node_key_data(
501+
leaves[2],
502+
"activerecord.attributes.agenda_item.title",
503+
occurrences:
504+
make_occurrences(
505+
[
506+
{
507+
path: "app/views/application/_event.html.erb",
508+
pos: 180,
509+
line_num: 6,
510+
line_pos: 7,
511+
line: " <%= AgendaItem.human_attribute_name(:title) %>",
512+
raw_key: "activerecord.attributes.agenda_item.title"
513+
}
514+
]
515+
)
516+
)
517+
end
518+
end
449519
end

0 commit comments

Comments
 (0)