-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows adding AST-matchers via classes
- Adds `add_ast_matcher` to tasks similar to scanners. - Adds a class for matching Rails model translations. - User.human_attribute_name('name') and User.model_name.human(count: 2).
- Loading branch information
1 parent
ec8d473
commit 43db5dc
Showing
11 changed files
with
235 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lib/i18n/tasks/scanners/ast_matchers/rails_model_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'i18n/tasks/scanners/results/occurrence' | ||
|
||
module I18n::Tasks::Scanners::AstMatchers | ||
class RailsModelMatcher < BaseMatcher | ||
def convert_to_key_occurrences(send_node, _method_name, location: send_node.loc) | ||
human_attribute_name_to_key_occurences(send_node: send_node, location: location) || | ||
model_name_human_to_key_occurences(send_node: send_node, location: location) | ||
end | ||
|
||
private | ||
|
||
def human_attribute_name_to_key_occurences(send_node:, location:) | ||
children = Array(send_node&.children) | ||
receiver = children[0] | ||
method_name = children[1] | ||
|
||
return unless method_name == :human_attribute_name && receiver.type == :const | ||
|
||
value = children[2] | ||
|
||
model_name = underscore(receiver.to_a.last) | ||
attribute = extract_string(value) | ||
key = "activerecord.attributes.#{model_name}.#{attribute}" | ||
[ | ||
key, | ||
I18n::Tasks::Scanners::Results::Occurrence.from_range( | ||
raw_key: key, | ||
range: location.expression | ||
) | ||
] | ||
end | ||
|
||
# User.model_name.human(count: 2) | ||
# s(:send, | ||
# s(:send, | ||
# s(:const, nil, :User), :model_name), :human, | ||
# s(:hash, | ||
# s(:pair, | ||
# s(:sym, :count), | ||
# s(:int, 2)))) | ||
def model_name_human_to_key_occurences(send_node:, location:) | ||
children = Array(send_node&.children) | ||
return unless children[1] == :human | ||
|
||
base_children = Array(children[0]&.children) | ||
class_node = base_children[0] | ||
|
||
return unless class_node&.type == :const && base_children[1] == :model_name | ||
|
||
model_name = underscore(class_node.to_a.last) | ||
key = "activerecord.models.#{model_name}" | ||
[ | ||
key, | ||
I18n::Tasks::Scanners::Results::Occurrence.from_range( | ||
raw_key: key, | ||
range: location.expression | ||
) | ||
] | ||
end | ||
|
||
def underscore(value) | ||
value = value.dup.to_s | ||
value.gsub!(/(.)([A-Z])/, '\1_\2') | ||
value.downcase! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.