From 17b79b6748bf36e59aca0c1d3e310e0c221f80c5 Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Mon, 16 Oct 2023 18:48:58 +0800 Subject: [PATCH 1/6] delete Logical --- .../lib/template-compiler/bind-this.js | 29 +++++++++++++++++++ .../test/platform/common/bind-this.spec.js | 26 +++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index abc1fef41c..f6e626749d 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -31,6 +31,28 @@ function checkBindThis (path) { !hash[path.node.name] } +// 获取 Logical container 中存在多少个Identifier +function getIdentifiersCount (path) { + let count = 0 + let current = path + while (t.isLogicalExpression(current.container)) { + current = current.parentPath + } + + function checkIdentifiers (cur) { + ['left', 'right'].forEach(key => { + if (t.isLogicalExpression(cur[key])) { + checkIdentifiers(cur[key]) + } else if (!t.isLiteral(cur[key])) { + count++ + } + }) + } + checkIdentifiers(current.node) + + return count +} + // 计算访问路径 function calPropName (path) { let current = path.parentPath @@ -96,6 +118,13 @@ function checkDelAndGetPath (path) { } else { delPath = current.parentPath } + } else if (t.isLogicalExpression(current.container)) { // case: a || '' + const count = getIdentifiersCount(current) + if (count === 1) { + delPath = current.parentPath + } else { + break + } } else { break } diff --git a/packages/webpack-plugin/test/platform/common/bind-this.spec.js b/packages/webpack-plugin/test/platform/common/bind-this.spec.js index ecaa591040..4a51f3ff23 100644 --- a/packages/webpack-plugin/test/platform/common/bind-this.spec.js +++ b/packages/webpack-plugin/test/platform/common/bind-this.spec.js @@ -647,4 +647,30 @@ global.currentInject = { const output = ['a', 'b', 'a', 'c', 'a', 'd', 'name', 'name2'] expect(res.propKeys.join('')).toBe(output.join('')) }) + + it('should logicalExpression is correct', function () { + const input = ` + global.currentInject = { + render: function () { + a + a || '' + a && a.b + b + b || 123 || '' + '456' || b || '' + '' || 123 || b + b || a || '' + } + } + ` + const res = bindThis(input, { needCollect: false, renderReduce: true }).code + const output = ` +global.currentInject = { + render: function () { + this.a && this.a.b; + this.b || this.a || ''; + } +};` + expect(trimBlankRow(res)).toBe(trimBlankRow(output)) + }) }) From fee2d4211af93f7a9692e23badeec3947ed89da7 Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Tue, 17 Oct 2023 21:17:52 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=B1=80=E9=83=A8?= =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/template-compiler/bind-this.js | 20 ++++++++++++-- .../test/platform/common/bind-this.spec.js | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index f6e626749d..84137d19e7 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -231,9 +231,21 @@ module.exports = { Identifier (path) { if ( checkBindThis(path) && - !path.scope.hasBinding(path.node.name) && !ignoreMap[path.node.name] ) { + const scopeBinding = path.scope.hasBinding(path.node.name) + // 删除局部作用域的变量 + if (scopeBinding && renderReduce) { + const { delPath, canDel, ignore, replace } = checkDelAndGetPath(path) + if (canDel && !ignore) { + delPath.delInfo = { + immediate: true, + canDel, + replace + } + } + return + } const { last, keyPath } = calPropName(path) path.needBind = true if (needCollect) { @@ -301,10 +313,14 @@ module.exports = { enter (path) { // 删除重复变量 if (path.delInfo) { - const { keyPath, canDel, replace } = path.delInfo + const { keyPath, canDel, immediate, replace } = path.delInfo delete path.delInfo if (canDel) { + if (immediate) { // 局部作用域里的变量,可直接删除 + dealRemove(path, replace) + return + } const data = bindingsMap.get(currentBlock) const { bindings, pBindings } = data const allBindings = Object.assign({}, pBindings, bindings) diff --git a/packages/webpack-plugin/test/platform/common/bind-this.spec.js b/packages/webpack-plugin/test/platform/common/bind-this.spec.js index 4a51f3ff23..45d435187a 100644 --- a/packages/webpack-plugin/test/platform/common/bind-this.spec.js +++ b/packages/webpack-plugin/test/platform/common/bind-this.spec.js @@ -670,6 +670,33 @@ global.currentInject = { this.a && this.a.b; this.b || this.a || ''; } +};` + expect(trimBlankRow(res)).toBe(trimBlankRow(output)) + }) + + it('should scope var is correct', function () { + const input = ` + global.currentInject = { + render: function () { + this._i(list, function (item, index) { + item; + index; + item.a ? "" : item.b; + item.a || ""; + item.a || item.b; + }); + } + } + ` + const res = bindThis(input, { needCollect: false, renderReduce: true }).code + const output = ` +global.currentInject = { + render: function () { + this._i(this.list, function (item, index) { + item.a ? "" : item.b; + item.a || item.b; + }); + } };` expect(trimBlankRow(res)).toBe(trimBlankRow(output)) }) From d9ba4869873288482172ae6c5abeeb51c245b806 Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Wed, 1 Nov 2023 15:08:21 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BC=98=E5=8C=96Logical=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/template-compiler/bind-this.js | 47 ++++++------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index 84137d19e7..6093a8420b 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -31,28 +31,6 @@ function checkBindThis (path) { !hash[path.node.name] } -// 获取 Logical container 中存在多少个Identifier -function getIdentifiersCount (path) { - let count = 0 - let current = path - while (t.isLogicalExpression(current.container)) { - current = current.parentPath - } - - function checkIdentifiers (cur) { - ['left', 'right'].forEach(key => { - if (t.isLogicalExpression(cur[key])) { - checkIdentifiers(cur[key]) - } else if (!t.isLiteral(cur[key])) { - count++ - } - }) - } - checkIdentifiers(current.node) - - return count -} - // 计算访问路径 function calPropName (path) { let current = path.parentPath @@ -119,10 +97,11 @@ function checkDelAndGetPath (path) { delPath = current.parentPath } } else if (t.isLogicalExpression(current.container)) { // case: a || '' - const count = getIdentifiersCount(current) - if (count === 1) { + const key = current.key === 'left' ? 'right' : 'left' + if (t.isLiteral(current.parent[key])) { delPath = current.parentPath } else { + canDel = false break } } else { @@ -235,13 +214,15 @@ module.exports = { ) { const scopeBinding = path.scope.hasBinding(path.node.name) // 删除局部作用域的变量 - if (scopeBinding && renderReduce) { - const { delPath, canDel, ignore, replace } = checkDelAndGetPath(path) - if (canDel && !ignore) { - delPath.delInfo = { - immediate: true, - canDel, - replace + if (scopeBinding) { + if (renderReduce) { + const { delPath, canDel, ignore, replace } = checkDelAndGetPath(path) + if (canDel && !ignore) { + delPath.delInfo = { + isLocal: true, + canDel, + replace + } } } return @@ -313,11 +294,11 @@ module.exports = { enter (path) { // 删除重复变量 if (path.delInfo) { - const { keyPath, canDel, immediate, replace } = path.delInfo + const { keyPath, canDel, isLocal, replace } = path.delInfo delete path.delInfo if (canDel) { - if (immediate) { // 局部作用域里的变量,可直接删除 + if (isLocal) { // 局部作用域里的变量,可直接删除 dealRemove(path, replace) return } From f5366dd8cdb5c9d98f4e9d884ae45cc9ad076bd5 Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Wed, 1 Nov 2023 20:42:52 +0800 Subject: [PATCH 4/6] fix code --- .../webpack-plugin/lib/template-compiler/bind-this.js | 10 ++++++---- .../test/platform/common/bind-this.spec.js | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index 6093a8420b..3742e32521 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -104,6 +104,8 @@ function checkDelAndGetPath (path) { canDel = false break } + } else if (current.key === 'expression' && t.isExpressionStatement(current.parentPath)) { + delPath = current.parentPath } else { break } @@ -160,9 +162,9 @@ function checkPrefix (keys, key) { } function dealRemove (path, replace) { - while (path.key === 'expression' && t.isExpressionStatement(path.parentPath)) { - path = path.parentPath - } + // while (path.key === 'expression' && t.isExpressionStatement(path.parentPath)) { + // path = path.parentPath + // } try { if (replace) { @@ -172,7 +174,7 @@ function dealRemove (path, replace) { path.remove() } } catch (e) { - console.error(e) + console.log(e) } } diff --git a/packages/webpack-plugin/test/platform/common/bind-this.spec.js b/packages/webpack-plugin/test/platform/common/bind-this.spec.js index 45d435187a..d340d9fece 100644 --- a/packages/webpack-plugin/test/platform/common/bind-this.spec.js +++ b/packages/webpack-plugin/test/platform/common/bind-this.spec.js @@ -644,7 +644,7 @@ global.currentInject = { } ` const res = bindThis(input, { renderReduce: true }) - const output = ['a', 'b', 'a', 'c', 'a', 'd', 'name', 'name2'] + const output = ['b', 'a', 'c', 'a', 'd', 'name', 'name2'] expect(res.propKeys.join('')).toBe(output.join('')) }) @@ -668,6 +668,7 @@ global.currentInject = { global.currentInject = { render: function () { this.a && this.a.b; + '' || 123 || this.b; this.b || this.a || ''; } };` From b8c98f554ee7580256615daf077276bdd93aa9b8 Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Wed, 1 Nov 2023 20:43:41 +0800 Subject: [PATCH 5/6] fix code --- packages/webpack-plugin/lib/template-compiler/bind-this.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index 3742e32521..c9ca34b77f 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -162,10 +162,6 @@ function checkPrefix (keys, key) { } function dealRemove (path, replace) { - // while (path.key === 'expression' && t.isExpressionStatement(path.parentPath)) { - // path = path.parentPath - // } - try { if (replace) { path.replaceWith(t.stringLiteral('')) From ee6b4a8a19cef24973fa66254825e3a1f17d5e9f Mon Sep 17 00:00:00 2001 From: zhuzhh Date: Wed, 1 Nov 2023 20:59:26 +0800 Subject: [PATCH 6/6] fix code --- packages/webpack-plugin/lib/template-compiler/bind-this.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/webpack-plugin/lib/template-compiler/bind-this.js b/packages/webpack-plugin/lib/template-compiler/bind-this.js index c9ca34b77f..ddd892435e 100644 --- a/packages/webpack-plugin/lib/template-compiler/bind-this.js +++ b/packages/webpack-plugin/lib/template-compiler/bind-this.js @@ -104,7 +104,7 @@ function checkDelAndGetPath (path) { canDel = false break } - } else if (current.key === 'expression' && t.isExpressionStatement(current.parentPath)) { + } else if (current.key === 'expression' && t.isExpressionStatement(current.parentPath)) { // dealRemove删除节点时需要 delPath = current.parentPath } else { break @@ -169,9 +169,7 @@ function dealRemove (path, replace) { t.validate(path, path.key, null) path.remove() } - } catch (e) { - console.log(e) - } + } catch (e) {} } module.exports = {