Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

var eventListenerCount = require('./lib/compat').eventListenerCount
var wrapFunctionInner = require('./lib/compat').wrapFunctionInner
var relative = require('path').relative

/**
Expand Down Expand Up @@ -65,20 +66,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 @@ -398,21 +385,13 @@ 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)

return deprecatedfn
return wrapFunctionInner(fn, log, deprecate, message, site)
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/compat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ lazyProperty(module.exports, 'eventListenerCount', function eventListenerCount (
return EventEmitter.listenerCount || require('./event-listener-count')
})

lazyProperty(module.exports, 'wrapFunctionInner', function wrapFunctionInner () {
return require('./wrap-function-inner')
})

/**
* Define a lazy property.
*/
Expand Down
72 changes: 72 additions & 0 deletions lib/compat/wrap-function-inner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*!
* depd
* Copyright(c) 2014-2018 Douglas Christopher Wilson
* MIT Licensed
*/

'use strict'

function canUseDefinePropertyOnFunctionLength () {
function detectfn () {}

try {
// Node.js 3.3+.
Object.defineProperty(detectfn, 'length', {
configurable: true,
enumerable: false,
value: 1,
writable: false
})
} catch (err) {
// Likely Node.js 2.5 or older.
}

return detectfn.length === 1
}

/**
* 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)
}

function wrapFunctionWithDefineProperty (fn, log, deprecate, message, site) {
var deprecatedfn = function () {
log.call(deprecate, message, site)
return fn.apply(this, arguments)
}

// Preserve fn's arity.
Object.defineProperty(deprecatedfn, 'length', {
configurable: true,
enumerable: false,
value: fn.length,
writable: false
})

return deprecatedfn
}

function wrapFunctionWithEval (fn, log, deprecate, message, site) {
// Preserve fn's arity by manually constructing an arguments string and
// eval'ing it into a new function.
var args = createArgumentsString(fn.length)
// eslint-disable-next-line no-new-func
return 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, deprecate, message, site)
}

module.exports = canUseDefinePropertyOnFunctionLength()
? wrapFunctionWithDefineProperty : wrapFunctionWithEval