@@ -360,6 +360,20 @@ function formatLocation (callSite) {
360
360
':' + callSite [ 2 ]
361
361
}
362
362
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
+
363
377
/**
364
378
* Get the stack as array of call sites.
365
379
*/
@@ -407,11 +421,23 @@ function wrapfunction (fn, message) {
407
421
408
422
site . name = fn . name
409
423
410
- function deprecatedfn ( ) {
424
+ var deprecatedfn = function ( ) {
411
425
log . call ( deprecate , message , site )
412
426
return fn . apply ( this , arguments )
413
427
}
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
+ }
415
441
return deprecatedfn
416
442
}
417
443
0 commit comments