@material-ui/codemod
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -69,3 +69,65 @@ "use strict"; | ||
} | ||
/** | ||
* Update all `spacing.unit` usages to use `spacing()`. | ||
* Find and replace string literal AST nodes to ensure all spacing API usages get updated, regardless | ||
* of any calculation being performed. | ||
* @param {jscodeshift_api_object} j | ||
* @param {jscodeshift_ast_object} root | ||
*/ | ||
function transformThemeSpacingApiDestructured(j, root) { | ||
var mightContainApi = function mightContainApi(path) { | ||
return j(path).find(j.MemberExpression, { | ||
object: { | ||
name: 'spacing' | ||
}, | ||
property: { | ||
name: 'unit' | ||
} | ||
}).size() > 0; | ||
}; | ||
var replaceApi = function replaceApi(pathArg) { | ||
pathArg.find(j.MemberExpression, { | ||
object: { | ||
name: 'spacing' | ||
}, | ||
property: { | ||
name: 'unit' | ||
} | ||
}).replaceWith(function (path) { | ||
var param = null; | ||
var spacingParam = path.node.object.name; | ||
if (j.BinaryExpression.check(path.parent.node)) { | ||
var expression = path.parent.node; | ||
var operation = expression.operator; // check if it's a variable | ||
if (j.Identifier.check(expression.right)) { | ||
param = expression.right; | ||
} else if (j.Literal.check(expression.right)) { | ||
var value = expression.right.value; | ||
if (operation === '*' || operation === '/') { | ||
param = j.literal(eval("1 ".concat(operation, " ").concat(value))); | ||
} | ||
} | ||
} | ||
if (param) { | ||
path.parent.replace(j.callExpression(j.identifier(spacingParam), [param])); | ||
return path.node; | ||
} | ||
return j.callExpression(j.identifier(spacingParam), [j.literal(1)]); | ||
}); | ||
}; | ||
var arrowFunctions = root.find(j.ArrowFunctionExpression).filter(mightContainApi); | ||
var functionDeclarations = root.find(j.FunctionDeclaration).filter(mightContainApi); | ||
replaceApi(arrowFunctions); | ||
replaceApi(functionDeclarations); | ||
} | ||
module.exports = function transformer(fileInfo, api) { | ||
@@ -76,2 +138,3 @@ var j = api.jscodeshift; | ||
transformThemeSpacingApi(j, root); | ||
transformThemeSpacingApiDestructured(j, root); | ||
return root.toSource({ | ||
@@ -78,0 +141,0 @@ quote: 'single' |
{ | ||
"name": "@material-ui/codemod", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "author": "Material-UI Team", |
@@ -81,2 +81,69 @@ /* eslint-disable no-eval */ | ||
/** | ||
* Update all `spacing.unit` usages to use `spacing()`. | ||
* Find and replace string literal AST nodes to ensure all spacing API usages get updated, regardless | ||
* of any calculation being performed. | ||
* @param {jscodeshift_api_object} j | ||
* @param {jscodeshift_ast_object} root | ||
*/ | ||
function transformThemeSpacingApiDestructured(j, root) { | ||
const mightContainApi = path => { | ||
return ( | ||
j(path) | ||
.find(j.MemberExpression, { | ||
object: { | ||
name: 'spacing', | ||
}, | ||
property: { | ||
name: 'unit', | ||
}, | ||
}) | ||
.size() > 0 | ||
); | ||
}; | ||
const replaceApi = pathArg => { | ||
pathArg | ||
.find(j.MemberExpression, { | ||
object: { | ||
name: 'spacing', | ||
}, | ||
property: { | ||
name: 'unit', | ||
}, | ||
}) | ||
.replaceWith(path => { | ||
let param = null; | ||
const spacingParam = path.node.object.name; | ||
if (j.BinaryExpression.check(path.parent.node)) { | ||
const expression = path.parent.node; | ||
const operation = expression.operator; | ||
// check if it's a variable | ||
if (j.Identifier.check(expression.right)) { | ||
param = expression.right; | ||
} else if (j.Literal.check(expression.right)) { | ||
const value = expression.right.value; | ||
if (operation === '*' || operation === '/') { | ||
param = j.literal(eval(`1 ${operation} ${value}`)); | ||
} | ||
} | ||
} | ||
if (param) { | ||
path.parent.replace(j.callExpression(j.identifier(spacingParam), [param])); | ||
return path.node; | ||
} | ||
return j.callExpression(j.identifier(spacingParam), [j.literal(1)]); | ||
}); | ||
}; | ||
const arrowFunctions = root.find(j.ArrowFunctionExpression).filter(mightContainApi); | ||
const functionDeclarations = root.find(j.FunctionDeclaration).filter(mightContainApi); | ||
replaceApi(arrowFunctions); | ||
replaceApi(functionDeclarations); | ||
} | ||
module.exports = function transformer(fileInfo, api) { | ||
@@ -88,3 +155,4 @@ const j = api.jscodeshift; | ||
transformThemeSpacingApi(j, root); | ||
transformThemeSpacingApiDestructured(j, root); | ||
return root.toSource({ quote: 'single' }); | ||
}; |
66485
36
1587