From 560f64f2a8974dfe1f736a271fa6d597be6d922b Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 25 Sep 2023 18:09:04 -0700 Subject: [PATCH] Add support for predicate types (#71) --- sphinx_js/typedoc.py | 12 ++++++++++++ tests/test_build_ts/source/class.ts | 4 ++++ tests/test_build_ts/source/docs/predicate.rst | 1 + tests/test_build_ts/test_build_ts.py | 16 ++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 tests/test_build_ts/source/docs/predicate.rst diff --git a/sphinx_js/typedoc.py b/sphinx_js/typedoc.py index a170b92d..68fb1f40 100644 --- a/sphinx_js/typedoc.py +++ b/sphinx_js/typedoc.py @@ -1073,6 +1073,17 @@ def _render_name_root(self, converter: Converter) -> Iterator[str | ir.TypeXRef] yield self.name +class PredicateType(TypeBase): + type: Literal["predicate"] + name: str + targetType: "TypeD" + + def _render_name_root(self, converter: Converter) -> Iterator[str | ir.TypeXRef]: + yield self.name + yield " is " + yield from self.targetType.render_name(converter) + + AnyNode = Node | Project | Signature @@ -1086,6 +1097,7 @@ def _render_name_root(self, converter: Converter) -> Iterator[str | ir.TypeXRef] | ReferenceType | ReflectionType | TupleType + | PredicateType | UnknownType ) diff --git a/tests/test_build_ts/source/class.ts b/tests/test_build_ts/source/class.ts index 0624cddf..3d32758f 100644 --- a/tests/test_build_ts/source/class.ts +++ b/tests/test_build_ts/source/class.ts @@ -98,3 +98,7 @@ export class Iterable { yield 3; } } + +export function predicate(c): c is ConstructorlessClass { + return false; +} diff --git a/tests/test_build_ts/source/docs/predicate.rst b/tests/test_build_ts/source/docs/predicate.rst new file mode 100644 index 00000000..ce3a986b --- /dev/null +++ b/tests/test_build_ts/source/docs/predicate.rst @@ -0,0 +1 @@ +.. js:autofunction:: predicate diff --git a/tests/test_build_ts/test_build_ts.py b/tests/test_build_ts/test_build_ts.py index 43fc4390..f0271d8d 100644 --- a/tests/test_build_ts/test_build_ts.py +++ b/tests/test_build_ts/test_build_ts.py @@ -168,6 +168,22 @@ class Iterable() ), ) + def test_predicate(self): + self._file_contents_eq( + "predicate", + dedent( + """\ + predicate(c) + + Arguments: + * **c** (*any*) -- + + Returns: + c is "ConstructorlessClass()" + """ + ), + ) + class TestHtmlBuilder(SphinxBuildTestCase): """Tests which require an HTML build of our Sphinx tree, for checking