mozilla / sphinx-js

Autodoc-style extraction into Sphinx for your JS project
https://pypi.python.org/pypi/sphinx-js/
MIT License
282 stars 81 forks source link

Fix crashes caused by false assumptions about tsdoc output format #169

Open topiolli opened 3 years ago

topiolli commented 3 years ago

It was assumed that "sources" exists in each item of "signatures", but this is not always the case. The fix is to look up "sources" recursively in parent nodes.

It was also assumed that some node types have an "id", which isn't always true.

erikrose commented 3 years ago

It would be really helpful to have a test to demonstrate this and ensure we don't break it in the future. Do you have a small piece of code that exhibits these sources and id behaviors?

topiolli commented 3 years ago

@erikrose: I encountered this problem while producing documentation for a relatively small TS code base, but I'm not sure if I can pinpoint the exact cause of the failure. The file that seems to be the culprit is almost 900 lines, which I don't consider "small". I can send that file if you don't mind finding out the root cause yourself. I'm not familiar with the assumptions the sphinx-js code makes about the structure, so finding out what goes wrong would be hard to me. In fact, my "fix" can very well break something else. I just changed the code so that the problems went away. :smiley:

Now that I made changes to the aforementioned file I'm getting yet another crash. Do you have any insights on this:

Traceback (most recent call last):
  File "/home/topi/code/sphinx/sphinx/events.py", line 111, in emit
    results.append(listener.handler(self.app, *args))
  File "/home/topi/code/sphinx-js/sphinx_js/__init__.py", line 60, in analyze
    app._sphinxjs_analyzer = analyzer.from_disk(abs_source_paths,
  File "/home/topi/code/sphinx-js/sphinx_js/typedoc.py", line 40, in from_disk
    return cls(json, base_dir)
  File "/home/topi/code/sphinx-js/sphinx_js/typedoc.py", line 33, in __init__
    self._objects_by_path.add_many((obj.path.segments, obj) for obj in ir_objects)
  File "/home/topi/code/sphinx-js/sphinx_js/suffix_tree.py", line 46, in add_many
    self.add(segs, value)
  File "/home/topi/code/sphinx-js/sphinx_js/suffix_tree.py", line 31, in add
    tree[seg] = Value(value)
TypeError: 'Value' object does not support item assignment

Edit: hunted this one down. My class had a method called request which somehow broke sphinx-js.

erikrose commented 3 years ago

Hmm, I don't immediately see how the name of a method could make a difference. Are you saying that renaming it made the problem go away? The 'Value' object error means either our SuffixTree implementation is buggy, or our use of it is. If the former, I'm especially interested in fixing it. I'll gladly take your 900-line file! Do you want me to keep it a secret, or is it okay to attach to the ticket?

topiolli commented 3 years ago

I realized that it is not possible to run typedoc on a single file without its dependencies and I'm not able to ship the whole code base. I however attached the typedoc output file that failed to parse.

typedoc-output.json.txt

erikrose commented 3 years ago

Can you merge in from e16789fbe5a9edc6eafb96fafdf2929d2603825b and see if that gets you past the Value error? I sat down and rewrote the suffix tree, hoping that will stem the steady trickle of issues like that one.