From 43c2313d55fe59f0f6226c083bf5677286c7979e Mon Sep 17 00:00:00 2001 From: Treecat Date: Wed, 11 Jan 2023 11:38:54 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14076=20feat:=20es6=20=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E9=80=92=E5=BD=92=E6=96=87=E4=BB=B6=E5=A4=B9=EF=BC=8Ceslint=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es6.js | 269 ++++++++++++++++++++++++++++++--------------------- package.json | 3 +- 2 files changed, 160 insertions(+), 112 deletions(-) diff --git a/es6.js b/es6.js index 9c0b18bf4..b3e9e1b6f 100644 --- a/es6.js +++ b/es6.js @@ -1,121 +1,137 @@ const fs = require("fs"); - -const srcName = process.argv[2]; - -const sourceCode = fs.readFileSync(srcName).toString(); - -const clzName = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode)[1]; - -const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1]; - -// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1]; - -const collection = { - "static": {}, - attr: {}, -}; - -const BI = { - inherit(_, options) { - 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; - }, - shortcut(xtype) { - collection.xtype = xtype; - }, -}; - -eval(sourceCode); - -let M = ""; -let E = ""; -let I = ""; -let A = ""; - -const coreImport = { - shortcut: true, -}; - -if (superName === "Widget") { - coreImport.Widget = true; +const path = require("path"); +const prettier = require("prettier"); +const { exec } = require("child_process"); + +async function fix(path) { + new Promise(res => { + exec(`yarn eslint --fix ${path}`, () => { + res(); + }); + }); } -Object.keys(collection.attr).forEach(key => { - A = `${key} = ${JSON.stringify(collection.attr[key])};`; -}); - -// 静态方法 -Object.keys(collection.static).forEach(key => { - E += `\tstatic ${key} = "${collection.static[key]}"\n`; -}); - -// 对函数进行替换 -collection.methods.forEach(el => { - let f = `${el.toString().replace(/^function/, el.name)}\n`; - - // 换 BI.Button.superclass - f = f.replace(`BI.${clzName}.superclass`, "super"); - // 换 super._defaultConfig - f = f.replace( - `super\._defaultConfig\.apply\(this\,\sarguments\)`, - "super._defaultConfig(arguments)" - ); - // 换 super.xxx.apply - f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, a => { - const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a); - - return `super.${f[1]}(...arguments)`; +async function handleFile(srcName) { + const sourceCode = fs.readFileSync(srcName).toString(); + + const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); + if (!result) { + console.log(`可能是已经es6过了 ${srcName}`); + + return; + } + const clzName = result[1]; + + const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1]; + + // const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1]; + + const collection = { + "static": {}, + attr: {}, + }; + + const BI = { + inherit(_, options) { + 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; + }, + shortcut(xtype) { + collection.xtype = xtype; + }, + }; + + eval(sourceCode); + + let M = ""; + let E = ""; + let I = ""; + let A = ""; + + const coreImport = { + shortcut: true, + }; + + if (superName === "Widget") { + coreImport.Widget = true; + } + + Object.keys(collection.attr).forEach(key => { + A = `${key} = ${JSON.stringify(collection.attr[key])};`; }); - const target = [ - "isNull", - "toPix", - "isKey", - "isObject", - "map", - "extend", - "isFunction", - "isEmptyArray", - "isArray", - "Controller", - clzName, - "createWidget", - "Events", - "emptyFn", - "nextTick", - "bind", - "i18nText", - "isNotNull", - "isString", - "isNumber", - "isEmpty", - ]; - - target.forEach(t => { - const arr = f.split(`BI.${t}`); - // nodejs 低版本没有 replaceAll - if (arr.length > 1) { - if (t !== clzName) coreImport[t] = true; - f = arr.join(t); - } + // 静态方法 + Object.keys(collection.static).forEach(key => { + E += `\tstatic ${key} = "${collection.static[key]}"\n`; }); - M += `${f}\n`; -}); + // 对函数进行替换 + collection.methods.forEach(el => { + let f = `${el.toString().replace(/^function/, el.name)}\n`; + + // 换 BI.Button.superclass + f = f.replace(`BI.${clzName}.superclass`, "super"); + // 换 super._defaultConfig + f = f.replace( + `super\._defaultConfig\.apply\(this\,\sarguments\)`, + "super._defaultConfig(arguments)" + ); + // 换 super.xxx.apply + f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, a => { + const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a); + + return `super.${f[1]}(...arguments)`; + }); + + const target = [ + "isNull", + "toPix", + "isKey", + "isObject", + "map", + "extend", + "isFunction", + "isEmptyArray", + "isArray", + "Controller", + clzName, + "createWidget", + "Events", + "emptyFn", + "nextTick", + "bind", + "i18nText", + "isNotNull", + "isString", + "isNumber", + "isEmpty", + ]; + + target.forEach(t => { + const arr = f.split(`BI.${t}`); + // nodejs 低版本没有 replaceAll + if (arr.length > 1) { + if (t !== clzName) coreImport[t] = true; + f = arr.join(t); + } + }); + + M += `${f}\n`; + }); -Object.keys(coreImport).forEach(el => { - I += `${el},`; -}); + Object.keys(coreImport).forEach(el => { + I += `${el},`; + }); -const outputCode = ` + const outputCode = ` import {${I}} from "@/core" @shortcut() @@ -130,5 +146,36 @@ ${M} } `; -// fs.writeFileSync(`${srcName}.js.raw`, sourceCode); -fs.writeFileSync(srcName, outputCode); + const prettierCode = prettier.format(outputCode); + fs.writeFileSync(srcName, prettierCode); + await fix(srcName); + + return clzName; +} + +async function traverse(srcName) { + if (srcName.indexOf("__test__") >= 0) return; + + if (srcName.endsWith(".js")) { + return await handleFile(srcName); + } else { + const stat = fs.statSync(srcName); + const flag = stat.isDirectory(); + if (flag) { + const files = fs.readdirSync(srcName); + // let indexContent = ""; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + await traverse(path.resolve(srcName, file)); + // const clzName = await traverse(path.resolve(srcName, file)); + // const moduleName = path.basename(srcName).replace(/.js$/, ""); + // if (clzName) { + // indexContent += `export { ${clzName} } from '${moduleName}'\n`; + // } + } + } + } +} + +const srcName = process.argv[2]; +traverse(srcName); diff --git a/package.json b/package.json index f5b71dcc2..cc33beee2 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "cross-env": "6.0.0", "css-loader": "3.0.0", "es6-promise": "4.2.8", - "eslint": "6.0.1", + "eslint": "7.32.0", "expose-loader": "0.7.5", "express": "4.15.2", "fork-ts-checker-webpack-plugin": "1.4.3", @@ -47,6 +47,7 @@ "optimize-css-assets-webpack-plugin": "5.0.3", "postcss-loader": "3.0.0", "postcss-simple-vars": "5.0.2", + "prettier": "2.8.2", "puppeteer": "^13.3.0", "rimraf": "3.0.2", "script-loader": "0.7.2",