diff --git a/sphinx_js/ir.py b/sphinx_js/ir.py index 971e45de..47a81aff 100644 --- a/sphinx_js/ir.py +++ b/sphinx_js/ir.py @@ -45,6 +45,7 @@ class TypeXRefInternal: name: str path: list[str] type: Literal["internal"] = "internal" + kind: str | None = None @define diff --git a/sphinx_js/js/convertType.ts b/sphinx_js/js/convertType.ts index 597d7c2f..f86c8569 100644 --- a/sphinx_js/js/convertType.ts +++ b/sphinx_js/js/convertType.ts @@ -297,7 +297,6 @@ class TypeConverter implements TypeVisitor { } reference(type: ReferenceType): Type { - let res; if (type.isIntentionallyBroken()) { // If it's intentionally broken, don't add an xref. It's probably a type // parameter. @@ -305,7 +304,7 @@ class TypeConverter implements TypeVisitor { } else { // if we got a reflection use that. It's not all that clear how to deal // with type arguments here though... - res = this.convertPrivateReferenceToReflection(type); + const res = this.convertPrivateReferenceToReflection(type); // else use convertReferenceToXRef if (res) { return res; diff --git a/sphinx_js/renderers.py b/sphinx_js/renderers.py index ffb19c1f..746d364b 100644 --- a/sphinx_js/renderers.py +++ b/sphinx_js/renderers.py @@ -175,7 +175,7 @@ class HasDepPath(Protocol): class Renderer: - _type_xref_formatter: Callable[[TypeXRef, str | None], str] + _type_xref_formatter: Callable[[TypeXRef], str] # We turn the in the analyzer tests because it # makes a big mess. _add_span: bool @@ -235,13 +235,13 @@ def from_directive(cls: type[R], directive: Directive, app: Sphinx) -> R: ) def _set_type_xref_formatter( - self, formatter: Callable[[Config, TypeXRef, str | None], str] | None + self, formatter: Callable[[Config, TypeXRef], str] | None ) -> None: if formatter: self._type_xref_formatter = partial(formatter, self._app.config) return - def default_type_xref_formatter(xref: TypeXRef, _: str | None) -> str: + def default_type_xref_formatter(xref: TypeXRef) -> str: return xref.name self._type_xref_formatter = default_type_xref_formatter @@ -453,11 +453,13 @@ def strs() -> Iterator[str]: def render_xref(self, s: TypeXRef, escape: bool = False) -> str: obj = None - kind = None - if isinstance(s, TypeXRefInternal | TypeXRefInternal): + if isinstance(s, TypeXRefInternal): obj = self.lookup_object(s.path) - kind = type(obj).__name__.lower() - result = self._type_xref_formatter(s, kind) + # Stick the kind on the xref so that the formatter will know what + # xref role to emit. I'm not sure how to compute this earlier. It's + # convenient to do it here. + s.kind = type(obj).__name__.lower() + result = self._type_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 62b954c4..bd89766e 100644 --- a/tests/test_build_ts/source/docs/conf.py +++ b/tests/test_build_ts/source/docs/conf.py @@ -14,9 +14,9 @@ from sphinx_js.ir import TypeXRef, TypeXRefInternal -def ts_type_xref_formatter(config, xref: TypeXRef, kind: str) -> str: +def ts_type_xref_formatter(config, xref: TypeXRef) -> str: if isinstance(xref, TypeXRefInternal): name = rst.escape(xref.name) - return f":js:{kind}:`{name}`" + return f":js:{xref.kind}:`{name}`" else: return xref.name diff --git a/tests/test_renderers.py b/tests/test_renderers.py index b234ca6a..e9fe7830 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -60,10 +60,10 @@ def test_render_description(): ) -def ts_xref_formatter(config, xref, kind): +def ts_xref_formatter(config, xref): if isinstance(xref, TypeXRefInternal): name = rst.escape(xref.name) - return f":js:{kind}:`{name}`" + return f":js:{xref.kind}:`{name}`" else: return xref.name @@ -339,8 +339,12 @@ def test_render_xref(function_renderer: AutoFunctionRenderer): assert function_renderer.render_type([xref_external]) == "A" res = [] - def xref_render(config, val, kind): + def xref_render(config, val): res.append([config, val]) + kind = None + if isinstance(val, TypeXRefInternal): + kind = val.kind + return f"{val.package}::{val.name}::{kind}" function_renderer._set_type_xref_formatter(xref_render)