GeezerVison is showing github.com — util: preserve `length` of deprecated functions · nodejs/node@8ca56a8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -142,6 +142,12 @@ function pendingDeprecate(fn, msg, code) { | 142 | 142 | emitDeprecationWarning(); | 143 | 143 | return ReflectApply(fn, this, args); | 144 | 144 | } | | 145 | + | | 146 | + ObjectDefineProperty(deprecated, 'length', { | | 147 | + __proto__: null, | | 148 | + ...ObjectGetOwnPropertyDescriptor(fn, 'length'), | | 149 | + }); | | 150 | + | 145 | 151 | return deprecated; | 146 | 152 | } | 147 | 153 |
| @@ -180,6 +186,11 @@ function deprecate(fn, msg, code, useEmitSync) { | 180 | 186 | deprecated.prototype = fn.prototype; | 181 | 187 | } | 182 | 188 |
| | 189 | + ObjectDefineProperty(deprecated, 'length', { | | 190 | + __proto__: null, | | 191 | + ...ObjectGetOwnPropertyDescriptor(fn, 'length'), | | 192 | + }); | | 193 | + | 183 | 194 | return deprecated; | 184 | 195 | } | 185 | 196 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-deprecate.js +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --expose-internals | 1 | 2 | 'use strict'; | 2 | 3 |
| 3 | 4 | require('../common'); | @@ -6,9 +7,27 @@ require('../common'); | 6 | 7 |
| 7 | 8 | const assert = require('assert'); | 8 | 9 | const util = require('util'); | | 10 | +const internalUtil = require('internal/util'); | 9 | 11 |
| 10 | 12 | const expectedWarnings = new Map(); | 11 | 13 |
| | 14 | +// Deprecated function length is preserved | | 15 | +for (const fn of [ | | 16 | + function() {}, | | 17 | + function(a) {}, | | 18 | + function(a, b, c) {}, | | 19 | + function(...args) {}, | | 20 | + function(a, b, c, ...args) {}, | | 21 | + () => {}, | | 22 | + (a) => {}, | | 23 | + (a, b, c) => {}, | | 24 | + (...args) => {}, | | 25 | + (a, b, c, ...args) => {}, | | 26 | +]) { | | 27 | + assert.strictEqual(util.deprecate(fn).length, fn.length); | | 28 | + assert.strictEqual(internalUtil.pendingDeprecate(fn).length, fn.length); | | 29 | +} | | 30 | + | 12 | 31 | // Emits deprecation only once if same function is called. | 13 | 32 | { | 14 | 33 | const msg = 'fhqwhgads'; | |
|