From 3a45099f8ae794496f2f4b2fe0aaec764f301279 Mon Sep 17 00:00:00 2001 From: delangle Date: Wed, 24 Jan 2024 15:22:41 +0100 Subject: [PATCH 1/3] [typescrip-to-proptypes] Allow to represent dates as PropTypes.Object --- .../src/getPropTypesFromFile.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts index 68d57fbdd35d08..baf1b881c68d45 100644 --- a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts +++ b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts @@ -114,7 +114,9 @@ function checkType({ return createInstanceOfType({ jsDoc, instance: 'RegExp' }); } case 'Date': { - return createInstanceOfType({ jsDoc, instance: 'Date' }); + if (!project.shouldUseObjectForDate?.({ name })) { + return createInstanceOfType({ jsDoc, instance: 'Date' }); + } } default: // continue with function execution @@ -462,6 +464,7 @@ export function getPropTypesFromFile({ project, shouldInclude: inShouldInclude, shouldResolveObject: inShouldResolveObject, + shouldUseObjectForDate, checkDeclarations, }: GetPropTypesFromFileOptions) { const sourceFile = project.program.getSourceFile(filePath); @@ -513,6 +516,7 @@ export function getPropTypesFromFile({ ...project, reactComponentName, shouldResolveObject, + shouldUseObjectForDate, shouldInclude, createPropTypeId, }; @@ -553,11 +557,18 @@ export interface GetPropTypesFromFileOptions propertyCount: number; depth: number; }) => boolean | undefined; + /** + * Called to know if a date should be represented as `PropTypes.object` or `PropTypes.instanceOf(Date) + * @returns true to use `PropTypes.object`, false to use `PropTypes.instanceOf(Date)`. + * @default false + */ + shouldUseObjectForDate?: (data: { name: string }) => boolean; } interface PropTypesProject extends TypeScriptProject { reactComponentName: string | undefined; shouldResolveObject: NonNullable; + shouldUseObjectForDate: GetPropTypesFromFileOptions['shouldUseObjectForDate']; shouldInclude: NonNullable; createPropTypeId: (sigil: ts.Symbol | ts.Type) => number; } From 87e481655f19356661ff681b255f7a6d4d9a90ac Mon Sep 17 00:00:00 2001 From: delangle Date: Wed, 24 Jan 2024 15:39:24 +0100 Subject: [PATCH 2/3] Fix --- packages/typescript-to-proptypes/src/getPropTypesFromFile.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts index baf1b881c68d45..6ff5e3b4c91d83 100644 --- a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts +++ b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts @@ -117,6 +117,7 @@ function checkType({ if (!project.shouldUseObjectForDate?.({ name })) { return createInstanceOfType({ jsDoc, instance: 'Date' }); } + break; } default: // continue with function execution From 8ae7c387380ec69dc36e6bef9d625e4a98c4794a Mon Sep 17 00:00:00 2001 From: delangle Date: Thu, 25 Jan 2024 12:58:27 +0100 Subject: [PATCH 3/3] Code review: Alex --- packages/typescript-to-proptypes/src/getPropTypesFromFile.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts index 6ff5e3b4c91d83..04214f5ca1290b 100644 --- a/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts +++ b/packages/typescript-to-proptypes/src/getPropTypesFromFile.ts @@ -117,7 +117,8 @@ function checkType({ if (!project.shouldUseObjectForDate?.({ name })) { return createInstanceOfType({ jsDoc, instance: 'Date' }); } - break; + + return createObjectType({ jsDoc }); } default: // continue with function execution