-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Require JSDoc comment (require-jsdoc) | ||
|
||
This rule generates warnings for nodes which do not have JSDoc comments. It considered a good practice to document the behavior of different nodes to help engineers understand the functionality of the node. | ||
Supported nodes: | ||
|
||
* `FunctionExpression` | ||
* `FunctionDeclaration` | ||
|
||
## Rule details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
/*eslint require-jsdoc: 2*/ | ||
|
||
function foo() { /*error Missing JSDoc comment.*/ | ||
return 10; | ||
} | ||
|
||
var foo = function() { /*error Missing JSDoc comment.*/ | ||
return 10; | ||
} | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
/*eslint require-jsdoc: 2*/ | ||
|
||
/** | ||
* It returns 10 | ||
*/ | ||
function foo() { | ||
return 10; | ||
} | ||
|
||
/** | ||
* It returns 10 | ||
*/ | ||
var foo = function() { | ||
return 10; | ||
} | ||
|
||
var array = [1,2,3]; | ||
array.filter(function(item) { | ||
return item > 2; | ||
}); | ||
``` | ||
|
||
## When not to use | ||
|
||
If you do not require node documentation, then you can leave this rule off. | ||
|
||
## Related Rules | ||
|
||
* [valid-jsdoc](valid-jsdoc.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @fileoverview Rule to check for jsdoc presence. | ||
* @author Gyandeep Singh | ||
* @copyright 2015 Gyandeep Singh. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
module.exports = function(context) { | ||
var source = context.getSourceCode(); | ||
|
||
/** | ||
* Report the error message | ||
* @param {ASTNode} node node to report | ||
* @returns {void} | ||
*/ | ||
function report(node) { | ||
context.report(node, "Missing JSDoc comment."); | ||
} | ||
|
||
/** | ||
* Check if the jsdoc comment is present or not. | ||
* @param {ASTNode} node node to examine | ||
* @returns {void} | ||
*/ | ||
function checkJsDoc(node) { | ||
var jsdocComment = source.getJSDocComment(node); | ||
|
||
if (!jsdocComment) { | ||
switch (node.type) { | ||
case "FunctionExpression": | ||
if (node.parent.type !== "MemberExpression" && node.parent.type !== "CallExpression" ) { | ||
report(node); | ||
} | ||
break; | ||
|
||
default: | ||
report(node); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
"FunctionExpression": checkJsDoc, | ||
"FunctionDeclaration": checkJsDoc | ||
}; | ||
}; | ||
|
||
module.exports.schema = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @fileoverview Test file for require-jsdoc rule | ||
* @author Gyandeep Singh | ||
* @copyright 2015 Gyandeep Singh. All rights reserved. | ||
*/ | ||
"use strict"; | ||
|
||
var rule = require("../../../lib/rules/require-jsdoc"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run("require-jsdoc", rule, { | ||
valid: [ | ||
"var array = [1,2,3];\narray.forEach(function() {});", | ||
"/**\n @class MyClass \n*/\nfunction MyClass() {}", | ||
"/**\n Function doing something\n*/\nfunction myFunction() {}", | ||
"/**\n Function doing something\n*/\nvar myFunction = function() {};", | ||
"/**\n Function doing something\n*/\nObject.myFunction = function () {};", | ||
"var obj = { \n /**\n Function doing something\n*/\n myFunction: function () {} };", | ||
|
||
"/**\n @func myFunction \n*/\nfunction myFunction() {}", | ||
"/**\n @method myFunction\n*/\nfunction myFunction() {}", | ||
"/**\n @function myFunction\n*/\nfunction myFunction() {}", | ||
|
||
"/**\n @func myFunction \n*/\nvar myFunction = function () {}", | ||
"/**\n @method myFunction\n*/\nvar myFunction = function () {}", | ||
"/**\n @function myFunction\n*/\nvar myFunction = function () {}", | ||
|
||
"/**\n @func myFunction \n*/\nObject.myFunction = function() {}", | ||
"/**\n @method myFunction\n*/\nObject.myFunction = function() {}", | ||
"/**\n @function myFunction\n*/\nObject.myFunction = function() {}", | ||
"(function(){})();", | ||
|
||
"var object = {\n/**\n @func myFunction - Some function \n*/\nmyFunction: function() {} }", | ||
"var object = {\n/**\n @method myFunction - Some function \n*/\nmyFunction: function() {} }", | ||
"var object = {\n/**\n @function myFunction - Some function \n*/\nmyFunction: function() {} }", | ||
|
||
"var array = [1,2,3];\narray.forEach(function() {});", | ||
"var array = [1,2,3];\narray.filter(function() {});", | ||
"Object.keys(this.options.rules || {}).forEach(function(name) {}.bind(this));", | ||
"var object = { name: 'key'};\nObject.keys(object).forEach(function() {})" | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "var testFunction = function() {}", | ||
errors: [{ | ||
message: "Missing JSDoc comment.", | ||
type: "FunctionExpression" | ||
}] | ||
}, | ||
{ | ||
code: "function myFunction() {}", | ||
errors: [{ | ||
message: "Missing JSDoc comment.", | ||
type: "FunctionDeclaration" | ||
}] | ||
}, | ||
{ | ||
code: "var object = { myFunction: function() {} };", | ||
errors: [{ | ||
message: "Missing JSDoc comment.", | ||
type: "FunctionExpression" | ||
}] | ||
}, | ||
{ | ||
code: "var object = {};\nobject.testFunction = function() {}", | ||
errors: [{ | ||
message: "Missing JSDoc comment.", | ||
type: "FunctionExpression" | ||
}] | ||
} | ||
] | ||
}); |