Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeXRefIntrinsic wrapper for intrinsic types #86

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sphinx_js/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class TypeXRef:
name: str


@define
class TypeXRefIntrinsic(TypeXRef):
pass


@define
class TypeXRefInternal(TypeXRef):
path: list[str]
Expand Down
2 changes: 1 addition & 1 deletion sphinx_js/typedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ class IntrinsicType(TypeBase):
name: str

def _render_name_root(self, converter: Converter) -> Iterator[str | ir.TypeXRef]:
yield self.name
yield ir.TypeXRefIntrinsic(self.name)


class ReferenceType(TypeBase):
Expand Down
42 changes: 28 additions & 14 deletions tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TypeXRef,
TypeXRefExternal,
TypeXRefInternal,
TypeXRefIntrinsic,
)
from sphinx_js.renderers import AutoClassRenderer, AutoFunctionRenderer
from sphinx_js.typedoc import Comment, Converter, DescriptionItem, parse
Expand Down Expand Up @@ -307,20 +308,23 @@ def test_function(self):
description=[DescriptionText("Some number")],
has_default=True,
is_variadic=False,
type=["number"],
type=[TypeXRefIntrinsic("number")],
default="1",
),
Param(
name="b",
description=[DescriptionText("Some strings")],
has_default=False,
is_variadic=True,
type=["string", "[]"],
type=[TypeXRefIntrinsic("string"), "[]"],
),
]
assert func.exceptions == []
assert func.returns == [
Return(type=["number"], description=[DescriptionText("The best number")])
Return(
type=[TypeXRefIntrinsic("number")],
description=[DescriptionText("The best number")],
)
]

def test_constructor(self):
Expand Down Expand Up @@ -376,14 +380,14 @@ def test_getter(self):
types."""
getter = self.analyzer.get_object(["gettable"])
assert isinstance(getter, Attribute)
assert getter.type == ["number"]
assert getter.type == [TypeXRefIntrinsic("number")]

def test_setter(self):
"""Test that we represent setters as Attributes and find the type of
their 1 param."""
setter = self.analyzer.get_object(["settable"])
assert isinstance(setter, Attribute)
assert setter.type == ["string"]
assert setter.type == [TypeXRefIntrinsic("string")]


class TestTypeName(TypeDocAnalyzerTestCase):
Expand Down Expand Up @@ -426,7 +430,7 @@ def test_interface_readonly_member(self):
obj = self.analyzer.get_object(["Interface"])
read_only_num = obj.members[0]
assert read_only_num.name == "readOnlyNum"
assert read_only_num.type == ["number"]
assert read_only_num.type == [TypeXRefIntrinsic("number")]

def test_array(self):
"""Make sure array types are rendered correctly.
Expand All @@ -436,7 +440,7 @@ def test_array(self):

"""
obj = self.analyzer.get_object(["overload"])
assert obj.params[0].type == ["string", "[]"]
assert obj.params[0].type == [TypeXRefIntrinsic("string"), "[]"]

def test_literal_types(self):
"""Make sure a thing of a named literal type has that type name
Expand All @@ -452,9 +456,9 @@ def test_unions(self):
"""Make sure unions get rendered properly."""
obj = self.analyzer.get_object(["union"])
assert obj.type == [
"number",
TypeXRefIntrinsic("number"),
" | ",
"string",
TypeXRefIntrinsic("string"),
" | ",
TypeXRefInternal(name="Color", path=["./", "types.", "Color"]),
]
Expand Down Expand Up @@ -523,13 +527,14 @@ def test_constrained_by_key(self):
a = AutoFunctionRenderer.__new__(AutoFunctionRenderer)
a._add_span = False
a._set_type_text_formatter(None)
a._set_type_xref_formatter(None)
a._explicit_formal_params = None
a._content = []
rst = a.rst([obj.name], obj)
rst = rst.replace("\\", "").replace(" ", " ")
assert ":typeparam T: The type of the object" in rst
assert (
":typeparam K extends string \\| number \\| symbol: The type of the key"
in rst
":typeparam K extends string | number | symbol: The type of the key" in rst
)

def test_class_constrained(self):
Expand All @@ -544,12 +549,14 @@ def test_class_constrained(self):
)
a = AutoClassRenderer.__new__(AutoClassRenderer)
a._set_type_text_formatter(None)
a._set_type_xref_formatter(None)
a._explicit_formal_params = None
a._add_span = False
a._content = []
a._options = {}
rst = a.rst([obj.name], obj)
assert ":typeparam S extends number\\[\\]: The type we contain" in rst
rst = rst.replace("\\ ", "").replace("\\", "").replace(" ", " ")
assert ":typeparam S extends number[]: The type we contain" in rst

def test_constrained_by_constructor(self):
"""Make sure ``new ()`` expressions and, more generally, per-property
Expand All @@ -567,14 +574,21 @@ def test_utility_types(self):
assert t == [
TypeXRefExternal("Partial", "typescript", "xxx", "Partial"),
"<",
"string",
TypeXRefIntrinsic("string"),
">",
]

def test_constrained_by_property(self):

obj = self.analyzer.get_object(["objProps"])
assert obj.params[0].type == ["{ ", "label", ": ", "string", "; ", "}"]
assert obj.params[0].type == [
"{ ",
"label",
": ",
TypeXRefIntrinsic("string"),
"; ",
"}",
]
assert (
join_type(obj.params[1].type) == "{ [key: number]: string; label: string; }"
)
Expand Down
Loading