Skip to content

Commit

Permalink
[core] Enforce import * as React (#34878)
Browse files Browse the repository at this point in the history
  • Loading branch information
da0x authored Oct 31, 2022
1 parent a0606ca commit 2a77a33
Show file tree
Hide file tree
Showing 38 changed files with 52 additions and 42 deletions.
18 changes: 14 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const { rules: baseStyleRules } = require('eslint-config-airbnb-base/rules/style');

const forbidTopLevelMessage = [
'Prefer one level nested imports to avoid bundling everything in dev mode',
Expand All @@ -19,10 +20,10 @@ module.exports = {
node: true,
},
extends: [
'plugin:import/recommended',
'plugin:import/typescript',
'airbnb-typescript',
'prettier',
'plugin:eslint-plugin-import/recommended',
'plugin:eslint-plugin-import/typescript',
'eslint-config-airbnb-typescript',
'eslint-config-prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand Down Expand Up @@ -161,6 +162,15 @@ module.exports = {
'react/static-property-placement': 'off',
// Currently not in recommended ruleset but catches real bugs.
'react/no-unstable-nested-components': 'error',
'no-restricted-syntax': [
// See https://github.com/eslint/eslint/issues/9192 for why it's needed
...baseStyleRules['no-restricted-syntax'],
{
message:
"Do not import default from React. Use a namespace import (import * as React from 'react';) instead.",
selector: 'ImportDeclaration[source.value="react"] ImportDefaultSpecifier',
},
],
},
overrides: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import Button from '@mui/material/Button';
import { SnackbarProvider, useSnackbar } from 'notistack';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import Button from '@mui/material/Button';
import { SnackbarProvider, VariantType, useSnackbar } from 'notistack';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/prefer-default-export */
import React from 'react';
import * as React from 'react';
import { CacheProvider } from '@emotion/react';
import getEmotionCache from './getEmotionCache';

Expand Down
2 changes: 1 addition & 1 deletion examples/mui-base-with-tailwind-css/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
Expand Down
2 changes: 1 addition & 1 deletion examples/tailwind-css/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { createTheme, StyledEngineProvider, ThemeProvider } from '@mui/material/styles';
import { CssBaseline } from '@mui/material';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps, Simplify } from '@mui/types';
import { UseButtonParameters, UseButtonRootSlotProps } from './useButton.types';
import { SlotComponentProps } from '../utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/InputUnstyled/InputUnstyled.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps, Simplify } from '@mui/types';
import { FormControlUnstyledState } from '../FormControlUnstyled';
import { UseInputParameters, UseInputRootSlotProps } from './useInput.types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import {
ListboxState,
UseListboxPropsWithDefaults,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/MenuUnstyled/MenuUnstyled.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OverrideProps } from '@mui/types';
import React from 'react';
import * as React from 'react';
import PopperUnstyled, { PopperUnstyledProps } from '../PopperUnstyled';
import { SlotComponentProps } from '../utils';
import { UseMenuListboxSlotProps } from './useMenu.types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/ModalUnstyled/ModalUnstyled.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OverridableComponent, OverridableTypeMap, OverrideProps } from '@mui/types';
import React from 'react';
import * as React from 'react';
import { PortalProps } from '../Portal';
import { SlotComponentProps } from '../utils';
import { ModalUnstyledClasses } from './modalUnstyledClasses';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import PropTypes from 'prop-types';
import { OverridableComponent } from '@mui/types';
import composeClasses from '../composeClasses';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OverrideProps } from '@mui/types';
import React from 'react';
import * as React from 'react';
import { SlotComponentProps } from '../utils';

export interface OptionGroupUnstyledComponentsPropsOverrides {}
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/OptionUnstyled/OptionUnstyled.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useForkRef as useForkRef } from '@mui/utils';
import { OptionState } from '../ListboxUnstyled';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { DefaultComponentProps, OverrideProps, Simplify } from '@mui/types';
import { OptionState } from '../ListboxUnstyled';
import { UseSelectOptionSlotProps } from '../SelectUnstyled/useSelect.types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/SelectUnstyled/useSelect.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { UseButtonRootSlotProps } from '../ButtonUnstyled';
import {
OptionState,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/SelectUnstyled/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OptionUnstyledProps } from '../OptionUnstyled';
import { OptionGroupUnstyledProps } from '../OptionGroupUnstyled';
import { isOptionGroup, SelectChild, SelectOption, SelectOptionGroup } from './useSelect.types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OverridableComponent, OverridableTypeMap, OverrideProps } from '@mui/types';
import React from 'react';
import * as React from 'react';
import { SlotComponentProps } from '../utils';
import { SliderUnstyledClasses } from './sliderUnstyledClasses';
import SliderValueLabelUnstyled from './SliderValueLabelUnstyled';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/SliderUnstyled/useSlider.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';

