-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[codemod] Prepare the import path breaking change
- Loading branch information
1 parent
1d76ede
commit 0c7b82e
Showing
9 changed files
with
236 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const entryModuleToFlatten = [ | ||
'Menu', | ||
'Tabs', | ||
'BottomNavigation', | ||
'Card', | ||
'Collapse', | ||
'List', | ||
'Dialog', | ||
'Slide', | ||
'Radio', | ||
'ExpansionPanel', | ||
'GridList', | ||
'Progress', | ||
'Form', | ||
'Fade', | ||
'Stepper', | ||
'Table', | ||
'Input', | ||
'Grow', | ||
]; | ||
|
||
export default function transformer(fileInfo, api, options) { | ||
const j = api.jscodeshift; | ||
let hasModifications = false; | ||
const printOptions = options.printOptions || { | ||
quote: 'single', | ||
trailingComma: true, | ||
}; | ||
|
||
const root = j(fileInfo.source); | ||
const importRegExp = /^material-ui\/(.+)/; | ||
|
||
root.find(j.ImportDeclaration).forEach(path => { | ||
const importPath = path.value.source.value; | ||
let entryModule = importPath.match(importRegExp); | ||
|
||
// Remove non-Material-UI imports | ||
if (!entryModule) { | ||
return; | ||
} | ||
entryModule = entryModule[1].split('/'); | ||
entryModule = entryModule[entryModule.length - 1]; | ||
|
||
// No need to flatten | ||
if (!entryModuleToFlatten.includes(entryModule)) { | ||
return; | ||
} | ||
|
||
hasModifications = true; | ||
// console.log('entryModule', entryModule); | ||
|
||
path.node.specifiers.forEach(specifier => { | ||
const localName = specifier.local.name; | ||
const importedName = specifier.imported ? specifier.imported.name : null; | ||
|
||
if (!importedName) { | ||
const importStatement = j.importDeclaration( | ||
[j.importDefaultSpecifier(j.identifier(localName))], | ||
j.literal(`material-ui/${entryModule}`), | ||
); | ||
|
||
j(path).insertBefore(importStatement); | ||
} else { | ||
const importStatement = j.importDeclaration( | ||
[j.importDefaultSpecifier(j.identifier(localName))], | ||
j.literal(`material-ui/${importedName}`), | ||
); | ||
|
||
j(path).insertBefore(importStatement); | ||
} | ||
}); | ||
|
||
path.prune(); | ||
}); | ||
|
||
return hasModifications ? root.toSource(printOptions) : null; | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/material-ui-codemod/src/v1.0.0/import-path.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { assert } from 'chai'; | ||
import jscodeshift from 'jscodeshift'; | ||
import transform from './import-path'; | ||
|
||
function trim(str) { | ||
return str ? str.replace(/^\s+|\s+$/, '') : ''; | ||
} | ||
|
||
function read(fileName) { | ||
return fs.readFileSync(path.join(__dirname, fileName), 'utf8').toString(); | ||
} | ||
|
||
describe('@material-ui/codemod', () => { | ||
describe('v1.0.0', () => { | ||
describe('import-path', () => { | ||
it('convert path as needed', () => { | ||
const actual = transform( | ||
{ source: read('./import-path.test/actual.js') }, | ||
{ jscodeshift: jscodeshift }, | ||
{}, | ||
); | ||
|
||
const expected = read('./import-path.test/expected.js'); | ||
|
||
assert.strictEqual( | ||
trim(actual), | ||
trim(expected), | ||
'The transformed version should be correct', | ||
); | ||
}); | ||
|
||
it('should be idempotent', () => { | ||
const actual = transform( | ||
{ source: read('./import-path.test/expected.js') }, | ||
{ jscodeshift: jscodeshift }, | ||
{}, | ||
); | ||
|
||
const expected = read('./import-path.test/expected.js'); | ||
|
||
assert.strictEqual( | ||
trim(actual), | ||
trim(expected), | ||
'The transformed version should be correct', | ||
); | ||
}) | ||
}); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
packages/material-ui-codemod/src/v1.0.0/import-path.test/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { withStyles } from 'material-ui/styles' | ||
import { MenuItem } from 'material-ui/Menu'; | ||
import MuiTabs, { Tab } from 'material-ui/Tabs'; | ||
import BottomNavigation, { BottomNavigationAction } from 'material-ui/BottomNavigation'; | ||
import Card, { CardActions, CardContent } from 'material-ui/Card'; | ||
import { CardHeader, CardMedia } from 'material-ui/Card'; | ||
import MuiCollapse from 'material-ui/transitions/Collapse'; | ||
import List, { ListItemIcon, ListItem, ListItemAvatar, ListItemText, ListItemSecondaryAction } from 'material-ui/List'; | ||
import Dialog, { DialogTitle } from 'material-ui/Dialog'; | ||
import { withMobileDialog, DialogActions, DialogContent, DialogContentText } from 'material-ui/Dialog'; | ||
import Slide from 'material-ui/transitions/Slide'; | ||
import Radio, { RadioGroup } from 'material-ui/Radio'; | ||
import { FormControlLabel } from 'material-ui/Form'; | ||
import ExpansionPanel, { ExpansionPanelSummary, ExpansionPanelDetails, ExpansionPanelActions } from 'material-ui/ExpansionPanel'; | ||
import GridList, { GridListTile } from 'material-ui/GridList'; | ||
import { CircularProgress } from 'material-ui/Progress'; | ||
import { LinearProgress as MuiLinearProgress } from 'material-ui/Progress'; | ||
import { FormLabel, FormControl, FormGroup, FormControlLabel, FormHelperText } from 'material-ui/Form'; | ||
import Fade from 'material-ui/transitions/Fade'; | ||
import Stepper, { Step, StepButton, StepContent } from 'material-ui/Stepper'; | ||
import Table, { | ||
TableBody, | ||
TableCell, | ||
TableFooter, | ||
TablePagination, | ||
TableRow, | ||
} from 'material-ui/Table'; | ||
import Input, { InputLabel } from 'material-ui/Input'; | ||
import Grow from 'material-ui/transitions/Grow'; |
56 changes: 56 additions & 0 deletions
56
packages/material-ui-codemod/src/v1.0.0/import-path.test/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react' | ||
import { withStyles } from 'material-ui/styles' | ||
import MenuItem from 'material-ui/MenuItem'; | ||
import Tab from 'material-ui/Tab'; | ||
import MuiTabs from 'material-ui/Tabs'; | ||
import BottomNavigationAction from 'material-ui/BottomNavigationAction'; | ||
import BottomNavigation from 'material-ui/BottomNavigation'; | ||
import CardContent from 'material-ui/CardContent'; | ||
import CardActions from 'material-ui/CardActions'; | ||
import Card from 'material-ui/Card'; | ||
import CardMedia from 'material-ui/CardMedia'; | ||
import CardHeader from 'material-ui/CardHeader'; | ||
import MuiCollapse from 'material-ui/Collapse'; | ||
import ListItemSecondaryAction from 'material-ui/ListItemSecondaryAction'; | ||
import ListItemText from 'material-ui/ListItemText'; | ||
import ListItemAvatar from 'material-ui/ListItemAvatar'; | ||
import ListItem from 'material-ui/ListItem'; | ||
import ListItemIcon from 'material-ui/ListItemIcon'; | ||
import List from 'material-ui/List'; | ||
import DialogTitle from 'material-ui/DialogTitle'; | ||
import Dialog from 'material-ui/Dialog'; | ||
import DialogContentText from 'material-ui/DialogContentText'; | ||
import DialogContent from 'material-ui/DialogContent'; | ||
import DialogActions from 'material-ui/DialogActions'; | ||
import withMobileDialog from 'material-ui/withMobileDialog'; | ||
import Slide from 'material-ui/Slide'; | ||
import RadioGroup from 'material-ui/RadioGroup'; | ||
import Radio from 'material-ui/Radio'; | ||
import FormControlLabel from 'material-ui/FormControlLabel'; | ||
import ExpansionPanelActions from 'material-ui/ExpansionPanelActions'; | ||
import ExpansionPanelDetails from 'material-ui/ExpansionPanelDetails'; | ||
import ExpansionPanelSummary from 'material-ui/ExpansionPanelSummary'; | ||
import ExpansionPanel from 'material-ui/ExpansionPanel'; | ||
import GridListTile from 'material-ui/GridListTile'; | ||
import GridList from 'material-ui/GridList'; | ||
import CircularProgress from 'material-ui/CircularProgress'; | ||
import MuiLinearProgress from 'material-ui/LinearProgress'; | ||
import FormHelperText from 'material-ui/FormHelperText'; | ||
import FormControlLabel from 'material-ui/FormControlLabel'; | ||
import FormGroup from 'material-ui/FormGroup'; | ||
import FormControl from 'material-ui/FormControl'; | ||
import FormLabel from 'material-ui/FormLabel'; | ||
import Fade from 'material-ui/Fade'; | ||
import StepContent from 'material-ui/StepContent'; | ||
import StepButton from 'material-ui/StepButton'; | ||
import Step from 'material-ui/Step'; | ||
import Stepper from 'material-ui/Stepper'; | ||
import TableRow from 'material-ui/TableRow'; | ||
import TablePagination from 'material-ui/TablePagination'; | ||
import TableFooter from 'material-ui/TableFooter'; | ||
import TableCell from 'material-ui/TableCell'; | ||
import TableBody from 'material-ui/TableBody'; | ||
import Table from 'material-ui/Table'; | ||
import InputLabel from 'material-ui/InputLabel'; | ||
import Input from 'material-ui/Input'; | ||
import Grow from 'material-ui/Grow'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters