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
limitations under the License.
*/
/**
* Utility functions to support the JSDoc plugin framework.
*/
function addHandlers(handlers, parser, deps) {
function addHandlers(handlers, parser, env) {
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 plugin;
@ -33,18 +34,18 @@ export async function installPlugins(plugins, parser, deps) {
// allow user-defined plugins to...
// ...register event handlers
if (plugin.handlers) {
addHandlers(plugin.handlers, parser, deps);
addHandlers(plugin.handlers, parser, env);
}
// ...define tags
if (plugin.defineTags) {
dictionary = deps.get('tags');
plugin.defineTags(dictionary, deps);
dictionary = env.tags;
plugin.defineTags(dictionary, env);
}
// ...add an ESTree node visitor
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', () => {
function makeDoclet(tagStrings, deps) {
function makeDoclet(tagStrings, env) {
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');
@ -394,7 +394,7 @@ describe('@jsdoc/doclet/lib/doclet', () => {
});
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) => {
let newDoclet;
const tags = ['@function', '@name foo'];
@ -402,7 +402,7 @@ describe('@jsdoc/doclet/lib/doclet', () => {
if (value) {
tags.push('@' + value);
}
newDoclet = makeDoclet(tags, fakeDeps);
newDoclet = makeDoclet(tags, fakeEnv);
// Just to be sure.
if (!value) {
newDoclet.access = undefined;
@ -417,29 +417,29 @@ describe('@jsdoc/doclet/lib/doclet', () => {
});
it('returns `false` for `package` doclets when config omits `package`', () => {
const fakeDeps = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@package'], fakeDeps);
const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@package'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse();
});
it('returns `false` for `protected` doclets when config omits `protected`', () => {
const fakeDeps = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@protected'], fakeDeps);
const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo', '@protected'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse();
});
it('returns `false` for `public` doclets when config omits `public`', () => {
const fakeDeps = makeEnv(['private']);
const newDoclet = makeDoclet(['@function', '@name foo', '@public'], fakeDeps);
const fakeEnv = makeEnv(['private']);
const newDoclet = makeDoclet(['@function', '@name foo', '@public'], fakeEnv);
expect(newDoclet.isVisible()).toBeFalse();
});
it('returns `false` for undefined-access doclets when config omits `undefined`', () => {
const fakeDeps = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo'], fakeDeps);
const fakeEnv = makeEnv(['public']);
const newDoclet = makeDoclet(['@function', '@name foo'], fakeEnv);
// Just to be sure.
newDoclet.access = undefined;

View File

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

View File

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

View File

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

View File

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