export interface UseSliderParameters {
'aria-labelledby'?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps } from '@mui/types';
import ClickAwayListener, { ClickAwayListenerProps } from '../ClickAwayListener';
import { UseSnackbarParameters } from './useSnackbar.types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/SwitchUnstyled/useSwitch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import React from 'react';
import * as React from 'react';
import { spy } from 'sinon';
import {
act,
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/SwitchUnstyled/useSwitch.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { HTMLInputTypeAttribute } from 'react';
import * as React from 'react';

export interface UseSwitchParameters {
/**
Expand Down Expand Up @@ -44,7 +44,7 @@ interface UseSwitchInputSlotOwnProps {
readOnly?: boolean;
ref: React.Ref<any>;
required?: boolean;
type: HTMLInputTypeAttribute;
type: React.HTMLInputTypeAttribute;
}

export type UseSwitchInputSlotProps<TOther = {}> = Omit<TOther, keyof UseSwitchInputSlotOwnProps> &
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/TabUnstyled/TabUnstyled.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OverrideProps, Simplify } from '@mui/types';
import React from 'react';
import * as React from 'react';
import { ButtonUnstyledOwnProps } from '../ButtonUnstyled';
import { SlotComponentProps } from '../utils';
import { UseTabRootSlotProps } from './useTab.types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/TabUnstyled/useTab.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { UseButtonRootSlotProps } from '../ButtonUnstyled';

export interface UseTabParameters {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps } from '@mui/types';
import { UseTabsListRootSlotProps } from './useTabsList.types';
import { SlotComponentProps } from '../utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/TabsUnstyled/TabsUnstyled.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps } from '@mui/types';
import { SlotComponentProps } from '../utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/utils/isHostComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';

/**
* Determines if a given element is a DOM element name (i.e. not a React component).
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/utils/useSlotProps.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { EventHandlers } from '@mui/base';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import clsx from "clsx";
/*
Comments that should not be lost when the clsx import comments are preserved.
*/
import React from "react";
import * as React from "react";
import { makeStyles } from "@material-ui/core";

const useStyles = makeStyles(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ any comments that they get combined with.
/*
Comments that should not be lost when the clsx import comments are preserved.
*/
import React from "react";
import * as React from "react";
import { makeStyles } from 'tss-react/mui';

// TODO jss-to-tss-react codemod: Unable to handle style definition reliably. ArrowFunctionExpression in CSS prop.
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Button/ButtonProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import {
OverridableComponent,
OverridableStringUnion,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Grid/GridProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps } from '@mui/types';
import { GridBaseProps } from '@mui/system/Unstable_Grid';
import { SxProps, SystemProps } from '../styles/types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/IconButton/IconButtonProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import {
OverridableComponent,
OverridableStringUnion,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Input/InputProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverridableStringUnion, OverrideProps } from '@mui/types';
import { SlotComponentProps } from '@mui/base/utils';
import { ColorPaletteProp, VariantProp, SxProps, ApplyColorInversion } from '../styles/types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Link/LinkProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverridableStringUnion, OverrideProps } from '@mui/types';
import { SlotComponentProps } from '@mui/base/utils';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Select/SelectProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverridableStringUnion, OverrideProps } from '@mui/types';
import { SelectUnstyledCommonProps, SelectOption } from '@mui/base/SelectUnstyled';
import { PopperUnstyledOwnProps } from '@mui/base/PopperUnstyled';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-joy/src/Textarea/TextareaProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverridableStringUnion, OverrideProps } from '@mui/types';
import { SlotComponentProps } from '@mui/base/utils';
import { ColorPaletteProp, VariantProp, SxProps, ApplyColorInversion } from '../styles/types';
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Unstable_Grid2/Grid2Props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { OverrideProps } from '@mui/types';
import { SxProps, SystemProps } from '@mui/system';
import { GridBaseProps } from '@mui/system/Unstable_Grid';
Expand Down

0 comments on commit 2a77a33

Please sign in to comment.