From 763db092cef3aacd463f55fc3a385d5a4b99ec08 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 6 May 2024 15:25:12 +0200 Subject: [PATCH 1/3] Move the way that we render extends bound on type parameter It didn't work correctly before because the description list label does not render rst. --- sphinx_js/renderers.py | 5 +++-- tests/test_build_ts/source/module.ts | 7 ++++-- tests/test_build_ts/test_build_ts.py | 22 +++++++++++++++++-- .../test_typedoc_analysis.py | 5 +++-- tests/testing.py | 1 + 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/sphinx_js/renderers.py b/sphinx_js/renderers.py index 12bc0ef7..f722dad3 100644 --- a/sphinx_js/renderers.py +++ b/sphinx_js/renderers.py @@ -467,10 +467,11 @@ def _return_formatter(self, return_: Return) -> tuple[list[str], str]: def _type_param_formatter(self, tparam: TypeParam) -> tuple[list[str], str] | None: v = tparam.name + descr = render_description(tparam.description) if tparam.extends: - v += " extends " + self.render_type(tparam.extends) + descr += " (extends " + self.render_type(tparam.extends) + ")" heads = ["typeparam", v] - return heads, render_description(tparam.description) + return heads, descr def _param_formatter(self, param: Param) -> tuple[list[str], str] | None: """Derive heads and tail from ``@param`` blocks.""" diff --git a/tests/test_build_ts/source/module.ts b/tests/test_build_ts/source/module.ts index 3b49ab04..71256aca 100644 --- a/tests/test_build_ts/source/module.ts +++ b/tests/test_build_ts/source/module.ts @@ -25,8 +25,11 @@ export class A { } } -export class Z { - x: number; +/** + * @typeParam T Description of T + */ +export class Z { + x: T; constructor(a: number, b: number) {} z() {} diff --git a/tests/test_build_ts/test_build_ts.py b/tests/test_build_ts/test_build_ts.py index 362aea56..5bacd1f6 100644 --- a/tests/test_build_ts/test_build_ts.py +++ b/tests/test_build_ts/test_build_ts.py @@ -1,6 +1,6 @@ from textwrap import dedent -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup, Tag from conftest import TYPEDOC_VERSION from tests.testing import SphinxBuildTestCase @@ -303,6 +303,9 @@ class module.Z(a, b) *exported from* "module" + Type parameters: + **T** -- (extends "A()") + Arguments: * **a** (number) @@ -310,7 +313,7 @@ class module.Z(a, b) Z.x - type: number + type: T Z.z() @@ -342,6 +345,21 @@ def test_implements_links(self): "autoclass_class_with_interface_and_supers" ) + def test_extends_type_param_links(self): + """Make sure implemented interfaces link to their definitions.""" + soup = BeautifulSoup(self._file_contents("automodule"), "html.parser") + z = soup.find(id="module.Z") + assert z + assert z.parent + t = z.parent.find_all(class_="sphinx_js-type") + s: Tag = t[0] + href: Tag = list(s.children)[0] + assert href.name == "a" + assert href.get_text() == "A()" + assert href.attrs["class"] == ["reference", "internal"] + assert href.attrs["title"] == "module.A" + assert href.attrs["href"] == "#module.A" + def test_xrefs(self): soup = BeautifulSoup(self._file_contents("xrefs"), "html.parser") diff --git a/tests/test_typedoc_analysis/test_typedoc_analysis.py b/tests/test_typedoc_analysis/test_typedoc_analysis.py index 32b96a0c..acc7a367 100644 --- a/tests/test_typedoc_analysis/test_typedoc_analysis.py +++ b/tests/test_typedoc_analysis/test_typedoc_analysis.py @@ -501,7 +501,8 @@ def test_constrained_by_key(self): rst = rst.replace("\\", "").replace(" ", " ") assert ":typeparam T: The type of the object" in rst assert ( - ":typeparam K extends string | number | symbol: The type of the key" in rst + ":typeparam K: The type of the key (extends string | number | symbol)" + in rst ) def test_class_constrained(self): @@ -522,7 +523,7 @@ def test_class_constrained(self): a._options = {} rst = a.rst([obj.name], obj) rst = rst.replace("\\ ", "").replace("\\", "").replace(" ", " ") - assert ":typeparam S extends number[]: The type we contain" in rst + assert ":typeparam S: The type we contain (extends number[])" in rst def test_constrained_by_constructor(self): """Make sure ``new ()`` expressions and, more generally, per-property diff --git a/tests/testing.py b/tests/testing.py index 5ad64990..0e28931d 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -43,6 +43,7 @@ def setup_class(cls): @classmethod def teardown_class(cls): + return rmtree(join(cls.docs_dir, "_build")) def _file_contents(self, filename): From 51dc42b0ca6025c23ddac1cce3a332044470d9a7 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 6 May 2024 15:38:16 +0200 Subject: [PATCH 2/3] Fix test --- tests/test_build_ts/test_build_ts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_build_ts/test_build_ts.py b/tests/test_build_ts/test_build_ts.py index 5bacd1f6..cfa08971 100644 --- a/tests/test_build_ts/test_build_ts.py +++ b/tests/test_build_ts/test_build_ts.py @@ -304,7 +304,7 @@ class module.Z(a, b) *exported from* "module" Type parameters: - **T** -- (extends "A()") + **T** -- Description of T (extends "A()") Arguments: * **a** (number) From 8b621d7a9a4a0ad4f3588e819eb037c70d48794f Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 6 May 2024 15:40:53 +0200 Subject: [PATCH 3/3] Revert accidental change --- tests/testing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testing.py b/tests/testing.py index 0e28931d..5ad64990 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -43,7 +43,6 @@ def setup_class(cls): @classmethod def teardown_class(cls): - return rmtree(join(cls.docs_dir, "_build")) def _file_contents(self, filename):