fix: prevent matching native statements in serializeFunction (#4585)

This commit is contained in:
Jonas Galvez 2018-12-19 06:16:00 -02:00 committed by Pooya Parsa
parent 186b3cb233
commit 5b58272d1a

View File

@ -463,10 +463,10 @@ export function defineAlias(src, target, prop, opts = {}) {
export function serializeFunction(func) {
let open = false
return serialize(func)
.replace(/^(\s*):(\w+)\(/gm, (_, spaces) => {
.replace(serializeFunction.assignmentRE, (_, spaces) => {
return `${spaces}:function(`
})
.replace(/^(\s*)(\w+)\s*\((.*?)\)\s*\{/gm, (_, spaces, name, args) => {
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
if (open) {
return `${spaces}${name}:function(${args}) {`
} else {
@ -476,3 +476,6 @@ export function serializeFunction(func) {
})
.replace(`${func.name}(`, 'function(')
}
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm