Skip to content

Commit

Permalink
make console method configurable for deprecated annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Anderson committed Apr 5, 2015
1 parent e0786ea commit a97c1e4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
8 changes: 5 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ The options for each annotation can be found below.

#### `@deprecated`

The `@deprecated` annotation does not currently accept options.
| option | type | default | effect
|:------ |:---- |:------- |:-------
| `logger` | `String` | `trace` | Specifies which `console` method to call when deprecated method is called.

#### `@constructor`

| option | type | default | effect
|:------ |:---- |:------- |:-------
| `force` | `Boolean` | `false` | Call `console.warn` (`true`) or throw a `SyntaxError` (`false`) when constructor is called without `new` keyword
| `force` | `Boolean` | `false` | Call `console.warn` (`true`) or throw a `SyntaxError` (`false`) when constructor is called without `new` keyword.

#### `@param`

| option | type | default | effect
|:------ |:---- |:------- |:-------
| `force` | `Boolean` | `true` | Call `console.warn` (`true`) or throw a `TypeError` (`false`) when function argument does not match expected parameter type
| `force` | `Boolean` | `true` | Call `console.warn` (`true`) or throw a `TypeError` (`false`) when function argument does not match expected parameter type.



Expand Down
6 changes: 2 additions & 4 deletions examples/deprecated/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
* @deprecated addTwoNumbers will be removed in 2.0.0. Please use addArbitraryNumbers
*/
var addTwoNumbers = function addTwoNumbers(a, b) {
if (typeof console.trace === 'function') {
console.trace("addTwoNumbers will be removed in 2.0.0. Please use addArbitraryNumbers");
} else if (typeof console.warn === 'function') {
console.warn("addTwoNumbers will be removed in 2.0.0. Please use addArbitraryNumbers");
if (typeof console["trace"] === 'function') {
console["trace"]("addTwoNumbers will be removed in 2.0.0. Please use addArbitraryNumbers");
}
{
var result = a + b;
Expand Down
4 changes: 3 additions & 1 deletion lib/annotations/deprecated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var _ = require('lodash');
* @deprecated Specific message about deprecation and/or recourse
*/
module.exports = function(options) {
var defaultOptions = {};
var defaultOptions = {
logger: 'trace'
};

return new Annotation({
name: 'deprecated',
Expand Down
6 changes: 2 additions & 4 deletions lib/annotations/deprecated/transformer.tmpl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
function __original_id__(__original_parameters__) {
if (typeof console.trace === 'function') {
console.trace(__annotation_value__);
} else if (typeof console.warn === 'function') {
console.warn(__annotation_value__);
if (typeof console[__options_logger__] === 'function') {
console[__options_logger__](__annotation_value__);
}
__original_body__;
}
22 changes: 17 additions & 5 deletions test/DeprecatedTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@ describe('Deprecated Annotation', function() {
}
});
});
it('falls back to console.warn when console.trace isn\'t found', function() {
_trace = console.trace;
console.trace = null;
});
describe('when options.logger === \'info\'', function() {
var dest;
var module;
before(function() {
dest = utils.generateDestination();
module = utils.createModule('deprecated', type, dest, {
deprecated: {
logger: 'info'
}
});
});
after(function() {
fs.unlink(dest);
});
it('calls console.info instead of the default console.trace', function() {
utils.assertConsoleCalls({
method: 'warn',
method: 'info',
callCount: 1,
fn: function() {
module.beep();
}
});
console.trace = _trace;
});
});
describe('when disabled by options', function() {
Expand Down

0 comments on commit a97c1e4

Please sign in to comment.