refactor: use env instead of deps

This commit is contained in:
Jeff Williams 2024-12-07 21:02:01 -08:00
parent 373919b0eb
commit fdf8f6b481
No known key found for this signature in database
6 changed files with 37 additions and 39 deletions

View File

@ -13,17 +13,18 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
/** /**
* Utility functions to support the JSDoc plugin framework. * Utility functions to support the JSDoc plugin framework.
*/ */
function addHandlers(handlers, parser, deps) { function addHandlers(handlers, parser, env) {
Object.keys(handlers).forEach((eventName) => { Object.keys(handlers).forEach((eventName) => {
parser.on(eventName, handlers[eventName], deps); parser.on(eventName, handlers[eventName], env);
}); });
} }
export async function installPlugins(plugins, parser, deps) { export async function installPlugins(plugins, parser, env) {
let dictionary; let dictionary;
let plugin; let plugin;
@ -33,18 +34,18 @@ export async function installPlugins(plugins, parser, deps) {
// allow user-defined plugins to... // allow user-defined plugins to...
// ...register event handlers // ...register event handlers
if (plugin.handlers) { if (plugin.handlers) {
addHandlers(plugin.handlers, parser, deps); addHandlers(plugin.handlers, parser, env);
} }
// ...define tags // ...define tags
if (plugin.defineTags) { if (plugin.defineTags) {
dictionary = deps.get('tags'); dictionary = env.tags;
plugin.defineTags(dictionary, deps); plugin.defineTags(dictionary, env);
} }
// ...add an ESTree node visitor // ...add an ESTree node visitor
if (plugin.astNodeVisitor) { if (plugin.astNodeVisitor) {
parser.addAstNodeVisitor(plugin.astNodeVisitor, deps); parser.addAstNodeVisitor(plugin.astNodeVisitor, env);
} }
} }
} }

View File

