Skip to content

Commit

Permalink
Fix the rendering of callable arguments (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Sep 23, 2023
1 parent 36b2c34 commit e5b780e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
41 changes: 22 additions & 19 deletions sphinx_js/typedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,15 @@ def _path_segments(self, base_dir: str) -> list[str]:

class TypeLiteral(NodeBase):
kindString: Literal["Type literal"]
variant: Literal["declaration"]

signatures: list["Signature"] = []
indexSignature: "Signature | None" = None
children: Sequence["Member"] = []

def render(self, converter: Converter) -> Iterator[str | ir.TypeXRef]:
if self.signatures:
yield from self.signatures[0].type._render_name(converter)
yield from self.signatures[0].render(converter)
return
yield "{ "
index_sig = self.indexSignature
Expand Down Expand Up @@ -857,6 +859,23 @@ def _destructure_params(self) -> list[Param]:
params.extend(self._destructure_param(p))
return params

def render(self, converter: Converter) -> Iterator[str | ir.TypeXRef]:
yield "("

def inner(param: Param) -> Iterator[str | ir.TypeXRef]:
yield param.name + ": "
yield from param.type._render_name(converter)

yield from riffle((inner(param) for param in self.parameters), ", ")

yield "): "
ret = self.return_type(converter)[0].type
assert ret
if isinstance(ret, str):
yield ret
else:
yield from ret

def to_ir(
self, converter: Converter
) -> tuple[ir.Function | None, Sequence["Node"]]:
Expand Down Expand Up @@ -1000,24 +1019,8 @@ def _render_name_root(self, converter: Converter) -> Iterator[str | ir.TypeXRef]

if isinstance(self.declaration, Callable):
if self.declaration.kindString == "Constructor":
yield "{new ("
else:
yield "("
sig = self.declaration.signatures[0]

def inner(param: Param) -> Iterator[str | ir.TypeXRef]:
yield param.name + ": "
yield from param.type._render_name(converter)

yield from riffle((inner(param) for param in sig.parameters), ", ")

yield "): "
ret = sig.return_type(converter)[0].type
assert ret
if isinstance(ret, str):
yield ret
else:
yield from ret
yield "{new "
yield from self.declaration.signatures[0].render(converter)
if self.declaration.kindString == "Constructor":
yield "}"
return
Expand Down
7 changes: 7 additions & 0 deletions tests/test_typedoc_analysis/source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,10 @@ export function destructureTest2({
* @param options.b - The 'b' string.
*/
export function destructureTest3({ a, b }: { a: string; b: { c: string } }) {}

/**
* An example with a function as argument
*
* @param a - A number reducer.
*/
export function funcArg(a: (b: number, c: number) => number) {}
5 changes: 5 additions & 0 deletions tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,8 @@ def test_destructured(self):
obj = self.analyzer.get_object(["destructureTest3"])
assert obj.params[0].name == "options"
assert join_type(obj.params[0].type) == "{ a: string; b: { c: string; }; }"

def test_funcarg(self):
obj = self.analyzer.get_object(["funcArg"])
assert obj.params[0].name == "a"
assert join_type(obj.params[0].type) == "(b: number, c: number): number"

0 comments on commit e5b780e

Please sign in to comment.