From 9f397fcee1936fb310b6d391dc72b77a18345c03 Mon Sep 17 00:00:00 2001 From: Treecat Date: Tue, 10 Jan 2023 17:12:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0jira=20feat:=20es6=E5=8C=96=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es6.js | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 es6.js diff --git a/es6.js b/es6.js new file mode 100644 index 000000000..665270440 --- /dev/null +++ b/es6.js @@ -0,0 +1,106 @@ +const fs = require("fs"); + +const srcName = process.argv[2]; + +const sourceCode = fs.readFileSync(srcName).toString(); + +const clzName = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode)[1]; + +const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1]; + +// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1]; + +const collection = { + static: {} +}; + +const BI = { + inherit(_, options) { + collection.methods = Object.keys(options).map((key) => options[key]); + return collection.static; + }, + shortcut(xtype) { + collection.xtype = xtype; + } +}; + +eval(sourceCode); + +let M = ""; +let E = ""; +let I = ""; + +const coreImport = { + shortcut: true +}; + +if (superName === "Widget") { + coreImport.Widget = true; +} + +// 静态方法 +Object.keys(collection.static).forEach((key) => { + E += `\tstatic ${key} = "${collection.static[key]}"\n`; +}); + +// 对函数进行替换 +collection.methods.forEach((el) => { + let f = el.toString().replace(/^function/, el.name) + "\n"; + + + // 换 BI.Button.superclass + f = f.replace(`BI.${clzName}.superclass`, "super"); + // 换 super._defaultConfig + f = f.replace( + `super\._defaultConfig\.apply\(this\,\sarguments\)`, + "super._defaultConfig(arguments)" + ); + // 换 super.xxx.apply + f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, (a) => { + const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a); + return `super.${f[1]}(...arguments)`; + }); + + const target = [ + "isNull", + "toPix", + "isKey", + "isObject", + "map", + "extend", + "isFunction", + "isEmptyArray", + "isArray" + ]; + + target.forEach((t) => { + const arr = f.split(`BI.${t}`); + // nodejs 低版本没有 replaceAll + if (arr.length > 1) { + coreImport[t] = true; + f = arr.join(t); + } + + }); + + M += f; +}); + +Object.keys(coreImport).forEach((el) => { + I += `${el},`; +}); + +const outputCode = ` +import {${I}} from "@/core" + +@shortcut() +export class ${clzName} extends ${superName} { +\tstatic xtype = "${collection.xtype}" + +${E} + +${M} +} +`; + +fs.writeFileSync(srcName + ".js", outputCode);