@lcap/builder
Version:
lcap builder utils
244 lines (243 loc) • 11.7 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAPITs = void 0;
const path_1 = __importDefault(require("path"));
const babel = __importStar(require("@babel/core"));
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 babel_utils_1 = require("../utils/babel-utils");
function transformAPITs(tsCode, context, all = false) {
var _a;
const ast = babel.parse(tsCode, {
filename: 'result.ts',
presets: [require('@babel/preset-typescript')],
plugins: [
[require('@babel/plugin-proposal-decorators'), { legacy: true }],
],
rootMode: 'root',
root: __dirname,
});
const insertAST = babel.parseSync('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,
};
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, babel_utils_1.getAST)((0, utils_1.replaceAllTagNameInCode)(JSON.stringify(obj), context.replaceTagMap), false);
}
else {
ast = (0, babel_utils_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;
}
exports.transformAPITs = transformAPITs;