Browse Source

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

Merge in VISUAL/fineui from ~TREECAT/fineui:es6 to es6

* commit 'db7ac5df35f983918dc107b4c98d0870655130c6':
  KERNEL-14076 fix: 修复 extend 的问题和识别静态变量
es6
treecat-罗群 2 years ago
parent
commit
9389349e85
  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 { exec } = require("child_process");
const { search, initDepts, depts } = require("./es6.xtype"); 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) { async function fix(path) {
new Promise(res => { new Promise(res => {
exec(`yarn eslint --fix ${path}`, () => { exec(`yarn eslint --fix ${path}`, () => {
@ -54,7 +70,7 @@ const target = [
// 加载模块 // 加载模块
const loader = { const loader = {
G: { "@/core": { shortcut: true } }, // G: { "@/core": { shortcut: true } },
load(srcName, module) { load(srcName, module) {
const G = loader.G; const G = loader.G;
if (target.indexOf(module) >= 0) { if (target.indexOf(module) >= 0) {
@ -88,13 +104,15 @@ const loader = {
}; };
async function handleFile(srcName) { async function handleFile(srcName) {
const G = loader.G; // 全局状态回归
const G = loader.G = { "@/core": { shortcut: true } };
const sourceCode = fs.readFileSync(srcName).toString(); const sourceCode = fs.readFileSync(srcName).toString();
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode);
if (!result) { if (!result) {
console.log(`可能是已经es6过了 ${srcName}, 尝试替换 xtype`); console.log(`已经es6过,替换 xtype => ${srcName}, `);
// 处理 xtype // 处理 xtype
// 尝试对 xtype 进行替换 // 尝试对 xtype 进行替换
@ -105,7 +123,7 @@ async function handleFile(srcName) {
return `${clzName}.xtype`; return `${clzName}.xtype`;
} else { } else {
console.log(`加载 ${matchedSentence}失败`); console.log(`xtype 替换失败 ${matchedSentence} `);
return matchedSentence; return matchedSentence;
} }
@ -128,6 +146,7 @@ async function handleFile(srcName) {
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const BI = { const BI = {
[clzName]: clzName,
inherit(_, options) { inherit(_, options) {
collection.methods = Object.keys(options) collection.methods = Object.keys(options)
.filter(key => typeof options[key] === "function") .filter(key => typeof options[key] === "function")
@ -140,6 +159,9 @@ async function handleFile(srcName) {
return collection.static; return collection.static;
}, },
extend(targetClz, o) {
Object.assign(collection.static, o);
},
shortcut(xtype) { shortcut(xtype) {
collection.xtype = xtype; collection.xtype = xtype;
}, },
@ -156,12 +178,35 @@ async function handleFile(srcName) {
loader.load(srcName, superName); loader.load(srcName, superName);
Object.keys(collection.attr).forEach(key => { 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 => { 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) { if (loadSuccess) {
return target + end; return target + end;
} else { } else {
console.log(`BI.xxx 加载 ${target}失败`); console.log(`BI 变量替换失败 BI.${target}`);
return matchedSentence; return matchedSentence;
} }
@ -208,7 +253,7 @@ async function handleFile(srcName) {
return `${clzName}.xtype`; return `${clzName}.xtype`;
} else { } else {
console.log(`加载 ${matchedSentence}失败`); // console.log(`(没事) xtype 替换失败 ${matchedSentence} `);
return matchedSentence; return matchedSentence;
} }
@ -226,8 +271,10 @@ async function handleFile(srcName) {
Object.keys(G[moduleKey]).forEach(key => { Object.keys(G[moduleKey]).forEach(key => {
i += `${key}, `; i += `${key}, `;
}); });
const single = !/\//.test(moduleKey);
if (single) { // 必须以 . 开头
const moduleInValid = /^[^@.]/.test(moduleKey);
if (moduleInValid) {
moduleKey = `./${moduleKey}`; moduleKey = `./${moduleKey}`;
} }
@ -251,6 +298,7 @@ ${M}
const prettierCode = prettier.format(outputCode, { const prettierCode = prettier.format(outputCode, {
tabWidth: 4, tabWidth: 4,
parser: 'babel',
}); });
fs.writeFileSync(srcName, prettierCode); fs.writeFileSync(srcName, prettierCode);
await fix(srcName); await fix(srcName);
@ -262,7 +310,13 @@ async function traverse(srcName) {
if (srcName.indexOf("__test__") >= 0) return; if (srcName.indexOf("__test__") >= 0) return;
if (srcName.endsWith(".js")) { if (srcName.endsWith(".js")) {
return await handleFile(srcName); try {
return await handleFile(srcName);
} catch (error) {
console.log(`文件处理失败 ${srcName} \n${error}`);
return;
}
} else { } else {
const stat = fs.statSync(srcName); const stat = fs.statSync(srcName);
const flag = stat.isDirectory(); const flag = stat.isDirectory();

3
es6.xtype.js

@ -10,9 +10,8 @@ async function handle(filename) {
let clzName; let clzName;
if (inheritRegResult) { if (inheritRegResult) {
clzName = inheritRegResult[1]; clzName = inheritRegResult[1];
// 把
} else { } else {
const clzRegResult = /export\sclass\s(.*?)\sextend/.exec(code); const clzRegResult = /export\s+class\s+(.*?)\s+/.exec(code);
if (clzRegResult) { if (clzRegResult) {
clzName = clzRegResult[1]; clzName = clzRegResult[1];

Loading…
Cancel
Save