Skip to content

Commit 71460f2

Browse files
committed
Put in fallback to old code for super-old platforms
1 parent dc99086 commit 71460f2

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,20 @@ function formatLocation (callSite) {
360360
':' + callSite[2]
361361
}
362362

363+
/**
364+
* Create arguments string to keep arity.
365+
*/
366+
367+
function createArgumentsString (arity) {
368+
var str = ''
369+
370+
for (var i = 0; i < arity; i++) {
371+
str += ', arg' + i
372+
}
373+
374+
return str.substr(2)
375+
}
376+
363377
/**
364378
* Get the stack as array of call sites.
365379
*/
@@ -407,11 +421,23 @@ function wrapfunction (fn, message) {
407421

408422
site.name = fn.name
409423

410-
function deprecatedfn () {
424+
var deprecatedfn = function () {
411425
log.call(deprecate, message, site)
412426
return fn.apply(this, arguments)
413427
}
414-
Object.defineProperty(deprecatedfn, 'length', { value: fn.length })
428+
try {
429+
Object.defineProperty(deprecatedfn, 'length', { value: fn.length })
430+
} catch (e) {
431+
// Fallback for NodeJS 2.5 and below
432+
var args = createArgumentsString(fn.length)
433+
// eslint-disable-next-line no-new-func
434+
deprecatedfn = new Function('fn', 'log', 'deprecate', 'message', 'site',
435+
'"use strict"\n' +
436+
'return function (' + args + ') {' +
437+
'log.call(deprecate, message, site)\n' +
438+
'return fn.apply(this, arguments)\n' +
439+
'}')(fn, log, this, message, site)
440+
}
415441
return deprecatedfn
416442
}
417443

0 commit comments

Comments
 (0)