From a97c1e4977a60dff3731410ab48bafeeff7236f7 Mon Sep 17 00:00:00 2001 From: Jack Anderson Date: Sun, 5 Apr 2015 16:57:55 -0400 Subject: [PATCH] make console method configurable for deprecated annotation --- API.md | 8 ++++--- examples/deprecated/output.js | 6 ++--- lib/annotations/deprecated/index.js | 4 +++- .../deprecated/transformer.tmpl.js | 6 ++--- test/DeprecatedTest.js | 22 ++++++++++++++----- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/API.md b/API.md index a0a5732..6374c50 100644 --- a/API.md +++ b/API.md @@ -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. diff --git a/examples/deprecated/output.js b/examples/deprecated/output.js index 0eb5588..305567d 100644 --- a/examples/deprecated/output.js +++ b/examples/deprecated/output.js @@ -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; diff --git a/lib/annotations/deprecated/index.js b/lib/annotations/deprecated/index.js index 99a9dce..e53d118 100644 --- a/lib/annotations/deprecated/index.js +++ b/lib/annotations/deprecated/index.js @@ -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', diff --git a/lib/annotations/deprecated/transformer.tmpl.js b/lib/annotations/deprecated/transformer.tmpl.js index 67b9946..83b71b3 100644 --- a/lib/annotations/deprecated/transformer.tmpl.js +++ b/lib/annotations/deprecated/transformer.tmpl.js @@ -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__; } diff --git a/test/DeprecatedTest.js b/test/DeprecatedTest.js index afe85f4..08f2223 100644 --- a/test/DeprecatedTest.js +++ b/test/DeprecatedTest.js @@ -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() {