-
-
Save tenderlove/13169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "johnson" | |
require "./visitor.rb" | |
js =<<-END | |
function top() { | |
"top docstring", { command: true } | |
doSomething(); | |
}; | |
function justADocstring() { | |
"justADocstring docstring" | |
doSomething(); | |
} | |
function noMeta() { | |
doSomething(); | |
}; | |
Namespace = { | |
nested: function() { | |
"nested docstring", { command: true } | |
doSomething(); | |
}, | |
Doubly: { | |
nested: function() { | |
"doubly nested docstring", { command: true } | |
doSomething(); | |
} | |
} | |
}; | |
var NamespaceWithVar = { | |
nested: function() { | |
"nested with var docstring", { command: true } | |
doSomething(); | |
} | |
}; | |
END | |
ast = Johnson.parse(js); | |
visitor = MetaVisitor.new do |node| | |
end | |
ast.accept(visitor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MetaVisitor < Johnson::Visitors::EnumeratingVisitor | |
attr_accessor :block | |
def initialize(&block) | |
@block = block | |
@namespace = [] | |
end | |
def visit_Function(o) | |
name = (@namespace + [o.name]).compact.join(".") | |
puts name | |
super | |
end | |
%w{ OpEqual AssignExpr Property }.each do |type| | |
define_method(:"visit_#{type}") do |o| | |
@namespace.push(o.left.value) | |
super | |
@namespace.pop | |
self | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment