Last active
August 5, 2019 15:40
-
-
Save felipeleusin/b67b5a63e92812161a47c6dcec35c488 to your computer and use it in GitHub Desktop.
styled-system to theme-ui
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
/** | |
MIT License | |
Copyright (c) 2019 Felipe Leusin | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
const allStyledProps = [ | |
'alignItems', | |
'alignSelf', | |
'bg', | |
'border', | |
'borderColor', | |
'borderRadius', | |
'borderTopLeftRadius', | |
'borderTopRightRadius', | |
'borderBottomLeftRadius', | |
'borderBottomRightRadius', | |
'borderTop', | |
'borderTopColor', | |
'borderRight', | |
'borderRightColor', | |
'borderBottom', | |
'borderBottomColor', | |
'borderLeft', | |
'borderLeftColor', | |
'color', | |
'textColor', | |
'display', | |
'flex', | |
'flexBasis', | |
'flexDirection', | |
'flexWrap', | |
'flexGrow', | |
'flexShrink', | |
'fontWeight', | |
'textDecoration', | |
'justifyContent', | |
'm', | |
'maxWidth', | |
'mb', | |
'minWidth', | |
'ml', | |
'mr', | |
'mr', | |
'mt', | |
'mx', | |
'my', | |
'order', | |
'overflow', | |
'overflowY', | |
'overflowX', | |
'p', | |
'pb', | |
'pl', | |
'pr', | |
'pr', | |
'pt', | |
'px', | |
'py', | |
'position', | |
'top', | |
'left', | |
'bottom', | |
'right', | |
'textAlign', | |
'width', | |
'height', | |
'minHeight', | |
'maxHeight', | |
'textScale', | |
'textStyle', | |
'verticalAlign', | |
'zIndex' | |
]; | |
function isLiteral(type) { | |
return ( | |
type === 'Literal' || type === 'StringLiteral' || type === 'NumericLiteral' | |
); | |
} | |
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
return j(file.source) | |
.find(j.JSXOpeningElement) | |
.forEach(path => { | |
if ( | |
!path.node.attributes || | |
!path.node.attributes.length || | |
path.node.attributes.find(path => 'sx' === path.name) | |
) { | |
return; | |
} | |
let styledProps = []; | |
let otherProps = []; | |
let spreadProps = undefined; | |
path.node.attributes.forEach(path => { | |
if (path.type === 'JSXSpreadAttribute') { | |
spreadProps = path; | |
} else if (path.name && allStyledProps.indexOf(path.name.name) > -1) { | |
styledProps.push(path); | |
} else { | |
otherProps.push(path); | |
} | |
}); | |
let sxProps = []; | |
styledProps.forEach(path => { | |
if (isLiteral(path.value.type)) { | |
sxProps.push( | |
j.property('init', j.identifier(path.name.name), path.value) | |
); | |
} else if (path.value.type === 'JSXExpressionContainer') { | |
let expression = path.value.expression; | |
sxProps.push( | |
j.property('init', j.identifier(path.name.name), expression) | |
); | |
} else { | |
otherProps.push(path); | |
} | |
}); | |
if (sxProps.length > 0) { | |
if (spreadProps) { | |
sxProps.push( | |
j.spreadElement( | |
j.memberExpression( | |
j.identifier(spreadProps.argument.name), | |
j.identifier('sx'), | |
false | |
) | |
) | |
); | |
} | |
let sxProp = j.jsxAttribute( | |
j.jsxIdentifier('sx'), | |
j.jsxExpressionContainer(j.objectExpression(sxProps)) | |
); | |
if (spreadProps) { | |
path.node.attributes = [...otherProps, spreadProps, sxProp]; | |
} else { | |
path.node.attributes = [...otherProps, sxProp]; | |
} | |
} | |
}) | |
.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can run this with https://github.com/facebook/jscodeshift