util: introduce `printDeprecationMessage` function
authorVladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
Thu, 28 May 2015 14:01:56 +0000 (17:01 +0300)
committerVladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
Thu, 4 Jun 2015 07:59:43 +0000 (10:59 +0300)
`printDeprecationMessage` is used to deprecate modules
and execution branches.

PR-URL: https://github.com/nodejs/io.js/pull/1822
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
lib/buffer.js
lib/internal/util.js [new file with mode: 0644]
lib/sys.js
lib/util.js
node.gyp

index 1b9c684..ccd899d 100644 (file)
@@ -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;
@@ -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;
diff --git a/lib/internal/util.js b/lib/internal/util.js
new file mode 100644 (file)
index 0000000..7657633
--- /dev/null
@@ -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;
+};
index 74143b5..a34ea0b 100644 (file)
@@ -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.');
index 401d55b..c4aea9d 100644 (file)
@@ -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) {
@@ -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);
   }
 
index de0e47e..cbc536d 100644 (file)
--- a/node.gyp
+++ b/node.gyp
@@ -75,6 +75,7 @@
       'lib/internal/smalloc.js',
       'lib/internal/socket_list.js',
       'lib/internal/repl.js',
+      'lib/internal/util.js',
     ],
   },