Skip to content

Commit

Permalink
[typescrip-to-proptypes] Allow to represent dates as PropTypes.Object
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Jan 24, 2024
1 parent 8c039dd commit 3a45099
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/typescript-to-proptypes/src/getPropTypesFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -462,6 +464,7 @@ export function getPropTypesFromFile({
project,
shouldInclude: inShouldInclude,
shouldResolveObject: inShouldResolveObject,
shouldUseObjectForDate,
checkDeclarations,
}: GetPropTypesFromFileOptions) {
const sourceFile = project.program.getSourceFile(filePath);
Expand Down Expand Up @@ -513,6 +516,7 @@ export function getPropTypesFromFile({
...project,
reactComponentName,
shouldResolveObject,
shouldUseObjectForDate,
shouldInclude,
createPropTypeId,
};
Expand Down Expand Up @@ -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<GetPropTypesFromFileOptions['shouldResolveObject']>;
shouldUseObjectForDate: GetPropTypesFromFileOptions['shouldUseObjectForDate'];
shouldInclude: NonNullable<GetPropTypesFromFileOptions['shouldInclude']>;
createPropTypeId: (sigil: ts.Symbol | ts.Type) => number;
}
Expand Down

0 comments on commit 3a45099

Please sign in to comment.