Skip to content

Commit

Permalink
[core] Fix prop-types generation (#38831)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored Sep 11, 2023
1 parent 80f8e00 commit cda1ffe
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 214 deletions.
33 changes: 23 additions & 10 deletions packages/api-docs-builder/utils/getPropsFromComponentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,38 @@ export interface ParsedComponent {
}

function isTypeJSXElementLike(type: ts.Type, project: TypeScriptProject): boolean {
const symbol = type.symbol ?? type.aliasSymbol;
if (symbol) {
const name = project.checker.getFullyQualifiedName(symbol);
return (
// Remove once global JSX namespace is no longer used by React
name === 'global.JSX.Element' ||
name === 'React.JSX.Element' ||
name === 'React.ReactElement' ||
name === 'React.ReactNode'
);
}

if (type.isUnion()) {
return type.types.every(
// eslint-disable-next-line no-bitwise
(subType) => subType.flags & ts.TypeFlags.Null || isTypeJSXElementLike(subType, project),
);
}
if (type.symbol) {
const name = project.checker.getFullyQualifiedName(type.symbol);
return (
// Remove once global JSX namespace is no longer used by React
name === 'global.JSX.Element' || name === 'React.JSX.Element' || name === 'React.ReactElement'
);
}

return false;
}

function isStyledFunction(node: ts.VariableDeclaration): boolean {
return (
!!node.initializer &&
ts.isCallExpression(node.initializer) &&
ts.isCallExpression(node.initializer.expression) &&
ts.isIdentifier(node.initializer.expression.expression) &&
node.initializer.expression.expression.escapedText === 'styled'
);
}

function getJSXLikeReturnValueFromFunction(type: ts.Type, project: TypeScriptProject) {
return type
.getCallSignatures()
Expand Down Expand Up @@ -250,14 +265,13 @@ function getPropsFromVariableDeclaration({
});
}
}

return null;
}

// handle component factories: x = createComponent()
if (
checkDeclarations &&
node.initializer &&
!isStyledFunction(node) &&
getJSXLikeReturnValueFromFunction(type, project).length > 0
) {
return parseFunctionComponent({
Expand All @@ -277,7 +291,6 @@ export function getPropsFromComponentNode({
checkDeclarations,
}: GetPropsFromComponentDeclarationOptions): ParsedComponent | null {
let parsedComponent: ParsedComponent | null = null;

// function x(props: type) { return <div/> }
if (
ts.isFunctionDeclaration(node) &&
Expand Down
106 changes: 7 additions & 99 deletions packages/mui-material-next/src/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Identity<Type>(x: Type): Type {
return x;
}

const SliderRoot = styled('span', {
export const SliderRoot = styled('span', {
name: 'MuiSlider',
slot: 'Root',
overridesResolver: (props, styles) => {
Expand Down Expand Up @@ -95,21 +95,7 @@ const SliderRoot = styled('span', {
},
};
});

SliderRoot.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderRoot };

const SliderRail = styled('span', {
export const SliderRail = styled('span', {
name: 'MuiSlider',
slot: 'Rail',
overridesResolver: (props, styles) => styles.rail,
Expand All @@ -136,20 +122,7 @@ const SliderRail = styled('span', {
}),
}));

SliderRail.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderRail };

const SliderTrack = styled('span', {
export const SliderTrack = styled('span', {
name: 'MuiSlider',
slot: 'Track',
overridesResolver: (props, styles) => styles.track,
Expand Down Expand Up @@ -184,20 +157,7 @@ const SliderTrack = styled('span', {
};
});

SliderTrack.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderTrack };

const SliderThumb = styled('span', {
export const SliderThumb = styled('span', {
name: 'MuiSlider',
slot: 'Thumb',
shouldForwardProp: (prop) => shouldForwardProp(prop) && prop !== 'isOverlapping',
Expand Down Expand Up @@ -285,20 +245,7 @@ const SliderThumb = styled('span', {
};
});

SliderThumb.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderThumb };

const SliderValueLabel = styled('span', {
export const SliderValueLabel = styled('span', {
name: 'MuiSlider',
slot: 'ValueLabel',
shouldForwardProp: (prop) =>
Expand Down Expand Up @@ -382,20 +329,7 @@ const SliderValueLabel = styled('span', {
};
});

SliderValueLabel.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.element,
} as any;

export { SliderValueLabel };

const SliderMark = styled('span', {
export const SliderMark = styled('span', {
name: 'MuiSlider',
slot: 'Mark',
shouldForwardProp: (prop) => shouldForwardProp(prop) && prop !== 'markActive',
Expand Down Expand Up @@ -429,20 +363,7 @@ const SliderMark = styled('span', {
}),
);

SliderMark.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderMark };

const SliderMarkLabel = styled('span', {
export const SliderMarkLabel = styled('span', {
name: 'MuiSlider',
slot: 'MarkLabel',
shouldForwardProp: (prop) => shouldForwardProp(prop) && prop !== 'markLabelActive',
Expand Down Expand Up @@ -481,19 +402,6 @@ const SliderMarkLabel = styled('span', {
},
);

SliderMarkLabel.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* @ignore
*/
children: PropTypes.node,
} as any;

export { SliderMarkLabel };

const useUtilityClasses = (ownerState: SliderOwnerState) => {
const { disabled, dragging, marked, orientation, track, classes, color, size } = ownerState;

Expand Down
Loading

0 comments on commit cda1ffe

Please sign in to comment.