scrapinghub / js2xml

Convert Javascript code to an XML document
MIT License
186 stars 23 forks source link

Issue while parsing js code #53

Open r0oth3x49 opened 3 years ago

r0oth3x49 commented 3 years ago

first of all thank you so much for creating such a great project I 'm facing an issue while trying to parse the attach js code. it throws an NoneType Exception, the js code is attached below.

javascript code which causes the error

Code to reproduce the error

>>> import js2xml
>>> p = js2xml.parse('new Error')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/env/lib/python3.9/site-packages/js2xml/__init__.py", line 18, in parse
    xml = _visitor.visit(tree)
...
  File "/tmp/env/lib/python3.9/site-packages/js2xml/xmlvisitor.py", line 402, in visit_NewExpr
    for arg in node.args:
TypeError: 'NoneType' object is not iterable
>>> 

what i did is simply added an if conditioni in xmlvisitor on line number 402 and it fixed the above error but i don't know if it's really a good fix.

def visit_NewExpr(self, node):
        newel = E.new()
        newel.extend(self.visit(node.identifier))
        arguments = E.arguments()
        if node.args: # check if none before starting a loop as loop can't be run on NoneType objects
            for arg in node.args:
                arguments.extend(self.visit(arg))
            newel.append(arguments)
        return [newel]

Edit The issue i mistakenly created on calmjs/calmjs.parse#40 but he explained it very well.