Skip to content

Commit

Permalink
fix(require-jsdoc): allow contexts to be usable in place of `requ…
Browse files Browse the repository at this point in the history
…ire` items
  • Loading branch information
brettz9 committed Sep 25, 2020
1 parent 6d0f351 commit a1d95e6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9422,6 +9422,18 @@ export const test = () => {
// Options: [{"publicOnly":true,"require":{"ArrowFunctionExpression":true}}]
// Message: Missing JSDoc comment.
export const test = () => {
};
// Options: [{"contexts":["ArrowFunctionExpression"],"publicOnly":true}]
// Message: Missing JSDoc comment.
export const test = () => {
};
// Options: [{"contexts":[{"context":"ArrowFunctionExpression"}],"publicOnly":true}]
// Message: Missing JSDoc comment.
export let test = class {
};
Expand Down
18 changes: 12 additions & 6 deletions src/rules/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,16 @@ export default {
}
};

const hasOption = (prop) => {
return requireOption[prop] || contexts.some((ctxt) => {
return typeof ctxt === 'object' ? ctxt.context === prop : ctxt === prop;
});
};

return {
...jsdocUtils.getContextObject(jsdocUtils.enforcedContexts(context, []), checkJsDoc),
ArrowFunctionExpression (node) {
if (!requireOption.ArrowFunctionExpression) {
if (!hasOption('ArrowFunctionExpression')) {
return;
}

Expand All @@ -285,37 +291,37 @@ export default {
},

ClassDeclaration (node) {
if (!requireOption.ClassDeclaration) {
if (!hasOption('ClassDeclaration')) {
return;
}

checkJsDoc(node);
},

ClassExpression (node) {
if (!requireOption.ClassExpression) {
if (!hasOption('ClassExpression')) {
return;
}

checkJsDoc(node);
},

FunctionDeclaration (node) {
if (!requireOption.FunctionDeclaration) {
if (!hasOption('FunctionDeclaration')) {
return;
}

checkJsDoc(node, true);
},

FunctionExpression (node) {
if (requireOption.MethodDefinition && node.parent.type === 'MethodDefinition') {
if (hasOption('MethodDefinition') && node.parent.type === 'MethodDefinition') {
checkJsDoc(node, true);

return;
}

if (!requireOption.FunctionExpression) {
if (!hasOption('FunctionExpression')) {
return;
}

Expand Down
58 changes: 58 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,64 @@ export default {
sourceType: 'module',
},
},
{
code: `
export const test = () => {
};
`,
errors: [
{
message: 'Missing JSDoc comment.',
type: 'ArrowFunctionExpression',
},
],
options: [{
contexts: ['ArrowFunctionExpression'],
publicOnly: true,
}],
output: `
/**
*
*/
export const test = () => {
};
`,
parserOptions: {
sourceType: 'module',
},
},
{
code: `
export const test = () => {
};
`,
errors: [
{
message: 'Missing JSDoc comment.',
type: 'ArrowFunctionExpression',
},
],
options: [{
contexts: [{
context: 'ArrowFunctionExpression',
}],
publicOnly: true,
}],
output: `
/**
*
*/
export const test = () => {
};
`,
parserOptions: {
sourceType: 'module',
},
},
{
code: `
export let test = class {
Expand Down

0 comments on commit a1d95e6

Please sign in to comment.