From 4aaeeb26bb642ad8ba44959e1720433cb6e163bd Mon Sep 17 00:00:00 2001 From: Travis Arnold Date: Sun, 14 Jul 2024 12:46:47 -0700 Subject: [PATCH] better component analysis --- .../src/types/getTypeDocumentation.test.ts | 12 ++-- packages/utils/src/types/processType.test.ts | 55 +++++++++++++++++-- packages/utils/src/types/processType.ts | 12 ++-- 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/packages/utils/src/types/getTypeDocumentation.test.ts b/packages/utils/src/types/getTypeDocumentation.test.ts index a07be7f..ab1f911 100644 --- a/packages/utils/src/types/getTypeDocumentation.test.ts +++ b/packages/utils/src/types/getTypeDocumentation.test.ts @@ -661,7 +661,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "test.ts", @@ -1167,7 +1167,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": { "variant": "body1", }, @@ -1257,7 +1257,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "node_modules/@types/react/index.d.ts", @@ -1404,7 +1404,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "node_modules/@types/react/index.d.ts", @@ -3008,7 +3008,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "test.tsx", @@ -3186,7 +3186,7 @@ describe('getTypeDocumentation', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "test.ts", diff --git a/packages/utils/src/types/processType.test.ts b/packages/utils/src/types/processType.test.ts index 517bd0e..7ddfb8f 100644 --- a/packages/utils/src/types/processType.test.ts +++ b/packages/utils/src/types/processType.test.ts @@ -2559,7 +2559,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "test.ts", @@ -2654,7 +2654,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "test.ts", @@ -2717,7 +2717,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": { "color": "red", }, @@ -2785,7 +2785,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": { "style": { "color": "blue", @@ -3342,7 +3342,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "node_modules/styled-components/dist/types.d.ts", @@ -3389,7 +3389,7 @@ describe('processProperties', () => { { "kind": "ComponentSignature", "modifier": undefined, - "properties": { + "parameter": { "defaultValue": undefined, "description": undefined, "filePath": "node_modules/@types/react/index.d.ts", @@ -4290,4 +4290,47 @@ describe('processProperties', () => { } `) }) + + test('infers component with no parameter from return type', () => { + const sourceFile = project.createSourceFile( + 'test.ts', + dedent` + import * as React from 'react' + export function Text(): React.ReactNode { + return null + } + `, + { overwrite: true } + ) + const typeAlias = sourceFile.getFunctionOrThrow('Text') + const processedProperties = processType(typeAlias.getType()) + + expect(processedProperties).toMatchInlineSnapshot(` + { + "filePath": "test.ts", + "kind": "Component", + "name": "Text", + "position": { + "end": { + "column": 2, + "line": 4, + }, + "start": { + "column": 1, + "line": 2, + }, + }, + "signatures": [ + { + "kind": "ComponentSignature", + "modifier": undefined, + "parameter": undefined, + "returnType": "ReactNode", + "type": "function Text(): ReactNode", + }, + ], + "type": "() => ReactNode", + } + `) + }) }) diff --git a/packages/utils/src/types/processType.ts b/packages/utils/src/types/processType.ts index 5102e32..e2ba224 100644 --- a/packages/utils/src/types/processType.ts +++ b/packages/utils/src/types/processType.ts @@ -172,7 +172,7 @@ export interface FunctionType extends BaseType { export interface ComponentSignatureType extends BaseType { kind: 'ComponentSignature' modifier?: 'async' | 'generator' - properties: ObjectType + parameter?: ObjectType | ReferenceType returnType: string } @@ -663,7 +663,10 @@ export function processType( return { ...processedCallSignature, kind: 'ComponentSignature', - properties: parameters.at(0)! as ObjectType, + parameter: parameters.at(0) as + | ObjectType + | ReferenceType + | undefined, } satisfies ComponentSignatureType } ), @@ -1426,8 +1429,9 @@ export function isComponent( } return callSignatures.every((signature) => { - const onlyOneParameter = signature.parameters.length === 1 - if (onlyOneParameter) { + if (signature.returnType === 'ReactNode') { + return true + } else if (signature.parameters.length === 1) { const firstParameter = signature.parameters.at(0)! if (firstParameter.kind === 'Reference') {