Skip to content

Commit

Permalink
ErbAst: Handles blocks
Browse files Browse the repository at this point in the history
- Fixes glebm#424 by allowing blocks to be parsed by ignoring `do`.
- Adds specs for block calls.
- Changes structure for erb tests to test relative keys as well.
- Inspired by
https://github.com/Shopify/better-html/blob/087943ffd2a5877fa977d71532010b0c91239519/lib/better_html/test_helper/ruby_node.rb#L24
  • Loading branch information
davidwessman committed Mar 24, 2022
1 parent 9c0c354 commit 5fe301d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/tasks/scanners/erb_ast_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ErbAstProcessor
include AST::Processor::Mixin
def initialize
super()
@ruby_parser = LocalRubyParser.new
@ruby_parser = LocalRubyParser.new(ignore_blocks: true)
@comments = []
end

Expand Down
13 changes: 11 additions & 2 deletions lib/i18n/tasks/scanners/local_ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@

module I18n::Tasks::Scanners
class LocalRubyParser
def initialize
# ignore_blocks feature inspired by shopify/better-html
# https://github.com/Shopify/better-html/blob/087943ffd2a5877fa977d71532010b0c91239519/lib/better_html/test_helper/ruby_node.rb#L24
BLOCK_EXPR = /\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze

def initialize(ignore_blocks: false)
@parser = ::Parser::CurrentRuby.new
@ignore_blocks = ignore_blocks
end

# Parse string and normalize location
def parse(source, location: nil)
buffer = ::Parser::Source::Buffer.new('(string)')
buffer.source = source
buffer.source = if @ignore_blocks
source.sub(BLOCK_EXPR, '')
else
source
end

@parser.reset
ast, comments = @parser.parse_with_comments(buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ I18n.t("this_should_not")
</h2>
<h3>
<%= t('with_parameter', parameter: "erb is the best") %>
<%= t 'with_scope', scope: "scope_a.scope_b", default: t("nested_call") %>
<%= t 'with_scope', scope: "scope_a.scope_b", default: t(".nested_call") %>
<%= link_to(edit_foo_path(foo), title: t(".edit")) do %>
<i class="fa fa-edit icon-fa"></i>
<% end %>
</h3>
48 changes: 35 additions & 13 deletions spec/used_keys_erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,30 @@
end

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

it '#used_keys' do
used_keys = task.used_tree
expect(used_keys.size).to eq 1
expect(used_keys.size).to eq(1)
leaves = used_keys.leaves.to_a
expect(leaves.size).to eq(5)
expect(leaves.size).to eq(6)

expect_node_key_data(
leaves[0],
'a',
occurrences: make_occurrences(
[
{
path: 'a.html.erb', pos: 17,
path: 'app/views/application/show.html.erb',
pos: 17,
line_num: 1, line_pos: 17,
line: "<div id=first><%= t('a') %></div>",
raw_key: 'a'
},
{
path: 'a.html.erb', pos: 44,
path: 'app/views/application/show.html.erb',
pos: 44,
line_num: 2, line_pos: 10,
line: "<% what = t 'a' %>",
raw_key: 'a'
Expand All @@ -45,7 +47,8 @@
occurrences: make_occurrences(
[
{
path: 'a.html.erb', pos: 318,
path: 'app/views/application/show.html.erb',
pos: 318,
line_num: 11, line_pos: 5,
line: " <%= t('with_parameter', parameter: \"erb is the best\") %>",
raw_key: 'with_parameter'
Expand All @@ -59,9 +62,10 @@
occurrences: make_occurrences(
[
{
path: 'a.html.erb', pos: 377,
path: 'app/views/application/show.html.erb',
pos: 377,
line_num: 12, line_pos: 5,
line: " <%= t 'with_scope', scope: \"scope_a.scope_b\", default: t(\"nested_call\") %>",
line: " <%= t 'with_scope', scope: \"scope_a.scope_b\", default: t(\".nested_call\") %>",
raw_key: 'scope_a.scope_b.with_scope'
}
]
Expand All @@ -70,26 +74,44 @@

expect_node_key_data(
leaves[3],
'nested_call',
'application.show.nested_call',
occurrences: make_occurrences(
[
{
path: 'a.html.erb', pos: 429,
path: 'app/views/application/show.html.erb',
pos: 429,
line_num: 12, line_pos: 57,
line: " <%= t 'with_scope', scope: \"scope_a.scope_b\", default: t(\"nested_call\") %>",
raw_key: 'nested_call'
line: " <%= t 'with_scope', scope: \"scope_a.scope_b\", default: t(\".nested_call\") %>",
raw_key: '.nested_call'
}
]
)
)

expect_node_key_data(
leaves[4],
"application.show.edit",
occurrences: make_occurrences(
[
{
path: 'app/views/application/show.html.erb',
pos: 491,
line_num: 13, line_pos: 41,
line: ' <%= link_to(edit_foo_path(foo), title: t(".edit")) do %>',
raw_key: '.edit'
}
]
)
)

expect_node_key_data(
leaves[5],
'activerecord.models.first.one',
occurrences: make_occurrences(
[
{
path: 'a.html.erb', pos: 88,
path: 'app/views/application/show.html.erb',
pos: 88,
line_num: 5, line_pos: 4,
line: " <% # i18n-tasks-use t('activerecord.models.first.one') %>",
raw_key: 'activerecord.models.first.one'
Expand Down

0 comments on commit 5fe301d

Please sign in to comment.