Browse Source

无jira fix:es6脚本修复一些问题

es6
Treecat 2 years ago
parent
commit
bda879d580
  1. 52
      es6.js

52
es6.js

@ -11,17 +11,26 @@ const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1];
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1]; // const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
const collection = { const collection = {
static: {} "static": {},
attr: {},
}; };
const BI = { const BI = {
inherit(_, options) { inherit(_, options) {
collection.methods = Object.keys(options).map((key) => options[key]); collection.methods = Object.keys(options)
.filter(key => typeof options[key] === "function")
.map(key => options[key]);
Object.keys(options)
.filter(key => typeof options[key] !== "function")
.forEach(key => {
collection.attr[key] = options[key];
});
return collection.static; return collection.static;
}, },
shortcut(xtype) { shortcut(xtype) {
collection.xtype = xtype; collection.xtype = xtype;
} },
}; };
eval(sourceCode); eval(sourceCode);
@ -29,24 +38,28 @@ eval(sourceCode);
let M = ""; let M = "";
let E = ""; let E = "";
let I = ""; let I = "";
let A = "";
const coreImport = { const coreImport = {
shortcut: true shortcut: true,
}; };
if (superName === "Widget") { if (superName === "Widget") {
coreImport.Widget = true; coreImport.Widget = true;
} }
Object.keys(collection.attr).forEach(key => {
A = `${key} = ${JSON.stringify(collection.attr[key])};`;
});
// 静态方法 // 静态方法
Object.keys(collection.static).forEach((key) => { Object.keys(collection.static).forEach(key => {
E += `\tstatic ${key} = "${collection.static[key]}"\n`; E += `\tstatic ${key} = "${collection.static[key]}"\n`;
}); });
// 对函数进行替换 // 对函数进行替换
collection.methods.forEach((el) => { collection.methods.forEach(el => {
let f = el.toString().replace(/^function/, el.name) + "\n"; let f = `${el.toString().replace(/^function/, el.name)}\n`;
// 换 BI.Button.superclass // 换 BI.Button.superclass
f = f.replace(`BI.${clzName}.superclass`, "super"); f = f.replace(`BI.${clzName}.superclass`, "super");
@ -56,8 +69,9 @@ collection.methods.forEach((el) => {
"super._defaultConfig(arguments)" "super._defaultConfig(arguments)"
); );
// 换 super.xxx.apply // 换 super.xxx.apply
f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, (a) => { f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, a => {
const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a); const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a);
return `super.${f[1]}(...arguments)`; return `super.${f[1]}(...arguments)`;
}); });
@ -70,23 +84,26 @@ collection.methods.forEach((el) => {
"extend", "extend",
"isFunction", "isFunction",
"isEmptyArray", "isEmptyArray",
"isArray" "isArray",
"Controller",
clzName,
"createWidget",
"Events",
]; ];
target.forEach((t) => { target.forEach(t => {
const arr = f.split(`BI.${t}`); const arr = f.split(`BI.${t}`);
// nodejs 低版本没有 replaceAll // nodejs 低版本没有 replaceAll
if (arr.length > 1) { if (arr.length > 1) {
coreImport[t] = true; if (t !== clzName) coreImport[t] = true;
f = arr.join(t); f = arr.join(t);
} }
}); });
M += f; M += `${f}\n`;
}); });
Object.keys(coreImport).forEach((el) => { Object.keys(coreImport).forEach(el => {
I += `${el},`; I += `${el},`;
}); });
@ -97,10 +114,13 @@ import {${I}} from "@/core"
export class ${clzName} extends ${superName} { export class ${clzName} extends ${superName} {
\tstatic xtype = "${collection.xtype}" \tstatic xtype = "${collection.xtype}"
${A}
${E} ${E}
${M} ${M}
} }
`; `;
fs.writeFileSync(srcName + ".js", outputCode); // fs.writeFileSync(`${srcName}.js.raw`, sourceCode);
fs.writeFileSync(srcName, outputCode);

Loading…
Cancel
Save