<commit_before>require_relative 'base'
require_relative '../po_entry'

require 'parser/current'

# opt-in to most recent Parser AST format:
Parser::Builders::Default.emit_lambda = true
Parser::Builders::Default.emit_procarg0 = true

module RXGetText::LanguageParsers
  class Ruby < Base
    extensions %w(.rb)

    def initialize
      @ruby_parser = Parser::CurrentRuby.new
    end

    def strings_from_source(source, filename: '(string)', first_line: 1)
      buffer = Parser::Source::Buffer.new(filename, first_line)
      buffer.source = source

      @ruby_parser.reset
      ast = @ruby_parser.parse(buffer)

      find_strings_in_ast(ast)
    end

    private

    def string_from_ast_node(ast_node)
      case ast_node.type
      when :str
        filename = ast_node.location.expression.source_buffer.name
        line     = ast_node.location.line

        RXGetText::POEntry.new(
          msgid: ast_node.children[0],
          references: ["#{filename}:#{line}"]
        )
      else
        raise _("unsupported AST node type: %{type}") % { type: ast_node.type }
      end
    end

    def find_strings_in_ast(ast_node)
      strings = []

      if ast_node.type == :send
        case ast_node.children[1]
        when :_
          strings << string_from_ast_node(ast_node.children[2])
        end
      end

      ast_node.children.each do |child|
        next unless child.is_a? Parser::AST::Node
        strings += find_strings_in_ast(child)
      end

      strings
    end<commit_msg>Handle empty Ruby files correctly<commit_after>require_relative 'base'
require_relative '../po_entry'

require 'parser/current'

# opt-in to most recent Parser AST format:
Parser::Builders::Default.emit_lambda = true
Parser::Builders::Default.emit_procarg0 = true

module RXGetText::LanguageParsers
  class Ruby < Base
    extensions %w(.rb)

    def initialize
      @ruby_parser = Parser::CurrentRuby.new
    end

    def strings_from_source(source, filename: '(string)', first_line: 1)
      buffer = Parser::Source::Buffer.new(filename, first_line)
      buffer.source = source

      @ruby_parser.reset
      ast = @ruby_parser.parse(buffer)

      find_strings_in_ast(ast)
    end

    private

    def string_from_ast_node(ast_node)
      case ast_node.type
      when :str
        filename = ast_node.location.expression.source_buffer.name
        line     = ast_node.location.line

        RXGetText::POEntry.new(
          msgid: ast_node.children[0],
          references: ["#{filename}:#{line}"]
        )
      else
        raise _("unsupported AST node type: %{type}") % { type: ast_node.type }
      end
    end

    def find_strings_in_ast(ast_node)
      return [] unless ast_node.is_a? Parser::AST::Node

      strings = []

      if ast_node.type == :send
        case ast_node.children[1]
        when :_
          strings << string_from_ast_node(ast_node.children[2])
        end
      end

      ast_node.children.each do |child|
        next unless child.is_a? Parser::AST::Node
        strings += find_strings_in_ast(child)
      end

      strings
    end
{'fraction_non_alphanumeric': 0.1005514109633474, 'fraction_numerical': 0.003243593902043464, 'mean_word_length': 2.6714285714285713, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4638573', 'n_tokens_mistral': 991, 'n_tokens_neox': 949, 'n_words': 201}