UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

107 lines 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transform2Logic = exports.transformFunctionExpression2Logic = exports.transformFunctionDeclaration2Logic = void 0; const utils_1 = require("./utils"); const utils_2 = require("../../utils"); const transform2LogicItem_1 = require("./transform2LogicItem"); const transform2TypeAnnotation_1 = require("./transform2TypeAnnotation"); function getLogicTypeFromName(logicName) { if (logicName.includes('on_') || logicName.includes('businessLogic_')) return 'event_logic'; if (logicName.includes('viewLogic_')) return 'view_logic'; return 'global_logic'; } function transformFunctionDeclaration2Logic(func, contextLogicName, currentPositionComment) { const logic = new utils_1.naslTypes.Logic(); logic.name = contextLogicName || func.id?.name; logic.body = [ new utils_1.naslTypes.Start(), ]; let parameters = []; let statements = []; parameters = func.params; statements = [...func?.body?.directives || [], ...func?.body?.body || []]; if (currentPositionComment) { statements = statements.filter((stat) => stat.start > currentPositionComment.start); } const newStatements = []; statements.forEach((node, index) => { if (node?.leadingComments?.length) { newStatements.push(...node?.leadingComments); } newStatements.push(node); if (node?.trailingComments?.length && index === statements.length - 1) { newStatements.push(...node?.trailingComments); } }); statements = newStatements; if (parameters?.length === 1 && parameters?.[0]?.type === 'ObjectPattern') { // interface 特殊处理 const properties = parameters?.[0]?.typeAnnotation?.typeAnnotation?.members; if (properties?.length) { parameters = properties; } } parameters.forEach((node) => { let param; param = (0, transform2LogicItem_1.transform2Param)(node); param && logic.params.push(param); }); statements.forEach((node) => { const item = (0, transform2LogicItem_1.transform2LogicItem)(node, { transformType: 'logic', logic, logicType: getLogicTypeFromName(logic.name), }); item && logic.body.push(item); }); const lastItem = logic.body[logic.body.length - 1]; if (lastItem && lastItem.concept !== 'End') { logic.body.push(new utils_1.naslTypes.End()); } if (logic.returns[0]?.name) { const index = logic.variables.findIndex((variable) => variable.name === logic.returns[0].name); if (index >= 0) logic.variables.splice(index, 1); } if (!logic?.returns?.length && func?.returnType) { const typeAnnotation = func.returnType?.typeAnnotation; if (typeAnnotation?.type !== 'TSVoidKeyword') { logic.returns.push(new utils_1.naslTypes.Return({ name: 'result', typeAnnotation: (0, transform2TypeAnnotation_1.transform2TypeAnnotation)(typeAnnotation), })); } } return logic; } exports.transformFunctionDeclaration2Logic = transformFunctionDeclaration2Logic; function transformFunctionExpression2Logic(func, contextLogicName) { let name = contextLogicName || `logic_${(0, utils_2.uuidv4)().slice(0, 2)}`; if (func.type === 'FunctionExpression') { name = func.id?.name || name; } return transformFunctionDeclaration2Logic({ type: 'FunctionDeclaration', generator: false, async: false, id: { type: 'Identifier', name, }, params: func.params, body: func.body, }, contextLogicName); } exports.transformFunctionExpression2Logic = transformFunctionExpression2Logic; function transform2Logic(func, contextLogicName) { if (func.type === 'ArrowFunctionExpression' || func.type === 'FunctionExpression') { return transformFunctionExpression2Logic(func, contextLogicName); } else { return transformFunctionDeclaration2Logic(func, contextLogicName); } } exports.transform2Logic = transform2Logic; //# sourceMappingURL=transform2Logic.js.map