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 a weird error when pointing js_source_paths directly to TypeScript files #135

Closed reuben closed 3 years ago

reuben commented 4 years ago

I'm not super confident in this fix, but it passes existing tests, so maybe it's fine :P

The problem here happens when specifying TypeScript source files directly in js_source_paths instead of a folder. The added test is a copy of the existing test_build_ts, but in conf.py I specify js_source_path = '../class.ts'. This is enough to trigger the problem.

The resulting doclets confuse the path parsing logic. Here's an example doclet and error from the project I was working on when I ran into this:

{'kind': 'module', 'comment': '<empty>', 'meta': {'path': './', 'filename': 'index.ts', 'lineno': 1, 'code': {}}, 'name': '"index"', 'longname': 'module:index', 'description': ''}

And error (modified to show larger context):

# Sphinx version: 2.2.0
# Python version: 3.7.4 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.1
# Last messages:

# Loaded extensions:
Traceback (most recent call last):
  File "/venv/lib/python3.7/site-packages/sphinx/cmd/build.py", line 275, in build_main
    args.tags, args.verbosity, args.jobs, args.keep_going)
  File "/venv/lib/python3.7/site-packages/sphinx/application.py", line 269, in __init__
    self._init_builder()
  File "/venv/lib/python3.7/site-packages/sphinx/application.py", line 330, in _init_builder
    self.events.emit('builder-inited')
  File "/venv/lib/python3.7/site-packages/sphinx/events.py", line 103, in emit
    results.append(callback(self.app, *args))
  File "/venv/lib/python3.7/site-packages/sphinx_js/doclets.py", line 45, in gather_doclets
    doclet_full_path(d, root_for_relative_paths),
  File "/venv/lib/python3.7/site-packages/sphinx_js/doclets.py", line 219, in doclet_full_path
    path_and_formal_params['path'].parse(path))
  File "/venv/lib/python3.7/site-packages/parsimonious/expressions.py", line 110, in parse
    node = self.match(text, pos=pos)
  File "/venv/lib/python3.7/site-packages/parsimonious/expressions.py", line 127, in match
    raise error
parsimonious.exceptions.ParseError: Rule 'path' didn't match at '../../../doc/index.module:index' (line 1, column 1).