MCPcopy Create free account
hub / github.com/Easy-Editor/EasyEditor / parseExpression

Function parseExpression

packages/renderer-core/src/utils/common.ts:191–229  ·  view source on GitHub ↗
(a: any, b?: any, c = false)

Source from the content-addressed store, hash-verified

189}): any
190function parseExpression(str: any, self: any, thisRequired?: boolean): any
191function parseExpression(a: any, b?: any, c = false) {
192 let str: any
193 let self: any
194 let thisRequired: boolean
195 let logScope: string
196 if (typeof a === 'object' && b === undefined) {
197 str = a.str
198 self = a.self
199 thisRequired = a.thisRequired
200 logScope = a.logScope
201 } else {
202 str = a
203 self = b
204 thisRequired = c
205 }
206 try {
207 const contextArr = ['"use strict";', 'var __self = arguments[0];']
208 contextArr.push('return ')
209 let tarStr: string
210
211 tarStr = (str.value || '').trim()
212
213 // NOTE: use __self replace 'this' in the original function str
214 // may be wrong in extreme case which contains '__self' already
215 tarStr = tarStr.replace(/this(\W|$)/g, (_a: any, b: any) => `__self${b}`)
216 tarStr = contextArr.join('\n') + tarStr
217
218 // 默认调用顶层窗口的parseObj, 保障new Function的window对象是顶层的window对象
219 if (inSameDomain() && (window.parent as any).__newFunc) {
220 return (window.parent as any).__newFunc(tarStr)(self)
221 }
222 const code = `with(${thisRequired ? '{}' : '$scope || {}'}) { ${tarStr} }`
223 return new Function('$scope', code)(self)
224 } catch (err) {
225 // @ts-ignore
226 logger.error(`${logScope || ''} parseExpression.error`, err, str, self?.__self ?? self)
227 return undefined
228 }
229}
230
231export { parseExpression }
232

Callers 5

executeLifeCycleMethodFunction · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
parseDataFunction · 0.85

Calls 2

inSameDomainFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected