GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: preserve `length` of deprecated functions · nodejs/node@8ca56a8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • util.js
  • test/parallel
    • test-util-deprecate.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ function pendingDeprecate(fn, msg, code) {
142142
emitDeprecationWarning();
143143
return ReflectApply(fn, this, args);
144144
}
145+
146+
ObjectDefineProperty(deprecated, 'length', {
147+
__proto__: null,
148+
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
149+
});
150+
145151
return deprecated;
146152
}
147153

@@ -180,6 +186,11 @@ function deprecate(fn, msg, code, useEmitSync) {
180186
deprecated.prototype = fn.prototype;
181187
}
182188

189+
ObjectDefineProperty(deprecated, 'length', {
190+
__proto__: null,
191+
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
192+
});
193+
183194
return deprecated;
184195
}
185196

Collapse file

‎test/parallel/test-util-deprecate.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-deprecate.js
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --expose-internals
12
'use strict';
23

34
require('../common');
@@ -6,9 +7,27 @@ require('../common');
67

78
const assert = require('assert');
89
const util = require('util');
10+
const internalUtil = require('internal/util');
911

1012
const expectedWarnings = new Map();
1113

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+
1231
// Emits deprecation only once if same function is called.
1332
{
1433
const msg = 'fhqwhgads';

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.