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

[core] Export everything from the second level #18306

Merged
merged 11 commits into from
Nov 25, 2019
33 changes: 14 additions & 19 deletions packages/material-ui-codemod/src/v4.0.0/top-level-imports.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { dirname } from 'path';
import getJSExports from '../util/getJSExports';
import addImports from 'jscodeshift-add-imports';

// istanbul ignore next
if (process.env.NODE_ENV === 'test') {
const resolve = require.resolve;
require.resolve = source =>
resolve(source.replace(/^@material-ui\/core\/es/, '../../../material-ui/src'));
}

export default function transformer(fileInfo, api, options) {
const j = api.jscodeshift;
const importModule = options.importModule || '@material-ui/core';
const targetModule = options.targetModule || '@material-ui/core';
const whitelist = getJSExports(
require.resolve(`${importModule}/es`, {
paths: [dirname(fileInfo.path)],
}),
);

let requirePath = importModule;

if (process.env.NODE_ENV === 'test') {
requirePath = requirePath.replace(/^@material-ui\/core/, '../../../material-ui/src');
}

// eslint-disable-next-line global-require, import/no-dynamic-require
const whitelist = require(requirePath);

const printOptions = options.printOptions || {
quote: 'single',
trailingComma: true,
Expand All @@ -41,21 +37,20 @@ export default function transformer(fileInfo, api, options) {
path.node.specifiers.forEach((specifier, index) => {
if (specifier.importKind && specifier.importKind !== 'value') return;
if (specifier.type === 'ImportNamespaceSpecifier') return;
const localName = specifier.local.name;

switch (specifier.type) {
case 'ImportNamespaceSpecifier':
return;
case 'ImportDefaultSpecifier': {
const localName = specifier.local.name;
const moduleName = match[1];
if (!whitelist.has(moduleName)) return;
if (whitelist[moduleName] == null) return;
resultSpecifiers.push(
j.importSpecifier(j.identifier(moduleName), j.identifier(localName)),
);
path.get('specifiers', index).prune();
break;
}
case 'ImportSpecifier':
if (!whitelist.has(specifier.imported.name)) return;
if (whitelist[specifier.imported.name] == null) return;
resultSpecifiers.push(specifier);
path.get('specifiers', index).prune();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ describe('@material-ui/codemod', () => {
describe('top-level-imports', () => {
it('convert path as needed', () => {
const actual = transform(
{ source: read('./top-level-imports.test/actual.js'), path: require.resolve('./top-level-imports.test/actual.js') },
{
source: read('./top-level-imports.test/actual.js'),
path: require.resolve('./top-level-imports.test/actual.js'),
},
{ jscodeshift: jscodeshift },
{},
);
Expand All @@ -33,7 +36,10 @@ describe('@material-ui/codemod', () => {

it('should be idempotent', () => {
const actual = transform(
{ source: read('./top-level-imports.test/expected.js'), path: require.resolve('./top-level-imports.test/expected.js') },
{
source: read('./top-level-imports.test/expected.js'),
path: require.resolve('./top-level-imports.test/expected.js'),
},
{ jscodeshift: jscodeshift },
{},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ import InputLabel from '@material-ui/core/InputLabel';
import Input from '@material-ui/core/Input';
import Grow from '@material-ui/core/Grow';
import TableFooter from '@material-ui/core/TableFooter';
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
import withWidth from '@material-ui/core/withWidth';
import { isWidthUp } from '@material-ui/core/withWidth';
Comment on lines +57 to +58
Copy link
Member Author

Choose a reason for hiding this comment

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

@oliviertassinari What happened here?

Copy link
Member

@oliviertassinari oliviertassinari Nov 22, 2019

Choose a reason for hiding this comment

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

Previously, isWidthUp wasn't exported from the barrel index.js. With the latest changes, imports can be changed. However, the codemod doesn't support the following case well:

import withWidth, { isWidthUp } from '@material-ui/core/withWidth';

It needs two runs to handle it correctly. This breaks the idempotent assertion.

I haven't found how to fix the 2 runs issue. Instead, I have changed the import syntax so the codemod test can be idempotent (equivalent to ignoring the problem).

import Zoom from '@material-ui/core/Zoom';
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import ListSubheader from '@material-ui/core/ListSubheader';
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { isWidthUp } from '@material-ui/core/withWidth';

import {
withStyles,
Expand Down Expand Up @@ -58,6 +57,7 @@ import {
Grow,
TableFooter,
withWidth,
isWidthUp,
Zoom,
ClickAwayListener,
ListSubheader,
Expand Down
6 changes: 3 additions & 3 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as React from 'react';
import { StandardProps } from '@material-ui/core';
import { UseAutocompleteProps, CreateFilterOptions } from '../useAutocomplete';
import { UseAutocompleteProps, CreateFilterOptions, createFilterOptions } from '../useAutocomplete';

export { createFilterOptions };

export interface PopperProps extends React.HTMLAttributes<HTMLElement> {
anchorEl?: HTMLElement;
open: boolean;
popperRef: React.Ref<unknown>;
}

export const createFilterOptions: CreateFilterOptions;

export interface RenderOptionState {
inputValue: string;
selected: boolean;
Expand Down
25 changes: 23 additions & 2 deletions packages/material-ui-lab/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
export { default as Autocomplete } from './Autocomplete';
export * from './Autocomplete';

export { default as Rating } from './Rating';
export * from './Rating';

export { default as Skeleton } from './Skeleton';
export * from './Skeleton';

export { default as SpeedDial } from './SpeedDial';
export * from './SpeedDial';

export { default as SpeedDialAction } from './SpeedDialAction';
export * from './SpeedDialAction';

export { default as SpeedDialIcon } from './SpeedDialIcon';
export * from './SpeedDialIcon';

export { default as ToggleButton } from './ToggleButton';
export * from './ToggleButton';

export { default as ToggleButtonGroup } from './ToggleButtonGroup';
export { default as TreeView } from './TreeView';
export * from './ToggleButtonGroup';

export { default as TreeItem } from './TreeItem';
export { default as useAutocomplete, createFilterOptions } from './useAutocomplete';
export * from './TreeItem';

export { default as TreeView } from './TreeView';
export * from './TreeView';

export { default as useAutocomplete } from './useAutocomplete';
export * from './useAutocomplete';
24 changes: 23 additions & 1 deletion packages/material-ui-lab/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
/* eslint-disable import/export */
export { default as Autocomplete } from './Autocomplete';
export * from './Autocomplete';

export { default as Rating } from './Rating';
export * from './Rating';

export { default as Skeleton } from './Skeleton';
export * from './Skeleton';

export { default as SpeedDial } from './SpeedDial';
export * from './SpeedDial';

export { default as SpeedDialAction } from './SpeedDialAction';
export * from './SpeedDialAction';

export { default as SpeedDialIcon } from './SpeedDialIcon';
export * from './SpeedDialIcon';

export { default as ToggleButton } from './ToggleButton';
export * from './ToggleButton';

export { default as ToggleButtonGroup } from './ToggleButtonGroup';
export * from './ToggleButtonGroup';

export { default as TreeItem } from './TreeItem';
export * from './TreeItem';

export { default as TreeView } from './TreeView';
export { default as useAutocomplete, createFilterOptions } from './useAutocomplete';
export * from './TreeView';

// createFilterOptions is exported from Autocomplete
export { default as useAutocomplete } from './useAutocomplete';
32 changes: 29 additions & 3 deletions packages/material-ui-styles/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
export { default as createGenerateClassName } from './createGenerateClassName';
export * from './createGenerateClassName';

export { default as createStyles } from './createStyles';
export * from './createStyles';

export { default as getThemeProps } from './getThemeProps';
export * from './getThemeProps';

export { default as jssPreset } from './jssPreset';
export * from './jssPreset';

export { default as makeStyles } from './makeStyles';
export * from './makeStyles';

export { default as mergeClasses } from './mergeClasses';
export * from './mergeClasses';

export { default as ServerStyleSheets } from './ServerStyleSheets';
export * from './ServerStyleSheets';

export { default as styled } from './styled';
export * from './styled';

export { default as StylesProvider } from './StylesProvider';
export * from './StylesProvider';

export { default as ThemeProvider } from './ThemeProvider';
export { default as useTheme } from '@material-ui/styles/useTheme';
export { default as withStyles, CSSProperties, StyleRules, WithStyles } from './withStyles';
export { default as withTheme, WithTheme, withThemeCreator } from './withTheme';
export * from './ThemeProvider';

export { default as useTheme } from './useTheme';
export * from './useTheme';

export { default as withStyles } from './withStyles';
export * from './withStyles';

export { default as withTheme } from './withTheme';
export * from './withTheme';

export { DefaultTheme } from './defaultTheme';
28 changes: 27 additions & 1 deletion packages/material-ui-styles/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/export */
import { ponyfillGlobal } from '@material-ui/utils';

/* Warning if there are several instances of @material-ui/styles */
Expand Down Expand Up @@ -25,15 +26,40 @@ if (
}

export { default as createGenerateClassName } from './createGenerateClassName';
export * from './createGenerateClassName';

export { default as createStyles } from './createStyles';
export * from './createStyles';

export { default as getThemeProps } from './getThemeProps';
export * from './getThemeProps';

export { default as jssPreset } from './jssPreset';
export * from './jssPreset';

export { default as makeStyles } from './makeStyles';
export * from './makeStyles';

export { default as mergeClasses } from './mergeClasses';
export * from './mergeClasses';

export { default as ServerStyleSheets } from './ServerStyleSheets';
export * from './ServerStyleSheets';

export { default as styled } from './styled';
export * from './styled';

export { default as StylesProvider } from './StylesProvider';
export * from './StylesProvider';

export { default as ThemeProvider } from './ThemeProvider';
export * from './ThemeProvider';

export { default as useTheme } from './useTheme';
export * from './useTheme';

export { default as withStyles } from './withStyles';
export { default as withTheme, withThemeCreator } from './withTheme';
export * from './withStyles';

export { default as withTheme } from './withTheme';
export * from './withTheme';
7 changes: 3 additions & 4 deletions packages/material-ui/src/TableCell/TableCell.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as React from 'react';
import { StandardProps } from '..';
import { Padding, Size } from '../Table';

export { Padding, Size };

/**
* `<TableCell>` will be rendered as an `<th>`or `<td>` depending
Expand All @@ -22,10 +25,6 @@ export interface TableCellProps
export type TableCellBaseProps = React.ThHTMLAttributes<HTMLTableHeaderCellElement> &
React.TdHTMLAttributes<HTMLTableDataCellElement>;

export type Padding = 'default' | 'checkbox' | 'none';

export type Size = 'small' | 'medium';

export type SortDirection = 'asc' | 'desc' | false;

export type TableCellClassKey =
Expand Down
Loading