pax_global_header00006660000000000000000000000064131364667100014521gustar00rootroot0000000000000052 comment=15c5604aaab7befd413506e86670168d7481043a nodejs-depd-1.1.1/000077500000000000000000000000001313646671000137155ustar00rootroot00000000000000nodejs-depd-1.1.1/.eslintignore000066400000000000000000000000261313646671000164160ustar00rootroot00000000000000coverage node_modules nodejs-depd-1.1.1/.eslintrc000066400000000000000000000000341313646671000155360ustar00rootroot00000000000000{ "extends": "standard" } nodejs-depd-1.1.1/.gitignore000066400000000000000000000000701313646671000157020ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log package-lock.json nodejs-depd-1.1.1/.travis.yml000066400000000000000000000024221313646671000160260ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.8" - "5.12" - "6.11" - "7.10" - "8.1" sudo: false cache: directories: - node_modules before_install: # Skip updating shrinkwrap / lock - "npm config set shrinkwrap false" # Remove all non-test dependencies - "npm rm --save-dev benchmark beautify-benchmark || true" # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul" - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" - "test $TRAVIS_NODE_VERSION = '0.6' -o $TRAVIS_NODE_VERSION = '0.8' || npm install browserify" - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev eslint eslint-config-standard eslint-plugin-markdown eslint-plugin-promise eslint-plugin-standard" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-ci" - "test -z $(npm -ps ls eslint ) || npm run-script lint" after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" nodejs-depd-1.1.1/History.md000066400000000000000000000036361313646671000157100ustar00rootroot000000000000001.1.1 / 2017-07-27 ================== * Remove unnecessary `Buffer` loading * Support Node.js 0.6 to 8.x 1.1.0 / 2015-09-14 ================== * Enable strict mode in more places * Support io.js 3.x * Support io.js 2.x * Support web browser loading - Requires bundler like Browserify or webpack 1.0.1 / 2015-04-07 ================== * Fix `TypeError`s when under `'use strict'` code * Fix useless type name on auto-generated messages * Support io.js 1.x * Support Node.js 0.12 1.0.0 / 2014-09-17 ================== * No changes 0.4.5 / 2014-09-09 ================== * Improve call speed to functions using the function wrapper * Support Node.js 0.6 0.4.4 / 2014-07-27 ================== * Work-around v8 generating empty stack traces 0.4.3 / 2014-07-26 ================== * Fix exception when global `Error.stackTraceLimit` is too low 0.4.2 / 2014-07-19 ================== * Correct call site for wrapped functions and properties 0.4.1 / 2014-07-19 ================== * Improve automatic message generation for function properties 0.4.0 / 2014-07-19 ================== * Add `TRACE_DEPRECATION` environment variable * Remove non-standard grey color from color output * Support `--no-deprecation` argument * Support `--trace-deprecation` argument * Support `deprecate.property(fn, prop, message)` 0.3.0 / 2014-06-16 ================== * Add `NO_DEPRECATION` environment variable 0.2.0 / 2014-06-15 ================== * Add `deprecate.property(obj, prop, message)` * Remove `supports-color` dependency for node.js 0.8 0.1.0 / 2014-06-15 ================== * Add `deprecate.function(fn, message)` * Add `process.on('deprecation', fn)` emitter * Automatically generate message when omitted from `deprecate()` 0.0.1 / 2014-06-15 ================== * Fix warning for dynamic calls at singe call site 0.0.0 / 2014-06-15 ================== * Initial implementation nodejs-depd-1.1.1/LICENSE000066400000000000000000000021061313646671000147210ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014-2017 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. nodejs-depd-1.1.1/Readme.md000066400000000000000000000237131313646671000154420ustar00rootroot00000000000000# depd [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-image]][node-url] [![Linux Build][travis-image]][travis-url] [![Windows Build][appveyor-image]][appveyor-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Gratipay][gratipay-image]][gratipay-url] Deprecate all the things > With great modules comes great responsibility; mark things deprecated! ## Install This module is installed directly using `npm`: ```sh $ npm install depd ``` This module can also be bundled with systems like [Browserify](http://browserify.org/) or [webpack](https://webpack.github.io/), though by default this module will alter it's API to no longer display or track deprecations. ## API ```js var deprecate = require('depd')('my-module') ``` This library allows you to display deprecation messages to your users. This library goes above and beyond with deprecation warnings by introspection of the call stack (but only the bits that it is interested in). Instead of just warning on the first invocation of a deprecated function and never again, this module will warn on the first invocation of a deprecated function per unique call site, making it ideal to alert users of all deprecated uses across the code base, rather than just whatever happens to execute first. The deprecation warnings from this module also include the file and line information for the call into the module that the deprecated function was in. **NOTE** this library has a similar interface to the `debug` module, and this module uses the calling file to get the boundary for the call stacks, so you should always create a new `deprecate` object in each file and not within some central file. ### depd(namespace) Create a new deprecate function that uses the given namespace name in the messages and will display the call site prior to the stack entering the file this function was called from. It is highly suggested you use the name of your module as the namespace. ### deprecate(message) Call this function from deprecated code to display a deprecation message. This message will appear once per unique caller site. Caller site is the first call site in the stack in a different file from the caller of this function. If the message is omitted, a message is generated for you based on the site of the `deprecate()` call and will display the name of the function called, similar to the name displayed in a stack trace. ### deprecate.function(fn, message) Call this function to wrap a given function in a deprecation message on any call to the function. An optional message can be supplied to provide a custom message. ### deprecate.property(obj, prop, message) Call this function to wrap a given property on object in a deprecation message on any accessing or setting of the property. An optional message can be supplied to provide a custom message. The method must be called on the object where the property belongs (not inherited from the prototype). If the property is a data descriptor, it will be converted to an accessor descriptor in order to display the deprecation message. ### process.on('deprecation', fn) This module will allow easy capturing of deprecation errors by emitting the errors as the type "deprecation" on the global `process`. If there are no listeners for this type, the errors are written to STDERR as normal, but if there are any listeners, nothing will be written to STDERR and instead only emitted. From there, you can write the errors in a different format or to a logging source. The error represents the deprecation and is emitted only once with the same rules as writing to STDERR. The error has the following properties: - `message` - This is the message given by the library - `name` - This is always `'DeprecationError'` - `namespace` - This is the namespace the deprecation came from - `stack` - This is the stack of the call to the deprecated thing Example `error.stack` output: ``` DeprecationError: my-cool-module deprecated oldfunction at Object. ([eval]-wrapper:6:22) at Module._compile (module.js:456:26) at evalScript (node.js:532:25) at startup (node.js:80:7) at node.js:902:3 ``` ### process.env.NO_DEPRECATION As a user of modules that are deprecated, the environment variable `NO_DEPRECATION` is provided as a quick solution to silencing deprecation warnings from being output. The format of this is similar to that of `DEBUG`: ```sh $ NO_DEPRECATION=my-module,othermod node app.js ``` This will suppress deprecations from being output for "my-module" and "othermod". The value is a list of comma-separated namespaces. To suppress every warning across all namespaces, use the value `*` for a namespace. Providing the argument `--no-deprecation` to the `node` executable will suppress all deprecations (only available in Node.js 0.8 or higher). **NOTE** This will not suppress the deperecations given to any "deprecation" event listeners, just the output to STDERR. ### process.env.TRACE_DEPRECATION As a user of modules that are deprecated, the environment variable `TRACE_DEPRECATION` is provided as a solution to getting more detailed location information in deprecation warnings by including the entire stack trace. The format of this is the same as `NO_DEPRECATION`: ```sh $ TRACE_DEPRECATION=my-module,othermod node app.js ``` This will include stack traces for deprecations being output for "my-module" and "othermod". The value is a list of comma-separated namespaces. To trace every warning across all namespaces, use the value `*` for a namespace. Providing the argument `--trace-deprecation` to the `node` executable will trace all deprecations (only available in Node.js 0.8 or higher). **NOTE** This will not trace the deperecations silenced by `NO_DEPRECATION`. ## Display ![message](files/message.png) When a user calls a function in your library that you mark deprecated, they will see the following written to STDERR (in the given colors, similar colors and layout to the `debug` module): ``` bright cyan bright yellow | | reset cyan | | | | ▼ ▼ ▼ ▼ my-cool-module deprecated oldfunction [eval]-wrapper:6:22 ▲ ▲ ▲ ▲ | | | | namespace | | location of mycoolmod.oldfunction() call | deprecation message the word "deprecated" ``` If the user redirects their STDERR to a file or somewhere that does not support colors, they see (similar layout to the `debug` module): ``` Sun, 15 Jun 2014 05:21:37 GMT my-cool-module deprecated oldfunction at [eval]-wrapper:6:22 ▲ ▲ ▲ ▲ ▲ | | | | | timestamp of message namespace | | location of mycoolmod.oldfunction() call | deprecation message the word "deprecated" ``` ## Examples ### Deprecating all calls to a function This will display a deprecated message about "oldfunction" being deprecated from "my-module" on STDERR. ```js var deprecate = require('depd')('my-cool-module') // message automatically derived from function name // Object.oldfunction exports.oldfunction = deprecate.function(function oldfunction () { // all calls to function are deprecated }) // specific message exports.oldfunction = deprecate.function(function () { // all calls to function are deprecated }, 'oldfunction') ``` ### Conditionally deprecating a function call This will display a deprecated message about "weirdfunction" being deprecated from "my-module" on STDERR when called with less than 2 arguments. ```js var deprecate = require('depd')('my-cool-module') exports.weirdfunction = function () { if (arguments.length < 2) { // calls with 0 or 1 args are deprecated deprecate('weirdfunction args < 2') } } ``` When calling `deprecate` as a function, the warning is counted per call site within your own module, so you can display different deprecations depending on different situations and the users will still get all the warnings: ```js var deprecate = require('depd')('my-cool-module') exports.weirdfunction = function () { if (arguments.length < 2) { // calls with 0 or 1 args are deprecated deprecate('weirdfunction args < 2') } else if (typeof arguments[0] !== 'string') { // calls with non-string first argument are deprecated deprecate('weirdfunction non-string first arg') } } ``` ### Deprecating property access This will display a deprecated message about "oldprop" being deprecated from "my-module" on STDERR when accessed. A deprecation will be displayed when setting the value and when getting the value. ```js var deprecate = require('depd')('my-cool-module') exports.oldprop = 'something' // message automatically derives from property name deprecate.property(exports, 'oldprop') // explicit message deprecate.property(exports, 'oldprop', 'oldprop >= 0.10') ``` ## License [MIT](LICENSE) [npm-version-image]: https://img.shields.io/npm/v/depd.svg [npm-downloads-image]: https://img.shields.io/npm/dm/depd.svg [npm-url]: https://npmjs.org/package/depd [travis-image]: https://img.shields.io/travis/dougwilson/nodejs-depd/master.svg?label=linux [travis-url]: https://travis-ci.org/dougwilson/nodejs-depd [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/nodejs-depd/master.svg?label=windows [appveyor-url]: https://ci.appveyor.com/project/dougwilson/nodejs-depd [coveralls-image]: https://img.shields.io/coveralls/dougwilson/nodejs-depd/master.svg [coveralls-url]: https://coveralls.io/r/dougwilson/nodejs-depd?branch=master [node-image]: https://img.shields.io/node/v/depd.svg [node-url]: https://nodejs.org/en/download/ [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg [gratipay-url]: https://www.gratipay.com/dougwilson/ nodejs-depd-1.1.1/appveyor.yml000066400000000000000000000017551313646671000163150ustar00rootroot00000000000000environment: matrix: - nodejs_version: "0.8" - nodejs_version: "0.10" - nodejs_version: "0.12" - nodejs_version: "1.8" - nodejs_version: "2.5" - nodejs_version: "3.3" - nodejs_version: "4.8" - nodejs_version: "5.12" - nodejs_version: "6.11" - nodejs_version: "7.10" - nodejs_version: "8.1" cache: - node_modules install: - ps: Install-Product node $env:nodejs_version - npm config set shrinkwrap false - npm rm --save-dev benchmark beautify-benchmark & ver > nul - if "%nodejs_version%" equ "0.8" npm rm --save-dev istanbul - npm rm --save-dev eslint eslint-config-standard eslint-plugin-markdown eslint-plugin-promise eslint-plugin-standard - if exist node_modules npm prune - if exist node_modules npm rebuild - npm install build: off test_script: - node --version - npm --version - set npm_test_command=test - for /f %%l in ('npm -ps ls istanbul') do set npm_test_command=test-ci - npm run %npm_test_command% version: "{build}" nodejs-depd-1.1.1/benchmark/000077500000000000000000000000001313646671000156475ustar00rootroot00000000000000nodejs-depd-1.1.1/benchmark/index.js000066400000000000000000000012201313646671000173070ustar00rootroot00000000000000var fs = require('fs') var path = require('path') var spawn = require('child_process').spawn var exe = process.argv[0] var cwd = process.cwd() runScripts(fs.readdirSync(__dirname)) function runScripts (fileNames) { var fileName = fileNames.shift() if (!fileName) return if (!/\.js$/i.test(fileName)) return runScripts(fileNames) if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames) var fullPath = path.join(__dirname, fileName) console.log('> %s %s', exe, path.relative(cwd, fullPath)) var proc = spawn(exe, [fullPath], { 'stdio': 'inherit' }) proc.on('exit', function () { runScripts(fileNames) }) } nodejs-depd-1.1.1/benchmark/wrapfunction.js000066400000000000000000000012511313646671000207230ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ process.env.NO_DEPRECATION = 'my-lib' global.mylib = require('../test/fixtures/libs/my') var suite = new benchmark.Suite() suite.add({ name: 'function', minSamples: 100, fn: 'mylib.fn()' }) suite.add({ name: 'wrapped', minSamples: 100, fn: 'mylib.oldfn()' }) suite.add({ name: 'call log', minSamples: 100, fn: 'mylib.old()' }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) nodejs-depd-1.1.1/benchmark/wrapproperty.js000066400000000000000000000011671313646671000207700ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ process.env.NO_DEPRECATION = 'my-lib' global.mylib = require('../test/fixtures/libs/my') var suite = new benchmark.Suite() suite.add({ name: 'property', minSamples: 100, fn: 'mylib.prop = mylib.prop' }) suite.add({ name: 'wrapped', minSamples: 100, fn: 'mylib.propa = mylib.propa' }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) nodejs-depd-1.1.1/files/000077500000000000000000000000001313646671000150175ustar00rootroot00000000000000nodejs-depd-1.1.1/files/message.png000066400000000000000000000054511313646671000171560ustar00rootroot00000000000000PNG  IHDRF3gAMA aPLTE+3:33:33+:++LHf+L3\+l:fH\3f:fH\\\HnnHn\nnLL+fl+f:L+lLLff:L\nf+l::\nffې۶Ll:Lfl+ې۶LlGi pHYs(JtEXtSoftwarePaint.NET v3.5.11GB7IDAThC {Eǯ6TP-%TDP~o^vgvJ(O{}LY,W\Vt䦰 w2@铓6Lv 3D""RyH Eё xpg=Rv S9Kpw1E:-%im{rqFkT2c::M>O6F ْlO|mip&:_)[ 9n<~=ۂχT&b:[%;TTf& bDCلIU%ou)Lk/rux|NNzdxoK᧙< _?&). ۖZS{5<ޟMLN;ɖݣUI 66RdWcϙ޶&ͷA\W'1l>.x_?x76'.֥p!Ax"/Lou;DvU}%ӼeL.Xm-`H3oKd)w8Wo3qHf@z؃e/ 0[ mI̳:ә,Ӧu}>/Vߜh,=:f0!Ok]tb] 7F,Ll{'xGscE1MK/'*au Nx5`8TɈ>19e)wiKia_rcNr 4,Ni =mL Ŝ`r&w\j|=Ǡ3H0d&i=>$źv[3JMKi@~f%#ǻ.08үNz fXJSd66\FxŚ<}Rf[S5e $^`xlPH>Hf;5,0Kvx~j֝fxt=+!K(Hv9Dsc [XN8ssa1xKl0'$eb͉F8-[.qVqjzM%IXg]+`UJcp#.@_-o(k9Obc TNZSy4Nrk|Y6(+W._i.  Dq@sMtfd$e%y{- Z NYP#,?-$,{{7V_ s&\'DYse7w\ZudkkA'ˀXdv,[[v _>sgbq[7pmMpb襌,=K03@E@ Ncz֜f8s!w%htEzW2ONuul]K/1 724AcXR&"of`ٲXtrwLRH=$N2)gHXOx ;L_A@|Yz&1໚P9{9 ZwucUě hHr?~,NMS̗.x}Qٯ Se%~n|OǛ;s:VZjuTUwҪ:8GIENDB`nodejs-depd-1.1.1/index.js000066400000000000000000000245741313646671000153760ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014-2017 Douglas Christopher Wilson * MIT Licensed */ /** * Module dependencies. */ var callSiteToString = require('./lib/compat').callSiteToString var eventListenerCount = require('./lib/compat').eventListenerCount var relative = require('path').relative /** * Module exports. */ module.exports = depd /** * Get the path to base files on. */ var basePath = process.cwd() /** * Determine if namespace is contained in the string. */ function containsNamespace (str, namespace) { var val = str.split(/[ ,]+/) namespace = String(namespace).toLowerCase() for (var i = 0; i < val.length; i++) { if (!(str = val[i])) continue // namespace contained if (str === '*' || str.toLowerCase() === namespace) { return true } } return false } /** * Convert a data descriptor to accessor descriptor. */ function convertDataDescriptorToAccessor (obj, prop, message) { var descriptor = Object.getOwnPropertyDescriptor(obj, prop) var value = descriptor.value descriptor.get = function getter () { return value } if (descriptor.writable) { descriptor.set = function setter (val) { return (value = val) } } delete descriptor.value delete descriptor.writable Object.defineProperty(obj, prop, descriptor) return descriptor } /** * Create arguments string to keep arity. */ function createArgumentsString (arity) { var str = '' for (var i = 0; i < arity; i++) { str += ', arg' + i } return str.substr(2) } /** * Create stack string from stack. */ function createStackString (stack) { var str = this.name + ': ' + this.namespace if (this.message) { str += ' deprecated ' + this.message } for (var i = 0; i < stack.length; i++) { str += '\n at ' + callSiteToString(stack[i]) } return str } /** * Create deprecate for namespace in caller. */ function depd (namespace) { if (!namespace) { throw new TypeError('argument namespace is required') } var stack = getStack() var site = callSiteLocation(stack[1]) var file = site[0] function deprecate (message) { // call to self as log log.call(deprecate, message) } deprecate._file = file deprecate._ignored = isignored(namespace) deprecate._namespace = namespace deprecate._traced = istraced(namespace) deprecate._warned = Object.create(null) deprecate.function = wrapfunction deprecate.property = wrapproperty return deprecate } /** * Determine if namespace is ignored. */ function isignored (namespace) { /* istanbul ignore next: tested in a child processs */ if (process.noDeprecation) { // --no-deprecation support return true } var str = process.env.NO_DEPRECATION || '' // namespace ignored return containsNamespace(str, namespace) } /** * Determine if namespace is traced. */ function istraced (namespace) { /* istanbul ignore next: tested in a child processs */ if (process.traceDeprecation) { // --trace-deprecation support return true } var str = process.env.TRACE_DEPRECATION || '' // namespace traced return containsNamespace(str, namespace) } /** * Display deprecation message. */ function log (message, site) { var haslisteners = eventListenerCount(process, 'deprecation') !== 0 // abort early if no destination if (!haslisteners && this._ignored) { return } var caller var callFile var callSite var i = 0 var seen = false var stack = getStack() var file = this._file if (site) { // provided site callSite = callSiteLocation(stack[1]) callSite.name = site.name file = callSite[0] } else { // get call site i = 2 site = callSiteLocation(stack[i]) callSite = site } // get caller of deprecated thing in relation to file for (; i < stack.length; i++) { caller = callSiteLocation(stack[i]) callFile = caller[0] if (callFile === file) { seen = true } else if (callFile === this._file) { file = this._file } else if (seen) { break } } var key = caller ? site.join(':') + '__' + caller.join(':') : undefined if (key !== undefined && key in this._warned) { // already warned return } this._warned[key] = true // generate automatic message from call site if (!message) { message = callSite === site || !callSite.name ? defaultMessage(site) : defaultMessage(callSite) } // emit deprecation if listeners exist if (haslisteners) { var err = DeprecationError(this._namespace, message, stack.slice(i)) process.emit('deprecation', err) return } // format and write message var format = process.stderr.isTTY ? formatColor : formatPlain var msg = format.call(this, message, caller, stack.slice(i)) process.stderr.write(msg + '\n', 'utf8') } /** * Get call site location as array. */ function callSiteLocation (callSite) { var file = callSite.getFileName() || '' var line = callSite.getLineNumber() var colm = callSite.getColumnNumber() if (callSite.isEval()) { file = callSite.getEvalOrigin() + ', ' + file } var site = [file, line, colm] site.callSite = callSite site.name = callSite.getFunctionName() return site } /** * Generate a default message from the site. */ function defaultMessage (site) { var callSite = site.callSite var funcName = site.name // make useful anonymous name if (!funcName) { funcName = '' } var context = callSite.getThis() var typeName = context && callSite.getTypeName() // ignore useless type name if (typeName === 'Object') { typeName = undefined } // make useful type name if (typeName === 'Function') { typeName = context.name || typeName } return typeName && callSite.getMethodName() ? typeName + '.' + funcName : funcName } /** * Format deprecation message without color. */ function formatPlain (msg, caller, stack) { var timestamp = new Date().toUTCString() var formatted = timestamp + ' ' + this._namespace + ' deprecated ' + msg // add stack trace if (this._traced) { for (var i = 0; i < stack.length; i++) { formatted += '\n at ' + callSiteToString(stack[i]) } return formatted } if (caller) { formatted += ' at ' + formatLocation(caller) } return formatted } /** * Format deprecation message with color. */ function formatColor (msg, caller, stack) { var formatted = '\x1b[36;1m' + this._namespace + '\x1b[22;39m' + // bold cyan ' \x1b[33;1mdeprecated\x1b[22;39m' + // bold yellow ' \x1b[0m' + msg + '\x1b[39m' // reset // add stack trace if (this._traced) { for (var i = 0; i < stack.length; i++) { formatted += '\n \x1b[36mat ' + callSiteToString(stack[i]) + '\x1b[39m' // cyan } return formatted } if (caller) { formatted += ' \x1b[36m' + formatLocation(caller) + '\x1b[39m' // cyan } return formatted } /** * Format call site location. */ function formatLocation (callSite) { return relative(basePath, callSite[0]) + ':' + callSite[1] + ':' + callSite[2] } /** * Get the stack as array of call sites. */ function getStack () { var limit = Error.stackTraceLimit var obj = {} var prep = Error.prepareStackTrace Error.prepareStackTrace = prepareObjectStackTrace Error.stackTraceLimit = Math.max(10, limit) // capture the stack Error.captureStackTrace(obj) // slice this function off the top var stack = obj.stack.slice(1) Error.prepareStackTrace = prep Error.stackTraceLimit = limit return stack } /** * Capture call site stack from v8. */ function prepareObjectStackTrace (obj, stack) { return stack } /** * Return a wrapped function in a deprecation message. */ function wrapfunction (fn, message) { if (typeof fn !== 'function') { throw new TypeError('argument fn must be a function') } var args = createArgumentsString(fn.length) var deprecate = this // eslint-disable-line no-unused-vars var stack = getStack() var site = callSiteLocation(stack[1]) site.name = fn.name // eslint-disable-next-line no-eval var deprecatedfn = eval('(function (' + args + ') {\n' + '"use strict"\n' + 'log.call(deprecate, message, site)\n' + 'return fn.apply(this, arguments)\n' + '})') return deprecatedfn } /** * Wrap property in a deprecation message. */ function wrapproperty (obj, prop, message) { if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) { throw new TypeError('argument obj must be object') } var descriptor = Object.getOwnPropertyDescriptor(obj, prop) if (!descriptor) { throw new TypeError('must call property on owner object') } if (!descriptor.configurable) { throw new TypeError('property must be configurable') } var deprecate = this var stack = getStack() var site = callSiteLocation(stack[1]) // set site name site.name = prop // convert data descriptor if ('value' in descriptor) { descriptor = convertDataDescriptorToAccessor(obj, prop, message) } var get = descriptor.get var set = descriptor.set // wrap getter if (typeof get === 'function') { descriptor.get = function getter () { log.call(deprecate, message, site) return get.apply(this, arguments) } } // wrap setter if (typeof set === 'function') { descriptor.set = function setter () { log.call(deprecate, message, site) return set.apply(this, arguments) } } Object.defineProperty(obj, prop, descriptor) } /** * Create DeprecationError for deprecation */ function DeprecationError (namespace, message, stack) { var error = new Error() var stackString Object.defineProperty(error, 'constructor', { value: DeprecationError }) Object.defineProperty(error, 'message', { configurable: true, enumerable: false, value: message, writable: true }) Object.defineProperty(error, 'name', { enumerable: false, configurable: true, value: 'DeprecationError', writable: true }) Object.defineProperty(error, 'namespace', { configurable: true, enumerable: false, value: namespace, writable: true }) Object.defineProperty(error, 'stack', { configurable: true, enumerable: false, get: function () { if (stackString !== undefined) { return stackString } // prepare stack trace return (stackString = createStackString.call(this, stack)) }, set: function setter (val) { stackString = val } }) return error } nodejs-depd-1.1.1/lib/000077500000000000000000000000001313646671000144635ustar00rootroot00000000000000nodejs-depd-1.1.1/lib/browser/000077500000000000000000000000001313646671000161465ustar00rootroot00000000000000nodejs-depd-1.1.1/lib/browser/index.js000066400000000000000000000027501313646671000176170ustar00rootroot00000000000000/*! * depd * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = depd /** * Create deprecate for namespace in caller. */ function depd (namespace) { if (!namespace) { throw new TypeError('argument namespace is required') } function deprecate (message) { // no-op in browser } deprecate._file = undefined deprecate._ignored = true deprecate._namespace = namespace deprecate._traced = false deprecate._warned = Object.create(null) deprecate.function = wrapfunction deprecate.property = wrapproperty return deprecate } /** * Return a wrapped function in a deprecation message. * * This is a no-op version of the wrapper, which does nothing but call * validation. */ function wrapfunction (fn, message) { if (typeof fn !== 'function') { throw new TypeError('argument fn must be a function') } return fn } /** * Wrap property in a deprecation message. * * This is a no-op version of the wrapper, which does nothing but call * validation. */ function wrapproperty (obj, prop, message) { if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) { throw new TypeError('argument obj must be object') } var descriptor = Object.getOwnPropertyDescriptor(obj, prop) if (!descriptor) { throw new TypeError('must call property on owner object') } if (!descriptor.configurable) { throw new TypeError('property must be configurable') } } nodejs-depd-1.1.1/lib/compat/000077500000000000000000000000001313646671000157465ustar00rootroot00000000000000nodejs-depd-1.1.1/lib/compat/callsite-tostring.js000066400000000000000000000042651313646671000217620ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. */ module.exports = callSiteToString /** * Format a CallSite file location to a string. */ function callSiteFileLocation (callSite) { var fileName var fileLocation = '' if (callSite.isNative()) { fileLocation = 'native' } else if (callSite.isEval()) { fileName = callSite.getScriptNameOrSourceURL() if (!fileName) { fileLocation = callSite.getEvalOrigin() } } else { fileName = callSite.getFileName() } if (fileName) { fileLocation += fileName var lineNumber = callSite.getLineNumber() if (lineNumber != null) { fileLocation += ':' + lineNumber var columnNumber = callSite.getColumnNumber() if (columnNumber) { fileLocation += ':' + columnNumber } } } return fileLocation || 'unknown source' } /** * Format a CallSite to a string. */ function callSiteToString (callSite) { var addSuffix = true var fileLocation = callSiteFileLocation(callSite) var functionName = callSite.getFunctionName() var isConstructor = callSite.isConstructor() var isMethodCall = !(callSite.isToplevel() || isConstructor) var line = '' if (isMethodCall) { var methodName = callSite.getMethodName() var typeName = getConstructorName(callSite) if (functionName) { if (typeName && functionName.indexOf(typeName) !== 0) { line += typeName + '.' } line += functionName if (methodName && functionName.lastIndexOf('.' + methodName) !== functionName.length - methodName.length - 1) { line += ' [as ' + methodName + ']' } } else { line += typeName + '.' + (methodName || '') } } else if (isConstructor) { line += 'new ' + (functionName || '') } else if (functionName) { line += functionName } else { addSuffix = false line += fileLocation } if (addSuffix) { line += ' (' + fileLocation + ')' } return line } /** * Get constructor name of reviver. */ function getConstructorName (obj) { var receiver = obj.receiver return (receiver.constructor && receiver.constructor.name) || null } nodejs-depd-1.1.1/lib/compat/event-listener-count.js000066400000000000000000000005221313646671000223750ustar00rootroot00000000000000/*! * depd * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = eventListenerCount /** * Get the count of listeners on an event emitter of a specific type. */ function eventListenerCount (emitter, type) { return emitter.listeners(type).length } nodejs-depd-1.1.1/lib/compat/index.js000066400000000000000000000026151313646671000174170ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014-2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var EventEmitter = require('events').EventEmitter /** * Module exports. * @public */ lazyProperty(module.exports, 'callSiteToString', function callSiteToString () { var limit = Error.stackTraceLimit var obj = {} var prep = Error.prepareStackTrace function prepareObjectStackTrace (obj, stack) { return stack } Error.prepareStackTrace = prepareObjectStackTrace Error.stackTraceLimit = 2 // capture the stack Error.captureStackTrace(obj) // slice the stack var stack = obj.stack.slice() Error.prepareStackTrace = prep Error.stackTraceLimit = limit return stack[0].toString ? toString : require('./callsite-tostring') }) lazyProperty(module.exports, 'eventListenerCount', function eventListenerCount () { return EventEmitter.listenerCount || require('./event-listener-count') }) /** * Define a lazy property. */ function lazyProperty (obj, prop, getter) { function get () { var val = getter() Object.defineProperty(obj, prop, { configurable: true, enumerable: true, value: val }) return val } Object.defineProperty(obj, prop, { configurable: true, enumerable: true, get: get }) } /** * Call toString() on the obj */ function toString (obj) { return obj.toString() } nodejs-depd-1.1.1/package.json000066400000000000000000000021571313646671000162100ustar00rootroot00000000000000{ "name": "depd", "description": "Deprecate all the things", "version": "1.1.1", "author": "Douglas Christopher Wilson ", "license": "MIT", "keywords": [ "deprecate", "deprecated" ], "repository": "dougwilson/nodejs-depd", "browser": "lib/browser/index.js", "devDependencies": { "benchmark": "2.1.4", "beautify-benchmark": "0.2.4", "eslint": "3.19.0", "eslint-config-standard": "7.1.0", "eslint-plugin-markdown": "1.0.0-beta.7", "eslint-plugin-promise": "3.5.0", "eslint-plugin-standard": "2.3.1", "istanbul": "0.4.5", "mocha": "~1.21.5" }, "files": [ "lib/", "History.md", "LICENSE", "index.js", "Readme.md" ], "engines": { "node": ">= 0.6" }, "scripts": { "bench": "node benchmark/index.js", "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail test/", "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --no-exit test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot test/" } } nodejs-depd-1.1.1/test/000077500000000000000000000000001313646671000146745ustar00rootroot00000000000000nodejs-depd-1.1.1/test/.eslintrc000066400000000000000000000000441313646671000165160ustar00rootroot00000000000000{ "env": { "mocha": true } } nodejs-depd-1.1.1/test/browserify.js000066400000000000000000000131131313646671000174240ustar00rootroot00000000000000 var assert = require('assert') var browserify = tryRequire('browserify') var captureStderr = require('./support/capture-stderr') var depd = null var mylib = null var path = require('path') var run = browserify ? describe : describe.skip run('when browserified', function () { before(function (done) { var b = browserify() // require depd b.require(path.join(__dirname, '..'), { expose: 'depd' }) // require libs b.require(path.join(__dirname, 'fixtures', 'libs'), { expose: 'libs' }) b.bundle(function (err, buf) { if (err) return done(err) var require = eval(buf.toString()) // eslint-disable-line no-eval depd = require('depd') mylib = require('libs').my done() }) }) describe('depd(namespace)', function () { it('creates deprecated function', function () { assert.equal(typeof depd('test'), 'function') }) it('requires namespace', function () { assert.throws(depd.bind(null), /namespace.*required/) }) }) describe('deprecate(message)', function () { it('should not log message', function () { function callold () { mylib.old() } assert.equal(captureStderr(callold), '') }) describe('when message omitted', function () { it('should not log message', function () { function callold () { mylib.automsgnamed() } assert.equal(captureStderr(callold), '') }) }) }) describe('deprecate.function(fn, message)', function () { it('should throw when not given function', function () { var deprecate = depd('test') assert.throws(deprecate.function.bind(deprecate, 2), /fn.*function/) }) it('should not log on call to function', function () { function callold () { mylib.oldfn() } assert.equal(captureStderr(callold), '') }) it('should have same arity', function () { assert.equal(mylib.oldfn.length, 2) }) it('should pass arguments', function () { var ret function callold () { ret = mylib.oldfn(1, 2) } assert.equal(captureStderr(callold), '') assert.equal(ret, 2) }) describe('when message omitted', function () { it('should not log message', function () { function callold () { mylib.oldfnauto() } assert.equal(captureStderr(callold), '') }) }) }) describe('deprecate.property(obj, prop, message)', function () { it('should throw when given primitive', function () { var deprecate = depd('test') assert.throws(deprecate.property.bind(deprecate, 2), /obj.*object/) }) it('should throw when given missing property', function () { var deprecate = depd('test') var obj = {} assert.throws(deprecate.property.bind(deprecate, obj, 'blargh'), /property.*owner/) }) it('should throw when given non-configurable property', function () { var deprecate = depd('test') var obj = {} Object.defineProperty(obj, 'thing', {value: 'thingie'}) assert.throws(deprecate.property.bind(deprecate, obj, 'thing'), /property.*configurable/) }) it('should not log on access to property', function () { function callprop () { return mylib.propa } assert.equal(captureStderr(callprop), '') }) it('should not log on setting property', function () { var val function callprop () { val = mylib.propa } function setprop () { mylib.propa = 'newval' } assert.equal(captureStderr(setprop), '') assert.equal(captureStderr(callprop), '') assert.equal(val, 'newval') }) describe('when obj is a function', function () { it('should not log on access to property on function', function () { function callprop () { return mylib.fnprop.propa } assert.equal(captureStderr(callprop), '') }) it('should not generate message on named function', function () { function callprop () { return mylib.fnprop.propautomsg } assert.equal(captureStderr(callprop), '') }) }) describe('when value descriptor', function () { it('should not log on access and set', function () { function callold () { return mylib.propa } function setold () { mylib.propa = 'val' } assert.equal(captureStderr(callold), '') assert.equal(captureStderr(setold), '') }) it('should not log on set to non-writable', function () { function callold () { return mylib.propget } function setold () { mylib.propget = 'val' } assert.equal(captureStderr(callold), '') assert.equal(captureStderr(setold), '') }) }) describe('when accessor descriptor', function () { it('should log on access and set', function () { function callold () { return mylib.propdyn } function setold () { mylib.propdyn = 'val' } assert.equal(captureStderr(callold), '') assert.equal(captureStderr(setold), '') }) it('should not log on access when no accessor', function () { function callold () { return mylib.propsetter } assert.equal(captureStderr(callold), '') }) it('should not log on set when no setter', function () { function callold () { mylib.propgetter = 'val' } assert.equal(captureStderr(callold), '') }) }) describe('when message omitted', function () { it('should not generate message for method call on named function', function () { function callold () { return mylib.propauto } assert.equal(captureStderr(callold), '') }) }) }) }) function tryRequire (name) { try { return require(name) } catch (e) { return undefined } } nodejs-depd-1.1.1/test/fixtures/000077500000000000000000000000001313646671000165455ustar00rootroot00000000000000nodejs-depd-1.1.1/test/fixtures/libs/000077500000000000000000000000001313646671000174765ustar00rootroot00000000000000nodejs-depd-1.1.1/test/fixtures/libs/cool.js000066400000000000000000000003151313646671000207670ustar00rootroot00000000000000 var deprecate1 = require('../../..')('cool-lib') var deprecate2 = require('../../..')('neat-lib') exports.cool = function () { deprecate1('cool') } exports.neat = function () { deprecate2('neat') } nodejs-depd-1.1.1/test/fixtures/libs/index.js000066400000000000000000000012601313646671000211420ustar00rootroot00000000000000 exports.my = require('./my') exports.strict = require('./strict') lazyRequireProperty(exports, 'cool', './cool') lazyRequireProperty(exports, 'multi', './multi') lazyRequireProperty(exports, 'new', './new') lazyRequireProperty(exports, 'old', './old') lazyRequireProperty(exports, 'thing', './thing') lazyRequireProperty(exports, 'trace', './trace') function lazyRequireProperty (obj, prop, path) { function get () { var val = require(path) Object.defineProperty(obj, prop, { configurable: true, enumerable: true, value: val }) return val } Object.defineProperty(obj, prop, { configurable: true, enumerable: true, get: get }) } nodejs-depd-1.1.1/test/fixtures/libs/multi.js000066400000000000000000000003231313646671000211640ustar00rootroot00000000000000 var deprecate1 = require('../../..')('multi-lib') var deprecate2 = require('../../..')('multi-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-1.1.1/test/fixtures/libs/my.js000066400000000000000000000032551313646671000204660ustar00rootroot00000000000000 var deprecate = require('../../..')('my-lib') exports.fn = fn exports.prop = 'thingie' exports.old = function () { deprecate('old') } exports.old2 = function () { deprecate('old2') } exports.oldfn = deprecate.function(fn, 'oldfn') exports.oldfnauto = deprecate.function(fn) exports.oldfnautoanon = deprecate.function(function () {}) exports.propa = 'thingie' exports.propauto = 'thingie' Object.defineProperty(exports, 'propget', { configurable: true, value: 'thingie', writable: false }) Object.defineProperty(exports, 'propdyn', { configurable: true, get: function () { return 'thingie' }, set: function () {} }) Object.defineProperty(exports, 'propgetter', { configurable: true, get: function () { return 'thingie' } }) // eslint-disable-next-line accessor-pairs Object.defineProperty(exports, 'propsetter', { configurable: true, set: function () {} }) deprecate.property(exports, 'propa', 'propa gone') deprecate.property(exports, 'propauto') deprecate.property(exports, 'propdyn') deprecate.property(exports, 'propget') deprecate.property(exports, 'propgetter') deprecate.property(exports, 'propsetter') exports.automsg = function () { deprecate() } exports.automsgnamed = function automsgnamed () { deprecate() } exports.automsganon = function () { (function () { deprecate() }()) } exports.fnprop = function thefn () {} exports.fnprop.propa = 'thingie' exports.fnprop.propautomsg = 'thingie' deprecate.property(exports.fnprop, 'propa', 'fn propa gone') deprecate.property(exports.fnprop, 'propautomsg') exports.layerfn = function () { exports.oldfn() } exports.layerprop = function () { return exports.propa } function fn (a1, a2) { return a2 } nodejs-depd-1.1.1/test/fixtures/libs/new.js000066400000000000000000000001421313646671000206220ustar00rootroot00000000000000 var deprecate = require('../../..')('new-lib') exports.old = function () { deprecate('old') } nodejs-depd-1.1.1/test/fixtures/libs/old.js000066400000000000000000000005111313646671000206070ustar00rootroot00000000000000 var deprecate1 = require('../../..')('old-lib') var deprecate2 = require('../../..')('old-lib-other') var deprecate3 = require('../../..')('my-cool-module') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } exports.oldfunction = function () { deprecate3('oldfunction') } nodejs-depd-1.1.1/test/fixtures/libs/strict.js000066400000000000000000000017231313646671000213470ustar00rootroot00000000000000 'use strict' var deprecate = require('../../..')('strict-lib') exports.old = function () { deprecate('old') } exports.oldfn = deprecate.function(fn, 'oldfn') exports.oldfnauto = deprecate.function(fn) exports.oldfnautoanon = deprecate.function(function () {}) exports.propa = 'thingie' exports.propauto = 'thingie' deprecate.property(exports, 'propa', 'propa gone') deprecate.property(exports, 'propauto') exports.automsg = function () { deprecate() } exports.automsgnamed = function automsgnamed () { deprecate() } exports.automsganon = function () { (function () { deprecate() }()) } exports.fnprop = function thefn () {} exports.fnprop.propa = 'thingie' exports.fnprop.propautomsg = 'thingie' deprecate.property(exports.fnprop, 'propa', 'fn propa gone') deprecate.property(exports.fnprop, 'propautomsg') exports.layerfn = function () { exports.oldfn() } exports.layerprop = function () { return exports.propa } function fn (a1, a2) { return a2 } nodejs-depd-1.1.1/test/fixtures/libs/thing.js000066400000000000000000000001441313646671000211440ustar00rootroot00000000000000 var deprecate = require('../../..')('thing-lib') exports.old = function () { deprecate('old') } nodejs-depd-1.1.1/test/fixtures/libs/trace.js000066400000000000000000000003231313646671000211300ustar00rootroot00000000000000 var deprecate1 = require('../../..')('trace-lib') var deprecate2 = require('../../..')('trace-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-1.1.1/test/fixtures/script.js000066400000000000000000000001271313646671000204070ustar00rootroot00000000000000 var oldlib = require('./libs/old') run() function run () { oldlib.oldfunction() } nodejs-depd-1.1.1/test/support/000077500000000000000000000000001313646671000164105ustar00rootroot00000000000000nodejs-depd-1.1.1/test/support/buffer-concat.js000066400000000000000000000012051313646671000214620ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014-2017 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var Buffer = require('buffer').Buffer /** * Module exports. * @public */ module.exports = Buffer.concat || bufferConcat /** * Concatenate an array of Buffers. * @public */ function bufferConcat (bufs) { var i var len var length = 0 for (i = 0, len = bufs.length; i < len; i++) { length += bufs[i].length } var buf = new Buffer(length) var pos = 0 for (i = 0, len = bufs.length; i < len; i++) { bufs[i].copy(buf, pos) pos += bufs[i].length } return buf } nodejs-depd-1.1.1/test/support/capture-stderr.js000066400000000000000000000013701313646671000217130ustar00rootroot00000000000000/*! * depd * Copyright(c) 2017 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var bufferConcat = require('./buffer-concat') /** * Module exports. * @public */ module.exports = captureStderr /** * Capture STDERR output during a function invokation. * @public */ function captureStderr (fn, color) { var chunks = [] var isTTY = process.stderr.isTTY var write = process.stderr.write process.stderr.isTTY = Boolean(color) process.stderr.write = function write (chunk, encoding) { chunks.push(new Buffer(chunk, encoding)) } try { fn() } finally { process.stderr.isTTY = isTTY process.stderr.write = write } return bufferConcat(chunks).toString('utf8') } nodejs-depd-1.1.1/test/test.js000066400000000000000000000634321313646671000162210ustar00rootroot00000000000000 var assert = require('assert') var basename = require('path').basename var bufferConcat = require('./support/buffer-concat') var captureStderr = require('./support/capture-stderr') var depd = require('..') var libs = require('./fixtures/libs') var mylib = libs.my var path = require('path') var script = path.join(__dirname, 'fixtures', 'script.js') var spawn = require('child_process').spawn var strictlib = libs.strict describe('depd(namespace)', function () { it('creates deprecated function', function () { assert.equal(typeof depd('test'), 'function') }) it('requires namespace', function () { assert.throws(depd.bind(null), /namespace.*required/) }) }) describe('deprecate(message)', function () { it('should log namespace', function () { function callold () { mylib.old() } assert.ok(captureStderr(callold).indexOf('my-lib') !== -1) }) it('should log deprecation', function () { function callold () { mylib.old() } assert.ok(captureStderr(callold).indexOf('deprecate') !== -1) }) it('should log message', function () { function callold () { mylib.old() } assert.ok(captureStderr(callold).indexOf('old') !== -1) }) it('should log call site', function () { function callold () { mylib.old() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should log call site from strict lib', function () { function callold () { strictlib.old() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should log call site regardless of Error.stackTraceLimit', function () { function callold () { mylib.old() } var limit = Error.stackTraceLimit try { Error.stackTraceLimit = 1 var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) } finally { Error.stackTraceLimit = limit } }) it('should log call site within eval', function () { function callold () { eval('mylib.old()') } // eslint-disable-line no-eval var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf(':1:') !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should log call site within strict', function () { function callold () { 'use strict'; mylib.old() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should only warn once per call site', function () { function callold () { for (var i = 0; i < 5; i++) { mylib.old() // single call site process.stderr.write('invoke ' + i + '\n') } } var stderr = captureStderr(callold) assert.equal(stderr.split('deprecated').length, 2) assert.equal(stderr.split('invoke').length, 6) }) it('should warn for different fns on same call site', function () { var prop function callold () { mylib[prop]() // call from same site } prop = 'old' assert.ok(captureStderr(callold).indexOf(basename(__filename)) !== -1) prop = 'old2' assert.ok(captureStderr(callold).indexOf(basename(__filename)) !== -1) }) it('should warn for different calls on same line', function () { function callold () { mylib.old(); mylib.old() } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.equal(stderr.split('deprecated').length, 3) assert.equal(stderr.split(fileline[0]).length, 3) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold () { mylib.automsgnamed() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' automsgnamed ') !== -1) }) it('should generate message for function call on named function', function () { function callold () { var fn = mylib.automsgnamed fn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' automsgnamed ') !== -1) }) it('should generate message for method call on unnamed function', function () { function callold () { mylib.automsg() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' exports.automsg ') !== -1) }) it('should generate message for function call on unnamed function', function () { function callold () { var fn = mylib.automsg fn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' exports.automsg ') !== -1) }) it('should generate message for function call on anonymous function', function () { function callold () { mylib.automsganon() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(/ exports\.automsganon | /.test(stderr)) }) describe('in strict mode library', function () { it('should generate message for method call on named function', function () { function callold () { strictlib.automsgnamed() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' automsgnamed ') !== -1) }) it('should generate message for function call on named function', function () { function callold () { var fn = strictlib.automsgnamed fn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' automsgnamed ') !== -1) }) it('should generate message for method call on unnamed function', function () { function callold () { strictlib.automsg() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' exports.automsg ') !== -1) }) it('should generate message for function call on unnamed function', function () { function callold () { var fn = strictlib.automsg fn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' exports.automsg ') !== -1) }) it('should generate message for function call on anonymous function', function () { function callold () { strictlib.automsganon() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(/ exports\.automsganon | /.test(stderr)) }) }) }) describe('when output supports colors', function () { var stderr before(function () { function callold () { mylib.old() } stderr = captureStderr(callold, true) }) it('should log in color', function () { assert.notEqual(stderr, '') assert.ok(stderr.indexOf('\x1b[') !== -1) }) it('should log namespace', function () { assert.ok(stderr.indexOf('my-lib') !== -1) }) it('should log deprecation', function () { assert.ok(stderr.indexOf('deprecate') !== -1) }) it('should log message', function () { assert.ok(stderr.indexOf('old') !== -1) }) it('should log call site', function () { assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) }) describe('when output does not support colors', function () { var stderr before(function () { function callold () { mylib.old() } stderr = captureStderr(callold, false) }) it('should not log in color', function () { assert.notEqual(stderr, '') assert.ok(stderr.indexOf('\x1b[') === -1) }) it('should log namespace', function () { assert.ok(stderr.indexOf('my-lib') !== -1) }) it('should log timestamp', function () { assert.ok(/\w+, \d+ \w+ \d{4} \d{2}:\d{2}:\d{2} \w{3}/.test(stderr)) }) it('should log deprecation', function () { assert.ok(stderr.indexOf('deprecate') !== -1) }) it('should log message', function () { assert.ok(stderr.indexOf('old') !== -1) }) it('should log call site', function () { assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\.js:[0-9]+:[0-9]+/.test(stderr)) }) }) }) describe('deprecate.function(fn, message)', function () { it('should throw when not given function', function () { var deprecate = depd('test') assert.throws(deprecate.function.bind(deprecate, 2), /fn.*function/) }) it('should log on call to function', function () { function callold () { mylib.oldfn() } assert.ok(captureStderr(callold).indexOf(' oldfn ') !== -1) }) it('should have same arity', function () { assert.equal(mylib.oldfn.length, 2) }) it('should pass arguments', function () { var ret function callold () { ret = mylib.oldfn(1, 2) } assert.ok(captureStderr(callold).indexOf(' oldfn ') !== -1) assert.equal(ret, 2) }) it('should show call site outside scope', function () { function callold () { mylib.layerfn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(' oldfn ') !== -1) assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should show call site outside scope from strict lib', function () { function callold () { strictlib.layerfn() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(' oldfn ') !== -1) assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should only warn once per call site', function () { function callold () { for (var i = 0; i < 5; i++) { mylib.oldfn() // single call site process.stderr.write('invoke ' + i + '\n') } } var stderr = captureStderr(callold) assert.equal(stderr.split('deprecated').length, 2) assert.equal(stderr.split('invoke').length, 6) }) it('should handle rapid calling of deprecated thing', function () { this.timeout(5000) function callold () { for (var i = 0; i < 10000; i++) { mylib.oldfn() } } var stderr = captureStderr(callold) assert.equal(stderr.split('deprecated').length, 2) }) it('should warn for different calls on same line', function () { function callold () { mylib.oldfn(); mylib.oldfn() } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.equal(stderr.split('deprecated').length, 3) assert.equal(stderr.split(fileline[0]).length, 3) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold () { mylib.oldfnauto() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' fn ') !== -1) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) it('should generate message for method call on anonymous function', function () { function callold () { mylib.oldfnautoanon() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(/ /.test(stderr)) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) describe('in strict mode library', function () { it('should generate message for method call on named function', function () { function callold () { strictlib.oldfnauto() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' fn ') !== -1) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) it('should generate message for method call on anonymous function', function () { function callold () { strictlib.oldfnautoanon() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(/ /.test(stderr)) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) }) }) }) describe('deprecate.property(obj, prop, message)', function () { it('should throw when given primitive', function () { var deprecate = depd('test') assert.throws(deprecate.property.bind(deprecate, 2), /obj.*object/) }) it('should throw when given missing property', function () { var deprecate = depd('test') var obj = {} assert.throws(deprecate.property.bind(deprecate, obj, 'blargh'), /property.*owner/) }) it('should throw when given non-configurable property', function () { var deprecate = depd('test') var obj = {} Object.defineProperty(obj, 'thing', {value: 'thingie'}) assert.throws(deprecate.property.bind(deprecate, obj, 'thing'), /property.*configurable/) }) it('should log on access to property', function () { function callprop () { return mylib.propa } var stderr = captureStderr(callprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' propa gone ') !== -1) }) it('should log on setting property', function () { var val function callprop () { val = mylib.propa } function setprop () { mylib.propa = 'newval' } var stderr = captureStderr(setprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' propa gone ') !== -1) assert.ok(captureStderr(callprop).indexOf(' deprecated ') !== -1) assert.equal(val, 'newval') }) it('should only warn once per call site', function () { function callold () { for (var i = 0; i < 5; i++) { var v = mylib.propa || v // single call site process.stderr.write('access ' + i + '\n') } } var stderr = captureStderr(callold) assert.equal(stderr.split('deprecated').length, 2) assert.equal(stderr.split('access').length, 6) }) it('should warn for different accesses on same line', function () { function callold () { mylib.old(); mylib.old() } var stderr = captureStderr(callold) var fileline = stderr.match(/\.js:[0-9]+:/) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.equal(stderr.split('deprecated').length, 3) assert.equal(stderr.split(fileline[0]).length, 3) }) it('should show call site outside scope', function () { function callold () { mylib.layerprop() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(' propa ') !== -1) assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr)) }) it('should show call site outside scope from strict lib', function () { function callold () { strictlib.layerprop() } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(' propa ') !== -1) assert.ok(/test.js:[0-9]+:[0-9]+/.test(stderr)) }) describe('when obj is a function', function () { it('should log on access to property on function', function () { function callprop () { return mylib.fnprop.propa } var stderr = captureStderr(callprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' fn propa gone ') !== -1) }) it('should generate message on named function', function () { function callprop () { return mylib.fnprop.propautomsg } var stderr = captureStderr(callprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' thefn.propautomsg ') !== -1) }) describe('in strict mode library', function () { it('should log on access to property on function', function () { function callprop () { return strictlib.fnprop.propa } var stderr = captureStderr(callprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' fn propa gone ') !== -1) }) it('should generate message on named function', function () { function callprop () { return strictlib.fnprop.propautomsg } var stderr = captureStderr(callprop) assert.ok(stderr.indexOf(' deprecated ') !== -1) assert.ok(stderr.indexOf(' thefn.propautomsg ') !== -1) }) }) }) describe('when value descriptor', function () { it('should log on access and set', function () { function callold () { return mylib.propa } function setold () { mylib.propa = 'val' } assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1) assert.ok(captureStderr(setold).indexOf(' deprecated ') !== -1) }) it('should not log on set to non-writable', function () { function callold () { return mylib.propget } function setold () { mylib.propget = 'val' } assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1) assert.equal(captureStderr(setold), '') }) }) describe('when accessor descriptor', function () { it('should log on access and set', function () { function callold () { return mylib.propdyn } function setold () { mylib.propdyn = 'val' } assert.ok(captureStderr(callold).indexOf(' deprecated ') !== -1) assert.ok(captureStderr(setold).indexOf(' deprecated ') !== -1) }) it('should not log on access when no accessor', function () { function callold () { return mylib.propsetter } assert.equal(captureStderr(callold), '') }) it('should not log on set when no setter', function () { function callold () { mylib.propgetter = 'val' } assert.equal(captureStderr(callold), '') }) }) describe('when message omitted', function () { it('should generate message for method call on named function', function () { function callold () { return mylib.propauto } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' propauto ') !== -1) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) describe('in strict mode library', function () { it('should generate message for method call on named function', function () { function callold () { return strictlib.propauto } var stderr = captureStderr(callold) assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(stderr.indexOf('deprecated') !== -1) assert.ok(stderr.indexOf(' propauto ') !== -1) assert.ok(/ at [^\\/]+[^:]+test\.js:/.test(stderr)) }) }) }) }) describe('process.on(\'deprecation\', fn)', function () { var error var stderr before(function () { process.on('deprecation', ondeprecation) function callold () { mylib.old() } stderr = captureStderr(callold) }) after(function () { process.removeListener('deprecation', ondeprecation) }) function ondeprecation (err) { error = err } it('should not write when listener exists', function () { assert.equal(stderr, '') }) it('should emit error', function () { assert.ok(error) }) it('should emit DeprecationError', function () { assert.equal(error.name, 'DeprecationError') }) it('should emit error with message', function () { assert.equal(error.message, 'old') }) it('should emit error with namespace', function () { assert.equal(error.namespace, 'my-lib') }) it('should emit error with proper [[Class]]', function () { assert.equal(Object.prototype.toString.call(error), '[object Error]') }) it('should be instanceof Error', function () { assert.ok(error instanceof Error) }) it('should emit error with proper stack', function () { var stack = error.stack.split('\n') assert.equal(stack[0], 'DeprecationError: my-lib deprecated old') assert.ok(/ {4}at callold \(.+test\.js:[0-9]+:[0-9]+\)/.test(stack[1])) }) it('should have writable properties', function () { error.name = 'bname' assert.equal(error.name, 'bname') error.message = 'bmessage' assert.equal(error.message, 'bmessage') error.stack = 'bstack' assert.equal(error.stack, 'bstack') }) }) describe('process.env.NO_DEPRECATION', function () { var error function ondeprecation (err) { error = err } beforeEach(function () { error = null }) afterEach(function () { process.removeListener('deprecation', ondeprecation) }) after(function () { process.env.NO_DEPRECATION = '' }) it('should suppress given namespace', function () { process.env.NO_DEPRECATION = 'old-lib' var oldlib = libs.old assert.equal(captureStderr(oldlib.old), '') assert.notEqual(captureStderr(oldlib.old2), '') }) it('should suppress multiple namespaces', function () { process.env.NO_DEPRECATION = 'cool-lib,neat-lib' var coollib = libs.cool assert.equal(captureStderr(coollib.cool), '') assert.equal(captureStderr(coollib.neat), '') }) it('should be case-insensitive', function () { process.env.NO_DEPRECATION = 'NEW-LIB' var newlib = libs.new assert.equal(captureStderr(newlib.old), '') }) it('should emit "deprecation" events anyway', function () { process.env.NO_DEPRECATION = 'thing-lib' var thinglib = libs.thing process.on('deprecation', ondeprecation) assert.equal(captureStderr(thinglib.old), '') assert.equal(error.namespace, 'thing-lib') }) describe('when *', function () { it('should suppress any namespace', function () { process.env.NO_DEPRECATION = '*' var multilib = libs.multi assert.equal(captureStderr(multilib.old), '') assert.equal(captureStderr(multilib.old2), '') }) }) }) describe('process.env.TRACE_DEPRECATION', function () { before(function () { process.env.TRACE_DEPRECATION = 'trace-lib' }) after(function () { process.env.TRACE_DEPRECATION = '' }) it('should trace given namespace', function () { var tracelib = libs.trace function callold () { tracelib.old() } assert.ok(captureStderr(callold).indexOf(' trace-lib deprecated old\n at callold (') !== -1) }) it('should not trace non-given namespace', function () { var tracelib = libs.trace function callold () { tracelib.old2() } assert.ok(captureStderr(callold).indexOf(' trace-lib-other deprecated old2 at ') !== -1) }) describe('when output supports colors', function () { var stderr before(function () { var tracelib = libs.trace function callold () { tracelib.old() } stderr = captureStderr(callold, true) }) it('should log in color', function () { assert.notEqual(stderr, '') assert.ok(stderr.indexOf('\x1b[') !== -1) }) it('should log namespace', function () { assert.ok(stderr.indexOf('trace-lib') !== -1) }) it('should log call site in color', function () { assert.ok(stderr.indexOf(basename(__filename)) !== -1) assert.ok(/\x1b\[\d+mat callold \(/.test(stderr)) // eslint-disable-line no-control-regex }) }) }) describe('node script.js', function () { it('should display deprecation message', function (done) { captureChildStderr([script], function (err, stderr) { if (err) return done(err) var filename = path.relative(process.cwd(), script) stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__') assert.equal(stderr, '__timestamp__ my-cool-module deprecated oldfunction at ' + filename + ':7:10\n') done() }) }) }) ;(function () { // --*-deprecation switches are 0.8+ // no good way to feature detect this sync var describe = /^v0\.6\./.test(process.version) ? global.describe.skip : global.describe describe('node --no-deprecation script.js', function () { it('should suppress deprecation message', function (done) { captureChildStderr(['--no-deprecation', script], function (err, stderr) { if (err) return done(err) assert.equal(stderr, '') done() }) }) }) describe('node --trace-deprecation script.js', function () { it('should suppress deprecation message', function (done) { captureChildStderr(['--trace-deprecation', script], function (err, stderr) { if (err) return done(err) stderr = stderr.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__') assert.ok(stderr.indexOf('__timestamp__ my-cool-module deprecated oldfunction\n at run (' + script + ':7:10)\n at') === 0) done() }) }) }) }()) function captureChildStderr (args, callback) { var chunks = [] var env = {PATH: process.env.PATH} var exec = process.execPath var proc = spawn(exec, args, { env: env }) proc.stdout.resume() proc.stderr.on('data', function ondata (chunk) { chunks.push(chunk) }) proc.on('error', callback) proc.on('exit', function () { var stderr = bufferConcat(chunks).toString('utf8') callback(null, stderr) }) }