diff --git a/fixtures/components/complex-types/button/index.tsx b/fixtures/components/complex-types/button/index.tsx new file mode 100644 index 0000000..2cdec22 --- /dev/null +++ b/fixtures/components/complex-types/button/index.tsx @@ -0,0 +1,36 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import * as React from 'react'; + +export interface ButtonProps { + style: ButtonProps.Style; +} + +export namespace ButtonProps { + export interface Style { + root?: { + background?: { + active?: string; + default?: string; + disabled?: string; + hover?: string; + }; + color?: { + active?: string; + default?: string; + disabled?: string; + hover?: string; + }; + borderColor?: { + active?: string; + default?: string; + disabled?: string; + hover?: string; + }; + }; + } +} + +export default function Button(props: ButtonProps) { + return
; +} diff --git a/src/components/object-definition.ts b/src/components/object-definition.ts index 24da72d..81f7b73 100644 --- a/src/components/object-definition.ts +++ b/src/components/object-definition.ts @@ -35,24 +35,27 @@ export function getObjectDefinition( return getUnionTypeDefinition(realTypeName, realType, rawTypeNode, checker); } if (realType.getProperties().length > 0) { - return { - type: type, - inlineType: { - name: realTypeName, - type: 'object', - properties: realType - .getProperties() - .map(prop => { - const propType = checker.getTypeAtLocation(extractDeclaration(prop)); - return { - name: prop.getName(), - type: stringifyType(propType, checker), - optional: isOptional(propType), - }; - }) - .sort((a, b) => a.name.localeCompare(b.name)), - }, - }; + const properties = realType + .getProperties() + .map(prop => { + const propType = checker.getTypeAtLocation(extractDeclaration(prop)); + return { + name: prop.getName(), + type: stringifyType(propType, checker), + optional: isOptional(propType), + }; + }) + .sort((a, b) => a.name.localeCompare(b.name)); + if (properties.every(prop => prop.type.length < 200)) { + return { + type: type, + inlineType: { + name: realTypeName, + type: 'object', + properties: properties, + }, + }; + } } if (realType.getCallSignatures().length > 0) { if (realType.getCallSignatures().length > 1) { diff --git a/test/components/complex-types.test.ts b/test/components/complex-types.test.ts index 2bb39aa..c99d591 100644 --- a/test/components/complex-types.test.ts +++ b/test/components/complex-types.test.ts @@ -4,6 +4,7 @@ import { expect, test, beforeAll } from 'vitest'; import { ComponentDefinition } from '../../src/components/interfaces'; import { buildProject } from './test-helpers'; +let button: ComponentDefinition; let buttonGroup: ComponentDefinition; let sideNavigation: ComponentDefinition; let columnLayout: ComponentDefinition; @@ -11,9 +12,9 @@ let table: ComponentDefinition; beforeAll(() => { const result = buildProject('complex-types'); - expect(result).toHaveLength(4); + expect(result).toHaveLength(5); - [buttonGroup, columnLayout, sideNavigation, table] = result; + [button, buttonGroup, columnLayout, sideNavigation, table] = result; }); test('should have camel and dash-cased names', () => { @@ -193,3 +194,13 @@ test('should parse string literal type as single-value union', () => { }, ]); }); + +test('should trim long inline types', () => { + expect(button.properties).toEqual([ + { + name: 'style', + type: 'ButtonProps.Style', + optional: false, + }, + ]); +});