Skip to content

require-description in additional contexts #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .README/rules/require-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ Requires that all functions have a description.
* All functions must have a `@description` tag.
* Every description tag must have a non-empty description that explains the purpose of the method.

#### Options

- `contexts` - Set to a string or array of strings representing the AST context
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
- `noDefaults` - By default, `contexts` will permit `ArrowFunctionExpression`,
`FunctionDeclaration`, and `FunctionExpression`. Set this instead to `true` to
have `contexts` override these.

|||
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|`description`|
|Aliases|`desc`|
|Options|`contexts`, `noDefaults`|

<!-- assertions requireDescription -->
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2192,11 +2192,21 @@ Requires that all functions have a description.
* All functions must have a `@description` tag.
* Every description tag must have a non-empty description that explains the purpose of the method.

<a name="eslint-plugin-jsdoc-rules-require-description-options"></a>
#### Options

- `contexts` - Set to a string or array of strings representing the AST context
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
- `noDefaults` - By default, `contexts` will permit `ArrowFunctionExpression`,
`FunctionDeclaration`, and `FunctionExpression`. Set this instead to `true` to
have `contexts` override these.

|||
|---|---|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`; others when `contexts` option enabled|
|Tags|`description`|
|Aliases|`desc`|
|Options|`contexts`, `noDefaults`|

The following patterns are considered problems:

Expand All @@ -2209,6 +2219,33 @@ function quux () {
}
// Message: Missing JSDoc @description declaration.

/**
*
*/
class quux {

}
// Options: [{"contexts":"ClassDeclaration"}]
// Message: Missing JSDoc @description declaration.

/**
*
*/
class quux {

}
// Options: [{"contexts":"ClassDeclaration","noDefaults":true}]
// Message: Missing JSDoc @description declaration.

/**
*
*/
class quux {

}
// Options: [{"contexts":["ClassDeclaration"]}]
// Message: Missing JSDoc @description declaration.

/**
* @description
*/
Expand Down Expand Up @@ -2247,6 +2284,21 @@ function quux () {
function quux () {

}

/**
*
*/
class quux {

}

/**
*
*/
function quux () {

}
// Options: [{"noDefaults":true}]
````


Expand Down
9 changes: 6 additions & 3 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,19 @@ export default (iterator, opts = {}) => {
});
};

let contexts = opts.returns;
if (typeof opts.returns === 'function') {
return opts.returns(context, sourceCode, checkJsdoc);
contexts = opts.returns(context, sourceCode, checkJsdoc);
}

if (Array.isArray(opts.returns)) {
return opts.returns.reduce((obj, prop) => {
if (Array.isArray(contexts)) {
return contexts.reduce((obj, prop) => {
obj[prop] = checkJsdoc;

return obj;
}, {});
} else if (contexts) {
return contexts;
}

return {
Expand Down
19 changes: 19 additions & 0 deletions src/rules/requireDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ export default iterateJsdoc(({
report('Missing JSDoc @' + targetTagName + ' description.');
}
});
}, {
returns (context) {
const defaultContexts = [
'ArrowFunctionExpression',
'FunctionDeclaration',
'FunctionExpression'
];

const {
noDefaults,
contexts: ctxts = []
} = context.options[0] || {};

const contexts = typeof ctxts === 'string' ? [ctxts] : ctxts;

return noDefaults ?
contexts :
[...new Set([...defaultContexts, ...contexts])];
}
});
86 changes: 86 additions & 0 deletions test/rules/assertions/requireDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,67 @@ export default {
}
]
},
{
code: `
/**
*
*/
class quux {

}
`,
errors: [
{
message: 'Missing JSDoc @description declaration.'
}
],
options: [
{
contexts: 'ClassDeclaration'
}
]
},
{
code: `
/**
*
*/
class quux {

}
`,
errors: [
{
message: 'Missing JSDoc @description declaration.'
}
],
options: [
{
contexts: 'ClassDeclaration',
noDefaults: true
}
]
},
{
code: `
/**
*
*/
class quux {

}
`,
errors: [
{
message: 'Missing JSDoc @description declaration.'
}
],
options: [
{
contexts: ['ClassDeclaration']
}
]
},
{
code: `
/**
Expand Down Expand Up @@ -67,6 +128,31 @@ export default {

}
`
},
{
code: `
/**
*
*/
class quux {

}
`
},
{
code: `
/**
*
*/
function quux () {

}
`,
options: [
{
noDefaults: true
}
]
}
]
};