Skip to content

Commit 548bd59

Browse files
committed
feat(require-jsdoc): support objects to contexts with a context and optional inlineCommentBlock property; fixes part of #530
`inlineCommentBlock` will cause the fixer to insert an inline jsdoc block instead of the regular multiline, indented block.
1 parent 658a5a1 commit 548bd59

File tree

5 files changed

+122
-12
lines changed

5 files changed

+122
-12
lines changed

.README/rules/require-jsdoc.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ Accepts one optional options object with the following optional keys.
3232
- `FunctionExpression`
3333
- `MethodDefinition`
3434

35-
- `contexts` - Set this to an array of strings representing the additional
36-
AST contexts where you wish the rule to be applied (e.g., `Property` for
37-
properties). Defaults to an empty array.
35+
- `contexts` - Set this to an array of strings or objects representing the
36+
additional AST contexts where you wish the rule to be applied (e.g.,
37+
`Property` for properties). If specified as an object, it should have a
38+
`context` property and can have an `inlineCommentBlock` property which,
39+
if set to `true`, will add an inline `/** */` instead of the regular,
40+
multi-line, indented jsdoc block which will otherwise be added. Defaults
41+
to an empty array.
3842

3943
- `exemptEmptyFunctions` (default: false) - When `true`, the rule will not report
4044
missing jsdoc blocks above functions/methods with no parameters or return values

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7837,9 +7837,13 @@ Accepts one optional options object with the following optional keys.
78377837
- `FunctionExpression`
78387838
- `MethodDefinition`
78397839
7840-
- `contexts` - Set this to an array of strings representing the additional
7841-
AST contexts where you wish the rule to be applied (e.g., `Property` for
7842-
properties). Defaults to an empty array.
7840+
- `contexts` - Set this to an array of strings or objects representing the
7841+
additional AST contexts where you wish the rule to be applied (e.g.,
7842+
`Property` for properties). If specified as an object, it should have a
7843+
`context` property and can have an `inlineCommentBlock` property which,
7844+
if set to `true`, will add an inline `/** */` instead of the regular,
7845+
multi-line, indented jsdoc block which will otherwise be added. Defaults
7846+
to an empty array.
78437847
78447848
- `exemptEmptyFunctions` (default: false) - When `true`, the rule will not report
78457849
missing jsdoc blocks above functions/methods with no parameters or return values
@@ -8293,6 +8297,14 @@ const hello = name => {
82938297
export const loginSuccessAction = (): BaseActionPayload => ({ type: LOGIN_SUCCESSFUL });
82948298
// Options: [{"require":{"ArrowFunctionExpression":true,"FunctionDeclaration":false}}]
82958299
// Message: Missing JSDoc comment.
8300+
8301+
export type Container = {
8302+
constants?: ObjByString;
8303+
enums?: { [key in string]: TypescriptEnum };
8304+
helpers?: { [key in string]: AnyFunction };
8305+
};
8306+
// Options: [{"contexts":["TSTypeAliasDeclaration",{"context":"TSPropertySignature","inlineCommentBlock":true}]}]
8307+
// Message: Missing JSDoc comment.
82968308
````
82978309
82988310
The following patterns are not considered problems:
@@ -10724,6 +10736,16 @@ const bboxToObj = function ({x, y, width, height}) {
1072410736
return {x, y, width, height};
1072510737
};
1072610738
// Options: [{"checkTypesPattern":"SVGRect"}]
10739+
10740+
class CSS {
10741+
/**
10742+
* Set one or more CSS properties for the set of matched elements.
10743+
*
10744+
* @param {Object} propertyObject - An object of property-value pairs to set.
10745+
*/
10746+
setCssObject(propertyObject: {[key: string]: string | number}): void {
10747+
}
10748+
}
1072710749
````
1072810750

1072910751

src/jsdocUtils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,11 @@ const enforcedContexts = (context, defaultContexts) => {
595595
*/
596596
const getContextObject = (contexts, checkJsdoc) => {
597597
return contexts.reduce((obj, prop) => {
598-
obj[prop] = checkJsdoc;
598+
if (typeof prop === 'object') {
599+
obj[prop.context] = checkJsdoc;
600+
} else {
601+
obj[prop] = checkJsdoc;
602+
}
599603

600604
return obj;
601605
}, {});

src/rules/requireJsdoc.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ const OPTIONS_SCHEMA = {
1010
properties: {
1111
contexts: {
1212
items: {
13-
type: 'string',
13+
anyOf: [
14+
{
15+
type: 'string',
16+
},
17+
{
18+
additionalProperties: false,
19+
properties: {
20+
context: {
21+
type: 'string',
22+
},
23+
inlineCommentBlock: {
24+
type: 'boolean',
25+
},
26+
},
27+
type: 'object',
28+
},
29+
],
1430
},
1531
type: 'array',
1632
},
@@ -89,10 +105,16 @@ const getOption = (context, baseObject, option, key) => {
89105
};
90106

91107
const getOptions = (context) => {
108+
const {
109+
publicOnly,
110+
contexts = [],
111+
exemptEmptyFunctions = false,
112+
} = context.options[0] || {};
113+
92114
return {
93-
exemptEmptyFunctions: context.options[0] ? context.options[0].exemptEmptyFunctions : false,
115+
contexts,
116+
exemptEmptyFunctions,
94117
publicOnly: ((baseObj) => {
95-
const publicOnly = _.get(context, 'options[0].publicOnly');
96118
if (!publicOnly) {
97119
return false;
98120
}
@@ -122,7 +144,11 @@ export default {
122144
const sourceCode = context.getSourceCode();
123145
const settings = getSettings(context);
124146

125-
const {require: requireOption, publicOnly, exemptEmptyFunctions} = getOptions(context);
147+
const {
148+
require: requireOption,
149+
contexts,
150+
publicOnly, exemptEmptyFunctions,
151+
} = getOptions(context);
126152

127153
const checkJsDoc = (node, isFunctionContext) => {
128154
const jsDocNode = getJSDocComment(sourceCode, node, settings);
@@ -149,7 +175,13 @@ export default {
149175
baseNode.loc.start.column,
150176
),
151177
});
152-
const insertion = `/**\n${indent}*\n${indent}*/${'\n'.repeat(lines)}${indent.slice(0, -1)}`;
178+
const {inlineCommentBlock} = contexts.find(({context: ctxt}) => {
179+
return ctxt === node.type;
180+
}) || {};
181+
const insertion = (inlineCommentBlock ?
182+
'/** ' :
183+
`/**\n${indent}*\n${indent}`) +
184+
`*/${'\n'.repeat(lines)}${indent.slice(0, -1)}`;
153185

154186
return fixer.insertTextBefore(baseNode, insertion);
155187
};

test/rules/assertions/requireJsdoc.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,54 @@ export default {
19111911
sourceType: 'module',
19121912
},
19131913
},
1914+
{
1915+
code: `
1916+
export type Container = {
1917+
constants?: ObjByString;
1918+
enums?: { [key in string]: TypescriptEnum };
1919+
helpers?: { [key in string]: AnyFunction };
1920+
};
1921+
`,
1922+
errors: [
1923+
{
1924+
message: 'Missing JSDoc comment.',
1925+
},
1926+
{
1927+
message: 'Missing JSDoc comment.',
1928+
},
1929+
{
1930+
message: 'Missing JSDoc comment.',
1931+
},
1932+
{
1933+
message: 'Missing JSDoc comment.',
1934+
},
1935+
],
1936+
options: [
1937+
{
1938+
contexts: [
1939+
'TSTypeAliasDeclaration',
1940+
{
1941+
context: 'TSPropertySignature',
1942+
inlineCommentBlock: true,
1943+
},
1944+
],
1945+
},
1946+
],
1947+
output: `
1948+
/**
1949+
*
1950+
*/
1951+
export type Container = {
1952+
/** */
1953+
constants?: ObjByString;
1954+
/** */
1955+
enums?: { [key in string]: TypescriptEnum };
1956+
/** */
1957+
helpers?: { [key in string]: AnyFunction };
1958+
};
1959+
`,
1960+
parser: require.resolve('@typescript-eslint/parser'),
1961+
},
19141962
],
19151963
valid: [{
19161964
code: `

0 commit comments

Comments
 (0)