From 5727c5d6fbd681b037f5196d58c6c2dcd7bef4d2 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 3 May 2024 16:00:33 +0200 Subject: [PATCH] NFC Use singular kind strings I think this looks a little neater. --- sphinx_js/ir.py | 8 ++++---- sphinx_js/js/convertTopLevel.ts | 12 ++++++------ sphinx_js/js/ir.ts | 8 ++++---- sphinx_js/typedoc.py | 8 +++++++- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/sphinx_js/ir.py b/sphinx_js/ir.py index c5c214ef..d12d1993 100644 --- a/sphinx_js/ir.py +++ b/sphinx_js/ir.py @@ -273,7 +273,7 @@ class Attribute(TopLevel, _Member): #: The type this property's value can have type: Type - kind: str = "attributes" + kind: Literal["attribute"] = "attribute" @define @@ -285,7 +285,7 @@ class Function(TopLevel, _Member): exceptions: list[Exc] returns: list[Return] type_params: list[TypeParam] = Factory(list) - kind: str = "functions" + kind: Literal["function"] = "function" @define @@ -307,7 +307,7 @@ class Interface(TopLevel, _MembersAndSupers): """An interface, a la TypeScript""" type_params: list[TypeParam] = Factory(list) - kind: str = "interfaces" + kind: Literal["interface"] = "interface" @define @@ -324,7 +324,7 @@ class Class(TopLevel, _MembersAndSupers): # `undocumented: True` doclet and so are presently filtered out. But we do # have the space to include them someday. type_params: list[TypeParam] = Factory(list) - kind: str = "classes" + kind: Literal["class"] = "class" TopLevelUnion = Class | Interface | Function | Attribute diff --git a/sphinx_js/js/convertTopLevel.ts b/sphinx_js/js/convertTopLevel.ts index dcb5a5aa..45269e2e 100644 --- a/sphinx_js/js/convertTopLevel.ts +++ b/sphinx_js/js/convertTopLevel.ts @@ -471,7 +471,7 @@ export class Converter { const result: Attribute = { ...this.memberProps(v), ...this.topLevelProperties(v), - kind: "attributes", + kind: "attribute", type, }; return [result, v.children]; @@ -506,7 +506,7 @@ export class Converter { interfaces: this.relatedTypes(cls, "implementedTypes"), type_params: this.typeParamsToIR(cls.typeParameters), ...this.topLevelProperties(cls), - kind: "classes", + kind: "class", }; return [result, cls.children]; } @@ -518,7 +518,7 @@ export class Converter { supers: this.relatedTypes(cls, "extendedTypes"), type_params: this.typeParamsToIR(cls.typeParameters), ...this.topLevelProperties(cls), - kind: "interfaces", + kind: "interface", }; return [result, cls.children]; } @@ -548,7 +548,7 @@ export class Converter { ...this.memberProps(prop), ...this.topLevelProperties(prop), description: renderCommentSummary(prop.comment), - kind: "attributes", + kind: "attribute", }; return [result, prop.children]; } @@ -586,7 +586,7 @@ export class Converter { type: this.renderType(type), ...this.memberProps(prop), ...this.topLevelProperties(prop), - kind: "attributes", + kind: "attribute", }; result.description = renderCommentSummary(sig.comment); return [result, prop.children]; @@ -854,7 +854,7 @@ export class Converter { type_params, returns, exceptions: [], - kind: "functions", + kind: "function", }; } typeParamsToIR( diff --git a/sphinx_js/js/ir.ts b/sphinx_js/js/ir.ts index c5fd68c5..d38813de 100644 --- a/sphinx_js/js/ir.ts +++ b/sphinx_js/js/ir.ts @@ -113,7 +113,7 @@ export type TopLevel = { export type Attribute = TopLevel & Member & { type: Type; - kind: "attributes"; + kind: "attribute"; }; export type IRFunction = TopLevel & @@ -122,7 +122,7 @@ export type IRFunction = TopLevel & params: Param[]; returns: Return[]; type_params: TypeParam[]; - kind: "functions"; + kind: "function"; exceptions: never[]; }; @@ -134,7 +134,7 @@ export type _MembersAndSupers = { export type Interface = TopLevel & _MembersAndSupers & { type_params: TypeParam[]; - kind: "interfaces"; + kind: "interface"; }; export type Class = TopLevel & @@ -143,7 +143,7 @@ export type Class = TopLevel & is_abstract: boolean; interfaces: Type[]; type_params: TypeParam[]; - kind: "classes"; + kind: "class"; }; export type TopLevelIR = Attribute | IRFunction | Class | Interface; diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index fbd3a431..5df3f37f 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -154,6 +154,12 @@ def _create_modules(self, ir_objects: Sequence[ir.TopLevel]) -> Iterable[ir.Modu summary section. Skip docs labeled as "@private". """ modules = {} + singular_kind_to_plural_kind = { + "class": "classes", + "interface": "interfaces", + "function": "functions", + "attribute": "attributes", + } for obj, path, kind in self._get_toplevel_objects(ir_objects): pathparts = path.split("/") for i in range(len(pathparts) - 1): @@ -163,7 +169,7 @@ def _create_modules(self, ir_objects: Sequence[ir.TopLevel]) -> Iterable[ir.Modu filename=path, deppath=path, path=ir.Pathname(pathparts), line=1 ) mod = modules[path] - getattr(mod, kind).append(obj) + getattr(mod, singular_kind_to_plural_kind[kind]).append(obj) for mod in modules.values(): mod.attributes = sorted(mod.attributes, key=attrgetter("name"))