@lcap/builder
Version:
lcap builder utils
212 lines (211 loc) • 10.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAPITs = transformAPITs;
const path_1 = __importDefault(require("path"));
const generator_1 = __importDefault(require("@babel/generator"));
const traverse_1 = __importDefault(require("@babel/traverse"));
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const shared_1 = require("../shared");
function transformAPITs(tsCode, context, all = false) {
var _a;
const ast = shared_1.babelParser.parse(tsCode);
const insertAST = shared_1.babelParser.parse('const { Component, Prop, ViewComponent, Slot, Method, Param, Event, ViewComponentOptions } = nasl.ui;');
(0, traverse_1.default)(ast, {
TSModuleDeclaration(path) {
const parentNode = path.parent;
if (parentNode.type === 'TSModuleDeclaration'
&& parentNode.id.type === 'Identifier'
&& parentNode.id.name === 'nasl'
&& path.node.id.type === 'Identifier'
&& path.node.id.name === 'ui'
&& path.node.body.type === 'TSModuleBlock') {
parentNode.id.name = 'extensions';
path.node.id.name = context.pkgName;
const tempBody = path.node.body;
path.node.body = {
type: 'TSModuleDeclaration',
id: {
type: 'Identifier',
name: 'viewComponents'
},
body: tempBody,
kind: 'namespace',
};
if (insertAST) {
tempBody.body.unshift(...insertAST.program.body);
}
}
},
TSTypeReference(path) {
if (path.node.typeName.type !== 'Identifier') {
return;
}
const addNaslUINamespace = [
'ViewComponent', 'CurrentDynamic', 'DataSourceParams',
'PoiInfo', 'File', 'SelectData', 'DragAndDropUpdateData',
'ValidateResult', 'BaseEvent', 'Current'
];
const typeName = path.node.typeName.name;
const replaceName = context.replaceNames.find((name) => typeName === `${name}Options`);
if (replaceName) {
path.node.typeName.name = (0, utils_1.addPrefix)(path.node.typeName.name, context.prefix);
}
else if (addNaslUINamespace.indexOf(path.node.typeName.name) !== -1
|| context.findNaslUIConfig((config) => config.name === typeName
|| (typeName.endsWith('Options') && typeName.startsWith(config.name)))) {
const typeName = path.node.typeName;
path.node.typeName = {
type: 'TSQualifiedName',
left: {
type: 'TSQualifiedName',
left: {
type: 'Identifier',
name: 'nasl',
},
right: {
type: 'Identifier',
name: 'ui',
}
},
right: typeName,
};
}
},
ClassDeclaration(path) {
if (path.node.superClass && path.node.id && path.node.id.type === 'Identifier' && path.node.superClass.type === 'Identifier' && path.node.superClass.name === 'ViewComponent') {
if (path.node.id.name !== context.naslUIConfig.name && !all && (!context.isWithForm || path.node.id.name !== context.withFormName)) {
path.remove();
return;
}
const className = path.node.id.name;
const replaceName = context.replaceNames.find((name) => className === name || className === `${name}Options`);
if (replaceName) {
path.node.id.name = (0, utils_1.addPrefix)(className, context.prefix);
}
if (!path.node.decorators) {
return;
}
const decorator = path.node.decorators.find((d) => d.expression.type === 'CallExpression' && d.expression.callee.type === 'Identifier' && d.expression.callee.name === 'Component');
const index = path.node.decorators.findIndex((d) => d.expression.type === 'CallExpression' && d.expression.callee.type === 'Identifier' && d.expression.callee.name === 'IDEExtraInfo');
if (index !== -1) {
path.node.decorators.splice(index, 1);
}
if (decorator) {
const decorators = path.node.decorators || [];
const properties = [{
type: 'ObjectProperty',
key: {
type: 'Identifier',
name: 'type',
},
value: {
type: 'StringLiteral',
value: context.type,
},
computed: false,
shorthand: false,
}, {
type: 'ObjectProperty',
key: {
type: 'Identifier',
name: 'show',
},
value: {
type: 'BooleanLiteral',
value: true,
},
computed: false,
shorthand: false,
}];
if (context.naslUIConfig.name === className) {
properties.unshift({
type: 'ObjectProperty',
key: {
type: 'Identifier',
name: 'replaceNaslUIComponent',
},
value: {
type: 'StringLiteral',
value: context.naslUIConfig.name,
},
computed: false,
shorthand: false,
});
}
const config = context.findNaslUIConfig(className);
['ideusage', 'extends'].forEach((key) => {
if (!config || !config[key]) {
return;
}
try {
let obj = config[key];
if (key === 'extends' && Array.isArray(obj)) {
obj = obj.map((ext) => {
if (typeof ext === 'string' || ext === context.naslUIConfig.name) {
return context.name;
}
if (typeof ext === 'object' && ext.name === context.naslUIConfig.name) {
return Object.assign(Object.assign({}, ext), { name: context.name });
}
return ext;
});
}
let ast;
if (key === 'ideusage') {
ast = (0, shared_1.getAST)((0, utils_1.replaceAllTagNameInCode)(JSON.stringify(obj), context.replaceTagMap), false);
}
else {
ast = (0, shared_1.getAST)(obj);
}
if (!ast) {
return;
}
properties.push({
type: 'ObjectProperty',
key: {
type: 'Identifier',
name: key,
},
value: ast,
computed: false,
shorthand: false,
});
}
catch (e) {
}
});
decorators.unshift({
type: 'Decorator',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'ExtensionComponent',
},
arguments: [{
type: 'ObjectExpression',
properties,
}],
}
});
}
}
else if (path.node.id && path.node.id.type === 'Identifier' && path.node.superClass && path.node.superClass.type === 'Identifier' && path.node.superClass.name === 'ViewComponentOptions') {
if (path.node.id.name !== `${context.naslUIConfig.name}Options` && !all && (!context.isWithForm || path.node.id.name !== `${context.withFormName}Options`)) {
path.remove();
return;
}
path.node.id.name = (0, utils_1.addPrefix)(path.node.id.name, context.prefix);
}
},
});
(_a = ast.program.body[0].leadingComments) === null || _a === void 0 ? void 0 : _a.push({
type: 'CommentLine',
value: `/ <reference types="${path_1.default.relative(context.componentFolderPath, path_1.default.resolve(context.rootPath, constants_1.LCAP_UI_PATH, 'runtime/nasl.ui.d.ts'))}" />`,
});
return (0, generator_1.default)(ast).code;
}