From c08f6ecc92f2fa39ba0b59ab230965ffe14dcf1c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 10 Dec 2018 23:16:42 -0500 Subject: [PATCH] Fixes logging of params with default values --- src/system/function.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/system/function.ts b/src/system/function.ts index 3ab2714..cb6fcfd 100644 --- a/src/system/function.ts +++ b/src/system/function.ts @@ -76,27 +76,39 @@ export namespace Functions { return tracked; } + const comma = ','; + const empty = ''; + const equals = '='; + const openBrace = '{'; + const openParen = '('; + const closeParen = ')'; + + const fnBodyRegex = /\(([\s\S]*)\)/; + const fnBodyStripCommentsRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/gm; + const fnBodyStripParamDefaultValueRegex = /\s?=.*$/; + export function getParameters(fn: Function): string[] { if (typeof fn !== 'function') throw new Error('Not supported'); if (fn.length === 0) return []; - const stripCommentsRegex = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/gm; let fnBody: string = Function.prototype.toString.call(fn); - fnBody = fnBody.replace(stripCommentsRegex, '') || fnBody; - fnBody = fnBody.slice(0, fnBody.indexOf('{')); + fnBody = fnBody.replace(fnBodyStripCommentsRegex, empty) || fnBody; + fnBody = fnBody.slice(0, fnBody.indexOf(openBrace)); - let open = fnBody.indexOf('('); - let close = fnBody.indexOf(')'); + let open = fnBody.indexOf(openParen); + let close = fnBody.indexOf(closeParen); open = open >= 0 ? open + 1 : 0; - close = close > 0 ? close : fnBody.indexOf('='); + close = close > 0 ? close : fnBody.indexOf(equals); fnBody = fnBody.slice(open, close); fnBody = `(${fnBody})`; - const match = fnBody.match(/\(([\s\S]*)\)/); - return match != null ? match[1].split(',').map(param => param.trim()) : []; + const match = fnBody.match(fnBodyRegex); + return match != null + ? match[1].split(comma).map(param => param.trim().replace(fnBodyStripParamDefaultValueRegex, empty)) + : []; } export function isPromise(o: any): o is Promise {