Skip to content

Commit

Permalink
NFC Refactor populate_index (#102)
Browse files Browse the repository at this point in the history
This moves the logic about the id'd children of each type of Node onto the nodes
themselves.
  • Loading branch information
hoodmane authored Sep 30, 2023
1 parent 3c02e36 commit 775cdf4
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions sphinx_js/typedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand All @@ -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,
Expand Down Expand Up @@ -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"]]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 []

Expand Down

0 comments on commit 775cdf4

Please sign in to comment.