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
1 change: 1 addition & 0 deletions src/generators/metadata/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// These openers/closers are used to determine if a type string is well-formed
export const TYPE_OPENERS = new Set(['<', '(', '{', '[']);
export const TYPE_CLOSERS = new Set(['>', ')', '}', ']']);
export const PREFIXES = ['typeof ', 'keyof ', 'readonly ', 'unique '];

// On "About this Documentation", we define the stability indices, and thus
// we don't need to check it for stability references
Expand Down
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
9 changes: 8 additions & 1 deletion src/generators/metadata/utils/typeParser.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TYPE_OPENERS, TYPE_CLOSERS } from '../constants.mjs';
import { TYPE_OPENERS, TYPE_CLOSERS, PREFIXES } from '../constants.mjs';

/** True when the `>` at `i` is the tail of `=>` and shouldn't pop depth. */
const isArrowTail = (str, i) => str[i] === '>' && str[i - 1] === '=';
Expand Down Expand Up @@ -206,6 +206,13 @@ export const parseType = (typeString, transformType) => {
return parts.map(p => resolveOr(p, transformType)).join(joiner);
}

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