Skip to content

Commit f754312

Browse files
committed
Fix display of index signatures
Resolves #1913
1 parent 88c0f1e commit f754312

File tree

5 files changed

+88
-55
lines changed

5 files changed

+88
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ These TODOs will be resolved before a full release. ([GitHub project](https://gi
8181
- Fixed `removeReflection` not completely removing reflections from the project, #1898.
8282
- Fixed `@hidden` / `@ignore` / `@exclude` comments on default exports with no associated variable, #1903.
8383
- `makeRecursiveVisitor` will now correctly call the `intersection` callback, #1910.
84+
- JS exports defined as `exports.foo = ...` will now be converted as variables rather than properties.
8485

8586
### Thanks!
8687

src/lib/converter/symbols.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const symbolConverters: {
4545
[ts.SymbolFlags.Alias]: convertAlias,
4646
[ts.SymbolFlags.BlockScopedVariable]: convertVariable,
4747
[ts.SymbolFlags.FunctionScopedVariable]: convertVariable,
48+
[ts.SymbolFlags.ExportValue]: convertVariable,
4849
[ts.SymbolFlags.GetAccessor]: convertAccessor,
4950
[ts.SymbolFlags.SetAccessor]: convertAccessor,
5051
};
@@ -66,6 +67,7 @@ const conversionOrder = [
6667
// Before type alias
6768
ts.SymbolFlags.BlockScopedVariable,
6869
ts.SymbolFlags.FunctionScopedVariable,
70+
ts.SymbolFlags.ExportValue,
6971

7072
ts.SymbolFlags.TypeAlias,
7173
ts.SymbolFlags.Function,
@@ -849,6 +851,8 @@ function convertVariable(
849851
reflection.defaultValue = convertDefaultValue(declaration);
850852

851853
context.finalizeDeclarationReflection(reflection);
854+
855+
return ts.SymbolFlags.Property;
852856
}
853857

854858
function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) {
@@ -934,6 +938,8 @@ function convertVariableAsFunction(
934938
signature
935939
);
936940
}
941+
942+
return ts.SymbolFlags.Property;
937943
}
938944

939945
function convertAccessor(

src/lib/output/themes/default/partials/type.tsx

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -283,65 +283,72 @@ const typeRenderers: {
283283
return name;
284284
},
285285
reflection(context, type, { needsParens }) {
286-
if (type.declaration.children) {
287-
// Object literal
288-
return (
289-
<>
290-
<span class="tsd-signature-symbol">{"{ "}</span>
291-
{join(<span class="tsd-signature-symbol">; </span>, type.declaration.children, (item) => {
292-
if (item.getSignature && item.setSignature) {
293-
return (
294-
<>
295-
{item.name}
296-
<span class="tsd-signature-symbol">: </span>
297-
{renderType(context, item.getSignature.type)}
298-
</>
299-
);
300-
}
286+
const members: JSX.Element[] = [];
301287

302-
if (item.getSignature) {
303-
return (
304-
<>
305-
<span class="tsd-signature-symbol">get </span>
306-
{item.name}
307-
<span class="tsd-signature-symbol">(): </span>
308-
{renderType(context, item.getSignature.type)}
309-
</>
310-
);
311-
}
288+
for (const item of type.declaration.children || []) {
289+
if (item.getSignature && item.setSignature) {
290+
members.push(
291+
<>
292+
{item.name}
293+
<span class="tsd-signature-symbol">: </span>
294+
{renderType(context, item.getSignature.type)}
295+
</>
296+
);
297+
continue;
298+
}
312299

313-
if (item.setSignature) {
314-
return (
315-
<>
316-
<span class="tsd-signature-symbol">set </span>
317-
{item.name}
318-
<span class="tsd-signature-symbol">(</span>
319-
{item.setSignature.parameters?.map((item) => (
320-
<>
321-
{item.name}
322-
<span class="tsd-signature-symbol">: </span>
323-
{renderType(context, item.type)}
324-
</>
325-
))}
326-
<span class="tsd-signature-symbol">)</span>
327-
</>
328-
);
329-
}
300+
if (item.getSignature) {
301+
members.push(
302+
<>
303+
<span class="tsd-signature-symbol">get </span>
304+
{item.name}
305+
<span class="tsd-signature-symbol">(): </span>
306+
{renderType(context, item.getSignature.type)}
307+
</>
308+
);
309+
continue;
310+
}
330311

331-
return (
312+
if (item.setSignature) {
313+
members.push(
314+
<>
315+
<span class="tsd-signature-symbol">set </span>
316+
{item.name}
317+
<span class="tsd-signature-symbol">(</span>
318+
{item.setSignature.parameters?.map((item) => (
332319
<>
333320
{item.name}
334-
<span class="tsd-signature-symbol">{item.flags.isOptional ? "?: " : ": "}</span>
321+
<span class="tsd-signature-symbol">: </span>
335322
{renderType(context, item.type)}
336323
</>
337-
);
338-
})}
339-
<span class="tsd-signature-symbol">{" }"}</span>
324+
))}
325+
<span class="tsd-signature-symbol">)</span>
326+
</>
327+
);
328+
continue;
329+
}
330+
331+
members.push(
332+
<>
333+
{item.name}
334+
<span class="tsd-signature-symbol">{item.flags.isOptional ? "?: " : ": "}</span>
335+
{renderType(context, item.type)}
340336
</>
341337
);
342338
}
343339

344-
if (type.declaration.signatures?.length === 1) {
340+
if (type.declaration.indexSignature) {
341+
const index = type.declaration.indexSignature;
342+
members.push(
343+
<>
344+
[{index.parameters![0].name}: {renderType(context, index.parameters![0].type)}]
345+
<span class="tsd-signature-symbol">: </span>
346+
{renderType(context, index.type)}
347+
</>
348+
);
349+
}
350+
351+
if (!members.length && type.declaration.signatures?.length === 1) {
345352
return (
346353
<>
347354
{needsParens && <span class="tsd-signature-symbol">(</span>}
@@ -354,16 +361,19 @@ const typeRenderers: {
354361
);
355362
}
356363

357-
if (type.declaration.signatures) {
364+
for (const item of type.declaration.signatures || []) {
365+
members.push(context.memberSignatureTitle(item, { hideName: true }));
366+
}
367+
368+
if (members.length) {
369+
const membersWithSeparators = members.flatMap((m) => [m, <span class="tsd-signature-symbol">; </span>]);
370+
membersWithSeparators.pop();
371+
358372
return (
359373
<>
360374
<span class="tsd-signature-symbol">{"{"} </span>
361-
{join(<span class="tsd-signature-symbol">; </span>, type.declaration.signatures, (item) =>
362-
context.memberSignatureTitle(item, {
363-
hideName: true,
364-
})
365-
)}
366-
<span class="tsd-signature-symbol">{" }"}</span>
375+
{membersWithSeparators}
376+
<span class="tsd-signature-symbol"> {"}"}</span>
367377
</>
368378
);
369379
}

src/test/converter2/issues/gh1913.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @returns {[{ [key1: string]: number }, { [key2: string]: string }]} ret
3+
*/
4+
exports.fn = () => [{ a: 42 }, { b: "42" }];

src/test/issueTests.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,16 @@ export const issueTests: {
467467
logger.discardDebugMessages();
468468
logger.expectNoOtherMessages();
469469
},
470+
471+
gh1913(project) {
472+
const fn = query(project, "fn");
473+
474+
equal(
475+
fn.signatures?.[0].comment,
476+
new Comment(
477+
[],
478+
[new CommentTag("@returns", [{ kind: "text", text: "ret" }])]
479+
)
480+
);
481+
},
470482
};

0 commit comments

Comments
 (0)