aws-sdk-js-codemod
Version:
Collection of codemod scripts that help update AWS SDK for JavaScript APIs
102 lines (101 loc) • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renameErrorCodeWithName = void 0;
const FUNCTION_EXPRESSION_TYPES = ["ArrowFunctionExpression", "FunctionExpression"];
const renameCodeWithName = (j, source, errorName) => {
source
.find(j.MemberExpression, {
object: { type: "Identifier", name: errorName },
property: { type: "Identifier", name: "code" },
})
.replaceWith(() => j.memberExpression(j.identifier(errorName), j.identifier("name")));
};
const getFirstExpression = (j, callExpression, expressionName) => {
const parentPath = callExpression.parentPath.value;
if (parentPath?.type !== "MemberExpression") {
return null;
}
const grandParentPath = callExpression.parentPath.parentPath.value;
if (grandParentPath?.type !== "CallExpression") {
return null;
}
if (parentPath.property.type === "Identifier" && parentPath.property.name === expressionName) {
return callExpression.parentPath.parentPath;
}
return getFirstExpression(j, callExpression.parentPath.parentPath, expressionName);
};
// Renames error.code with error.name.
const renameErrorCodeWithName = (j, source, clientIdentifiers) => {
for (const clientId of clientIdentifiers) {
const callExpressions = source.find(j.CallExpression, {
callee: { type: "MemberExpression", object: clientId },
});
// Replace error.code with error.name in try-catch clauses.
callExpressions.forEach((callExpression) => {
const tryStatement = j(callExpression).closest(j.TryStatement).nodes()[0];
if (!tryStatement || !tryStatement.handler) {
return;
}
const catchClause = tryStatement.handler;
const errorParam = catchClause.param;
if (errorParam?.type !== "Identifier") {
return;
}
renameCodeWithName(j, j(catchClause.body), errorParam.name);
});
// Replace error.code with error.name in promise catch.
callExpressions
.map((callExpression) => getFirstExpression(j, callExpression, "catch"))
.forEach((catchExpression) => {
if (!catchExpression) {
return;
}
if (!FUNCTION_EXPRESSION_TYPES.includes(catchExpression.value.arguments[0].type)) {
return;
}
const catchFunction = catchExpression.value.arguments[0];
const errorParam = catchFunction.params[0];
if (errorParam?.type !== "Identifier") {
return;
}
renameCodeWithName(j, j(catchFunction.body), errorParam.name);
});
// Replace error.code with error.name in promise failure callback.
callExpressions
.map((callExpression) => getFirstExpression(j, callExpression, "then"))
.forEach((thenExpression) => {
if (!thenExpression) {
return;
}
if (thenExpression.value.arguments.length !== 2) {
return;
}
if (!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[0].type) ||
!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[1].type)) {
return;
}
const failureCallbackFunction = thenExpression.value.arguments[1];
const errorParam = failureCallbackFunction.params[0];
if (errorParam?.type !== "Identifier") {
return;
}
renameCodeWithName(j, j(failureCallbackFunction.body), errorParam.name);
});
// Replace error.code with error.name in callbacks.
callExpressions.forEach((callExpression) => {
if (callExpression.value.arguments.length !== 2) {
return;
}
if (!FUNCTION_EXPRESSION_TYPES.includes(callExpression.value.arguments[1].type)) {
return;
}
const callbackFunction = callExpression.value.arguments[1];
const errorParam = callbackFunction.params[0];
if (errorParam?.type !== "Identifier") {
return;
}
renameCodeWithName(j, j(callbackFunction.body), errorParam.name);
});
}
};
exports.renameErrorCodeWithName = renameErrorCodeWithName;