-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[codemod] Prepare the import path breaking change #11249
Merged
oliviertassinari
merged 1 commit into
mui:v1-beta
from
oliviertassinari:code-mode-path-migration
May 5, 2018
+236
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small glitch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a stress test. Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the other imports are prefixed, so...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People might be aliasing the modules:
import foo from 'bar'
orimport { foo as bar} from 'baz'
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't get why
MuiLinearProgress
rather than justLinearProgress
, but whatever.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It tests that the following transformation is done:
input
output