From 775cdf432f5e4d496662a96713749b28481edaea Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sat, 30 Sep 2023 16:40:24 -0700 Subject: [PATCH] NFC Refactor populate_index (#102) This moves the logic about the id'd children of each type of Node onto the nodes themselves. --- sphinx_js/typedoc.py | 59 +++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index d7f8fb47..23c192f2 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -171,31 +171,7 @@ def _populate_index_inner( node.parent_member_properties = parent.member_properties() # Burrow into everything that could contain more ID'd items - children: list[Sequence[IndexType]] = [] - - children.append(node.children) - if isinstance(node, Accessor): - if node.getSignature: - children.append([node.getSignature]) - if node.setSignature: - children.append([node.setSignature]) - - if isinstance(node, (Callable, TypeLiteral)): - children.append(node.signatures) - - if isinstance(node, (Member, Param)) and isinstance(node.type, ReflectionType): - children.append([node.type.declaration]) - - if isinstance(node, Signature): - children.append(node.parameters) - children.append(node.typeParameter) - children.append(node.typeParameters) - - if isinstance(node, ClassOrInterface): - children.append(node.typeParameter) - children.append(node.typeParameters) - - for child in (c for l in children for c in l): + for child in node.children_with_ids(): self._populate_index_inner( child, parent=node, @@ -408,6 +384,9 @@ def member_properties(self) -> MemberProperties: def _path_segments(self, base_dir: str) -> list[str]: raise NotImplementedError + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + class Project(Base): # These are probably never present except "name" @@ -490,6 +469,13 @@ class Accessor(NodeBase): setSignature: "Signature | None" = None inheritedFrom: "ReferenceType | None" = None + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + if self.getSignature: + yield self.getSignature + if self.setSignature: + yield self.setSignature + @property def comment(self) -> Comment: if self.getSignature: @@ -553,6 +539,10 @@ def to_ir( ) -> tuple[ir.Function | None, Sequence["Node"]]: return callable_to_ir(self, converter) + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + yield from self.signatures + class ClassOrInterface(NodeBase): kindString: Literal["Class", "Interface"] @@ -562,6 +552,11 @@ class ClassOrInterface(NodeBase): typeParameter: list["TypeParameter"] = [] typeParameters: list["TypeParameter"] = [] + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + yield from self.typeParameter + yield from self.typeParameters + def _related_types( self, converter: Converter, @@ -674,6 +669,11 @@ class Member(NodeBase): type: "TypeD" inheritedFrom: "ReferenceType | None" = None + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + if isinstance(self.type, ReflectionType): + yield self.type.declaration + def to_ir( self, converter: Converter ) -> tuple[ir.Attribute | ir.Function | None, Sequence["Node"]]: @@ -709,6 +709,10 @@ class TypeLiteral(NodeBase): indexSignature: "Signature | None" = None children: Sequence["Member"] = [] + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.children + yield from self.signatures + @property def comment(self) -> Comment: if self.comment_ != DEFAULT_COMMENT: @@ -859,6 +863,11 @@ class Signature(TopLevelProperties): inheritedFrom: "ReferenceType | None" = None parent_member_properties: MemberProperties = {} # type: ignore[typeddict-item] + def children_with_ids(self) -> Iterator["IndexType"]: + yield from self.parameters + yield from self.typeParameter + yield from self.typeParameters + def _path_segments(self, base_dir: str) -> list[str]: return []