Skip to content

Commit

Permalink
util: introduce printDeprecationMessage function
Browse files Browse the repository at this point in the history
`printDeprecationMessage` is used to deprecate modules
and execution branches.

PR-URL: #1822
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
vkurchatkin committed Jun 4, 2015
1 parent 6537fd4 commit 628845b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
12 changes: 2 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const binding = process.binding('buffer');
const smalloc = process.binding('smalloc');
const util = require('util');
const internalUtil = require('internal/util');
const alloc = smalloc.alloc;
const truncate = smalloc.truncate;
const sliceOnto = smalloc.sliceOnto;
Expand Down Expand Up @@ -504,16 +505,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {

// XXX legacy write(string, encoding, offset, length) - remove in v0.13
} else {
if (!writeWarned) {
if (process.throwDeprecation)
throw new Error(writeMsg);
else if (process.traceDeprecation)
console.trace(writeMsg);
else
console.error(writeMsg);
writeWarned = true;
}

writeWarned = internalUtil.printDeprecationMessage(writeMsg, writeWarned);
var swap = encoding;
encoding = offset;
offset = length >>> 0;
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

exports.printDeprecationMessage = function(msg, warned) {
if (process.noDeprecation)
return true;

if (warned)
return warned;

if (process.throwDeprecation)
throw new Error(msg);
else if (process.traceDeprecation)
console.trace(msg);
else
console.error(msg);

return true;
};
9 changes: 3 additions & 6 deletions lib/sys.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';

const util = require('util');
const util = require('internal/util');

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

const setExports = util.deprecate(function() {
module.exports = util;
}, 'sys is deprecated. Use util instead.');

setExports();
module.exports = require('util');
util.printDeprecationMessage('sys is deprecated. Use util instead.');
12 changes: 2 additions & 10 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const uv = process.binding('uv');
const Debug = require('vm').runInDebugContext('Debug');
const internalUtil = require('internal/util');

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

var warned = false;
function deprecated() {
if (!warned) {
if (process.throwDeprecation) {
throw new Error(msg);
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
}
warned = true;
}
warned = internalUtil.printDeprecationMessage(msg, warned);
return fn.apply(this, arguments);
}

Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
'lib/internal/smalloc.js',
'lib/internal/socket_list.js',
'lib/internal/repl.js',
'lib/internal/util.js',
],
},

Expand Down

0 comments on commit 628845b

Please sign in to comment.