Skip to content

Commit

Permalink
Replace function constructor with defineProperty to preserve arity wi…
Browse files Browse the repository at this point in the history
…thout eval [dougwilson#41]
  • Loading branch information
davidje13 committed Jan 7, 2021
1 parent 73364d0 commit dc99086
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,6 @@ function convertDataDescriptorToAccessor (obj, prop, message) {
return descriptor
}

/**
* Create arguments string to keep arity.
*/

function createArgumentsString (arity) {
var str = ''

for (var i = 0; i < arity; i++) {
str += ', arg' + i
}

return str.substr(2)
}

/**
* Create stack string from stack.
*/
Expand Down Expand Up @@ -415,20 +401,17 @@ function wrapfunction (fn, message) {
throw new TypeError('argument fn must be a function')
}

var args = createArgumentsString(fn.length)
var deprecate = this
var stack = getStack()
var site = callSiteLocation(stack[1])

site.name = fn.name

// eslint-disable-next-line no-new-func
var deprecatedfn = new Function('fn', 'log', 'deprecate', 'message', 'site',
'"use strict"\n' +
'return function (' + args + ') {' +
'log.call(deprecate, message, site)\n' +
'return fn.apply(this, arguments)\n' +
'}')(fn, log, this, message, site)

function deprecatedfn () {
log.call(deprecate, message, site)
return fn.apply(this, arguments)
}
Object.defineProperty(deprecatedfn, 'length', { value: fn.length })
return deprecatedfn
}

Expand Down

0 comments on commit dc99086

Please sign in to comment.