Skip to content

Commit

Permalink
better component analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Jul 14, 2024
1 parent 5ba703e commit 4aaeeb2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
12 changes: 6 additions & 6 deletions packages/utils/src/types/getTypeDocumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "test.ts",
Expand Down Expand Up @@ -1167,7 +1167,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": {
"variant": "body1",
},
Expand Down Expand Up @@ -1257,7 +1257,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "node_modules/@types/react/index.d.ts",
Expand Down Expand Up @@ -1404,7 +1404,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "node_modules/@types/react/index.d.ts",
Expand Down Expand Up @@ -3008,7 +3008,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "test.tsx",
Expand Down Expand Up @@ -3186,7 +3186,7 @@ describe('getTypeDocumentation', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "test.ts",
Expand Down
55 changes: 49 additions & 6 deletions packages/utils/src/types/processType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@ describe('processProperties', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "test.ts",
Expand Down Expand Up @@ -2654,7 +2654,7 @@ describe('processProperties', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "test.ts",
Expand Down Expand Up @@ -2717,7 +2717,7 @@ describe('processProperties', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": {
"color": "red",
},
Expand Down Expand Up @@ -2785,7 +2785,7 @@ describe('processProperties', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": {
"style": {
"color": "blue",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -3389,7 +3389,7 @@ describe('processProperties', () => {
{
"kind": "ComponentSignature",
"modifier": undefined,
"properties": {
"parameter": {
"defaultValue": undefined,
"description": undefined,
"filePath": "node_modules/@types/react/index.d.ts",
Expand Down Expand Up @@ -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",
}
`)
})
})
12 changes: 8 additions & 4 deletions packages/utils/src/types/processType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
),
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 4aaeeb2

Please sign in to comment.