Skip to content

Commit

Permalink
Move the way that we render extends bound on type parameter (#154)
Browse files Browse the repository at this point in the history
It didn't work correctly before because the description list label does not
render rst.
  • Loading branch information
hoodmane authored May 6, 2024
1 parent 0ac271e commit f0a1dc6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
5 changes: 3 additions & 2 deletions sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
7 changes: 5 additions & 2 deletions tests/test_build_ts/source/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export class A {
}
}

export class Z {
x: number;
/**
* @typeParam T Description of T
*/
export class Z<T extends A> {
x: T;
constructor(a: number, b: number) {}

z() {}
Expand Down
22 changes: 20 additions & 2 deletions tests/test_build_ts/test_build_ts.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -303,14 +303,17 @@ class module.Z(a, b)
*exported from* "module"
Type parameters:
**T** -- Description of T (extends "A()")
Arguments:
* **a** (number)
* **b** (number)
Z.x
type: number
type: T
Z.z()
Expand Down Expand Up @@ -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")

Expand Down
5 changes: 3 additions & 2 deletions tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit f0a1dc6

Please sign in to comment.