Browse Source

KERNEL-14076 fix: 修复 extend 的问题和识别静态变量

es6
Treecat 2 years ago
parent
commit
7c628d3401
  1. 76
      es6.js
  2. 3
      es6.xtype.js

76
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();

3
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];

Loading…
Cancel
Save