Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 15, 2014
0 parents commit 9d80889
Show file tree
Hide file tree
Showing 10 changed files with 519 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
node_modules/
npm-debug.log
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
test/
.travis.yml
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.11"
matrix:
allow_failures:
- node_js: "0.11"
fast_finish: true
script: "npm run-script test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unreleased
==========

* Initial implementation
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2014 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.
124 changes: 124 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# depd

[![NPM version](https://badge.fury.io/js/depd.svg)](http://badge.fury.io/js/depd)
[![Build Status](https://travis-ci.org/dougwilson/nodejs-depd.svg?branch=master)](https://travis-ci.org/dougwilson/nodejs-depd)
[![Coverage Status](https://img.shields.io/coveralls/dougwilson/nodejs-depd.svg?branch=master)](https://coveralls.io/r/dougwilson/nodejs-depd)

Deprecate all the things

> With great modules comes great responsibility; mark things deprecated!
## Install

```sh
$ npm install depd
```

## API

```js
var depd = require('depd')
var deprecate = depd('my-module')
```

This library allows you to display deprecation messages to your users.
This library goes above and beyond with deprecation warnings by
introspecting 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.

### 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.

## Examples

This will display a deprecated message about "oldfunction" being deprecated
from "my-module" on STDERR.

```js
var deprecate = require('depd')('my-cool-module')

exports.oldfunction = function () {
// all calls to function are deprecated
deprecate('oldfunction')
}

exports.weirdfunction = function () {
if (arguments.length < 2) {
// calls with 0 or 1 args are deprectaed
deprecate('weirdfunction args < 2')
}
}
```

Then a user calls `mymodule.oldfunction()` and sees (in the given colors,
similar colors and layout to the `debug` module):

```
bright cyan bright yellow
| | grey 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"
```

## License

The MIT License (MIT)

Copyright (c) 2014 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.
178 changes: 178 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*!
* depd
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/

/**
* Module dependencies.
*/

var relative = require('path').relative
var supportsColor = require('supports-color')

/**
* Module exports.
*/

module.exports = depd

/**
* Get the path to base files on.
*/

var basePath = process.cwd()

/**
* 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._namespace = namespace
deprecate._warned = Object.create(null)

return deprecate
}

/**
* Display deprecation message.
*/

function log(message) {
var caller
var seen = false
var stack = getStack()
var site = callSiteLocation(stack[1])
var file = this._file

// get caller of deprecated thing in relation to file
for (var i = 1; i < stack.length; i++) {
caller = callSiteLocation(stack[i])

if (caller[0] === file) {
seen = true
} 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

// format and write message
var format = supportsColor && process.stderr.isTTY
? formatColor
: formatPlain
var msg = format.call(this, message, caller)
process.stderr.write(msg + '\n', 'utf8')

return
}

/**
* Get call site location as array.
*/

function callSiteLocation(callSite) {
var file = callSite.getFileName() || '<anonymous>'
var line = callSite.getLineNumber()
var colm = callSite.getColumnNumber()

if (callSite.isEval()) {
file = callSite.getEvalOrigin() + ', ' + file
}

return [file, line, colm]
}

/**
* Format deprecation message without color.
*/

function formatPlain(msg, caller) {
var timestamp = new Date().toUTCString()

var formatted = timestamp
+ ' ' + this._namespace
+ ' deprecated ' + msg

if (caller) {
formatted += ' at ' + formatLocation(caller)
}

return formatted
}

/**
* Format deprecation message with color.
*/

function formatColor(msg, caller) {
var formatted = '\x1b[36;1m' + this._namespace + '\x1b[22;39m' // bold cyan
+ ' \x1b[33;1mdeprecated\x1b[22;39m' // bold yellow
+ ' \x1b[90m' + msg + '\x1b[39m' // grey

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 obj = {}
var prep = Error.prepareStackTrace

Error.prepareStackTrace = prepareObjectStackTrace
Error.captureStackTrace(obj, getStack)

var stack = obj.stack

Error.prepareStackTrace = prep

return stack
}

/**
* Capture call site stack from v8.
*/

function prepareObjectStackTrace(obj, stack) {
return stack
}
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "depd",
"description": "Deprecate all the things",
"version": "0.0.0",
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
"license": "MIT",
"repository": "dougwilson/nodejs-depd",
"dependencies": {
"supports-color": "0.2.0"
},
"devDependencies": {
"istanbul": "0.2.10",
"mocha": "~1.20.1",
"should": "~4.0.4"
},
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "mocha --reporter spec --bail --require should test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --require should test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --require should test/"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/my-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

var deprecate = require('../..')('my-lib')

exports.old = function () {
deprecate('old')
}

0 comments on commit 9d80889

Please sign in to comment.