From 6b206b07e91c684b2f861b65c29d048b7e1a388a Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 23 Dec 2024 21:03:05 +0100 Subject: [PATCH] Use file and name as key for private type aliases (#177) It's probably better to use the position as the key but since 0.26.0 it doesn't seem to work correctly. This also redirects aliases that are marked as `@hidden` --- sphinx_js/js/convertType.ts | 7 +++++-- sphinx_js/js/redirectPrivateAliases.ts | 14 ++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sphinx_js/js/convertType.ts b/sphinx_js/js/convertType.ts index 7f89c244..022b2966 100644 --- a/sphinx_js/js/convertType.ts +++ b/sphinx_js/js/convertType.ts @@ -233,9 +233,12 @@ class TypeConverter implements TypeVisitor { throw new Error("This should not happen"); } // See if this refers to a private type. In that case we should inline the - // type reflection rather than referring to the non-exported name. + // type reflection rather than referring to the non-exported name. Ideally + // we should key on position rather than name (the same file can have + // multiple private types with the same name potentially). But it doesn't + // seem to be working. const newTarget = this.symbolToType.get( - `${type.symbolId.fileName}:${type.symbolId.pos}`, + `${type.symbolId.fileName}:${type.name}`, ); if (newTarget) { // TODO: this doesn't handle parentheses correctly. diff --git a/sphinx_js/js/redirectPrivateAliases.ts b/sphinx_js/js/redirectPrivateAliases.ts index d36c6c6e..543974d7 100644 --- a/sphinx_js/js/redirectPrivateAliases.ts +++ b/sphinx_js/js/redirectPrivateAliases.ts @@ -101,7 +101,11 @@ export function redirectPrivateTypes(app: Application): ReadonlySymbolToType { const referenced = getReferencedSymbols(owningModule); return Array.from(referenced).filter((s) => { const refl = context.project.getReflectionFromSymbol(s); - return !refl || refl.flags.isPrivate; + return ( + !refl || + refl.flags.isPrivate || + refl?.comment?.modifierTags.has("@hidden") + ); }); } @@ -141,15 +145,9 @@ export function redirectPrivateTypes(app: Application): ReadonlySymbolToType { if (ts.isTypeAliasDeclaration(decl)) { const sf = decl.getSourceFile(); const fileName = sf.fileName; - const pos = Application.VERSION.startsWith("0.25") - ? decl.pos - : decl.getStart(); const converted = context.converter.convertType(context, decl.type); - // Depending on whether we have a symbolId or a reflection in - // renderType we'll use different keys to look this up. - symbolToType.set(`${fileName}:${pos}`, converted); // Ideally we should be able to key on position rather than file and - // name when the reflection is present but I couldn't figure out how. + // name but I couldn't figure out how. symbolToType.set(`${fileName}:${decl.name.getText()}`, converted); } }