From 0fafa6e5faecbe6f8a793da63d5f1c1ee7e071cf Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 2 May 2024 10:06:44 +0200 Subject: [PATCH] Give interfaces their own autosummary and automodule sections Prior to this, we crash if we run automodule on a module that exports an interface. This fix automodule for exported interfaces and adds a new section to automodule and autosummary for these. --- sphinx_js/ir.py | 3 ++- sphinx_js/js/convertTopLevel.ts | 2 +- sphinx_js/js/ir.ts | 2 +- sphinx_js/renderers.py | 4 +++- sphinx_js/typedoc.py | 1 + tests/test_build_ts/source/module.ts | 5 +++++ tests/test_build_ts/test_build_ts.py | 11 +++++++++++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/sphinx_js/ir.py b/sphinx_js/ir.py index 6ce1db26..c5c214ef 100644 --- a/sphinx_js/ir.py +++ b/sphinx_js/ir.py @@ -202,6 +202,7 @@ class Module: attributes: list["TopLevel"] = Factory(list) functions: list["Function"] = Factory(list) classes: list["Class"] = Factory(list) + interfaces: list["Interface"] = Factory(list) @define(slots=False) @@ -306,7 +307,7 @@ class Interface(TopLevel, _MembersAndSupers): """An interface, a la TypeScript""" type_params: list[TypeParam] = Factory(list) - kind: str = "classes" + kind: str = "interfaces" @define diff --git a/sphinx_js/js/convertTopLevel.ts b/sphinx_js/js/convertTopLevel.ts index 35a61d3d..dcb5a5aa 100644 --- a/sphinx_js/js/convertTopLevel.ts +++ b/sphinx_js/js/convertTopLevel.ts @@ -518,7 +518,7 @@ export class Converter { supers: this.relatedTypes(cls, "extendedTypes"), type_params: this.typeParamsToIR(cls.typeParameters), ...this.topLevelProperties(cls), - kind: "classes", + kind: "interfaces", }; return [result, cls.children]; } diff --git a/sphinx_js/js/ir.ts b/sphinx_js/js/ir.ts index 468c7781..c5fd68c5 100644 --- a/sphinx_js/js/ir.ts +++ b/sphinx_js/js/ir.ts @@ -134,7 +134,7 @@ export type _MembersAndSupers = { export type Interface = TopLevel & _MembersAndSupers & { type_params: TypeParam[]; - kind: "classes"; + kind: "interfaces"; }; export type Class = TopLevel & diff --git a/sphinx_js/renderers.py b/sphinx_js/renderers.py index 8a1c509c..f1cefd70 100644 --- a/sphinx_js/renderers.py +++ b/sphinx_js/renderers.py @@ -333,7 +333,7 @@ def rst_for(self, obj: TopLevel) -> str: renderer_class = AutoAttributeRenderer case Function(_): renderer_class = AutoFunctionRenderer - case Class(_): + case Class(_) | Interface(_): renderer_class = AutoClassRenderer case _: raise RuntimeError("This shouldn't happen...") @@ -676,6 +676,7 @@ def rst( # type:ignore[override] rst.append(self.rst_for_group(obj.attributes)) rst.append(self.rst_for_group(obj.functions)) rst.append(self.rst_for_group(obj.classes)) + rst.append(self.rst_for_group(obj.interfaces)) return "\n\n".join(["\n\n".join(r) for r in rst]) @@ -699,6 +700,7 @@ def rst_nodes(self) -> list[Node]: ("attributes", module.attributes), ("functions", module.functions), ("classes", module.classes), + ("interfaces", module.interfaces), ] pkgname = "".join(self._partial_path) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index e955b2e8..fbd3a431 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -169,4 +169,5 @@ def _create_modules(self, ir_objects: Sequence[ir.TopLevel]) -> Iterable[ir.Modu mod.attributes = sorted(mod.attributes, key=attrgetter("name")) mod.functions = sorted(mod.functions, key=attrgetter("name")) mod.classes = sorted(mod.classes, key=attrgetter("name")) + mod.interfaces = sorted(mod.interfaces, key=attrgetter("name")) return modules.values() diff --git a/tests/test_build_ts/source/module.ts b/tests/test_build_ts/source/module.ts index b4c29e1f..ccc046fc 100644 --- a/tests/test_build_ts/source/module.ts +++ b/tests/test_build_ts/source/module.ts @@ -36,3 +36,8 @@ export class Z { * Another thing. */ export const q = { a: "z29", b: 76 }; + +/** + * Interface documentation + */ +export interface I {} diff --git a/tests/test_build_ts/test_build_ts.py b/tests/test_build_ts/test_build_ts.py index ed61e396..0b97e98a 100644 --- a/tests/test_build_ts/test_build_ts.py +++ b/tests/test_build_ts/test_build_ts.py @@ -315,6 +315,14 @@ class module.Z(a, b) type: number Z.z() + + class module.I() + + Interface documentation + + *interface* + + *exported from* "module" """ ), ) @@ -412,3 +420,6 @@ def test_autosummary(self): classes = soup.find(class_="classes") assert classes.find(class_="summary").get_text() == "This is a summary." + + classes = soup.find(class_="interfaces") + assert classes.find(class_="summary").get_text() == "Interface documentation"