Skip to content

Instantly share code, notes, and snippets.

@okuramasafumi
Last active June 7, 2023 13:46
Show Gist options
  • Save okuramasafumi/8dde6e4cbdf1e65cda4a904bbc9cc7cb to your computer and use it in GitHub Desktop.
Save okuramasafumi/8dde6e4cbdf1e65cda4a904bbc9cc7cb to your computer and use it in GitHub Desktop.
Niwa plugin
require_relative "../plugin"
module Niwa
module Plugins
class Comment < Niwa::Plugin
class Visitor < SyntaxTree::Visitor
attr_reader :result
def initialize
super
@result = []
@class = nil
end
visit_methods do
def visit_statements(node)
if node.child_nodes.any? { _1.instance_of?(SyntaxTree::DefNode) }
node.child_nodes[1..4].each_slice(2) do |comment, def_node|
@result << Niwa::Item.new(klass: @class, name: def_node.name.value, comment: comment.value)
end
elsif node.child_nodes.first.instance_of?(SyntaxTree::ClassDeclaration)
@class = node.child_nodes.first.constant.constant.value
end
super
end
end
end
end
end
end
module Niwa
module Plugin
module_function
def visitor
visitor_class = "#{name}::Visitor"
@visitor ||= Object.const_get(visitor_class).new
end
def call(filenames)
filenames.map do |filename|
visitor.visit(SyntaxTree.parse(SyntaxTree.read(filename)))
visitor.result.map do
_1.filename = filename
_1
end
end.flatten
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment