-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): add in codemod for size prop updates
- Loading branch information
Showing
4 changed files
with
475 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import fs from 'fs'; | ||
|
||
const defaultOptions = { | ||
quote: 'auto', | ||
trailingComma: true, | ||
}; | ||
|
||
function transform(fileInfo, api, options) { | ||
const printOptions = options.printOptions || defaultOptions; | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
function replacePropForComponent(name) { | ||
// Multiselect.Filterable has a different AST structure, so we need to | ||
// adjust the query to find it properly. If it is not 'Filterable', we use | ||
// the default query | ||
let isFilterable = name === 'Filterable'; | ||
let openingElementQuery = { | ||
name: { | ||
name, | ||
}, | ||
}; | ||
if (isFilterable) { | ||
openingElementQuery = { | ||
name: { | ||
object: { | ||
name: 'MultiSelect', | ||
}, | ||
property: { | ||
name: name, | ||
}, | ||
}, | ||
}; | ||
} | ||
root | ||
.find(j.JSXOpeningElement, openingElementQuery) | ||
.forEach((openingElement) => { | ||
// Multiselect.Filterable has a different AST structure, so the name is located differently | ||
const { name } = isFilterable | ||
? openingElement.node.name.property | ||
: openingElement.node.name; | ||
|
||
// Some components were originally set to `xl`, but are being reduced to `lg` | ||
const downsizedComponents = [ | ||
'Accordion', | ||
'ComboBox', | ||
'ContentSwitcher', | ||
'DatePickerInput', | ||
'Dropdown', | ||
'MultiSelect', | ||
'Filterable', | ||
'NumberInput', | ||
'OverflowMenu', | ||
'Search', | ||
'Select', | ||
'TextInput', | ||
'TimePicker', | ||
]; | ||
|
||
const isDownsized = downsizedComponents.includes(name); | ||
|
||
j(openingElement) | ||
.find(j.JSXAttribute, { | ||
name: { | ||
name: 'size', | ||
}, | ||
}) | ||
.forEach((path) => { | ||
j(path).replaceWith((nodePath) => { | ||
const { node } = nodePath; | ||
const { value } = node.value; | ||
|
||
switch (value) { | ||
case 'compact': | ||
node.value.value = 'xs'; | ||
return node; | ||
case 'short': | ||
case 'small': | ||
node.value.value = 'sm'; | ||
return node; | ||
case 'field': | ||
node.value.value = 'md'; | ||
return node; | ||
case 'default': | ||
case 'normal': | ||
node.value.value = 'lg'; | ||
return node; | ||
case 'tall': | ||
case 'lg': | ||
node.value.value = isDownsized ? 'md' : 'xl'; | ||
return node; | ||
case 'xl': | ||
node.value.value = isDownsized ? 'lg' : '2xl'; | ||
return node; | ||
} | ||
return node; | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
const components = [ | ||
'Accordion', | ||
'Button', | ||
'ComboBox', | ||
'ContentSwitcher', | ||
'DataTable', | ||
'DatePickerInput', | ||
'Dropdown', | ||
'FileUploader', | ||
'FileUploaderButton', | ||
'FileUploaderDropContainer', | ||
'FileUploaderItem', | ||
'MultiSelect', | ||
'Filterable', | ||
'NumberInput', | ||
'OverflowMenu', | ||
'Search', | ||
'Select', | ||
'Table', | ||
'TableToolbar', | ||
'TextInput', | ||
'TimePicker', | ||
]; | ||
for (const component of components) { | ||
replacePropForComponent(component); | ||
} | ||
|
||
// Remove before merging | ||
// Prints test file to `Test` folder in React package | ||
fs.writeFile( | ||
'../packages/react/src/components/Test/output.js', | ||
root.toSource(), | ||
(err) => { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log('File written'); | ||
} | ||
} | ||
); | ||
|
||
return root.toSource(printOptions); | ||
} | ||
|
||
module.exports = transform; |
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
Oops, something went wrong.