From 7d9916bc0187364bd60c6613b63dd40d57b8e3f4 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 28 Sep 2023 11:42:53 -0700 Subject: [PATCH] Make ts_xref_formatter apply to all kinds of xref (#81) Before it specifically applied to external xrefs but I think it's more convenient to use it for all xrefs. --- sphinx_js/renderers.py | 15 ++++----------- tests/test_build_ts/source/docs/conf.py | 11 +++++++++++ tests/test_renderers.py | 11 ++++++++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/sphinx_js/renderers.py b/sphinx_js/renderers.py index 40fd802a..f6e7cd6f 100644 --- a/sphinx_js/renderers.py +++ b/sphinx_js/renderers.py @@ -31,8 +31,6 @@ Type, TypeParam, TypeXRef, - TypeXRefExternal, - TypeXRefInternal, ) from .jsdoc import Analyzer as JsAnalyzer from .parsers import PathVisitor @@ -57,7 +55,7 @@ class JsRenderer: _renderer_type: Literal["function", "class", "attribute"] _template: str - _xref_formatter: Callable[[TypeXRefExternal], str] + _xref_formatter: Callable[[TypeXRef], str] _partial_path: list[str] _explicit_formal_params: str _content: list[str] @@ -67,13 +65,13 @@ def _template_vars(self, name: str, obj: TopLevel) -> dict[str, Any]: raise NotImplementedError def _set_xref_formatter( - self, formatter: Callable[[Config, TypeXRefExternal], str] | None + self, formatter: Callable[[Config, TypeXRef], str] | None ) -> None: if formatter: self._xref_formatter = partial(formatter, self._app.config) return - def default_xref_formatter(xref: TypeXRefExternal) -> str: + def default_xref_formatter(xref: TypeXRef) -> str: return xref.name self._xref_formatter = default_xref_formatter @@ -313,12 +311,7 @@ def strs() -> Iterator[str]: return "".join(res) def render_xref(self, s: TypeXRef, escape: bool = False) -> str: - if isinstance(s, TypeXRefInternal): - name = rst.escape(s.name) - result = f":js:class:`{name}`" - else: - assert isinstance(s, TypeXRefExternal) - result = self._xref_formatter(s) + result = self._xref_formatter(s) if escape: result = rst.escape(result) return result diff --git a/tests/test_build_ts/source/docs/conf.py b/tests/test_build_ts/source/docs/conf.py index 362420c0..0912e003 100644 --- a/tests/test_build_ts/source/docs/conf.py +++ b/tests/test_build_ts/source/docs/conf.py @@ -8,3 +8,14 @@ jsdoc_config_path = "../tsconfig.json" js_language = "typescript" +from sphinx.util import rst + +from sphinx_js.ir import TypeXRefInternal + + +def ts_xref_formatter(config, xref): + if isinstance(xref, TypeXRefInternal): + name = rst.escape(xref.name) + return f":js:class:`{name}`" + else: + return xref.name diff --git a/tests/test_renderers.py b/tests/test_renderers.py index 12f7c3a1..7cf58ea2 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -1,6 +1,7 @@ from textwrap import dedent, indent import pytest +from sphinx.util import rst from sphinx_js.ir import ( DescriptionCode, @@ -51,6 +52,14 @@ def test_render_description(): ) +def ts_xref_formatter(config, xref): + if isinstance(xref, TypeXRefInternal): + name = rst.escape(xref.name) + return f":js:class:`{name}`" + else: + return xref.name + + @pytest.fixture() def function_renderer(): class _config: @@ -63,7 +72,7 @@ class _app: renderer._app = _app renderer._explicit_formal_params = None renderer._content = [] - renderer._set_xref_formatter(None) + renderer._set_xref_formatter(ts_xref_formatter) return renderer