diff --git a/gulpfile.js b/gulpfile.js index f7f1eb54..7738a6c2 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -73,10 +73,10 @@ gulp.task('test-rhino', function(cb) { exec(cmd, execCb.bind(null, cb)); }); -gulp.task('test-rhino-esprima', function(cb) { - var cmd = util.format('"%s" -T -q "parser=esprima"', options.rhinoBin); +gulp.task('test-rhino-jsparser', function(cb) { + var cmd = util.format('"%s" -T -q "parser=js"', options.rhinoBin); exec(cmd, execCb.bind(null, cb)); }); -gulp.task('test', ['test-node', 'test-rhino', 'test-rhino-esprima']); +gulp.task('test', ['test-node', 'test-rhino', 'test-rhino-jsparser']); gulp.task('default', ['lint', 'test']); diff --git a/lib/jsdoc/src/parser.js b/lib/jsdoc/src/parser.js index 5891e7b2..bcea2cb8 100644 --- a/lib/jsdoc/src/parser.js +++ b/lib/jsdoc/src/parser.js @@ -27,7 +27,7 @@ var Syntax = jsdoc.src.syntax.Syntax; // TODO: docs var PARSERS = exports.PARSERS = { - esprima: 'jsdoc/src/parser', + js: 'jsdoc/src/parser', rhino: 'rhino/jsdoc/src/parser' }; /*eslint-disable no-script-url */ @@ -40,22 +40,19 @@ exports.createParser = function(type) { var modulePath; if (!type) { - type = jsdoc.util.runtime.isRhino() ? 'rhino' : 'esprima'; + /* istanbul ignore next */ + type = jsdoc.util.runtime.isRhino() ? 'rhino' : 'js'; } - if (PARSERS[type]) { + if (hasOwnProp.call(PARSERS, type)) { modulePath = PARSERS[type]; } else { - modulePath = path.join( path.getResourcePath(path.dirname(type)), path.basename(type) ); + logger.fatal('The parser type "%s" is not recognized.', type); + return null; } - try { - return new ( require(modulePath) ).Parser(); - } - catch (e) { - logger.fatal('Unable to create the parser type "' + type + '": ' + e); - } + return new ( require(modulePath) ).Parser(); }; // TODO: docs diff --git a/test/jasmine-jsdoc.js b/test/jasmine-jsdoc.js index ebab00a8..33f46a75 100644 --- a/test/jasmine-jsdoc.js +++ b/test/jasmine-jsdoc.js @@ -40,9 +40,9 @@ jasmine.getParseResults = function() { return parseResults; }; -// use the requested parser, or default to Esprima (on Node.js) or Rhino (on Rhino) +// use the requested parser, or default to the pure JS parser (on Node.js) or Rhino (on Rhino) jasmine.jsParser = (function() { - var parser = jsdoc.util.runtime.isRhino() ? 'rhino' : 'esprima'; + var parser = jsdoc.util.runtime.isRhino() ? 'rhino' : 'js'; if (jsdoc.env.opts.query && jsdoc.env.opts.query.parser) { parser = jsdoc.env.opts.query.parser; diff --git a/test/specs/jsdoc/src/parser.js b/test/specs/jsdoc/src/parser.js index 42c13ac4..913312c6 100644 --- a/test/specs/jsdoc/src/parser.js +++ b/test/specs/jsdoc/src/parser.js @@ -8,6 +8,10 @@ describe('jsdoc/src/parser', function() { src: { handlers: require('jsdoc/src/handlers'), parser: require('jsdoc/src/parser') + }, + util: { + logger: require('jsdoc/util/logger'), + runtime: require('jsdoc/util/runtime') } }; var path = require('jsdoc/path'); @@ -17,11 +21,45 @@ describe('jsdoc/src/parser', function() { expect(typeof jsdoc.src.parser).toBe('object'); }); + it('should export a "createParser" method', function() { + expect(typeof jsdoc.src.parser.createParser).toBe('function'); + }); + it('should export a "Parser" constructor', function() { - expect(jsdoc.src.parser.Parser).toBeDefined(); expect(typeof jsdoc.src.parser.Parser).toBe('function'); }); + describe('createParser', function() { + it('should return a Parser when called without arguments', function() { + // we don't check instanceof because we get different objects on Node.js and Rhino + expect(typeof jsdoc.src.parser.createParser()).toBe('object'); + }); + + it('should create a jsdoc/src/parser.Parser instance with the argument "js"', function() { + var parser = jsdoc.src.parser.createParser('js'); + + expect(parser instanceof jsdoc.src.parser.Parser).toBe(true); + }); + + if (jsdoc.util.runtime.isRhino()) { + it('should create a Rhino parser with the argument "rhino"', function() { + var RhinoParser = require('rhino/jsdoc/src/parser').Parser; + var parser = jsdoc.src.parser.createParser('rhino'); + + expect(parser instanceof RhinoParser).toBe(true); + }); + } + + it('should log a fatal error on bad input', function() { + var parser; + + spyOn(jsdoc.util.logger, 'fatal'); + parser = jsdoc.src.parser.createParser('not-a-real-parser-ever'); + + expect(jsdoc.util.logger.fatal).toHaveBeenCalled(); + }); + }); + describe('Parser', function() { var parser;