Skip to content

Commit e569b50

Browse files
authored
Merge pull request #2047 from didi/fix-render-code
fix: 修复render函数复杂computed取值有误的问题(#2037)
2 parents 282cb4e + c754afe commit e569b50

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

packages/webpack-plugin/lib/template-compiler/bind-this.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const babylon = require('@babel/parser')
22
const traverse = require('@babel/traverse').default
33
const t = require('@babel/types')
44
const generate = require('@babel/generator').default
5+
const isValidIdentifierStr = require('../utils/is-valid-identifier-str')
56

67
const names = 'Infinity,undefined,NaN,isFinite,isNaN,' +
78
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
@@ -41,7 +42,7 @@ function getCollectPath (path) {
4142
if (current.node.computed) {
4243
if (t.isLiteral(current.node.property)) {
4344
if (t.isStringLiteral(current.node.property)) {
44-
if (dangerousKeyMap[current.node.property.value]) {
45+
if (dangerousKeyMap[current.node.property.value] || !isValidIdentifierStr(current.node.property.value)) {
4546
break
4647
}
4748
keyPath += `.${current.node.property.value}`

packages/webpack-plugin/test/platform/common/bind-this.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,14 @@ global.currentInject.render = function (_i, _c, _r, _sc) {
114114
a[c];
115115
c.d;
116116
a.b[c.d];
117-
e;
118-
e[0].name
117+
118+
e1[0].name;
119+
e2["a.b.c"]
120+
f1["a.b.c"]["d"]
121+
f2["a"]["b.c.d"]
122+
g1.a["b.c"].d
123+
g2.a["b"].c.d
124+
g3.a["b"]["c.d"].e
119125
}`
120126
const res = bindThis(input, { needCollect: true, renderReduce: true }).code
121127
const output = `
@@ -125,7 +131,13 @@ global.currentInject.render = function (_i, _c, _r, _sc) {
125131
_sc("a")[_sc("c")];
126132
_c("a.b")[_c("c.d")];
127133
128-
_sc("e");
134+
_c("e1[0].name");
135+
_sc("e2")["a.b.c"];
136+
_sc("f1")["a.b.c"]["d"];
137+
_c("f2.a")["b.c.d"];
138+
_c("g1.a")["b.c"].d;
139+
_c("g2.a.b.c.d");
140+
_c("g3.a.b")["c.d"].e;
129141
};`
130142
expect(trimBlankRow(res)).toBe(trimBlankRow(output))
131143
})

0 commit comments

Comments
 (0)