From 7c628d3401ec834332455c54523d55f1e11b0694 Mon Sep 17 00:00:00 2001 From: Treecat Date: Thu, 12 Jan 2023 15:06:19 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14076=20fix:=20=E4=BF=AE=E5=A4=8D=20exte?= =?UTF-8?q?nd=20=E7=9A=84=E9=97=AE=E9=A2=98=E5=92=8C=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es6.js | 76 ++++++++++++++++++++++++++++++++++++++++++++-------- es6.xtype.js | 3 +-- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/es6.js b/es6.js index 5e5353d93..a3d0ce376 100644 --- a/es6.js +++ b/es6.js @@ -4,6 +4,22 @@ const prettier = require("prettier"); const { exec } = require("child_process"); const { search, initDepts, depts } = require("./es6.xtype"); +// const XTYPE_ONLY = false; +// const THIS_REPLACE = false; + +function objHaveFunction(obj) { + return Object.keys(obj).some(key => { + const value = obj[key]; + if (typeof value === "object") { + return objHaveFunction(value); + } else if (typeof value === "function") { + return true; + } + + return false; + }); +} + async function fix(path) { new Promise(res => { exec(`yarn eslint --fix ${path}`, () => { @@ -49,7 +65,7 @@ const target = [ // 加载模块 const loader = { - G: { "@/core": { shortcut: true } }, + // G: { "@/core": { shortcut: true } }, load(srcName, module) { const G = loader.G; if (target.indexOf(module) >= 0) { @@ -83,13 +99,15 @@ const loader = { }; async function handleFile(srcName) { - const G = loader.G; + // 全局状态回归 + const G = loader.G = { "@/core": { shortcut: true } }; const sourceCode = fs.readFileSync(srcName).toString(); const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); if (!result) { - console.log(`可能是已经es6过了 ${srcName}, 尝试替换 xtype`); + console.log(`已经es6过,替换 xtype => ${srcName}, `); + // 处理 xtype // 尝试对 xtype 进行替换 @@ -100,7 +118,7 @@ async function handleFile(srcName) { return `${clzName}.xtype`; } else { - console.log(`加载 ${matchedSentence}失败`); + console.log(`xtype 替换失败 ${matchedSentence} `); return matchedSentence; } @@ -123,6 +141,7 @@ async function handleFile(srcName) { // eslint-disable-next-line no-unused-vars const BI = { + [clzName]: clzName, inherit(_, options) { collection.methods = Object.keys(options) .filter(key => typeof options[key] === "function") @@ -135,6 +154,9 @@ async function handleFile(srcName) { return collection.static; }, + extend(targetClz, o) { + Object.assign(collection.static, o); + }, shortcut(xtype) { collection.xtype = xtype; }, @@ -151,12 +173,35 @@ async function handleFile(srcName) { loader.load(srcName, superName); Object.keys(collection.attr).forEach(key => { - A = `${key} = ${JSON.stringify(collection.attr[key])};`; + const value = collection.attr[key]; + if (typeof value === "function" || typeof value === "number") { + A += `\t${key} = ${value}\n`; + } else if (typeof value === "string") { + A += `\t${key} = "${value}"\n`; + } else if (typeof value === "object") { + if (objHaveFunction(value)) { + throw new Error("G"); + } else { + A += `\t${key} = ${JSON.stringify(value)}\n`; + } + } }); // 静态方法 Object.keys(collection.static).forEach(key => { - E += `\tstatic ${key} = "${collection.static[key]}"\n`; + // console.log(key, collection.static[key], typeof collection.static[key]) + const value = collection.static[key]; + if (typeof value === "function" || typeof value === "number") { + E += `\tstatic ${key} = ${value}\n`; + } else if (typeof value === "string") { + E += `\tstatic ${key} = "${value}"\n`; + } else if (typeof value === "object") { + if (objHaveFunction(value)) { + throw new Error("G"); + } else { + E += `\tstatic ${key} = ${JSON.stringify(value)}\n`; + } + } }); // 对函数进行替换 @@ -189,7 +234,7 @@ async function handleFile(srcName) { if (loadSuccess) { return target + end; } else { - console.log(`BI.xxx 加载 ${target}失败`); + console.log(`BI 变量替换失败 BI.${target}`); return matchedSentence; } @@ -203,7 +248,7 @@ async function handleFile(srcName) { return `${clzName}.xtype`; } else { - console.log(`加载 ${matchedSentence}失败`); + // console.log(`(没事) xtype 替换失败 ${matchedSentence} `); return matchedSentence; } @@ -221,8 +266,10 @@ async function handleFile(srcName) { Object.keys(G[moduleKey]).forEach(key => { i += `${key}, `; }); - const single = !/\//.test(moduleKey); - if (single) { + + // 必须以 . 开头 + const moduleInValid = /^[^@.]/.test(moduleKey); + if (moduleInValid) { moduleKey = `./${moduleKey}`; } @@ -246,6 +293,7 @@ ${M} const prettierCode = prettier.format(outputCode, { tabWidth: 4, + parser: 'babel', }); fs.writeFileSync(srcName, prettierCode); await fix(srcName); @@ -257,7 +305,13 @@ async function traverse(srcName) { if (srcName.indexOf("__test__") >= 0) return; if (srcName.endsWith(".js")) { - return await handleFile(srcName); + try { + return await handleFile(srcName); + } catch (error) { + console.log(`文件处理失败 ${srcName} \n${error}`); + + return; + } } else { const stat = fs.statSync(srcName); const flag = stat.isDirectory(); diff --git a/es6.xtype.js b/es6.xtype.js index 28c934c80..351134bf3 100644 --- a/es6.xtype.js +++ b/es6.xtype.js @@ -10,9 +10,8 @@ async function handle(filename) { let clzName; if (inheritRegResult) { clzName = inheritRegResult[1]; - // 把 } else { - const clzRegResult = /export\sclass\s(.*?)\sextend/.exec(code); + const clzRegResult = /export\s+class\s+(.*?)\s+/.exec(code); if (clzRegResult) { clzName = clzRegResult[1];