From 07eb00c0b93244f12b48664f7488264b90d7f0ba Mon Sep 17 00:00:00 2001 From: Sebastian Mohr Date: Fri, 3 Dec 2021 17:21:21 +0100 Subject: [PATCH 1/5] Recursive search for node sources. Fixed #166 for me --- sphinx_js/typedoc.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index a6315d1e..9543239e 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -194,7 +194,15 @@ def _convert_node(self, node) -> Tuple[TopLevel, List[dict]]: # many attr of Functions. sigs = node.get('signatures') first_sig = sigs[0] # Should always have at least one - first_sig['sources'] = node['sources'] + def rec_helper(node): + if "sources" in node["__parent"]: + return node["__parent"]["sources"] + else: + rec_helper(node["__parent"]) + if "sources" in node: + first_sig['sources'] = node['sources'] + else: + first_sig['sources'] = rec_helper(node) return self._convert_node(first_sig) elif kind in ['Call signature', 'Constructor signature']: # This is the real meat of a function, method, or constructor. From ebf7597d1eff4d802ca6bfd80f068bde52e2c50e Mon Sep 17 00:00:00 2001 From: Fox Danger Piacenti Date: Tue, 8 Feb 2022 23:58:46 -0600 Subject: [PATCH 2/5] Fix name derivation for strange self-referencing case. Add ability to recognize literals as a type for TypeScript. --- .gitignore | 2 ++ sphinx_js/typedoc.py | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 3b19bf13..73d9e124 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ sphinx_js.egg-info/ .vscode .DS_Store venv +# Pyenv +.python-version diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index 9543239e..5d8bb3ef 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -198,7 +198,7 @@ def rec_helper(node): if "sources" in node["__parent"]: return node["__parent"]["sources"] else: - rec_helper(node["__parent"]) + return rec_helper(node["__parent"]) if "sources" in node: first_sig['sources'] = node['sources'] else: @@ -252,8 +252,12 @@ def _type_name(self, type): type_of_type = type.get('type') if type_of_type == 'reference' and type.get('id'): - node = self._index[type['id']] - name = node['name'] + try: + node = self._index[type['id']] + name = node['name'] + except KeyError: + # Try to get the name directly from the type, in one last attempt. + name = type['name'] elif type_of_type == 'unknown': if re.match(r'-?\d*(\.\d+)?', type['name']): # It's a number. # TypeDoc apparently sticks numeric constants' values into the @@ -283,10 +287,15 @@ def _type_name(self, type): if constraint is not None: name += ' extends ' + self._type_name(constraint) # e.g. K += extends + keyof T + elif type_of_type == 'literal': + if type['value'] is None: + name = 'null' + else: + name = repr(type['value']) elif type_of_type == 'reflection': name = '' - # test_generic_member() (currently skipped) tests this. else: + # test_generic_member() (currently skipped) tests this. name = '' type_args = type.get('typeArguments') @@ -336,6 +345,8 @@ def typedoc_output(abs_source_paths, sphinx_conf_dir, config_path): with NamedTemporaryFile(mode='w+b') as temp: command.add('--json', temp.name, *abs_source_paths) try: + from pprint import pprint + pprint(command.make()) subprocess.call(command.make()) except OSError as exc: if exc.errno == ENOENT: From 615e8351988f0924f267a47d3b9b064fc5a3040e Mon Sep 17 00:00:00 2001 From: Fox Piacenti Date: Mon, 14 Mar 2022 12:58:37 -0500 Subject: [PATCH 3/5] Simplify parent traversal in Typedoc type identifier Change the recursive function call to a while loop. Co-authored-by: Tavian Barnes --- sphinx_js/typedoc.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index 5d8bb3ef..249cf840 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -194,15 +194,10 @@ def _convert_node(self, node) -> Tuple[TopLevel, List[dict]]: # many attr of Functions. sigs = node.get('signatures') first_sig = sigs[0] # Should always have at least one - def rec_helper(node): - if "sources" in node["__parent"]: - return node["__parent"]["sources"] - else: - return rec_helper(node["__parent"]) - if "sources" in node: - first_sig['sources'] = node['sources'] - else: - first_sig['sources'] = rec_helper(node) + parent = node + while 'sources' not in node and '__parent' in node: + parent = node['__parent'] + first_sig['sources'] = parent['sources'] return self._convert_node(first_sig) elif kind in ['Call signature', 'Constructor signature']: # This is the real meat of a function, method, or constructor. From 5c3067967552900b14c558a34928deacec8eb320 Mon Sep 17 00:00:00 2001 From: Fox Danger Piacenti Date: Mon, 14 Mar 2022 13:08:51 -0500 Subject: [PATCH 4/5] Remove accidentally committed debugging code. --- sphinx_js/typedoc.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index 249cf840..c0b895e1 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -340,8 +340,6 @@ def typedoc_output(abs_source_paths, sphinx_conf_dir, config_path): with NamedTemporaryFile(mode='w+b') as temp: command.add('--json', temp.name, *abs_source_paths) try: - from pprint import pprint - pprint(command.make()) subprocess.call(command.make()) except OSError as exc: if exc.errno == ENOENT: From 0d555c7748e99c58a3553e4ea2dc0ccb4219ef1d Mon Sep 17 00:00:00 2001 From: Fox Piacenti Date: Mon, 14 Mar 2022 13:39:50 -0500 Subject: [PATCH 5/5] fix: infinite loop in typedoc parent traversal resolved Co-authored-by: Tavian Barnes --- sphinx_js/typedoc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index c0b895e1..5cd0dd70 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -195,8 +195,8 @@ def _convert_node(self, node) -> Tuple[TopLevel, List[dict]]: sigs = node.get('signatures') first_sig = sigs[0] # Should always have at least one parent = node - while 'sources' not in node and '__parent' in node: - parent = node['__parent'] + while 'sources' not in parent and '__parent' in parent: + parent = parent['__parent'] first_sig['sources'] = parent['sources'] return self._convert_node(first_sig) elif kind in ['Call signature', 'Constructor signature']: