Skip to content

Commit

Permalink
Add default_i18n_subject AST matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilka2 authored and glebm committed Dec 9, 2023
1 parent 6c68163 commit 9da6ded
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'i18n/tasks/scanners/results/occurrence'

module I18n::Tasks::Scanners::AstMatchers
class DefaultI18nSubjectMatcher < BaseMatcher
def convert_to_key_occurrences(send_node, method_name, location: send_node.loc)
children = Array(send_node&.children)
return unless children[1] == :default_i18n_subject

key = @scanner.absolute_key(
'.subject',
location.expression.source_buffer.name,
calling_method: method_name
)
[
key,
I18n::Tasks::Scanners::Results::Occurrence.from_range(
raw_key: key,
range: location.expression
)
]
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/used_keys/app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UserMailer < ApplicationMailer
def welcome_notification
I18n.with_locale(:en) do
mail subject: default_i18n_subject
end
end
end
29 changes: 25 additions & 4 deletions spec/used_keys_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'spec_helper'
require 'i18n/tasks/scanners/ast_matchers/rails_model_matcher'
require 'i18n/tasks/scanners/ast_matchers/default_i18n_subject_matcher'

RSpec.describe 'UsedKeysRuby' do
let!(:task) { I18n::Tasks::BaseTask.new }
Expand All @@ -13,7 +14,8 @@
true
end
let(:ast_matchers) do
%w[I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher]
%w[I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher
I18n::Tasks::Scanners::AstMatchers::DefaultI18nSubjectMatcher]
end

around do |ex|
Expand Down Expand Up @@ -134,10 +136,12 @@
end

describe 'relative_roots' do
let(:paths) { %w[app/components/event_component.rb app/controllers/events_controller.rb] }
let(:paths) do
%w[app/components/event_component.rb app/controllers/events_controller.rb app/mailers/user_mailer.rb]
end
let(:extra_search_config) do
{
relative_roots: %w[app/components app/controllers],
relative_roots: %w[app/components app/controllers app/mailers],
relative_exclude_method_name_paths: %w[app/components]
}
end
Expand All @@ -146,7 +150,7 @@
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(leaves.size).to(eq(5))

expect_node_key_data(
leaves[0],
Expand Down Expand Up @@ -222,6 +226,23 @@
]
)
)

expect_node_key_data(
leaves[4],
'user_mailer.welcome_notification.subject',
occurrences: make_occurrences(
[
{
path: 'app/mailers/user_mailer.rb',
pos: 113,
line_num: 4,
line_pos: 20,
line: ' mail subject: default_i18n_subject',
raw_key: 'user_mailer.welcome_notification.subject'
}
]
)
)
end
end
end
3 changes: 3 additions & 0 deletions templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ search:
## - RailsModelMatcher
## Matches ActiveRecord translations like
## User.human_attribute_name(:email) and User.model_name.human
## - DefaultI18nSubjectMatcher
## Matches ActionMailer's default_i18n_subject method
##
## To implement your own, please see `I18n::Tasks::Scanners::AstMatchers::BaseMatcher`.
# <%# I18n::Tasks.add_ast_matcher('I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher') %>
# <%# I18n::Tasks.add_ast_matcher('I18n::Tasks::Scanners::AstMatchers::DefaultI18nSubjectMatcher') %>

## Multiple scanners can be used. Their results are merged.
## The options specified above are passed down to each scanner. Per-scanner options can be specified as well.
Expand Down

0 comments on commit 9da6ded

Please sign in to comment.