Skip to content
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

Update: Add FunctionExpression to require-jsdoc (fixes #5867) #9395

Merged
merged 2 commits into from Oct 14, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 59 additions & 12 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,39 @@ function foo() {

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

class Test{
getDate(){}
class Foo {
bar() {
return 10;
}
}

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 +141,40 @@ array.filter(function(item) {
});

/**
* It returns 10
* A class that can return the number 10
*/
class Test{
class Foo {
/**
* returns the date
* It returns 10
*/
getDate(){}
bar() {
return 10;
}
}

/**
* 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" && node === node.parent.value)))
) {
checkJsDoc(node);
}
},
ClassDeclaration(node) {
Expand Down
71 changes: 71 additions & 0 deletions tests/lib/rules/require-jsdoc.js
Expand Up @@ -191,6 +191,40 @@ 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
}
}]
},
{
code: " var foo = { [function() {}]: 1 };",
options: [{
require: {
FunctionExpression: true
}
}],
parserOptions: { ecmaVersion: 6 }
}
],

Expand Down Expand Up @@ -341,6 +375,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"
}]
}
]
});