Skip to content

Commit

Permalink
Label readonly properties in the docs (mozilla#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored May 6, 2024
1 parent b689246 commit 36bf56d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions sphinx_js/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class Attribute(TopLevel, _Member):

#: The type this property's value can have
type: Type
readonly: bool = False
kind: Literal["attribute"] = "attribute"


Expand Down
7 changes: 5 additions & 2 deletions sphinx_js/js/convertTopLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ export class Converter {
const result: Attribute = {
...this.memberProps(v),
...this.topLevelProperties(v),
readonly: false,
kind: "attribute",
type,
};
Expand Down Expand Up @@ -542,12 +543,12 @@ export class Converter {
} else {
type = this.convertType(prop.type!);
}
// TODO: add a readonly indicator if it's readonly
const result: Attribute = {
type,
...this.memberProps(prop),
...this.topLevelProperties(prop),
description: renderCommentSummary(prop.comment),
readonly: prop.flags.isReadonly,
kind: "attribute",
};
return [result, prop.children];
Expand Down Expand Up @@ -581,9 +582,11 @@ export class Converter {
sig = prop.setSignature;
type = sig.parameters![0].type!;
}
// TODO: add a readonly indicator if there's no setter.
// If there's no setter say it's readonly
const readonly = !prop.setSignature;
const result: Attribute = {
type: this.convertType(type),
readonly,
...this.memberProps(prop),
...this.topLevelProperties(prop),
kind: "attribute",
Expand Down
1 change: 1 addition & 0 deletions sphinx_js/js/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export type TopLevel = {
export type Attribute = TopLevel &
Member & {
type: Type;
readonly: boolean;
kind: "attribute";
};

Expand Down
5 changes: 4 additions & 1 deletion sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,11 @@ class AutoAttributeRenderer(JsRenderer):

def _template_vars(self, name: str, obj: Attribute | TypeAlias) -> dict[str, Any]: # type: ignore[override]
is_optional = False
ty = self.render_type(obj.type)
if isinstance(obj, Attribute):
is_optional = obj.is_optional
if obj.readonly:
ty = "readonly " + ty
type_params = ""
is_type_alias = isinstance(obj, TypeAlias)
fields: Iterator[tuple[list[str], str]] = iter([])
Expand All @@ -675,7 +678,7 @@ def _template_vars(self, name: str, obj: Attribute | TypeAlias) -> dict[str, Any
is_optional=is_optional,
see_also=obj.see_alsos,
examples=[render_description(ex) for ex in obj.examples],
type=self.render_type(obj.type),
type=ty,
content="\n".join(self._content),
)

Expand Down
5 changes: 4 additions & 1 deletion tests/test_build_ts/source/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export function weirdCodeInDescription() {}
export function spinxLinkInDescription() {}

export class GetSetDocs {
readonly x: number;
y: number;

/**
* Getter with comment
*/
Expand All @@ -119,7 +122,7 @@ export class GetSetDocs {
/**
* Setter with comment
*/
set b(x) {}
set b(x: number) {}
}

export class Base {
Expand Down
26 changes: 17 additions & 9 deletions tests/test_build_ts/test_build_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,29 @@ def test_get_set(self):
"getset",
dedent(
"""\
class GetSetDocs()
class GetSetDocs()
*exported from* "class"
*exported from* "class"
GetSetDocs.a
type: readonly number
Getter with comment
GetSetDocs.b
GetSetDocs.a
type: number
type: number
Setter with comment
Getter with comment
GetSetDocs.x
GetSetDocs.b
type: readonly number
type: any
GetSetDocs.y
Setter with comment
type: number
"""
),
)
Expand All @@ -224,7 +232,7 @@ class Base()
Base.a
type: number
type: readonly number
Base.f()
Expand Down
1 change: 1 addition & 0 deletions tests/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_missing_default():
is_optional=False,
is_static=False,
is_private=False,
readonly=False,
type=[]
)
attr_dict = {k: v for k, v in getmembers(attribute_base) if not k.startswith("_")}
Expand Down

0 comments on commit 36bf56d

Please sign in to comment.