Skip to content

Commit a86a6ff

Browse files
authored
fix: Prevent very long inline types (#76)
1 parent 701c751 commit a86a6ff

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import * as React from 'react';
4+
5+
export interface ButtonProps {
6+
style: ButtonProps.Style;
7+
}
8+
9+
export namespace ButtonProps {
10+
export interface Style {
11+
root?: {
12+
background?: {
13+
active?: string;
14+
default?: string;
15+
disabled?: string;
16+
hover?: string;
17+
};
18+
color?: {
19+
active?: string;
20+
default?: string;
21+
disabled?: string;
22+
hover?: string;
23+
};
24+
borderColor?: {
25+
active?: string;
26+
default?: string;
27+
disabled?: string;
28+
hover?: string;
29+
};
30+
};
31+
}
32+
}
33+
34+
export default function Button(props: ButtonProps) {
35+
return <div style={{ background: props.style.root?.background?.default }} />;
36+
}

src/components/object-definition.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,27 @@ export function getObjectDefinition(
3535
return getUnionTypeDefinition(realTypeName, realType, rawTypeNode, checker);
3636
}
3737
if (realType.getProperties().length > 0) {
38-
return {
39-
type: type,
40-
inlineType: {
41-
name: realTypeName,
42-
type: 'object',
43-
properties: realType
44-
.getProperties()
45-
.map(prop => {
46-
const propType = checker.getTypeAtLocation(extractDeclaration(prop));
47-
return {
48-
name: prop.getName(),
49-
type: stringifyType(propType, checker),
50-
optional: isOptional(propType),
51-
};
52-
})
53-
.sort((a, b) => a.name.localeCompare(b.name)),
54-
},
55-
};
38+
const properties = realType
39+
.getProperties()
40+
.map(prop => {
41+
const propType = checker.getTypeAtLocation(extractDeclaration(prop));
42+
return {
43+
name: prop.getName(),
44+
type: stringifyType(propType, checker),
45+
optional: isOptional(propType),
46+
};
47+
})
48+
.sort((a, b) => a.name.localeCompare(b.name));
49+
if (properties.every(prop => prop.type.length < 200)) {
50+
return {
51+
type: type,
52+
inlineType: {
53+
name: realTypeName,
54+
type: 'object',
55+
properties: properties,
56+
},
57+
};
58+
}
5659
}
5760
if (realType.getCallSignatures().length > 0) {
5861
if (realType.getCallSignatures().length > 1) {

test/components/complex-types.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import { expect, test, beforeAll } from 'vitest';
44
import { ComponentDefinition } from '../../src/components/interfaces';
55
import { buildProject } from './test-helpers';
66

7+
let button: ComponentDefinition;
78
let buttonGroup: ComponentDefinition;
89
let sideNavigation: ComponentDefinition;
910
let columnLayout: ComponentDefinition;
1011
let table: ComponentDefinition;
1112

1213
beforeAll(() => {
1314
const result = buildProject('complex-types');
14-
expect(result).toHaveLength(4);
15+
expect(result).toHaveLength(5);
1516

16-
[buttonGroup, columnLayout, sideNavigation, table] = result;
17+
[button, buttonGroup, columnLayout, sideNavigation, table] = result;
1718
});
1819

1920
test('should have camel and dash-cased names', () => {
@@ -193,3 +194,13 @@ test('should parse string literal type as single-value union', () => {
193194
},
194195
]);
195196
});
197+
198+
test('should trim long inline types', () => {
199+
expect(button.properties).toEqual([
200+
{
201+
name: 'style',
202+
type: 'ButtonProps.Style',
203+
optional: false,
204+
},
205+
]);
206+
});

0 commit comments

Comments
 (0)