Skip to content

Commit

Permalink
patch: fixes to indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwolfe committed Nov 24, 2023
1 parent a356ecf commit 29bd7a2
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/augments/declaration/augmentDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ function augmentDeclaration(
isDeprecated: () => isDeprecated(declaration),
property,
properties,
typeToString: () =>
declarationToString(declaration, {
rootName: declaration.name,
}),
typeToString: () => declarationToString(declaration),
};
}

Expand Down
14 changes: 9 additions & 5 deletions src/declarations/declarationToString/declarationToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ interface Options {

function declarationToString(
declaration: JSONOutput.DeclarationReflection,
{ indentCount = 0, rootName = '' }: Partial<Options> = {}
options?: Options
): string {
const { indentCount = 0, rootName = declaration.name } = options || {};

switch (declaration.kind) {
case ReflectionKind.Function: {
return (declaration.signatures || [])
Expand All @@ -36,7 +38,7 @@ function declarationToString(
.filter((child) => !child.flags.isExternal)
.map((child) =>
declarationToString(child, {
indentCount,
indentCount: indentCount + 1,
rootName,
})
)
Expand All @@ -61,17 +63,19 @@ function declarationToString(

case ReflectionKind.TypeLiteral: {
if (declaration.children) {
const root = rootName ? `${rootName}: {` : '{';

return [
`${rootName}: {`,
root,
declaration.children
.map((child) =>
declarationToString(child, {
indentCount,
indentCount: indentCount + 1,
rootName,
})
)
.join(',\n'),
'}',
indent('}', indentCount),
].join('\n');
}

Expand Down
42 changes: 35 additions & 7 deletions src/parser/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,45 @@ exports[`parser > function 1`] = `"(props: MentionProps & RefAttributes<Mentions
exports[`parser > interface 1`] = `
"interface MentionProps {
loading?: boolean;
options?: DataDrivenOptionProps[];
popupClassName?: string;
rootClassName?: string;
status?: '' | 'error' | 'warning'
loading?: boolean;
options?: DataDrivenOptionProps[];
popupClassName?: string;
rootClassName?: string;
status?: '' | 'error' | 'warning'
}"
`;
exports[`parser > interface with nested properties 1`] = `
"interface ButtonProps {
block?: boolean;
children?: React.ReactNode;
className?: string;
classNames?: {
icon: string
};
danger?: boolean;
disabled?: boolean;
ghost?: boolean;
href?: string;
htmlType?: 'button' | 'reset' | 'submit';
icon?: React.ReactNode;
loading?: boolean | {
delay?: number
};
prefixCls?: string;
rootClassName?: string;
shape?: 'default' | 'circle' | 'round';
size?: SizeType;
styles?: {
icon: React.CSSProperties
};
type?: 'dashed' | 'default' | 'link' | 'text' | 'primary'
}"
`;
exports[`parser > type alias 1`] = `
"MenuRef: {
focus: (options?: FocusOptions) => void,
menu: MenuRef | null
focus: (options?: FocusOptions) => void,
menu: MenuRef | null
}"
`;
6 changes: 6 additions & 0 deletions src/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('parser', () => {
expect(child?.typeToString()).toMatchSnapshot();
});

test('interface with nested properties', () => {
const child = parsed.findChildByName<InterfaceDeclaration>('ButtonProps');

expect(child?.typeToString()).toMatchSnapshot();
});

test('type alias', () => {
const child = parsed.findChildByName('MenuRef');

Expand Down
48 changes: 48 additions & 0 deletions src/type/someTypeToString/someTypeToString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,53 @@ describe('typeToString', () => {

expect(result).toEqual('[string | number, string | number]');
});

test('nested', () => {
const result = someTypeToString({
type: 'reflection',
declaration: {
id: 423,
name: '__type',
variant: 'declaration',
kind: 65536,
flags: {},
children: [
{
id: 424,
name: 'icon',
variant: 'declaration',
kind: 1024,
flags: {},
sources: [
{
fileName: 'Projects/ant-design/components/button/button.tsx',
line: 47,
character: 17,
url: 'https://github.com/ant-design/ant-design/blob/d8d53f14c/components/button/button.tsx#L47',
},
],
type: {
type: 'intrinsic',
name: 'string',
},
},
],
groups: [
{
title: 'Properties',
children: [424],
},
],
sources: [
{
fileName: 'Projects/ant-design/components/button/button.tsx',
line: 47,
character: 15,
url: 'https://github.com/ant-design/ant-design/blob/d8d53f14c/components/button/button.tsx#L47',
},
],
},
});
});
});
});
2 changes: 1 addition & 1 deletion src/type/someTypeToString/someTypeToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function someTypeToString(
type: JSONOutput.SomeType | undefined,
options?: Partial<Options>
): string {
const { indentCount = 0, rootName = '' } = options || {};
const { indentCount = 1, rootName = '' } = options || {};

if (!type) {
return '';
Expand Down
17 changes: 17 additions & 0 deletions src/utils/indent/indent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, test } from 'vitest';
import { indent } from './indent';

describe('indent', () => {
test('adds correct number of spaces', () => {
const result = indent('test', 1);

expect(result.length).toEqual(6);
expect(result).toEqual(' test');
});
test('adds correct number of spaces for deep indent', () => {
const result = indent('test', 3);

expect(result.length).toEqual(10);
expect(result).toEqual(' test');
});
});

0 comments on commit 29bd7a2

Please sign in to comment.