从基于 Ruby 的源文件中提取代码片段

Dan*_*off 2 ruby parsing abstract-syntax-tree code-complexity

我最近一直在玩flog,它是一个非常好的工具,可以为 ruby​​ 应用程序生成代码复杂性报告。在项目代码库上运行的结果是, flog您会得到类似于以下内容的输出:

  1272.3: flog total
     7.3: flog/method average

    62.2: MyClass#foobar lib/myclass#foobar:123
    ... more similar lines ...
Run Code Online (Sandbox Code Playgroud)

上面的示例提供了方法的分数,并引用了定义该方法的源代码中的确切行号。这可以是常规实例/类方法或任何其他“动态”方法,例如。耙子任务等等。

因此,目标是从源文件中提取一段代码(很可能是一个方法),该代码以 flog 输出中定义的行号开头。然后,该代码片段可以在某些 Web UI 中用于显示各种代码指标(基于其他工具,例如flay)和/或存储在数据库中。据我所知,这个任务涉及将 ruby​​ 代码解析为 AST,然后遍历树找到相应的起始行并找出结束行号。我已经用这个库做了一些实验 - https://github.com/whitequark/parser,大部分时间都有效,但获得正确的结果有点棘手。

还有其他解决方案可以从用 ruby​​ 编写的源文件中快速提取方法代码吗?

nac*_*yot 5

您可以实现按名称或行号查找方法的功能。

此示例代码展示了如何按名称查找方法代码。它很脏,但它可以工作(在 Ruby 2.1.2 上)。此处使用parser( gem install parser)。

# method_finder.rb
require 'parser'
require 'parser/current'

class MethodFinder
  def initialize(filename)
@ast = parse(filename)
  end

  def find(method_name)
recursive_search_ast(@ast, method_name)
return @method_source
  end

  private
  def parse(filename)
Parser::CurrentRuby.parse(File.open(filename, "r").read)
  end

  def recursive_search_ast(ast, method_name)
ast.children.each do |child|
  if child.class.to_s == "Parser::AST::Node"
    if (child.type.to_s == "def" or child.type.to_s == "defs") and (child.children[0].to_s == method_name or child.children[1].to_s == method_name)
      @method_source = child.loc.expression.source
    else
      recursive_search_ast(child, method_name)
    end
  end
end
  end
end
Run Code Online (Sandbox Code Playgroud)

你可以MethodFinder像下面这样使用。

mf = MethodFinder.new("./method_finder.rb")
puts mf.find("find")

=> def find(method_name)
=>     recursive_search_ast(@ast, method_name)
=>     return @method_source
=>   end
Run Code Online (Sandbox Code Playgroud)