diff --git a/src/markdown-helpers.ts b/src/markdown-helpers.ts index d147c0d..1c70ab8 100644 --- a/src/markdown-helpers.ts +++ b/src/markdown-helpers.ts @@ -195,10 +195,12 @@ export const safelySeparateTypeStringOn = (typeString: string, targetChar: strin switch (char) { case '<': case '{': + case '(': depth++; break; case '>': case '}': + case ')': depth--; break; } diff --git a/tests/markdown-helpers.spec.ts b/tests/markdown-helpers.spec.ts index 6c99a27..c8097f3 100644 --- a/tests/markdown-helpers.spec.ts +++ b/tests/markdown-helpers.spec.ts @@ -498,6 +498,25 @@ def fn(): ).toMatchSnapshot(); }); + it('should keep a conditional type with a parenthesized union as a single inner type', () => { + expect( + rawTypeToTypeInformation( + "Promise", + '', + null, + ), + ).toEqual({ + collection: false, + type: 'Promise', + innerTypes: [ + { + collection: false, + type: "string extends T ? (A | B) : T extends 'a' ? A : B", + }, + ], + }); + }); + it('should allow commas in object types', () => { expect( rawTypeToTypeInformation('Function<{a: string, b: string}>', '', null), @@ -1125,6 +1144,16 @@ Second level methods.`; const result = safelySeparateTypeStringOn(' string | number ', '|'); expect(result).toEqual(['string', 'number']); }); + + it('should not split inside parentheses', () => { + const result = safelySeparateTypeStringOn('(A | B)[] | C', '|'); + expect(result).toEqual(['(A | B)[]', 'C']); + }); + + it('should not split commas inside parentheses', () => { + const result = safelySeparateTypeStringOn('Function<(A, B)>, C', ','); + expect(result).toEqual(['Function<(A, B)>', 'C']); + }); }); describe('getTopLevelMultiTypes', () => { @@ -1142,6 +1171,16 @@ Second level methods.`; const result = getTopLevelMultiTypes('string'); expect(result).toEqual(['string']); }); + + it('should not split a union wrapped in parentheses inside a conditional type', () => { + const result = getTopLevelMultiTypes('string extends T ? (A | B) : C'); + expect(result).toEqual(['string extends T ? (A | B) : C']); + }); + + it('should still split top-level unions next to parenthesized types', () => { + const result = getTopLevelMultiTypes('(A | B) | C'); + expect(result).toEqual(['(A | B)', 'C']); + }); }); describe('getTopLevelOrderedTypes', () => {