@ -111,10 +111,10 @@ describe('@jsdoc/doclet/lib/doclet', () => {
}); });
describe('Doclet', () => { describe('Doclet', () => {
function makeDoclet(tagStrings, deps) { function makeDoclet(tagStrings, env) {
const comment = `/**\n${tagStrings.join('\n')}\n*/`; const comment = `/**\n${tagStrings.join('\n')}\n*/`;
return new Doclet(comment, {}, deps || jsdoc.env); return new Doclet(comment, {}, env || jsdoc.env);
} }
const docSet = jsdoc.getDocSetFromFile('test/fixtures/doclet.js'); const docSet = jsdoc.getDocSetFromFile('test/fixtures/doclet.js');
@ -394,7 +394,7 @@ describe('@jsdoc/doclet/lib/doclet', () => {
}); });
it('always returns `true` based on `doclet.access` when `access` config includes `all`', () => { it('always returns `true` based on `doclet.access` when `access` config includes `all`', () => {
const fakeDeps = makeEnv(['all']); const fakeEnv = makeEnv(['all']);
const doclets = ACCESS_VALUES.map((value) => { const doclets = ACCESS_VALUES.map((value) => {
let newDoclet; let newDoclet;
const tags = ['@function', '@name foo']; const tags = ['@function', '@name foo'];
@ -402,7 +402,7 @@ describe('@jsdoc/doclet/lib/doclet', () => {
if (value) { if (value) {
tags.push('@' + value); tags.push('@' + value);
} }
newDoclet = makeDoclet(tags, fakeDeps); newDoclet = makeDoclet(tags, fakeEnv);
// Just to be sure. // Just to be sure.
if (!value) { if (!value) {
newDoclet.access = undefined; newDoclet.access = undefined;
@ -417,29 +417,29 @@ describe('@jsdoc/doclet/lib/doclet', () => {
}); });
it('returns `false` for `package` doclets when config omits `package`', () => { it('returns `false` for `package` doclets when config omits `package`', () => {
const fakeDeps = makeEnv(['public']); const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@package'], fakeDeps); const newDoclet = makeDoclet(['@function', '@name foo', '@package'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse(); expect(newDoclet.isVisible()).toBeFalse();
}); });
it('returns `false` for `protected` doclets when config omits `protected`', () => { it('returns `false` for `protected` doclets when config omits `protected`', () => {
const fakeDeps = makeEnv(['public']); const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@protected'], fakeDeps); const newDoclet = makeDoclet(['@function', '@name foo', '@protected'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse(); expect(newDoclet.isVisible()).toBeFalse();
}); });
it('returns `false` for `public` doclets when config omits `public`', () => { it('returns `false` for `public` doclets when config omits `public`', () => {
const fakeDeps = makeEnv(['private']); const fakeEnv = makeEnv(['private']);
const newDoclet = makeDoclet(['@function', '@name foo', '@public'], fakeDeps); const newDoclet = makeDoclet(['@function', '@name foo', '@public'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse(); expect(newDoclet.isVisible()).toBeFalse();
}); });
it('returns `false` for undefined-access doclets when config omits `undefined`', () => { it('returns `false` for undefined-access doclets when config omits `undefined`', () => {
const fakeDeps = makeEnv(['public']); const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo'], fakeDeps); const newDoclet = makeDoclet(['@function', '@name foo'], fakeEnv);
// Just to be sure. // Just to be sure.
newDoclet.access = undefined; newDoclet.access = undefined;

View File

@ -13,6 +13,7 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { Syntax } from '@jsdoc/ast'; import { Syntax } from '@jsdoc/ast';
import { name } from '@jsdoc/core'; import { name } from '@jsdoc/core';
import { Doclet } from '@jsdoc/doclet'; import { Doclet } from '@jsdoc/doclet';
@ -46,20 +47,18 @@ function filterByLongname({ longname }) {
return false; return false;
} }
function createDoclet(comment, e, deps) { function createDoclet(comment, e, env) {
let doclet; let doclet;
let flatComment; let flatComment;
let log;
let msg; let msg;
try { try {
doclet = new Doclet(comment, e, deps); doclet = new Doclet(comment, e, env);
} catch (error) { } catch (error) {
flatComment = comment.replace(/[\r\n]/g, ''); flatComment = comment.replace(/[\r\n]/g, '');
msg = `cannot create a doclet for the comment "${flatComment}": ${error.message}`; msg = `cannot create a doclet for the comment "${flatComment}": ${error.message}`;
log = deps.get('log'); env.log.error(msg);
log.error(msg); doclet = new Doclet('', e, env);
doclet = new Doclet('', e, deps);
} }
return doclet; return doclet;
@ -84,13 +83,13 @@ function createDoclet(comment, e, deps) {
* *
* @private * @private
*/ */
function createSymbolDoclet(comment, e, deps) { function createSymbolDoclet(comment, e, env) {
let doclet = createDoclet(comment, e, deps); let doclet = createDoclet(comment, e, env);
if (doclet.name) { if (doclet.name) {
// try again, without the comment // try again, without the comment
e.comment = '@undocumented'; e.comment = '@undocumented';
doclet = createDoclet(e.comment, e, deps); doclet = createDoclet(e.comment, e, env);
} }
return doclet; return doclet;

View File

@ -614,9 +614,9 @@ export class Parser extends EventEmitter {
} }
} }
// TODO: docs // TODO: remove
export function createParser(deps) { export function createParser(env) {
return new Parser(deps); return new Parser(env);
} }
// TODO: document other events // TODO: document other events

View File

@ -107,14 +107,14 @@ function cleanse(e, config) {
const handlers = {}; const handlers = {};
EVENT_TYPES.forEach((eventType) => { EVENT_TYPES.forEach((eventType) => {
handlers[eventType] = (e, deps) => { handlers[eventType] = (e, { config }) => {
const config = deps.get('config').eventDumper; const pluginConfig = config.eventDumper;
if (shouldLog(eventType, config)) { if (shouldLog(eventType, pluginConfig)) {
console.log( console.log(
JSON.stringify({ JSON.stringify({
type: eventType, type: eventType,
content: cleanse(e, config), content: cleanse(e, pluginConfig),
}), }),
null, null,
4 4

View File

@ -26,13 +26,11 @@ export const handlers = {
* @param e * @param e
* @param e.filename * @param e.filename
* @param e.source * @param e.source
* @param e.deps * @param env
* @example * @example
* @partial "partial_doc.jsdoc" * @partial "partial_doc.jsdoc"
*/ */
beforeParse(e, deps) { beforeParse(e, { options }) {
const options = deps.get('options');
e.source = e.source.replace(/(@partial ".*")+/g, ($) => { e.source = e.source.replace(/(@partial ".*")+/g, ($) => {
const pathArg = $.match(/".*"/)[0].replace(/"/g, ''); const pathArg = $.match(/".*"/)[0].replace(/"/g, '');
const fullPath = path.join(e.filename, '..', pathArg); const fullPath = path.join(e.filename, '..', pathArg);