From c095b6e20c09a55d5a7f2794e9a0449ffc4325b6 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Mon, 2 Jul 2012 22:52:46 -0700 Subject: [PATCH] add a strict validation option (defaults to true) that controls whether validation errors are fatal (issue #134) --- rhino_modules/jsdoc/opts/parser.js | 4 +++- rhino_modules/jsdoc/tag.js | 12 +++++++++- test/specs/jsdoc/tag.js | 38 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/specs/jsdoc/tag.js diff --git a/rhino_modules/jsdoc/opts/parser.js b/rhino_modules/jsdoc/opts/parser.js index e71e7806..96128f47 100644 --- a/rhino_modules/jsdoc/opts/parser.js +++ b/rhino_modules/jsdoc/opts/parser.js @@ -12,7 +12,8 @@ var common = { var argParser = new common.args.ArgParser(), ourOptions, defaults = { - destination: './out/' + destination: './out/', + strict: true }; argParser.addOption('t', 'template', true, 'The name of the template to use. Default: the "default" template'); @@ -22,6 +23,7 @@ argParser.addOption('T', 'test', false, 'Run all tests and quit.'); argParser.addOption('d', 'destination', true, 'The path to the output folder. Use "console" to dump data to the console. Default: console'); argParser.addOption('p', 'private', false, 'Display symbols marked with the @private tag. Default: false.'); argParser.addOption('r', 'recurse', false, 'Recurse into subdirectories when scanning for source code files.'); +argParser.addOption('s', 'strict', false, 'Exit immediately if a doclet is incomplete or contains errors. Default: true'); argParser.addOption('h', 'help', false, 'Print this message and quit.'); argParser.addOption('X', 'explain', false, 'Dump all found doclet internals to console and quit.'); argParser.addOption('q', 'query', true, 'Provide a querystring to define custom variable names/values to add to the options hash.'); diff --git a/rhino_modules/jsdoc/tag.js b/rhino_modules/jsdoc/tag.js index 3c672aac..645b9242 100644 --- a/rhino_modules/jsdoc/tag.js +++ b/rhino_modules/jsdoc/tag.js @@ -91,7 +91,17 @@ exports.Tag = function(tagTitle, tagBody, meta) { } } - jsdoc.tag.validator.validate(this, meta); + // validate the tag. for strict validation, throw an exception; otherwise, log a warning. + try { + jsdoc.tag.validator.validate(this, meta); + } + catch (e) { + if (env.opts.strict) { + throw e; + } else { + console.log(e); + } + } } function trim(text, newlines) { diff --git a/test/specs/jsdoc/tag.js b/test/specs/jsdoc/tag.js new file mode 100644 index 00000000..e2624338 --- /dev/null +++ b/test/specs/jsdoc/tag.js @@ -0,0 +1,38 @@ +/*global describe: true, env: true, it: true */ +describe("jsdoc/tag", function() { + // TODO: more tests + + function badTag() { + var Tag = require("jsdoc/tag").Tag; + var tag = new Tag("name"); + return tag; + } + + it("has strict validation enabled by default", function() { + expect(badTag).toThrow(); + }); + + it("throws an exception for bad tags if strict validation is enabled", function() { + var strict = !!env.opts.strict; + + env.opts.strict = true; + + expect(badTag).toThrow(); + + env.opts.strict = strict; + }); + + it("doesn't throw an exception for bad tags if strict validation is disabled", function() { + /*jshint evil: true */ + var strict = !!env.opts.strict, + log = new Function(console.log); + + console.log = function() {}; + env.opts.strict = false; + + expect(badTag).not.toThrow(); + + env.opts.strict = strict; + console.log = log; + }); +}); \ No newline at end of file