-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Obtaining the string representation of a single Node #17
Comments
Ah, what you want is still there, just cast the identifier to Based on the base example on extracting specific nodes on the readme, you might want to modify your base class to the following: class ClassDependencies(object):
# note the subclass change
# ... skipped the other unchanged methods for the mean time
def process(self, node):
# using the `u''` to ensure unicode in Python 2
fname = u'%s' % node.identifier
if fname == u'Ext.define':
args = node.args.items # argument lists are now classes
if not isinstance(args[0], String):
# to simplify following demo, I have this
print('Ignoring exotic definition: %s' % args[0])
# logging.debug('Ignoring exotic definition: %s', args[0])
else:
name = self.get_string(args[0])
print('calling handle_define', name, args[1]) Now the example >>> from calmjs.parse import es5
>>> from calmjs.parse.walkers import Walker
>>> from calmjs.parse.asttypes import FunctionCall
>>> from calmjs.parse.asttypes import String
>>> source = '''Ext.define('My.sample.Person', {
... name: 'Unknown',
...
... constructor: function(name) {
... if (name) {
... this.name = name;
... }
... },
...
... eat: function(foodType) {
... alert(this.name + " is eating: " + foodType);
... }
... });
...
... Ext.define(some.exotic.thing, {})
... '''
>>> tree = es5(source)
>>> walker = Walker()
>>> dependencies = ClassDependencies()
>>>
>>> for node in walker.filter(tree, lambda node: isinstance(node, FunctionCall)):
... dependencies.process(node)
...
calling handle_define My.sample.Person {
name: 'Unknown',
constructor: function(name) {
if (name) {
this.name = name;
}
},
eat: function(foodType) {
alert(this.name + " is eating: " + foodType);
}
}
Ignoring exotic definition: some.exotic.thing Another benefit: |
Oh, one more thing: for the string conversion ( |
Doh, “missing the obvious”, as it's called! 😄 Thanks a lot, I will try to apply your hints and report back! |
Thanks again, I got it working, with a few other minor adaptation! |
Hi, I spent some time trying to upgrade a script I wrote on top of
slimit
replacing it withcalmjs.parse
, but I failed to find an equivalent of slimit'sNode.to_ecma()
method.The script in question basically analyzes the arguments passed to every call of particular functions, to extract some info. It uses an
ASTVisitor
(this class) and when it finds a call to (say)Ext.define
it extracts some info from the parameters.I tried several approaches around
calmjs.parse.unparsers.es5
to obtain the complete name of aFunctionCall
identifier
, but I always end up with the whole subtree (that is, not just the name, but the whole function, body included) stringification...Can you give me an hint on how can I achieve that?
Thanks in advance!
The text was updated successfully, but these errors were encountered: