scottrogowski / code2flow

Pretty good call graphs for dynamic languages
MIT License
4.01k stars 296 forks source link

AttributeError: 'NoneType' object has no attribute 'get' #90

Open Pyrrha opened 1 year ago

Pyrrha commented 1 year ago

Hello,

For my first use, after successfully fighting with dependencies, I ran into this error:

Traceback (most recent call last):
  File "/Users/x/venv/bin/code2flow", line 8, in <module>
    sys.exit(main())
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/engine.py", line 874, in main
    level=level,
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/engine.py", line 737, in code2flow
    skip_parse_errors, lang_params)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/engine.py", line 478, in map_it
    file_group = make_file_group(file_ast_tree, source, extension)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/engine.py", line 359, in make_file_group
    file_group.add_node(language.make_root_node(body_trees, parent=file_group), is_root=True)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 372, in make_root_node
    calls = make_calls(lines)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 105, in make_calls
    for element in walk(body):
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 35, in walk
    ret += walk(el)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 42, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 35, in walk
    ret += walk(el)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 40, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 40, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 42, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 35, in walk
    ret += walk(el)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 40, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 42, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 35, in walk
    ret += walk(el)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 42, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 35, in walk
    ret += walk(el)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 40, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 42, in walk
    ret += walk(v)
  File "/Users/x/venv/lib/python3.7/site-packages/code2flow/javascript.py", line 33, in walk
    if el.get('type'):
AttributeError: 'NoneType' object has no attribute 'get'

Steps to reproduce:

I used a collection of JS files, separated into multiple folders. As the code stated, I suspect a bug inside the tool itself. Is there anything to fix this?

Thanks

bbailleux commented 8 months ago

Hello @Pyrrha

I've just faced the same problem and the only help found so far is your own unanswered question!

I have had a look to the …/code2flow/javascript.py file, and it appears that the related line (n 33) makes a test that may not be protected enough.

I just tried to replace it as follow:

--- /[…]/lib/python3.10/site-packages/code2flow/javascript.py.original  2024-03-05 14:49:51.895071571 +0100
+++ /[…]/lib/python3.10/site-packages/code2flow/javascript.py   2024-03-05 14:42:55.807765683 +0100
@@ -31,5 +31,5 @@ def walk(tree):
     if type(tree) == list:
         for el in tree:
-            if el.get('type'):
+            if el and el.get('type'):
                 ret.append(el)
                 ret += walk(el)

(just adds a test to check if el could be equal to None…)

By the way, that fixed the problem for me in some cases, but code2flow still crashes in some other cases.

Hope this helps.

Pyrrha commented 8 months ago

Hello @bbailleux,

Thanks for your help, I did not use this tool for my need since I opened this issue. However I'll keep your answer if I use this tool in the future 😄

You maybe should open a PR to share your fix for the others?

Thanks!