Skip to content

Commit 6cd7dfb

Browse files
authored
Merge pull request #3027 from alibaba/revert-2271-patch-3
Revert "fix: keep `this` in parse expression function"
2 parents 5d220ce + 418a0bd commit 6cd7dfb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/renderer-core/src/utils/common.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,22 @@ function parseExpression(a: any, b?: any, c = false) {
221221
thisRequired = c;
222222
}
223223
try {
224+
const contextArr = ['"use strict";', 'var __self = arguments[0];'];
225+
contextArr.push('return ');
224226
let tarStr: string;
225227

226228
tarStr = (str.value || '').trim();
227229

228-
let code = `"use strict"; function __wrapper(){ return ${tarStr}} return __wrapper.call(arguments[0])`;
230+
// NOTE: use __self replace 'this' in the original function str
231+
// may be wrong in extreme case which contains '__self' already
232+
tarStr = tarStr.replace(/this(\W|$)/g, (_a: any, b: any) => `__self${b}`);
233+
tarStr = contextArr.join('\n') + tarStr;
229234

230235
// 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象
231236
if (inSameDomain() && (window.parent as any).__newFunc) {
232-
return (window.parent as any).__newFunc(code)(self);
233-
}
234-
if (!thisRequired) {
235-
code = `with($scope){${code}}`;
237+
return (window.parent as any).__newFunc(tarStr)(self);
236238
}
239+
const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`;
237240
return new Function('$scope', code)(self);
238241
} catch (err) {
239242
logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self);

0 commit comments

Comments
 (0)