Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/generators/metadata/utils/__tests__/transformers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ describe('transformTypeToReferenceLink', () => {
);
});

it('should correctly extract TS prefix operators and link the underlying type', () => {
strictEqual(
transformTypeToReferenceLink('typeof Compiler', {
Compiler: '/api/Compiler',
}),
'typeof [`<Compiler>`](/api/Compiler)'
);
});

it('should not incorrectly strip prefixes if they are part of the type name (word boundary)', () => {
strictEqual(
transformTypeToReferenceLink('typeofSomething', {
typeofSomething: '/api/typeofSomething',
}),
'[`<typeofSomething>`](/api/typeofSomething)'
);
});

it('should handle extreme nested combinations of functions, arrays, generics, unions, and intersections', () => {
const input =
'(str: string[]) => Promise<Map<string, number & string>, Map<string | number>>';
Expand Down
8 changes: 8 additions & 0 deletions src/generators/metadata/utils/typeParser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export const parseType = (typeString, transformType) => {
return parts.map(p => resolveOr(p, transformType)).join(joiner);
}

const PREFIXES = ['typeof ', 'keyof ', 'readonly ', 'unique '];
for (const prefix of PREFIXES) {
if (trimmed.startsWith(prefix)) {
const rest = trimmed.slice(prefix.length).trim();
return `${prefix.trim()} ${resolveOr(rest, transformType)}`;
}
}

// Strip a trailing `[]` for now; reapply on the way out.
const isArray = trimmed.endsWith('[]');
const core = isArray ? trimmed.slice(0, -2).trim() : trimmed;
Expand Down
Loading