Skip to content

Commit 7b4e85d

Browse files
authored
[infra] Turn literal | (string & {}) to PropTypes.string (#46934)
1 parent 00f9f1f commit 7b4e85d

File tree

14 files changed

+66
-11
lines changed

14 files changed

+66
-11
lines changed

packages-internal/scripts/typescript-to-proptypes/src/getPropTypesFromFile.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ function checkType({
199199
}
200200

201201
if (type.isUnion()) {
202+
const hasStringIntersection = type.types.some((t) => {
203+
if (t.isIntersection && t.isIntersection()) {
204+
const hasString = t.types.some((it) => it.flags & ts.TypeFlags.String);
205+
const hasEmptyObject = t.types.some(
206+
(it) =>
207+
it.flags & ts.TypeFlags.Object &&
208+
(!it.symbol || !it.symbol.members || it.symbol.members.size === 0),
209+
);
210+
return hasString && hasEmptyObject;
211+
}
212+
return false;
213+
});
214+
215+
if (hasStringIntersection) {
216+
const hasLiterals = type.types.some((t) => t.flags & ts.TypeFlags.Literal);
217+
if (hasLiterals) {
218+
const hasUndefined = type.types.some((t) => t.flags & ts.TypeFlags.Undefined);
219+
if (hasUndefined) {
220+
return createUnionType({
221+
jsDoc,
222+
types: [
223+
createStringType({ jsDoc: undefined }),
224+
createUndefinedType({ jsDoc: undefined }),
225+
],
226+
});
227+
}
228+
return createStringType({ jsDoc });
229+
}
230+
}
231+
202232
const node = createUnionType({
203233
jsDoc,
204234
types: type.types.map((x) => checkType({ type: x, location, typeStack, name, project })),
@@ -207,6 +237,19 @@ function checkType({
207237
return node.types.length === 1 ? node.types[0] : node;
208238
}
209239

240+
if (type.isIntersection && type.isIntersection()) {
241+
const hasString = type.types.some((t) => t.flags & ts.TypeFlags.String);
242+
const hasEmptyObject = type.types.some(
243+
(t) =>
244+
t.flags & ts.TypeFlags.Object &&
245+
(!t.symbol || !t.symbol.members || t.symbol.members.size === 0),
246+
);
247+
248+
if (hasString && hasEmptyObject) {
249+
return createStringType({ jsDoc });
250+
}
251+
}
252+
210253
if (type.flags & ts.TypeFlags.TypeParameter) {
211254
const baseConstraintOfType = project.checker.getBaseConstraintOfType(type);
212255

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as React from 'react';
2+
3+
export interface ComponentProps {
4+
color?: 'primary' | 'secondary' | 'error' | (string & {});
5+
variant: 'text' | 'outlined' | 'contained' | (string & {});
6+
}
7+
8+
export default function Component(props: ComponentProps): React.JSX.Element;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Component.propTypes = {
2+
color: PropTypes.string,
3+
variant: PropTypes.string.isRequired,
4+
};

packages/mui-joy/src/Divider/Divider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Divider.propTypes /* remove-proptypes */ = {
181181
/**
182182
* @ignore
183183
*/
184-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
184+
role: PropTypes.string,
185185
/**
186186
* The props used for each slot inside.
187187
* @default {}

packages/mui-joy/src/Input/Input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ Input.propTypes /* remove-proptypes */ = {
359359
/**
360360
* @ignore
361361
*/
362-
autoComplete: PropTypes /* @typescript-to-proptypes-ignore */.string,
362+
autoComplete: PropTypes.string,
363363
/**
364364
* @ignore
365365
*/

packages/mui-joy/src/List/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ List.propTypes /* remove-proptypes */ = {
299299
/**
300300
* @ignore
301301
*/
302-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
302+
role: PropTypes.string,
303303
/**
304304
* The size of the component (affect other nested list* components).
305305
* @default 'md'

packages/mui-joy/src/ListDivider/ListDivider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ListDivider.propTypes /* remove-proptypes */ = {
171171
/**
172172
* @ignore
173173
*/
174-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
174+
role: PropTypes.string,
175175
/**
176176
* The props used for each slot inside.
177177
* @default {}

packages/mui-joy/src/ListItem/ListItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ ListItem.propTypes /* remove-proptypes */ = {
305305
/**
306306
* @ignore
307307
*/
308-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
308+
role: PropTypes.string,
309309
/**
310310
* The props used for each slot inside.
311311
* @default {}

packages/mui-joy/src/ListItemButton/ListItemButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ ListItemButton.propTypes /* remove-proptypes */ = {
270270
/**
271271
* @ignore
272272
*/
273-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
273+
role: PropTypes.string,
274274
/**
275275
* If `true`, the component is selected.
276276
* @default false

packages/mui-joy/src/RadioGroup/RadioGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ RadioGroup.propTypes /* remove-proptypes */ = {
237237
/**
238238
* @ignore
239239
*/
240-
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
240+
role: PropTypes.string,
241241
/**
242242
* The size of the component.
243243
* @default 'md'

0 commit comments

Comments
 (0)