Skip to content

Commit b1743ce

Browse files
committed
refactor(require-description): move context defaulting to utility
refactor: rename `opts` -> `ruleConfig` to better distinguish from user options
1 parent 1393fa1 commit b1743ce

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

src/iterateJsdoc.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ export {
277277
* @typedef {ReturnType<typeof getSettings>} Settings
278278
*
279279
* @param {(arg: {utils: Utils, settings: Settings}) => any} iterator
280-
* @param {{meta: any, returns?: any}} opts
280+
* @param {{contextDefaults: (true|string[]), meta: any, returns?: any}} ruleConfig
281281
*/
282-
export default function iterateJsdoc (iterator, opts) {
283-
const metaType = _.get(opts, 'meta.type');
282+
export default function iterateJsdoc (iterator, ruleConfig) {
283+
const metaType = _.get(ruleConfig, 'meta.type');
284284
if (!metaType || !['problem', 'suggestion', 'layout'].includes(metaType)) {
285285
throw new TypeError('Rule must include `meta.type` option (with value "problem", "suggestion", or "layout")');
286286
}
287-
if (typeof iterator !== 'function' && (!opts || typeof opts.returns !== 'function')) {
287+
if (typeof iterator !== 'function' && (!ruleConfig || typeof ruleConfig.returns !== 'function')) {
288288
throw new TypeError('The iterator argument must be a function or an object with a `returns` method.');
289289
}
290290

@@ -303,9 +303,11 @@ export default function iterateJsdoc (iterator, opts) {
303303

304304
const settings = getSettings(context);
305305

306-
let contexts = opts.returns;
307-
if (typeof opts.returns === 'function') {
308-
contexts = opts.returns(context, sourceCode);
306+
let contexts = ruleConfig.returns;
307+
if (typeof ruleConfig.returns === 'function') {
308+
contexts = ruleConfig.returns(context, sourceCode);
309+
} else if (ruleConfig.contextDefaults) {
310+
contexts = jsdocUtils.enforcedContexts(context, ruleConfig.contextDefaults);
309311
}
310312

311313
if (!Array.isArray(contexts) && contexts) {
@@ -393,6 +395,6 @@ export default function iterateJsdoc (iterator, opts) {
393395
return obj;
394396
}, {});
395397
},
396-
meta: opts.meta
398+
meta: ruleConfig.meta
397399
};
398400
}

src/jsdocUtils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,36 @@ const parseClosureTemplateTag = (tag) => {
483483
});
484484
};
485485

486+
/**
487+
* Checks user option for `contexts` array, defaulting to
488+
* contexts designated by the rule. Returns an array of
489+
* ESTree AST types, indicating allowable contexts.
490+
*
491+
* @param {*} context
492+
* @param {true|string[]} defaultContexts
493+
* @returns {string[]}
494+
*/
495+
const enforcedContexts = (context, defaultContexts) => {
496+
/* istanbul ignore next */
497+
const defltContexts = defaultContexts === true ? [
498+
'ArrowFunctionExpression',
499+
'FunctionDeclaration',
500+
'FunctionExpression'
501+
] : defaultContexts;
502+
const {
503+
noDefaults,
504+
contexts: ctxts = []
505+
} = context.options[0] || {};
506+
507+
const contexts = typeof ctxts === 'string' ? [ctxts] : ctxts;
508+
509+
return noDefaults ?
510+
contexts :
511+
[...new Set([...defltContexts, ...contexts])];
512+
};
513+
486514
export default {
515+
enforcedContexts,
487516
getFunctionParameterNames,
488517
getJsdocParameterNames,
489518
getJsdocParameterNamesDeep,

src/rules/requireDescription.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default iterateJsdoc(({
3030
}
3131
});
3232
}, {
33+
contextDefaults: true,
3334
meta: {
3435
schema: [
3536
{
@@ -62,24 +63,5 @@ export default iterateJsdoc(({
6263
}
6364
],
6465
type: 'suggestion'
65-
},
66-
returns (context) {
67-
const defaultContexts = [
68-
'ArrowFunctionExpression',
69-
'FunctionDeclaration',
70-
'FunctionExpression'
71-
];
72-
73-
const {
74-
noDefaults,
75-
contexts: ctxts = []
76-
} = context.options[0] || {};
77-
78-
const contexts = typeof ctxts === 'string' ? [ctxts] : ctxts;
79-
80-
return noDefaults ?
81-
contexts :
82-
[...new Set([...defaultContexts, ...contexts])];
8366
}
84-
8567
});

0 commit comments

Comments
 (0)