lib/i18n/tasks/scanners/erb_ast_processor.rb in i18n-tasks-1.0.5 vs lib/i18n/tasks/scanners/erb_ast_processor.rb in i18n-tasks-1.0.6
- old
+ new
@@ -36,52 +36,36 @@
end
# @param node [::Parser::AST::Node]
# @return [::Parser::AST::Node]
def handler_missing(node)
- node = transform_misparsed_comment(node)
+ node = handle_comment(node)
+ return if node.nil?
+
node.updated(
nil,
node.children.map { |child| node?(child) ? process(child) : child }
)
end
private
- # Works around incorrect handling of comments of the form:
- # <%# ... #>
- # (no space between % and #)
+ # Convert ERB-comments to ::Parser::Source::Comment and skip processing node
#
- # With a space the AST is:
- #
- # s(:erb, nil, nil,
- # s(:code, " # this should not fail: ' "), nil)
- #
- # Without a space the AST is:
- #
- # s(:erb,
- # s(:indicator, "#"), nil,
- # s(:code, " this should not fail: ' "), nil)
- # @param node [::Parser::AST::Node]
- # @return [::Parser::AST::Node]
- def transform_misparsed_comment(node)
- return node unless node.type == :erb && node.children.size == 4 &&
- node.children[0]&.type == :indicator && node.children[0].children[0] == "#" &&
- node.children[1].nil? &&
- node.children[2]&.type == :code &&
- node.children[3].nil?
- code_node = node.children[2]
+ # @param node Parser::AST::Node Potential comment node
+ # @return Parser::AST::Node or nil
+ def handle_comment(node)
+ if node.type == :erb && node.children.size == 4 &&
+ node.children[0]&.type == :indicator && node.children[0].children[0] == '#' &&
+ node.children[2]&.type == :code
- # 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")
+ # Do not continue parsing this node
+ comment = node.children[2]
+ @comments << ::Parser::Source::Comment.new(comment.location.expression)
+ return
+ end
- node.updated(
- nil,
- [nil, nil, code_node.updated(nil, [code]), nil]
- )
+ node
end
def node?(node)
node.is_a?(::Parser::AST::Node)
end