From 5b58272d1a0371b3f5e307a279de74e35d83f1b5 Mon Sep 17 00:00:00 2001 From: Jonas Galvez Date: Wed, 19 Dec 2018 06:16:00 -0200 Subject: [PATCH] fix: prevent matching native statements in serializeFunction (#4585) --- packages/common/src/utils.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/common/src/utils.js b/packages/common/src/utils.js index 939a7287c3..127a4ce85c 100644 --- a/packages/common/src/utils.js +++ b/packages/common/src/utils.js @@ -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