(a: any, b?: any, c = false)
| 206 | }): any; |
| 207 | function parseExpression(str: any, self: any, thisRequired?: boolean): any; |
| 208 | function parseExpression(a: any, b?: any, c = false) { |
| 209 | let str; |
| 210 | let self; |
| 211 | let thisRequired; |
| 212 | let logScope; |
| 213 | if (typeof a === 'object' && b === undefined) { |
| 214 | str = a.str; |
| 215 | self = a.self; |
| 216 | thisRequired = a.thisRequired; |
| 217 | logScope = a.logScope; |
| 218 | } else { |
| 219 | str = a; |
| 220 | self = b; |
| 221 | thisRequired = c; |
| 222 | } |
| 223 | try { |
| 224 | const contextArr = ['"use strict";', 'var __self = arguments[0];']; |
| 225 | contextArr.push('return '); |
| 226 | let tarStr: string; |
| 227 | |
| 228 | tarStr = (str.value || '').trim(); |
| 229 | |
| 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; |
| 234 | |
| 235 | // 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象 |
| 236 | if (inSameDomain() && (window.parent as any).__newFunc) { |
| 237 | return (window.parent as any).__newFunc(tarStr)(self); |
| 238 | } |
| 239 | const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`; |
| 240 | return new Function('$scope', code)(self); |
| 241 | } catch (err) { |
| 242 | logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self); |
| 243 | return undefined; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | export { |
| 248 | parseExpression, |
no test coverage detected
searching dependent graphs…