Skip to content

Commit

Permalink
Uses AST-scanner for all ERB (#573)
Browse files Browse the repository at this point in the history
- Also fixes regex in PatternScanner to handle `theme_t "foo"` being
  marked as a translation.
- Fixes #572
  • Loading branch information
davidwessman authored May 14, 2024
1 parent 9e765a4 commit 80f9ab0
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 270 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

* Uses AST-parser for all ERB-files, not just `.html.erb`
* [Fixed regex in `PatternScanner`] (https://github.com/glebm/i18n-tasks/issues/572)

## v1.0.14

* Newlines are now preserved when using Google Translate.
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/scanners/pattern_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PatternScanner < FileScanner
include OccurrenceFromPosition
include RubyKeyLiterals

TRANSLATE_CALL_RE = /(?<=^|[^\p{L}'\-.]|[^\p{L}'-]I18n\.|I18n\.)t(?:!|ranslate!?)?/.freeze
TRANSLATE_CALL_RE = /(?<=^|[^\p{L}_'\-.]|[^\p{L}'-]I18n\.|I18n\.)t(?:!|ranslate!?)?/.freeze
IGNORE_LINES = {
'coffee' => /^\s*#(?!\si18n-tasks-use)/,
'erb' => /^\s*<%\s*#(?!\si18n-tasks-use)/,
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/tasks/used_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module UsedKeys # rubocop:disable Metrics/ModuleLength
relative_roots: %w[app/controllers app/helpers app/mailers app/presenters app/views].freeze,
scanners: [
['::I18n::Tasks::Scanners::RubyAstScanner', { only: %w[*.rb] }],
['::I18n::Tasks::Scanners::ErbAstScanner', { only: %w[*.html.erb] }],
['::I18n::Tasks::Scanners::PatternWithScopeScanner', { exclude: %w[*.html.erb *.rb] }]
['::I18n::Tasks::Scanners::ErbAstScanner', { only: %w[*.erb] }],
['::I18n::Tasks::Scanners::PatternWithScopeScanner', { exclude: %w[*.erb *.rb] }]
],
ast_matchers: [],
strict: true
Expand Down
2 changes: 0 additions & 2 deletions spec/fixtures/app/views/ignore.js.erb

This file was deleted.

2 changes: 2 additions & 0 deletions spec/fixtures/app/views/show.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This is javascript file
<% # i18n-tasks-use t('hello.world.from_javascript') %>
27 changes: 27 additions & 0 deletions spec/fixtures/used_keys/app/views/application/index.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= t('text.a') %>
<% what = t 'text.a' %>
I18n.t("text.this_should_not")

<% # i18n-tasks-use t('comment.absolute.attribute') %>
<%= Translate.absolute.attribute %>
<%= MeetingNote.model_name.human(count: 1) %>
<%= AgendaItem.human_attribute_name(:title) %>
<%= t('with_parameter', parameter: "erb is the best") %>
<%= t 'with_scope', scope: "scope_a.scope_b", default: t(".nested_call") %>
<% # https://github.com/glebm/i18n-tasks/issues/424 %>
<%= link_to(edit_foo_path(foo), title: t(".edit")) do %>
<i class="fa fa-edit icon-fa"></i>
<% end %>
<% # https://github.com/glebm/i18n-tasks/issues/426 %>
<%= render Blacklight::System::ModalComponent.new do |component| %>
<% component.title { t('blacklight.tools.citation') } %>
<%= render Blacklight::Document::CitationComponent.with_collection(@documents) if @documents.present? %>
<% end %>
<% # https://github.com/glebm/i18n-tasks/issues/572 %>
<%= theme_t "ignore.this.one" %>
3 changes: 3 additions & 0 deletions spec/fixtures/used_keys/app/views/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ I18n.t("this_should_not")
<%= render Blacklight::Document::CitationComponent.with_collection(@documents) if @documents.present? %>
<% end %>
<% # https://github.com/glebm/i18n-tasks/issues/572 %>
<%= theme_t "ignore.this.one" %>
</h3>
1 change: 1 addition & 0 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
.not_relative
scope.subscope.a.b
scope.key_in_erb
hello.world.from_javascript
scope.relative.index.title
reference-missing-target.a
nested.parent.rb
Expand Down
2 changes: 1 addition & 1 deletion spec/pattern_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
end
end

["t \"a.b'", 't a.b'].each do |string|
["t \"a.b'", 't a.b', 'theme_t "a.b."'].each do |string|
it "does not match #{string}" do
expect(pattern).not_to match string
end
Expand Down
43 changes: 39 additions & 4 deletions spec/support/keys_and_occurrences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

module KeysAndOccurrences
# rubocop:disable Metrics/ParameterLists
def make_occurrence(path: '', line: '', pos: 1, line_pos: 1, line_num: 1, raw_key: nil)
def make_occurrence(
path: '',
line: '',
pos: 1,
line_pos: 1,
line_num: 1,
raw_key: nil
)
::I18n::Tasks::Scanners::Results::Occurrence.new(
path: path, line: line, pos: pos, line_pos: line_pos, line_num: line_num, raw_key: raw_key
path: path,
line: line,
pos: pos,
line_pos: line_pos,
line_num: line_num,
raw_key: raw_key
)
end
# rubocop:enable Metrics/ParameterLists
Expand All @@ -14,7 +26,10 @@ def make_occurrences(occurrences)
end

def make_key_occurrences(key, occurrences)
::I18n::Tasks::Scanners::Results::KeyOccurrences.new(key: key, occurrences: make_occurrences(occurrences))
::I18n::Tasks::Scanners::Results::KeyOccurrences.new(
key: key,
occurrences: make_occurrences(occurrences)
)
end

# adjust position to account for \r on Windows
Expand All @@ -28,6 +43,26 @@ def adjust_occurrences(data)

# adjust position to account for \r on Windows
def adjust_occurrence(occurrence)
occurrence.dup.tap { |o| o.instance_variable_set(:@pos, o.pos + o.line_num - 1) }
occurrence.dup.tap do |o|
o.instance_variable_set(:@pos, o.pos + o.line_num - 1)
end
end

def leaves_to_hash(leaves)
leaves.to_h { |leaf| [leaf.full_key(root: false), leaf] }
end

def expected_occurrences(leaves, expected)
expect(leaves.keys).to match_array(expected.keys)
leaves.each do |key, leaf|
expected_data = expected[key]
occurrences = leaf.data[:occurrences]
expect(occurrences).not_to be_nil
expect(occurrences.size).to(eq(expected_data.size))

occurrences_to_compare =
occurrences.map { |occ| { path: occ.path, line_num: occ.line_num } }
expect(occurrences_to_compare).to(match_array(expected_data))
end
end
end
Loading

0 comments on commit 80f9ab0

Please sign in to comment.