pax_global_header00006660000000000000000000000064133646461460014527gustar00rootroot0000000000000052 comment=6d59c85d093092e65ec77033576417d743079fa0 nodejs-depd-2.0.0/000077500000000000000000000000001336464614600137225ustar00rootroot00000000000000nodejs-depd-2.0.0/.eslintignore000066400000000000000000000000261336464614600164230ustar00rootroot00000000000000coverage node_modules nodejs-depd-2.0.0/.eslintrc.yml000066400000000000000000000000771336464614600163520ustar00rootroot00000000000000root: true extends: standard rules: no-param-reassign: error nodejs-depd-2.0.0/.gitignore000066400000000000000000000000701336464614600157070ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log package-lock.json nodejs-depd-2.0.0/.travis.yml000066400000000000000000000043141336464614600160350ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.9" - "5.12" - "6.14" - "7.10" - "8.12" - "9.11" - "10.12" sudo: false cache: directories: - node_modules before_install: # Configure npm - | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Remove all non-test dependencies - | # Remove benchmark dependencies npm rm --silent --save-dev benchmark beautify-benchmark # Setup Node.js version-specific dependencies - | # mocha for testing # - use 2.x for Node.js < 0.10 # - use 3.x for Node.js < 6 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then npm install --save-dev mocha@2.5.3 elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then npm install --save-dev mocha@3.5.3 fi - | # istanbul for coverage # - remove for Node.js < 0.10 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then npm rm --silent --save-dev istanbul fi - | # browserify for browser testing # - install for Node.js > 0.8 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -gt 0 || "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -gt 8 ]]; then npm install --silent --save-dev browserify fi - | # eslint for linting # - remove on Node.js < 6 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ xargs npm rm --save-dev fi # Update Node.js modules - | # Prune and rebuild node_modules if [[ -d node_modules ]]; then npm prune npm rebuild fi script: # Run test script - | if npm -ps ls istanbul | grep -q istanbul; then npm run test-ci else npm test fi # Run linting - | if npm -ps ls eslint | grep -q eslint; then npm run lint fi after_script: - | # Upload coverage to coveralls if [[ -f ./coverage/lcov.info ]]; then npm install --save-dev coveralls@2.10.0 coveralls < ./coverage/lcov.info fi nodejs-depd-2.0.0/History.md000066400000000000000000000043201336464614600157040ustar00rootroot000000000000002.0.0 / 2018-10-26 ================== * Drop support for Node.js 0.6 * Replace internal `eval` usage with `Function` constructor * Use instance methods on `process` to check for listeners 1.1.2 / 2018-01-11 ================== * perf: remove argument reassignment * Support Node.js 0.6 to 9.x 1.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-2.0.0/LICENSE000066400000000000000000000021061336464614600147260ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014-2018 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-2.0.0/Readme.md000066400000000000000000000234041336464614600154440ustar00rootroot00000000000000# 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] 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) [appveyor-image]: https://badgen.net/appveyor/ci/dougwilson/nodejs-depd/master?label=windows [appveyor-url]: https://ci.appveyor.com/project/dougwilson/nodejs-depd [coveralls-image]: https://badgen.net/coveralls/c/github/dougwilson/nodejs-depd/master [coveralls-url]: https://coveralls.io/r/dougwilson/nodejs-depd?branch=master [node-image]: https://badgen.net/npm/node/depd [node-url]: https://nodejs.org/en/download/ [npm-downloads-image]: https://badgen.net/npm/dm/depd [npm-url]: https://npmjs.org/package/depd [npm-version-image]: https://badgen.net/npm/v/depd [travis-image]: https://badgen.net/travis/dougwilson/nodejs-depd/master?label=linux [travis-url]: https://travis-ci.org/dougwilson/nodejs-depd nodejs-depd-2.0.0/appveyor.yml000066400000000000000000000041511336464614600163130ustar00rootroot00000000000000environment: 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.9" - nodejs_version: "5.12" - nodejs_version: "6.14" - nodejs_version: "7.10" - nodejs_version: "8.12" - nodejs_version: "9.11" - nodejs_version: "10.12" cache: - node_modules install: # Install Node.js - ps: >- try { Install-Product node $env:nodejs_version -ErrorAction Stop } catch { Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) } # Configure npm - ps: | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Skip SSL validation on Node.js < 0.10 if ([int]$env:nodejs_version.split(".")[0] -eq 0 -and [int]$env:nodejs_version.split(".")[1] -lt 10) { npm config set strict-ssl false } # Remove all non-test dependencies - ps: | # Remove benchmark dependencies npm rm --silent --save-dev benchmark beautify-benchmark # Remove coverage dependency npm rm --silent --save-dev istanbul # Remove lint dependencies cmd.exe /c "node -pe `"Object.keys(require('./package').devDependencies).join('\n')`"" | ` sls "^eslint(-|$)" | ` %{ npm rm --silent --save-dev $_ } # Setup Node.js version-specific dependencies - ps: | # mocha for testing # - use 2.x for Node.js < 0.10 # - use 3.x for Node.js < 6 if ([int]$env:nodejs_version.split(".")[0] -eq 0 -and [int]$env:nodejs_version.split(".")[1] -lt 10) { npm install --silent --save-dev mocha@2.5.3 } elseif ([int]$env:nodejs_version.split(".")[0] -lt 6) { npm install --silent --save-dev mocha@3.5.3 } # Update Node.js modules - ps: | # Prune & rebuild node_modules if (Test-Path -Path node_modules) { npm prune npm rebuild } # Install Node.js modules - npm install build: off test_script: # Output version data - ps: | node --version npm --version # Run test script - npm test version: "{build}" nodejs-depd-2.0.0/benchmark/000077500000000000000000000000001336464614600156545ustar00rootroot00000000000000nodejs-depd-2.0.0/benchmark/index.js000066400000000000000000000012201336464614600173140ustar00rootroot00000000000000var 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-2.0.0/benchmark/wrapfunction.js000066400000000000000000000012531336464614600207320ustar00rootroot00000000000000 /** * 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-2.0.0/benchmark/wrapproperty.js000066400000000000000000000011711336464614600207700ustar00rootroot00000000000000 /** * 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-2.0.0/files/000077500000000000000000000000001336464614600150245ustar00rootroot00000000000000nodejs-depd-2.0.0/files/message.png000066400000000000000000000054511336464614600171630ustar00rootroot00000000000000PNG  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-2.0.0/index.js000066400000000000000000000252641336464614600154000ustar00rootroot00000000000000/*! * depd * Copyright(c) 2014-2018 Douglas Christopher Wilson * MIT Licensed */ /** * Module dependencies. */ 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 vals = str.split(/[ ,]+/) var ns = String(namespace).toLowerCase() for (var i = 0; i < vals.length; i++) { var val = vals[i] // namespace contained if (val && (val === '*' || val.toLowerCase() === ns)) { 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 ' + stack[i].toString() } 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 event emitter has listeners of a given type. * * The way to do this check is done three different ways in Node.js >= 0.8 * so this consolidates them into a minimal set using instance methods. * * @param {EventEmitter} emitter * @param {string} type * @returns {boolean} * @private */ function eehaslisteners (emitter, type) { var count = typeof emitter.listenerCount !== 'function' ? emitter.listeners(type).length : emitter.listenerCount(type) return count > 0 } /** * Determine if namespace is ignored. */ function isignored (namespace) { 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) { 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 = eehaslisteners(process, 'deprecation') // abort early if no destination if (!haslisteners && this._ignored) { return } var caller var callFile var callSite var depSite var i = 0 var seen = false var stack = getStack() var file = this._file if (site) { // provided site depSite = site callSite = callSiteLocation(stack[1]) callSite.name = depSite.name file = callSite[0] } else { // get call site i = 2 depSite = callSiteLocation(stack[i]) callSite = depSite } // 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 ? depSite.join(':') + '__' + caller.join(':') : undefined if (key !== undefined && key in this._warned) { // already warned return } this._warned[key] = true // generate automatic message from call site var msg = message if (!msg) { msg = callSite === depSite || !callSite.name ? defaultMessage(depSite) : defaultMessage(callSite) } // emit deprecation if listeners exist if (haslisteners) { var err = DeprecationError(this._namespace, msg, stack.slice(i)) process.emit('deprecation', err) return } // format and write message var format = process.stderr.isTTY ? formatColor : formatPlain var output = format.call(this, msg, caller, stack.slice(i)) process.stderr.write(output + '\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 ' + stack[i].toString() } 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 ' + stack[i].toString() + '\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 stack = getStack() var site = callSiteLocation(stack[1]) site.name = fn.name // eslint-disable-next-line no-new-func var deprecatedfn = new Function('fn', 'log', 'deprecate', 'message', 'site', '"use strict"\n' + 'return function (' + args + ') {' + 'log.call(deprecate, message, site)\n' + 'return fn.apply(this, arguments)\n' + '}')(fn, log, this, message, site) 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-2.0.0/lib/000077500000000000000000000000001336464614600144705ustar00rootroot00000000000000nodejs-depd-2.0.0/lib/browser/000077500000000000000000000000001336464614600161535ustar00rootroot00000000000000nodejs-depd-2.0.0/lib/browser/index.js000066400000000000000000000027501336464614600176240ustar00rootroot00000000000000/*! * 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-2.0.0/package.json000066400000000000000000000024671336464614600162210ustar00rootroot00000000000000{ "name": "depd", "description": "Deprecate all the things", "version": "2.0.0", "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": "5.7.0", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.14.0", "eslint-plugin-markdown": "1.0.0-beta.7", "eslint-plugin-node": "7.0.1", "eslint-plugin-promise": "4.0.1", "eslint-plugin-standard": "4.0.0", "istanbul": "0.4.5", "mocha": "5.2.0", "safe-buffer": "5.1.2", "uid-safe": "2.1.5" }, "files": [ "lib/", "History.md", "LICENSE", "index.js", "Readme.md" ], "engines": { "node": ">= 0.8" }, "scripts": { "bench": "node benchmark/index.js", "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail test/", "test-ci": "istanbul cover --print=none node_modules/mocha/bin/_mocha -- --reporter spec test/ && istanbul report lcovonly text-summary", "test-cov": "istanbul cover --print=none node_modules/mocha/bin/_mocha -- --reporter dot test/ && istanbul report lcov text-summary" } } nodejs-depd-2.0.0/test/000077500000000000000000000000001336464614600147015ustar00rootroot00000000000000nodejs-depd-2.0.0/test/.eslintrc.yml000066400000000000000000000000231336464614600173200ustar00rootroot00000000000000env: mocha: true nodejs-depd-2.0.0/test/browserify.js000066400000000000000000000133271336464614600174400ustar00rootroot00000000000000 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.strictEqual(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.strictEqual(captureStderr(callold), '') }) describe('when message omitted', function () { it('should not log message', function () { function callold () { mylib.automsgnamed() } assert.strictEqual(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.strictEqual(captureStderr(callold), '') }) it('should have same arity', function () { assert.strictEqual(mylib.oldfn.length, 2) }) it('should pass arguments', function () { var ret function callold () { ret = mylib.oldfn(1, 2) } assert.strictEqual(captureStderr(callold), '') assert.strictEqual(ret, 2) }) describe('when message omitted', function () { it('should not log message', function () { function callold () { mylib.oldfnauto() } assert.strictEqual(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.strictEqual(captureStderr(callprop), '') }) it('should not log on setting property', function () { var val function callprop () { val = mylib.propa } function setprop () { mylib.propa = 'newval' } assert.strictEqual(captureStderr(setprop), '') assert.strictEqual(captureStderr(callprop), '') assert.strictEqual(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.strictEqual(captureStderr(callprop), '') }) it('should not generate message on named function', function () { function callprop () { return mylib.fnprop.propautomsg } assert.strictEqual(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.strictEqual(captureStderr(callold), '') assert.strictEqual(captureStderr(setold), '') }) it('should not log on set to non-writable', function () { function callold () { return mylib.propget } function setold () { mylib.propget = 'val' } assert.strictEqual(captureStderr(callold), '') assert.strictEqual(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.strictEqual(captureStderr(callold), '') assert.strictEqual(captureStderr(setold), '') }) it('should not log on access when no accessor', function () { function callold () { return mylib.propsetter } assert.strictEqual(captureStderr(callold), '') }) it('should not log on set when no setter', function () { function callold () { mylib.propgetter = 'val' } assert.strictEqual(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.strictEqual(captureStderr(callold), '') }) }) }) }) function tryRequire (name) { try { return require(name) } catch (e) { return undefined } } nodejs-depd-2.0.0/test/fixtures/000077500000000000000000000000001336464614600165525ustar00rootroot00000000000000nodejs-depd-2.0.0/test/fixtures/libs/000077500000000000000000000000001336464614600175035ustar00rootroot00000000000000nodejs-depd-2.0.0/test/fixtures/libs/cool.js000066400000000000000000000003151336464614600207740ustar00rootroot00000000000000 var deprecate1 = require('../../..')('cool-lib') var deprecate2 = require('../../..')('neat-lib') exports.cool = function () { deprecate1('cool') } exports.neat = function () { deprecate2('neat') } nodejs-depd-2.0.0/test/fixtures/libs/index.js000066400000000000000000000012601336464614600211470ustar00rootroot00000000000000 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-2.0.0/test/fixtures/libs/multi.js000066400000000000000000000003231336464614600211710ustar00rootroot00000000000000 var deprecate1 = require('../../..')('multi-lib') var deprecate2 = require('../../..')('multi-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-2.0.0/test/fixtures/libs/my.js000066400000000000000000000032551336464614600204730ustar00rootroot00000000000000 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-2.0.0/test/fixtures/libs/new.js000066400000000000000000000001421336464614600206270ustar00rootroot00000000000000 var deprecate = require('../../..')('New-Lib') exports.old = function () { deprecate('old') } nodejs-depd-2.0.0/test/fixtures/libs/old.js000066400000000000000000000005111336464614600206140ustar00rootroot00000000000000 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-2.0.0/test/fixtures/libs/strict.js000066400000000000000000000017231336464614600213540ustar00rootroot00000000000000 '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-2.0.0/test/fixtures/libs/thing.js000066400000000000000000000001441336464614600211510ustar00rootroot00000000000000 var deprecate = require('../../..')('thing-lib') exports.old = function () { deprecate('old') } nodejs-depd-2.0.0/test/fixtures/libs/trace.js000066400000000000000000000003231336464614600211350ustar00rootroot00000000000000 var deprecate1 = require('../../..')('trace-lib') var deprecate2 = require('../../..')('trace-lib-other') exports.old = function () { deprecate1('old') } exports.old2 = function () { deprecate2('old2') } nodejs-depd-2.0.0/test/fixtures/script.js000066400000000000000000000001271336464614600204140ustar00rootroot00000000000000 var oldlib = require('./libs/old') run() function run () { oldlib.oldfunction() } nodejs-depd-2.0.0/test/support/000077500000000000000000000000001336464614600164155ustar00rootroot00000000000000nodejs-depd-2.0.0/test/support/capture-stderr.js000066400000000000000000000013671336464614600217260ustar00rootroot00000000000000/*! * depd * Copyright(c) 2017 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var Buffer = require('safe-buffer').Buffer /** * 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(Buffer.from(chunk, encoding)) } try { fn() } finally { process.stderr.isTTY = isTTY process.stderr.write = write } return Buffer.concat(chunks).toString('utf8') } nodejs-depd-2.0.0/test/test.js000066400000000000000000000642551336464614600162320ustar00rootroot00000000000000 var assert = require('assert') var basename = require('path').basename 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 var uid = require('uid-safe').sync describe('depd(namespace)', function () { it('creates deprecated function', function () { assert.strictEqual(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.strictEqual(stderr.split('deprecated').length, 2) assert.strictEqual(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.strictEqual(stderr.split('deprecated').length, 3) assert.strictEqual(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.notStrictEqual(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.notStrictEqual(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.strictEqual(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.strictEqual(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.strictEqual(stderr.split('deprecated').length, 2) assert.strictEqual(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.strictEqual(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.strictEqual(stderr.split('deprecated').length, 3) assert.strictEqual(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.strictEqual(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.strictEqual(stderr.split('deprecated').length, 2) assert.strictEqual(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.strictEqual(stderr.split('deprecated').length, 3) assert.strictEqual(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.strictEqual(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.strictEqual(captureStderr(callold), '') }) it('should not log on set when no setter', function () { function callold () { mylib.propgetter = 'val' } assert.strictEqual(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.strictEqual(stderr, '') }) it('should emit error', function () { assert.ok(error) }) it('should emit DeprecationError', function () { assert.strictEqual(error.name, 'DeprecationError') }) it('should emit error with message', function () { assert.strictEqual(error.message, 'old') }) it('should emit error with namespace', function () { assert.strictEqual(error.namespace, 'my-lib') }) it('should emit error with proper [[Class]]', function () { assert.strictEqual(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.strictEqual(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.strictEqual(error.name, 'bname') error.message = 'bmessage' assert.strictEqual(error.message, 'bmessage') error.stack = 'bstack' assert.strictEqual(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.strictEqual(captureStderr(oldlib.old), '') assert.notStrictEqual(captureStderr(oldlib.old2), '') }) it('should suppress multiple namespaces', function () { process.env.NO_DEPRECATION = 'cool-lib,neat-lib' var coollib = libs.cool assert.strictEqual(captureStderr(coollib.cool), '') assert.strictEqual(captureStderr(coollib.neat), '') }) it('should be case-insensitive', function () { process.env.NO_DEPRECATION = 'NEW-LIB' var newlib = libs.new assert.strictEqual(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.strictEqual(captureStderr(thinglib.old), '') assert.strictEqual(error.namespace, 'thing-lib') }) describe('when *', function () { it('should suppress any namespace', function () { process.env.NO_DEPRECATION = '*' var multilib = libs.multi assert.strictEqual(captureStderr(multilib.old), '') assert.strictEqual(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.notStrictEqual(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) assert.strictEqual(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(script, ['--no-deprecation'], function (err, stderr) { if (err) return done(err) assert.strictEqual(stderr, '') done() }) }) }) describe('node --trace-deprecation script.js', function () { it('should suppress deprecation message', function (done) { captureChildStderr(script, ['--trace-deprecation'], function (err, stderr) { if (err) return done(err) assert.ok(stderr.indexOf('__timestamp__ my-cool-module deprecated oldfunction\n at run (' + script + ':7:10)\n at') === 0) done() }) }) }) }()) function captureChildStderr (script, opts, callback) { var chunks = [] var env = { PATH: process.env.PATH } var exec = process.execPath var args = process.env.running_under_istanbul ? opts.concat(path.join(__dirname, '..', 'node_modules', 'istanbul', 'lib', 'cli.js'), 'cover', '--dir=./coverage/child-' + uid(8), '--print=none', script) : opts.concat(script) 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 = Buffer.concat(chunks) .toString('utf8') .replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '__timestamp__') callback(null, stderr) }) }