From 9d843fb803487a1113c77f2744bf6ad3da65d076 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sun, 26 Sep 2021 10:16:09 -0700 Subject: [PATCH] refactor(jsdoc): remove unused code for multiple parsers This code was used to support both a Mozilla Rhino-based parser and a native ES-based parser. We now have only one parser. --- packages/jsdoc/cli.js | 2 +- packages/jsdoc/lib/jsdoc/src/parser.js | 29 +++------------- packages/jsdoc/test/specs/jsdoc/src/parser.js | 33 +++---------------- 3 files changed, 11 insertions(+), 53 deletions(-) diff --git a/packages/jsdoc/cli.js b/packages/jsdoc/cli.js index 0d23d94b..a790a0cd 100644 --- a/packages/jsdoc/cli.js +++ b/packages/jsdoc/cli.js @@ -287,7 +287,7 @@ module.exports = (() => { const parser = require('jsdoc/src/parser'); const plugins = require('jsdoc/plugins'); - props.parser = parser.createParser(env.conf.parser, env.conf); + props.parser = parser.createParser(env.conf); if (env.conf.plugins) { plugins.installPlugins(env.conf.plugins, props.parser); diff --git a/packages/jsdoc/lib/jsdoc/src/parser.js b/packages/jsdoc/lib/jsdoc/src/parser.js index aeb04ed3..c13a5445 100644 --- a/packages/jsdoc/lib/jsdoc/src/parser.js +++ b/packages/jsdoc/lib/jsdoc/src/parser.js @@ -12,10 +12,6 @@ const { Walker } = require('jsdoc/src/walker'); const hasOwnProp = Object.prototype.hasOwnProperty; -// TODO: docs -const PARSERS = (exports.PARSERS = { - js: 'jsdoc/src/parser', -}); /* eslint-disable no-script-url */ // Prefix for JavaScript strings that were provided in lieu of a filename. const SCHEMA = 'javascript:'; @@ -44,26 +40,6 @@ class DocletCache { } } -// TODO: docs -exports.createParser = (type, conf) => { - let modulePath; - - if (!type) { - /* istanbul ignore next */ - type = 'js'; - } - - if (hasOwnProp.call(PARSERS, type)) { - modulePath = PARSERS[type]; - } else { - log.fatal(`The parser type "${type}" is not recognized.`); - - return null; - } - - return new (require(modulePath).Parser)(conf); -}; - // TODO: docs function pretreat(code) { return ( @@ -649,6 +625,11 @@ class Parser extends EventEmitter { } exports.Parser = Parser; +// TODO: docs +exports.createParser = (config) => { + return new Parser(config); +}; + // TODO: document other events /** * Fired once for each JSDoc comment in the current source code. diff --git a/packages/jsdoc/test/specs/jsdoc/src/parser.js b/packages/jsdoc/test/specs/jsdoc/src/parser.js index aa9d8739..5ad27196 100644 --- a/packages/jsdoc/test/specs/jsdoc/src/parser.js +++ b/packages/jsdoc/test/specs/jsdoc/src/parser.js @@ -2,6 +2,7 @@ describe('jsdoc/src/parser', () => { const _ = require('lodash'); const { attachTo } = require('jsdoc/src/handlers'); + const env = require('jsdoc/env'); const fs = require('fs'); const jsdocParser = require('jsdoc/src/parser'); const path = require('path'); @@ -21,33 +22,17 @@ describe('jsdoc/src/parser', () => { }); describe('createParser', () => { - it('should return a Parser when called without arguments', () => { - expect(jsdocParser.createParser()).toBeObject(); - }); - - it('should create a jsdoc/src/parser.Parser instance with the argument "js"', () => { - const parser = jsdocParser.createParser('js'); - - expect(parser instanceof jsdocParser.Parser).toBeTrue(); - }); - - it('should log a fatal error on bad input', () => { - function createParser() { - jsdocParser.createParser('not-a-real-parser-ever'); - } - - expect(jsdoc.didLog(createParser, 'fatal')).toBeTrue(); + it('should return a Parser when called with a config', () => { + expect(jsdocParser.createParser(env.conf)).toBeObject(); }); }); describe('Parser', () => { let parser; - function newParser() { + beforeEach(() => { parser = new jsdocParser.Parser(); - } - - newParser(); + }); it('should have a "visitor" property', () => { expect(parser.visitor).toBeObject(); @@ -90,8 +75,6 @@ describe('jsdoc/src/parser', () => { }); describe('parse', () => { - beforeEach(newParser); - it('should fire "parseBegin" events before it parses any files', () => { const spy = jasmine.createSpy(); const sourceFiles = ['javascript:/** @name foo */']; @@ -287,8 +270,6 @@ describe('jsdoc/src/parser', () => { }); describe('results', () => { - beforeEach(newParser); - it('returns an empty array before files are parsed', () => { const results = parser.results(); @@ -374,8 +355,6 @@ describe('jsdoc/src/parser', () => { let visitors; - beforeEach(newParser); - it('should work with a single node visitor', () => { parser.addAstNodeVisitor(visitorA); @@ -395,8 +374,6 @@ describe('jsdoc/src/parser', () => { }); describe('getAstNodeVisitors', () => { - beforeEach(newParser); - it('should return an empty array by default', () => { const visitors = parser.getAstNodeVisitors();