Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the way that we render extends bound on type parameter #154

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading