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

[code-infra] Inject display name #44067

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = function getBabelConfig(api) {
mode: 'unsafe-wrap',
},
],
['babel-plugin-transform-react-remove-display-name'],
[
'transform-inline-environment-variables',
{
Expand Down
2 changes: 1 addition & 1 deletion docs/translations/api-docs-base/menu-item/menu-item.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"componentDescription": "An unstyled menu item to be used within a Menu.",
"componentDescription": "",
"propDescriptions": {
"disabled": { "description": "If <code>true</code>, the menu item will be disabled." },
"disableFocusOnHover": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"babel-plugin-optimize-clsx": "^2.6.2",
"babel-plugin-react-remove-properties": "^0.3.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
"babel-plugin-transform-react-remove-display-name": "^1.1.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"chalk": "^5.3.0",
"compression-webpack-plugin": "^11.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ export interface InjectPropTypesInFileOptions
propType: PropTypeDefinition,
) => ((a: LiteralType, b: LiteralType) => number) | undefined;
/**
* Options passed to babel.transformSync
* Options passed to babel.transformSync.
*/
babelOptions?: babel.TransformOptions;
/**
* Inject display name into the component.
*/
includeDisplayName?: boolean;
}

/**
Expand Down Expand Up @@ -175,6 +179,7 @@ function createBabelPlugin({
let alreadyImported = false;
const originalPropTypesPaths = new Map<string, babel.NodePath>();
const previousPropTypesSources = new Map<string, Map<string, string>>();
const originalDisplayNamePaths = new Map<string, babel.NodePath>();

function injectPropTypes(injectOptions: {
path: babel.NodePath;
Expand Down Expand Up @@ -206,9 +211,13 @@ function createBabelPlugin({

const originalPropTypesPath = originalPropTypesPaths.get(nodeName);

let newPropTypesPath;

// `Component.propTypes` already exists
if (originalPropTypesPath) {
originalPropTypesPath.replaceWith(babel.template.ast(placeholder) as babel.Node);
[newPropTypesPath] = originalPropTypesPath.replaceWith(
babel.template.ast(placeholder) as babel.Node,
);
} else if (!emptyPropTypes && babelTypes.isExportNamedDeclaration(path.parent)) {
// in:
// export function Component() {}
Expand All @@ -217,7 +226,7 @@ function createBabelPlugin({
// Component.propTypes = {}
// export { Component }
path.insertAfter(babel.template.ast(`export { ${nodeName} };`));
path.insertAfter(babel.template.ast(placeholder));
[newPropTypesPath] = path.insertAfter(babel.template.ast(placeholder));
path.parentPath!.replaceWith(path.node);
} else if (!emptyPropTypes && babelTypes.isExportDefaultDeclaration(path.parent)) {
// in:
Expand All @@ -227,10 +236,23 @@ function createBabelPlugin({
// Component.propTypes = {}
// export default Component
path.insertAfter(babel.template.ast(`export default ${nodeName};`));
path.insertAfter(babel.template.ast(placeholder));
[newPropTypesPath] = path.insertAfter(babel.template.ast(placeholder));
path.parentPath!.replaceWith(path.node);
} else {
path.insertAfter(babel.template.ast(placeholder));
[newPropTypesPath] = path.insertAfter(babel.template.ast(placeholder));
}

if (options.includeDisplayName) {
const originalDisplayNamePath = originalDisplayNamePaths.get(nodeName);
if (originalDisplayNamePath) {
originalDisplayNamePath.replaceWith(
babel.template.ast(`${nodeName}.displayName = '${nodeName}';`) as babel.Node,
);
} else {
(newPropTypesPath || path).insertAfter(
babel.template.ast(`${nodeName}.displayName = '${nodeName}';`),
);
}
}
}

Expand Down Expand Up @@ -300,6 +322,18 @@ function createBabelPlugin({
});
}
}

// detect displayName assignments
if (
babelTypes.isExpressionStatement(node) &&
babelTypes.isAssignmentExpression(node.expression) &&
babelTypes.isMemberExpression(node.expression.left) &&
babelTypes.isIdentifier(node.expression.left.property, { name: 'displayName' }) &&
babelTypes.isIdentifier(node.expression.left.object)
) {
const componentName = node.expression.left.object.name;
originalDisplayNamePaths.set(componentName, nodePath);
}
});
},
exit(path) {
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,6 @@ Badge.propTypes /* remove-proptypes */ = {
}),
} as any;

Badge.displayName = 'Badge';

export { Badge };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,6 @@ Button.propTypes /* remove-proptypes */ = {
to: PropTypes.string,
} as any;

Button.displayName = 'Button';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about?

Suggested change
Button.displayName = 'Button';
if(process.env.NODE_ENV !== 'production') {
Button.displayName = 'Button';
}

this would mean:

  1. be sure there is no regressions
  2. give developers developer confidence that this isn't going to bloat their bundle
  3. In the code ejection story, we could automatically wrap this, but I would expect people to also copy and paste the source too

Copy link
Member Author

@Janpot Janpot Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it but went for the current solution because

  • Parity with the proptypes generation. Both work the exact same way: upsert code => wrap in build step. If you understand one, you also understand the other. If we add in the conditional for displayName, then I think we should do the same for propTypes and remove the babel plugin.
  • It's more straightforward and robust for the detection logic if it consists of a single top-level statement statement.
  • It prevents people from getting tempted adding more things in the coditional. Which in turn would either break the detection logic, or make it much more complex.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parity with the proptypes generation

Ah right ok, I forgot, I thought we were doing the opposite. Makes sense then 👍


export { Button };
2 changes: 2 additions & 0 deletions packages/mui-base/src/ClickAwayListener/ClickAwayListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ ClickAwayListener.propTypes /* remove-proptypes */ = {
touchEvent: PropTypes.oneOf(['onTouchEnd', 'onTouchStart', false]),
} as any;

ClickAwayListener.displayName = 'ClickAwayListener';

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
(ClickAwayListener as any)['propTypes' + ''] = exactProp(ClickAwayListener.propTypes);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Dropdown.propTypes /* remove-proptypes */ = {
open: PropTypes.bool,
} as any;

Dropdown.displayName = 'Dropdown';

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
(Dropdown as any)['propTypes' + ''] = exactProp(Dropdown.propTypes);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/FocusTrap/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ FocusTrap.propTypes /* remove-proptypes */ = {
open: PropTypes.bool.isRequired,
} as any;

FocusTrap.displayName = 'FocusTrap';

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
(FocusTrap as any)['propTypes' + ''] = exactProp(FocusTrap.propTypes);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/FormControl/FormControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,6 @@ FormControl.propTypes /* remove-proptypes */ = {
value: PropTypes.any,
} as any;

FormControl.displayName = 'FormControl';

export { FormControl };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,6 @@ Input.propTypes /* remove-proptypes */ = {
value: PropTypes.any,
} as any;

Input.displayName = 'Input';

export { Input };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@ Menu.propTypes /* remove-proptypes */ = {
}),
} as any;

Menu.displayName = 'Menu';

export { Menu };
2 changes: 2 additions & 0 deletions packages/mui-base/src/MenuButton/MenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@ MenuButton.propTypes /* remove-proptypes */ = {
}),
} as any;

MenuButton.displayName = 'MenuButton';

export { MenuButton };
4 changes: 4 additions & 0 deletions packages/mui-base/src/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const InnerMenuItem = React.memo(
* - [MenuItem API](https://mui.com/base-ui/react-menu/components-api/#menu-item)
*/

InnerMenuItem.displayName = 'InnerMenuItem';

const MenuItem = React.forwardRef(function MenuItem(
props: MenuItemProps,
ref: React.ForwardedRef<Element>,
Expand Down Expand Up @@ -147,4 +149,6 @@ MenuItem.propTypes /* remove-proptypes */ = {
}),
} as any;

MenuItem.displayName = 'MenuItem';

export { MenuItem };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,6 @@ Modal.propTypes /* remove-proptypes */ = {
}),
} as any;

Modal.displayName = 'Modal';

export { Modal };
2 changes: 2 additions & 0 deletions packages/mui-base/src/NoSsr/NoSsr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ NoSsr.propTypes /* remove-proptypes */ = {
fallback: PropTypes.node,
} as any;

NoSsr.displayName = 'NoSsr';

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
(NoSsr as any)['propTypes' + ''] = exactProp(NoSsr.propTypes);
Expand Down
4 changes: 4 additions & 0 deletions packages/mui-base/src/Option/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ InnerOption.propTypes /* remove-proptypes */ = {
value: PropTypes.any.isRequired,
} as any;

InnerOption.displayName = 'InnerOption';

const InnerOptionMemo = React.memo(InnerOption);

/**
Expand Down Expand Up @@ -180,4 +182,6 @@ Option.propTypes /* remove-proptypes */ = {
value: PropTypes.any.isRequired,
} as any;

Option.displayName = 'Option';

export { Option };
2 changes: 2 additions & 0 deletions packages/mui-base/src/OptionGroup/OptionGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,6 @@ OptionGroup.propTypes /* remove-proptypes */ = {
}),
} as any;

OptionGroup.displayName = 'OptionGroup';

export { OptionGroup };
4 changes: 4 additions & 0 deletions packages/mui-base/src/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ PopperTooltip.propTypes /* remove-proptypes */ = {
TransitionProps: PropTypes.object,
} as any;

