Skip to content

Commit bddffe8

Browse files
feat(meta): support types seperated by & (#727)
* metadata: support intersection types in transformTypeToReferenceLink splitByOuterUnion only split on |, so intersection types like {A&B} were treated as a single unresolvable piece instead of having each component individually linked. Rename the function to splitByOuterSeparator and extend it to also split on & at depth 0. It now returns both the pieces and the separator string (' | ' or ' & ') so the join at the call site uses the correct separator rather than a hardcoded ' | '. * address review: use nullish assignment and derive separator from char Co-Authored-By: Aviv Keller <me@aviv.sh>
1 parent f8d8ad5 commit bddffe8

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/generators/metadata/utils/__tests__/transformers.test.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,18 @@ describe('transformTypeToReferenceLink', () => {
6161
'[`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)&gt; | [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)'
6262
);
6363
});
64+
65+
it('should transform an intersection type joined with & into linked parts', () => {
66+
strictEqual(
67+
transformTypeToReferenceLink('{string&boolean}', {}),
68+
'[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) & [`<boolean>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type)'
69+
);
70+
});
71+
72+
it('should handle an intersection with generics like {Map<string, number>&Array<string>}', () => {
73+
strictEqual(
74+
transformTypeToReferenceLink('{Map<string, number>&Array<string>}', {}),
75+
'[`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)&lt;[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)&gt; & [`<Array>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)&gt;'
76+
);
77+
});
6478
});

src/generators/metadata/utils/transformers.mjs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,34 @@ export const transformUnixManualToLink = (
2323
return `[\`${text}\`](${DOC_MAN_BASE_URL}${sectionNumber}/${command}.${sectionNumber}${sectionLetter}.html)`;
2424
};
2525
/**
26-
* Safely splits the string by `|`, ignoring pipes that are inside `< >`
26+
* Safely splits the string by `|` or `&` at the top level (ignoring those
27+
* inside `< >`), and returns both the pieces and the separator used.
2728
*
2829
* @param {string} str The type string to split
29-
* @returns {string[]} An array of type pieces
30+
* @returns {{ pieces: string[], separator: string }} The split pieces and the separator string used to join them (` | ` or ` & `)
3031
*/
31-
const splitByOuterUnion = str => {
32-
const result = [];
32+
const splitByOuterSeparator = str => {
33+
const pieces = [];
3334
let current = '';
3435
let depth = 0;
36+
let separator;
3537

3638
for (const char of str) {
3739
if (char === '<') {
3840
depth++;
3941
} else if (char === '>') {
4042
depth--;
41-
} else if (char === '|' && depth === 0) {
42-
result.push(current);
43+
} else if ((char === '|' || char === '&') && depth === 0) {
44+
pieces.push(current);
4345
current = '';
46+
separator ??= ` ${char} `;
4447
continue;
4548
}
4649
current += char;
4750
}
4851

49-
result.push(current);
50-
return result;
52+
pieces.push(current);
53+
return { pieces, separator };
5154
};
5255

5356
/**
@@ -147,7 +150,9 @@ export const transformTypeToReferenceLink = (type, record) => {
147150
return '';
148151
};
149152

150-
const typePieces = splitByOuterUnion(typeInput).map(piece => {
153+
const { pieces: outerPieces, separator } = splitByOuterSeparator(typeInput);
154+
155+
const typePieces = outerPieces.map(piece => {
151156
// This is the content to render as the text of the Markdown link
152157
const trimmedPiece = piece.trim();
153158

@@ -169,8 +174,8 @@ export const transformTypeToReferenceLink = (type, record) => {
169174
});
170175

171176
// Filter out pieces that we failed to map and then join the valid ones
172-
// into different links separated by a ` | `
173-
const markdownLinks = typePieces.filter(Boolean).join(' | ');
177+
// using the same separator that appeared in the original type string
178+
const markdownLinks = typePieces.filter(Boolean).join(separator);
174179

175180
// Return the replaced links or the original content if they all failed to be replaced
176181
// Note that if some failed to get replaced, only the valid ones will be returned

0 commit comments

Comments
 (0)