From 7cd5c5c256cbae63c3189f165333156205fe9b43 Mon Sep 17 00:00:00 2001 From: "zude.hzd" Date: Thu, 12 Nov 2020 22:32:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20js=E7=BC=96=E8=BE=91=E5=99=A8=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=B0=BE=E9=80=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin-source-editor/src/transform.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/plugin-source-editor/src/transform.ts b/packages/plugin-source-editor/src/transform.ts index 99e486925..1ee574452 100644 --- a/packages/plugin-source-editor/src/transform.ts +++ b/packages/plugin-source-editor/src/transform.ts @@ -24,7 +24,8 @@ const transfrom = { const stateStartStr = this.pickupStateStartStr(codeNew); if (stateStartStr != null) { startIndex = codeNew.indexOf(stateStartStr) + stateStartStr.length; - const stateBodyStr = this.pickupFunctionBody(codeNew, startIndex); + let stateBodyStr = this.pickupFunctionBody(codeNew, startIndex); + stateBodyStr = this.filterEndComma(stateBodyStr); const stateData = JSON.parse('{' + stateBodyStr); functionMap.state = stateData; } @@ -78,6 +79,30 @@ const transfrom = { return functionMap; }, + /** + * 过滤掉尾逗号 + */ + filterEndComma(str) { + let commaIndex = -1; + for (let i = str.length - 1; i > 0; i--) { + if (str[i] == ',') { + commaIndex = i; + break; + } else if (/^[a-zA-Z]+$/.test(str[i])) { + break; + } + } + + + if (commaIndex > 0) { + const newStr = str.substr(0, commaIndex) + str.substr(commaIndex + 1, str.length - 1); + return newStr; + } + + return str; + + + }, pickupFunctionName(codeStr) { // 函数名的正则表达式