Skip to content

Commit 628845b

Browse files
committed
util: introduce printDeprecationMessage function
`printDeprecationMessage` is used to deprecate modules and execution branches. PR-URL: #1822 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 6537fd4 commit 628845b

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

lib/buffer.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const binding = process.binding('buffer');
44
const smalloc = process.binding('smalloc');
55
const util = require('util');
6+
const internalUtil = require('internal/util');
67
const alloc = smalloc.alloc;
78
const truncate = smalloc.truncate;
89
const sliceOnto = smalloc.sliceOnto;
@@ -504,16 +505,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
504505

505506
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
506507
} else {
507-
if (!writeWarned) {
508-
if (process.throwDeprecation)
509-
throw new Error(writeMsg);
510-
else if (process.traceDeprecation)
511-
console.trace(writeMsg);
512-
else
513-
console.error(writeMsg);
514-
writeWarned = true;
515-
}
516-
508+
writeWarned = internalUtil.printDeprecationMessage(writeMsg, writeWarned);
517509
var swap = encoding;
518510
encoding = offset;
519511
offset = length >>> 0;

lib/internal/util.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
exports.printDeprecationMessage = function(msg, warned) {
4+
if (process.noDeprecation)
5+
return true;
6+
7+
if (warned)
8+
return warned;
9+
10+
if (process.throwDeprecation)
11+
throw new Error(msg);
12+
else if (process.traceDeprecation)
13+
console.trace(msg);
14+
else
15+
console.error(msg);
16+
17+
return true;
18+
};

lib/sys.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
'use strict';
22

3-
const util = require('util');
3+
const util = require('internal/util');
44

55
// the sys module was renamed to 'util'.
66
// this shim remains to keep old programs working.
77
// sys is deprecated and shouldn't be used
88

9-
const setExports = util.deprecate(function() {
10-
module.exports = util;
11-
}, 'sys is deprecated. Use util instead.');
12-
13-
setExports();
9+
module.exports = require('util');
10+
util.printDeprecationMessage('sys is deprecated. Use util instead.');

lib/util.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const uv = process.binding('uv');
44
const Debug = require('vm').runInDebugContext('Debug');
5+
const internalUtil = require('internal/util');
56

67
const formatRegExp = /%[sdj%]/g;
78
exports.format = function(f) {
@@ -63,16 +64,7 @@ exports.deprecate = function(fn, msg) {
6364

6465
var warned = false;
6566
function deprecated() {
66-
if (!warned) {
67-
if (process.throwDeprecation) {
68-
throw new Error(msg);
69-
} else if (process.traceDeprecation) {
70-
console.trace(msg);
71-
} else {
72-
console.error(msg);
73-
}
74-
warned = true;
75-
}
67+
warned = internalUtil.printDeprecationMessage(msg, warned);
7668
return fn.apply(this, arguments);
7769
}
7870

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
'lib/internal/smalloc.js',
7676
'lib/internal/socket_list.js',
7777
'lib/internal/repl.js',
78+
'lib/internal/util.js',
7879
],
7980
},
8081

0 commit comments

Comments
 (0)