Skip to content

Commit

Permalink
Update: Add FunctionExpression to require-jsdoc (fixes #5867)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Oct 6, 2017
1 parent ee99876 commit 71be375
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 29 deletions.
65 changes: 54 additions & 11 deletions docs/rules/require-jsdoc.md
Expand Up @@ -24,6 +24,7 @@ This rule requires JSDoc comments for specified nodes. Supported nodes:
* `"ClassDeclaration"`
* `"MethodDefinition"`
* `"ArrowFunctionExpression"`
* `"FunctionExpression"`

## Options

Expand All @@ -40,22 +41,25 @@ Default option settings are:
"FunctionDeclaration": true,
"MethodDefinition": false,
"ClassDeclaration": false,
"ArrowFunctionExpression": false
"ArrowFunctionExpression": false,
"FunctionExpression": false
}
}]
}
```

### require

Examples of **incorrect** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true } }` option:
Examples of **incorrect** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true, "FunctionExpression": true } }` option:

```js
/*eslint "require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
"ClassDeclaration": true,
"ArrowFunctionExpression": true,
"FunctionExpression": true
}
}]*/

Expand All @@ -65,21 +69,37 @@ function foo() {

var foo = () => {
return 10;
}
};

class Test{
getDate(){}
class Foo {
getDate() {}
}

var foo = function() {
return 10;
};

var foo = {
bar: function() {
return 10;
},

baz() {
return 10;
}
};
```

Examples of **correct** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true } }` option:
Examples of **correct** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true, "FunctionExpression": true } }` option:

```js
/*eslint "require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
"ClassDeclaration": true,
"ArrowFunctionExpression": true,
"FunctionExpression": true
}
}]*/

Expand Down Expand Up @@ -119,15 +139,38 @@ array.filter(function(item) {
});

/**
* It returns 10
* Some class that can return the date
*/
class Test{
class Foo {
/**
* returns the date
*/
getDate(){}
getDate() {}
}

/**
* It returns 10
*/
var foo = function() {
return 10;
};

var foo = {
/**
* It returns 10
*/
bar: function() {
return 10;
},

/**
* It returns 10
*/
baz() {
return 10;
}
};

setTimeout(() => {}, 10); // since it's an anonymous arrow function
```

Expand Down
29 changes: 11 additions & 18 deletions lib/rules/require-jsdoc.js
Expand Up @@ -30,6 +30,9 @@ module.exports = {
},
ArrowFunctionExpression: {
type: "boolean"
},
FunctionExpression: {
type: "boolean"
}
},
additionalProperties: false
Expand All @@ -45,7 +48,9 @@ module.exports = {
const DEFAULT_OPTIONS = {
FunctionDeclaration: true,
MethodDefinition: false,
ClassDeclaration: false
ClassDeclaration: false,
ArrowFunctionExpression: false,
FunctionExpression: false
};
const options = Object.assign(DEFAULT_OPTIONS, context.options[0] && context.options[0].require || {});

Expand All @@ -58,21 +63,6 @@ module.exports = {
context.report({ node, message: "Missing JSDoc comment." });
}

/**
* Check if the jsdoc comment is present for class methods
* @param {ASTNode} node node to examine
* @returns {void}
*/
function checkClassMethodJsDoc(node) {
if (node.parent.type === "MethodDefinition") {
const jsdocComment = source.getJSDocComment(node);

if (!jsdocComment) {
report(node);
}
}
}

/**
* Check if the jsdoc comment is present or not.
* @param {ASTNode} node node to examine
Expand All @@ -93,8 +83,11 @@ module.exports = {
}
},
FunctionExpression(node) {
if (options.MethodDefinition) {
checkClassMethodJsDoc(node);
if (
(options.MethodDefinition && node.parent.type === "MethodDefinition") ||
(options.FunctionExpression && (node.parent.type === "VariableDeclarator" || node.parent.type === "Property"))
) {
checkJsDoc(node);
}
},
ClassDeclaration(node) {
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/require-jsdoc.js
Expand Up @@ -191,6 +191,31 @@ ruleTester.run("require-jsdoc", rule, {
}
}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "/**\nJSDoc Block\n*/\nvar foo = function() {}",
options: [{
require: {
FunctionExpression: true
}
}]
},
{
code: "const foo = {/**\nJSDoc Block\n*/\nbar() {}}",
options: [{
require: {
FunctionExpression: true
}
}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = {/**\nJSDoc Block\n*/\nbar: function() {}}",
options: [{
require: {
FunctionExpression: true
}
}]
}
],

Expand Down Expand Up @@ -341,6 +366,43 @@ ruleTester.run("require-jsdoc", rule, {
message: "Missing JSDoc comment.",
type: "ArrowFunctionExpression"
}]
},
{
code: "var foo = function() {}",
options: [{
require: {
FunctionExpression: true
}
}],
errors: [{
message: "Missing JSDoc comment.",
type: "FunctionExpression"
}]
},
{
code: "const foo = {bar() {}}",
options: [{
require: {
FunctionExpression: true
}
}],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing JSDoc comment.",
type: "FunctionExpression"
}]
},
{
code: "var foo = {bar: function() {}}",
options: [{
require: {
FunctionExpression: true
}
}],
errors: [{
message: "Missing JSDoc comment.",
type: "FunctionExpression"
}]
}
]
});

0 comments on commit 71be375

Please sign in to comment.