diff --git a/es6.js b/es6.js index 046137b06..7667443e2 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}`, () => { @@ -54,7 +70,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) { @@ -88,13 +104,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 进行替换 @@ -105,7 +123,7 @@ async function handleFile(srcName) { return `${clzName}.xtype`; } else { - console.log(`加载 ${matchedSentence}失败`); + console.log(`xtype 替换失败 ${matchedSentence} `); return matchedSentence; } @@ -128,6 +146,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") @@ -140,6 +159,9 @@ async function handleFile(srcName) { return collection.static; }, + extend(targetClz, o) { + Object.assign(collection.static, o); + }, shortcut(xtype) { collection.xtype = xtype; }, @@ -156,12 +178,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`; + } + } }); // 对函数进行替换 @@ -194,7 +239,7 @@ async function handleFile(srcName) { if (loadSuccess) { return target + end; } else { - console.log(`BI.xxx 加载 ${target}失败`); + console.log(`BI 变量替换失败 BI.${target}`); return matchedSentence; } @@ -208,7 +253,7 @@ async function handleFile(srcName) { return `${clzName}.xtype`; } else { - console.log(`加载 ${matchedSentence}失败`); + // console.log(`(没事) xtype 替换失败 ${matchedSentence} `); return matchedSentence; } @@ -226,8 +271,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}`; } @@ -251,6 +298,7 @@ ${M} const prettierCode = prettier.format(outputCode, { tabWidth: 4, + parser: 'babel', }); fs.writeFileSync(srcName, prettierCode); await fix(srcName); @@ -262,7 +310,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];