Skip to content

Commit

Permalink
Allows configure ast_matchers with classes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwessman committed Apr 9, 2022
1 parent 9013e9d commit 1b70818
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
11 changes: 11 additions & 0 deletions lib/i18n/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def add_commands(commands_module)
::I18n::Tasks::Commands.send :include, commands_module
self
end

# Add AST-matcher to i18n-tasks
#
# @param matcher_class_name
# @return self
def add_ast_matcher(matcher_class_name)
matchers = I18n::Tasks::Configuration::DEFAULTS[:search][:ast_matchers]
matchers << matcher_class_name
matchers.uniq!
self
end
end

@verbose = !ENV['VERBOSE'].nil?
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tasks/scanners/ast_matchers/base_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def initialize(scanner:)
@scanner = scanner
end

def convert_to_key_occurrences(send_node, method_name, location: send_node.loc)
raise("Not implemented")
end

protected

# If the node type is of `%i(sym str int false true)`, return the value as a string.
Expand Down
7 changes: 4 additions & 3 deletions lib/i18n/tasks/scanners/ruby_ast_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'i18n/tasks/scanners/relative_keys'
require 'i18n/tasks/scanners/ruby_ast_call_finder'
require 'i18n/tasks/scanners/ast_matchers/message_receivers_matcher'
require 'i18n/tasks/scanners/ast_matchers/rails_model_matcher'
require 'parser/current'

module I18n::Tasks::Scanners
Expand Down Expand Up @@ -133,9 +132,11 @@ def setup_matchers
scanner: self
)
end
if Array(config[:extra_matchers]).include?('rails_model')
matchers << AstMatchers::RailsModelMatcher.new(scanner: self)

Array(config[:ast_matchers]).each do |class_name|
matchers << ActiveSupport::Inflector.constantize(class_name).new(scanner: self)
end

matchers
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/used_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module UsedKeys # rubocop:disable Metrics/ModuleLength
['::I18n::Tasks::Scanners::ErbAstScanner', { only: %w[*.erb] }],
['::I18n::Tasks::Scanners::PatternWithScopeScanner', { exclude: %w[*.erb *.rb] }]
],
extra_matchers: [],
ast_matchers: [],
strict: true
}.freeze

Expand Down
13 changes: 9 additions & 4 deletions spec/used_keys_erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# frozen_string_literal: true

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

RSpec.describe 'UsedKeysErb' do
let!(:task) { I18n::Tasks::BaseTask.new }
around do |ex|
task.config[:search] = { paths: paths, extra_matchers: extra_matchers }
I18n::Tasks::Configuration::DEFAULTS[:search][:ast_matchers].clear
ast_matchers.each do |matcher|
I18n::Tasks.add_ast_matcher(matcher)
end
task.config[:search] = { paths: paths }
TestCodebase.in_test_app_dir(directory: 'spec/fixtures/used_keys') { ex.run }
end

let(:paths) {
%w[app/views/application/show.html.erb]
}

let(:extra_matchers) {
%w[rails_model]
let(:ast_matchers) {
%w[I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher]
}

it '#used_keys' do
Expand Down Expand Up @@ -319,7 +324,7 @@
end

describe 'without rails_model matcher' do
let(:extra_matchers) { [] }
let(:ast_matchers) { [] }

it '#used_keys' do
used_keys = task.used_tree
Expand Down
13 changes: 9 additions & 4 deletions spec/used_keys_ruby_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# frozen_string_literal: true

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

RSpec.describe 'UsedKeysRuby' do
let!(:task) { I18n::Tasks::BaseTask.new }
around do |ex|
task.config[:search] = { paths: paths, extra_matchers: extra_matchers }.compact
I18n::Tasks::Configuration::DEFAULTS[:search][:ast_matchers].clear
ast_matchers.each do |matcher|
I18n::Tasks.add_ast_matcher(matcher)
end
task.config[:search] = {paths: paths}.compact
TestCodebase.in_test_app_dir(directory: 'spec/fixtures/used_keys') { ex.run }
end

let(:paths) {
%w[a.rb]
}

let(:extra_matchers) {
%w[rails_model]
let(:ast_matchers) {
%w[I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher]
}

it '#used_keys - ruby' do
Expand Down Expand Up @@ -104,7 +109,7 @@
end

describe 'without rails_model matcher' do
let(:extra_matchers) { [] }
let(:ast_matchers) { [] }

it '#used_keys - ruby' do
used_keys = task.used_tree
Expand Down
5 changes: 3 additions & 2 deletions templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ search:
## If `strict` is `false`, guess usages such as t("categories.#{category}.title"). The default is `true`.
# strict: true

## Allows adding extra matchers for finding translations using the AST-scanners
## Allows adding ast_matchers for finding translations using the AST-scanners
## The available matchers are:
## - RailsModelMatcher
## Matches ActiveRecord translations like
## User.human_attribute_name(:email) and User.model_name.human
##
# extra_matchers: ['rails_model']
## To implement your own, please see `I18n::Tasks::Scanners::AstMatchers::BaseMatcher`.
<%# I18n::Tasks.add_ast_matcher('I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher') %>

## 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 1b70818

Please sign in to comment.