PopperTooltip.displayName = 'PopperTooltip';

/**
* Poppers rely on the 3rd party library [Popper.js](https://popper.js.org/docs/v2/) for positioning.
*
Expand Down Expand Up @@ -723,4 +725,6 @@ Popper.propTypes /* remove-proptypes */ = {
transition: PropTypes.bool,
} as any;

Popper.displayName = 'Popper';

export { Popper };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Portal.propTypes /* remove-proptypes */ = {
disablePortal: PropTypes.bool,
} as any;

Portal.displayName = 'Portal';

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
(Portal as any)['propTypes' + ''] = exactProp((Portal as any).propTypes);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-base/src/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ Select.propTypes /* remove-proptypes */ = {
value: PropTypes.any,
} as any;

Select.displayName = 'Select';

export { Select };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,6 @@ Slider.propTypes /* remove-proptypes */ = {
valueLabelFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
} as any;

Slider.displayName = 'Slider';

export { Slider };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Snackbar/Snackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,6 @@ Snackbar.propTypes /* remove-proptypes */ = {
}),
} as any;

Snackbar.displayName = 'Snackbar';

export { Snackbar };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,6 @@ Switch.propTypes /* remove-proptypes */ = {
}),
} as any;

Switch.displayName = 'Switch';

export { Switch };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ Tab.propTypes /* remove-proptypes */ = {
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
} as any;

Tab.displayName = 'Tab';

export { Tab };
2 changes: 2 additions & 0 deletions packages/mui-base/src/TabPanel/TabPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ TabPanel.propTypes /* remove-proptypes */ = {
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
} as any;

TabPanel.displayName = 'TabPanel';

export { TabPanel };
2 changes: 2 additions & 0 deletions packages/mui-base/src/TablePagination/TablePagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,6 @@ TablePagination.propTypes /* remove-proptypes */ = {
}),
} as any;

TablePagination.displayName = 'TablePagination';

export { TablePagination };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@ Tabs.propTypes /* remove-proptypes */ = {
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
} as any;

Tabs.displayName = 'Tabs';

export { Tabs };
2 changes: 2 additions & 0 deletions packages/mui-base/src/TabsList/TabsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ TabsList.propTypes /* remove-proptypes */ = {
}),
} as any;

TabsList.displayName = 'TabsList';

export { TabsList };
2 changes: 2 additions & 0 deletions packages/mui-base/src/TextareaAutosize/TextareaAutosize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,6 @@ TextareaAutosize.propTypes /* remove-proptypes */ = {
]),
} as any;

TextareaAutosize.displayName = 'TextareaAutosize';

export { TextareaAutosize };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Unstable_NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,6 @@ NumberInput.propTypes /* remove-proptypes */ = {
value: PropTypes.number,
} as any;

NumberInput.displayName = 'NumberInput';

export { NumberInput };
2 changes: 2 additions & 0 deletions packages/mui-base/src/Unstable_Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,6 @@ Popup.propTypes /* remove-proptypes */ = {
strategy: PropTypes.oneOf(['absolute', 'fixed']),
} as any;

Popup.displayName = 'Popup';

export { Popup };
2 changes: 2 additions & 0 deletions packages/mui-joy/src/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,6 @@ Accordion.propTypes /* remove-proptypes */ = {
variant: PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
} as any;

Accordion.displayName = 'Accordion';

export default Accordion;
2 changes: 2 additions & 0 deletions packages/mui-joy/src/AccordionDetails/AccordionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,6 @@ AccordionDetails.propTypes /* remove-proptypes */ = {
variant: PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
} as any;

AccordionDetails.displayName = 'AccordionDetails';

export default AccordionDetails;
2 changes: 2 additions & 0 deletions packages/mui-joy/src/AccordionGroup/AccordionGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,6 @@ AccordionGroup.propTypes /* remove-proptypes */ = {
]),
} as any;

AccordionGroup.displayName = 'AccordionGroup';

export default AccordionGroup;
2 changes: 2 additions & 0 deletions packages/mui-joy/src/AccordionSummary/AccordionSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,6 @@ AccordionSummary.propTypes /* remove-proptypes */ = {
variant: PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
} as any;

AccordionSummary.displayName = 'AccordionSummary';

export default AccordionSummary;
2 changes: 2 additions & 0 deletions packages/mui-joy/src/Alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,6 @@ Alert.propTypes /* remove-proptypes */ = {
]),
} as any;

Alert.displayName = 'Alert';

export default Alert;
2 changes: 2 additions & 0 deletions packages/mui-joy/src/AspectRatio/AspectRatio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,6 @@ AspectRatio.propTypes /* remove-proptypes */ = {
]),
} as any;

AspectRatio.displayName = 'AspectRatio';

export default AspectRatio;
Loading
Loading