我目前正在使用 JSCodeShift 以便在我的应用程序中从 amplify 迁移到 jquery ajax 服务调用。我正在尝试在我的 JSCodeShift 转换文件中创建一个 return 语句,但不断遇到一个冗余错误,由于文档较少,我无法弄清楚。有没有人创建了一个返回语句或用 JSCodeShift 术语“returnExpression”。
这就是我要添加到功能块主体的内容:
return $.ajax({
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(payload),
url: config.urls.updateContractExemptionUrl.replace(/\{accountNumber\}/, accountNumber)
});
这是我不断遇到的错误:
ERR services/dataservice.contracts.js Transformation error
Error: no value or default function given for field "argument" of ReturnStatement("argument": Expression | null)
at add (/usr/local/lib/node_modules/jscodeshift/node_modules/recast/node_modules/ast-types/lib/types.js:576:31)
at /usr/local/lib/node_modules/jscodeshift/node_modules/recast/node_modules/ast-types/lib/types.js:593:21
at Array.forEach (native)
at Function.Object.defineProperty.value [as returnStatement] (/usr/local/lib/node_modules/jscodeshift/node_modules/recast/node_modules/ast-types/lib/types.js:592:34)
at /home/dev/InventoryProjectGitNew/cbs-inventory-web/cbs-inventory-web-client/app/scripts/transform.js:29:41
at Array.forEach (native)
at NodePath.<anonymous> (/home/dev/InventoryProjectGitNew/cbs-inventory-web/cbs-inventory-web-client/app/scripts/transform.js:26:26)
at /usr/local/lib/node_modules/jscodeshift/dist/Collection.js:76:36
at Array.forEach (native)
at Collection.forEach (/usr/local/lib/node_modules/jscodeshift/dist/Collection.js:75:18)
这是我到目前为止编写的转换文件:
var _ = require('lodash');
module.exports = function (fileInfo, api, options) {
var j = api.jscodeshift,
root = j(fileInfo.source),
requestsInformation = [];
function RequestDetails() {
var self = this;
self.name = '';
self.dataType = '';
self.url = '';
self.parameters = [];
}
function hasJQueryDependencyImported() {
var checkJQueryLiteral = root.find(j.Literal, { value: 'jquery' }),
checkJQueryParameter = root.find(j.Identifier, { name: '$' });
return checkJQueryLiteral.length !== 0 && checkJQueryParameter.length !== 0;
}
function createAjaxRequest(requestInfo, returnExpression) {
var functionBlocks = root.find(j.FunctionDeclaration);
functionBlocks.forEach(function (block) {
var functionBody = block.value.body.body;
functionBody.forEach(function (statement, index) {
if (statement.type === 'ReturnStatement' && statement.argument.type === 'CallExpression' && !!statement.argument.callee.object && statement.argument.callee.object.name === 'amplify') {
functionBody.splice(index, 1);
functionBody.push(j.returnStatement());
}
});
});
}
function collectRequestInfo(requestDefinition) {
var currentRequest = j(requestDefinition),
requestInfo = new RequestDetails(),
url = currentRequest.find(j.ObjectExpression).get(0).parentPath.value.properties[1],
returnExpressions = root.find(j.ReturnStatement,
{
argument: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'amplify'
}
}
}
});
requestInfo.name = currentRequest.find(j.Literal).get(0).parentPath.value.value;
requestInfo.dataType = currentRequest.find(j.ObjectExpression).get(0).parentPath.value.properties[0].value.value;
requestInfo.url = url.value.object.object.name + '.' + url.value.object.property.name + '.' + url.value.property.name;
returnExpressions.forEach(function (amplifyRequest) {
var requestDetails = amplifyRequest,
properties;
if (requestDetails.value.argument.arguments[0].value === requestInfo.name) {
properties = requestDetails.value.argument.arguments[1].properties;
properties.forEach(function (property) {
requestInfo.parameters.push(property.value.name);
});
}
});
createAjaxRequest(requestInfo, returnExpressions);
requestsInformation.push(requestInfo);
}
var amplifyLiteral = root.find(j.Literal, { value: 'amplify' });
amplifyLiteral.remove();
var amplifyRequestDeferredLiteral = root.find(j.Literal, { value: 'amplify.request.deferred' });
amplifyRequestDeferredLiteral.remove();
var amplifyParameters = root.find(j.Identifier, { name: 'amplify' });
amplifyParameters.forEach(function (path) {
if (path.parentPath.parentPath.value.type === 'FunctionExpression' && path.parentPath.parentPath.value.id === null) {
j(path).remove();
}
});
var allAmplifyRequestDefinitions = root.find(
j.ExpressionStatement,
{
expression: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'amplify'
}
}
}
}
}
);
if (allAmplifyRequestDefinitions.length) {
allAmplifyRequestDefinitions.forEach(function (ampReqDef) {
collectRequestInfo(ampReqDef);
});
// Do this after collecting information about the requests to be built
allAmplifyRequestDefinitions.remove();
}
if (!hasJQueryDependencyImported()) {
var defineBlock = root.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'define'
}
});
defineBlock.forEach(function (expression) {
var dependencies = expression.value.arguments[0].elements,
depdendenciesArguments = expression.value.arguments[1].params,
jqueryDependency = j.literal('jquery'),
jqueryArgument = j.identifier('$');
// Should be at index of last argument + 1
dependencies[depdendenciesArguments.length + 1] = jqueryDependency;
depdendenciesArguments[depdendenciesArguments.length + 1] = jqueryArgument;
});
}
return root.toSource({quote: 'single'});
};
检查我尝试使用 jscodeshift 创建 Ajax 请求的函数 createAjaxRequest 。
谢谢, 安基特