From c2c257d76dd0d29f578151daea937ada66e633db Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 26 Mar 2022 10:13:16 +0000 Subject: [PATCH] Handle multiline ERB comments Fixes #431 --- lib/i18n/tasks/scanners/erb_ast_processor.rb | 9 ++++++++- .../used_keys/app/views/application/show.html.erb | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/i18n/tasks/scanners/erb_ast_processor.rb b/lib/i18n/tasks/scanners/erb_ast_processor.rb index 82aacef9..5a219753 100644 --- a/lib/i18n/tasks/scanners/erb_ast_processor.rb +++ b/lib/i18n/tasks/scanners/erb_ast_processor.rb @@ -70,9 +70,16 @@ def transform_misparsed_comment(node) node.children[2]&.type == :code && node.children[3].nil? code_node = node.children[2] + + # Prepend # to each line to make it a valid Ruby comment. + code = code_node.children[0].split("\n").map do |line| + next line if line =~ /^\s*#/ + "##{line}" + end.join("\n") + node.updated( nil, - [nil, nil, code_node.updated(nil, ["##{code_node.children[0]}"]), nil] + [nil, nil, code_node.updated(nil, [code]), nil] ) end diff --git a/spec/fixtures/used_keys/app/views/application/show.html.erb b/spec/fixtures/used_keys/app/views/application/show.html.erb index adb9a62e..b8b95fcd 100644 --- a/spec/fixtures/used_keys/app/views/application/show.html.erb +++ b/spec/fixtures/used_keys/app/views/application/show.html.erb @@ -23,4 +23,8 @@ I18n.t("this_should_not") <% end %> <%# this should not fail: ' %> + + <%# this is a multiline comment + it's totally fine + nothing should fail %>