Skip to content

Commit

Permalink
Fix documentation for getters and setters (#77)
Browse files Browse the repository at this point in the history
When documenting a get/set pair, take the documentation comment from the getter.
If for some weird reason there is only a setter, take it from the setter I
guess?? This makes `comment` into a getter so that we can automatically pull a
comment from the children of a class when appropriate
  • Loading branch information
hoodmane authored Sep 28, 2023
1 parent cf07822 commit 00c17fb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
27 changes: 24 additions & 3 deletions sphinx_js/typedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ class TopLevelPropertiesDict(TypedDict):
class TopLevelProperties(Base):
name: str
kindString: str
comment: Comment = Field(default_factory=Comment)
comment_: Comment = Field(default_factory=Comment, alias="comment")

@property
def comment(self) -> Comment:
return self.comment_

def short_name(self) -> str:
"""Overridden by Modules and Namespaces to strip quotes."""
Expand Down Expand Up @@ -473,6 +477,14 @@ class Accessor(NodeBase):
getSignature: "Signature | None" = None
setSignature: "Signature | None" = None

@property
def comment(self) -> Comment:
if self.getSignature:
return self.getSignature.comment
if self.setSignature:
return self.setSignature.comment
return self.comment_

def to_ir(self, converter: Converter) -> tuple[ir.Attribute, Sequence["Node"]]:
if self.getSignature:
# There's no signature to speak of for a getter: only a return type.
Expand Down Expand Up @@ -731,7 +743,11 @@ class TypeParameter(Base):
kindString: Literal["Type parameter"]
name: str
type: "OptionalTypeD"
comment: Comment = Field(default_factory=Comment)
comment_: Comment = Field(default_factory=Comment, alias="comment")

@property
def comment(self) -> Comment:
return self.comment_

def to_ir(self, converter: Converter) -> ir.TypeParam:
extends = None
Expand All @@ -747,7 +763,12 @@ def _path_segments(self, base_dir: str) -> list[str]:

class Param(Base):
kindString: Literal["Parameter"] = "Parameter"
comment: Comment = Field(default_factory=Comment)
comment_: Comment = Field(default_factory=Comment, alias="comment")

@property
def comment(self) -> Comment:
return self.comment_

defaultValue: str | None
flags: Flags
name: str
Expand Down
15 changes: 15 additions & 0 deletions tests/test_build_ts/source/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ export function weirdCodeInDescription() {
export function spinxLinkInDescription() {

}


export class GetSetDocs {
/**
* Getter with comment
*/
get a() {
return 7;
}

/**
* Setter with comment
*/
set b(x) {}
}
2 changes: 2 additions & 0 deletions tests/test_build_ts/source/docs/getset.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. js:autoclass:: GetSetDocs
:members:
24 changes: 24 additions & 0 deletions tests/test_build_ts/test_build_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ def test_predicate(self):
),
)

def test_get_set(self):
self._file_contents_eq(
"getset",
dedent(
"""\
class GetSetDocs()
*exported from* "class"
GetSetDocs.a
**type:** number
Getter with comment
GetSetDocs.b
**type:** any
Setter with comment
"""
),
)


class TestHtmlBuilder(SphinxBuildTestCase):
"""Tests which require an HTML build of our Sphinx tree, for checking
Expand Down
2 changes: 1 addition & 1 deletion tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class TestPathSegments(TypeDocTestCase):
def commented_object(self, comment, **kwargs):
"""Return the object from ``json`` having the given comment short-text."""
comment = Comment(summary=[DescriptionItem(kind="text", text=comment)])
return dict_where(self.json, comment=comment, **kwargs)
return dict_where(self.json, comment_=comment, **kwargs)

def commented_object_path(self, comment, **kwargs):
"""Return the path segments of the object with the given comment."""
Expand Down

0 comments on commit 00c17fb

Please sign in to comment.