Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/component-meta/lib/schemaResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ export function createSchemaResolvers(
else if (subtype.getCallSignatures().length === 1) {
return resolveCallbackSchema(subtype.getCallSignatures()[0]!);
}
else if (subtype.flags & ts.TypeFlags.EnumLiteral && subtype.isLiteral()) {
// enum members stringify to their qualified name (e.g. "MyEnum.Small"), which
// hides the runtime value from consumers such as docs generators; expose the
// value alongside, printed like any other literal
const { value } = subtype;
if (typeof value === 'string' || typeof value === 'number') {
return {
kind: 'literal',
type,
value: JSON.stringify(value),
};
}
}

return type;
}
Expand Down
1 change: 1 addition & 0 deletions packages/component-meta/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export interface ExposeMeta {

export type PropertyMetaSchema =
| string
| { kind: 'literal'; type: string; value: string }
| { kind: 'enum'; type: string; schema?: PropertyMetaSchema[] }
| { kind: 'array'; type: string; schema?: PropertyMetaSchema[] }
| { kind: 'event'; type: string; schema?: PropertyMetaSchema[] }
Expand Down
56 changes: 53 additions & 3 deletions packages/component-meta/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,21 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) =>
"schema": {
"kind": "enum",
"schema": [
"MyEnum.Small",
"MyEnum.Medium",
"MyEnum.Large",
{
"kind": "literal",
"type": "MyEnum.Small",
"value": "0",
},
{
"kind": "literal",
"type": "MyEnum.Medium",
"value": "1",
},
{
"kind": "literal",
"type": "MyEnum.Large",
"value": "2",
},
],
"type": "MyEnum",
},
Expand All @@ -623,6 +635,44 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) =>
}
`);

const stringEnumValue = meta.props.find(prop => prop.name === 'stringEnumValue');
expect(stringEnumValue).toMatchInlineSnapshot(`
{
"declarations": [],
"default": undefined,
"description": "string enum value",
"getDeclarations": [Function],
"getTypeObject": [Function],
"global": false,
"name": "stringEnumValue",
"rawType": undefined,
"required": true,
"schema": {
"kind": "enum",
"schema": [
{
"kind": "literal",
"type": "MyStringEnum.Small",
"value": ""small"",
},
{
"kind": "literal",
"type": "MyStringEnum.Medium",
"value": ""medium"",
},
{
"kind": "literal",
"type": "MyStringEnum.Large",
"value": ""large"",
},
],
"type": "MyStringEnum",
},
"tags": [],
"type": "MyStringEnum",
}
`);

const namespaceType = meta.props.find(prop => prop.name === 'namespaceType');
expect(namespaceType).toMatchInlineSnapshot(`
{
Expand Down
9 changes: 9 additions & 0 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,11 @@ declare enum MyEnum {
Medium = 1,
Large = 2
}
declare enum MyStringEnum {
Small = "small",
Medium = "medium",
Large = "large"
}
declare namespace MyNamespace {
type MyType = {};
}
Expand Down Expand Up @@ -818,6 +823,10 @@ export interface MyProps {
* enum value
*/
enumValue: MyEnum;
/**
* string enum value
*/
stringEnumValue: MyStringEnum;
/**
* namespace type
*/
Expand Down
10 changes: 10 additions & 0 deletions test-workspace/component-meta/reference-type-props/my-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ enum MyEnum {
Large,
}

enum MyStringEnum {
Small = 'small',
Medium = 'medium',
Large = 'large',
}

namespace MyNamespace {
export type MyType = {};
}
Expand Down Expand Up @@ -94,6 +100,10 @@ export interface MyProps {
* enum value
*/
enumValue: MyEnum;
/**
* string enum value
*/
stringEnumValue: MyStringEnum;
/**
* namespace type
*/
Expand Down
Loading