Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-to-proptypes] Allow to represent dates as PropTypes.object #40774

Merged
merged 4 commits into from
Jan 26, 2024
Merged
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
15 changes: 14 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,11 @@ function checkType({
return createInstanceOfType({ jsDoc, instance: 'RegExp' });
}
case 'Date': {
return createInstanceOfType({ jsDoc, instance: 'Date' });
if (!project.shouldUseObjectForDate?.({ name })) {
return createInstanceOfType({ jsDoc, instance: 'Date' });
}

return createObjectType({ jsDoc });
}
default:
// continue with function execution
Expand Down Expand Up @@ -462,6 +466,7 @@ export function getPropTypesFromFile({
project,
shouldInclude: inShouldInclude,
shouldResolveObject: inShouldResolveObject,
shouldUseObjectForDate,
checkDeclarations,
}: GetPropTypesFromFileOptions) {
const sourceFile = project.program.getSourceFile(filePath);
Expand Down Expand Up @@ -513,6 +518,7 @@ export function getPropTypesFromFile({
...project,
reactComponentName,
shouldResolveObject,
shouldUseObjectForDate,
shouldInclude,
createPropTypeId,
};
Expand Down Expand Up @@ -553,11 +559,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