Skip to content

Commit

Permalink
Fix: function expression doc in call expression (fixes #4964)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Jan 18, 2016
1 parent 1e1b456 commit 5cd5429
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/util/source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ SourceCode.prototype = {
}

return parent && (parent.type !== "FunctionDeclaration") ? findJSDocComment(parent.leadingComments, parent.loc.start.line) : null;
} else if (node.leadingComments) {
return findJSDocComment(node.leadingComments, node.loc.start.line);
}

// falls through
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/rules/valid-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ ruleTester.run("valid-jsdoc", rule, {
"/**\n* Description\n* @override */\nfunction foo(arg1, arg2){ return ''; }",
"/**\n* Description\n* @inheritdoc */\nfunction foo(arg1, arg2){ return ''; }",
"/**\n* Description\n* @inheritDoc */\nfunction foo(arg1, arg2){ return ''; }",
{
code:
"call(\n" +
" /**\n" +
" * Doc for a function expression in a call expression.\n" +
" * @param {string} argName This is the param description.\n" +
" * @return {string} This is the return description.\n" +
" */\n" +
" function(argName) {\n" +
" return 'the return';\n" +
" }\n" +
");\n",
options: [{requireReturn: false}]
},
{
code:
"/**\n" +
Expand Down Expand Up @@ -329,6 +343,24 @@ ruleTester.run("valid-jsdoc", rule, {
],

invalid: [
{
code:
"call(\n" +
" /**\n" +
" * Doc for a function expression in a call expression.\n" +
" * @param {string} bogusName This is the param description.\n" +
" * @return {string} This is the return description.\n" +
" */\n" +
" function(argName) {\n" +
" return 'the return';\n" +
" }\n" +
");\n",
options: [{requireReturn: false}],
errors: [{
message: "Expected JSDoc for 'argName' but found 'bogusName'.",
type: "Block"
}]
},
{
code: "/** @@foo */\nfunction foo(){}",
errors: [{
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/util/source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,36 @@ describe("SourceCode", function() {

});

it("should get JSDoc comment for FunctionExpression in a CallExpression", function() {
var code = [
"call(",
" /** Documentation. */",
" function(argName) {",
" return 'the return';",
" }",
");"
].join("\n");

/**
* Check jsdoc presence
* @param {ASTNode} node not to check
* @returns {void}
* @private
*/
function assertJSDoc(node) {
var sourceCode = eslint.getSourceCode();
var jsdoc = sourceCode.getJSDocComment(node);
assert.equal(jsdoc.type, "Block");
assert.equal(jsdoc.value, "* Documentation. ");
}

var spy = sandbox.spy(assertJSDoc);

eslint.on("FunctionExpression", spy);
eslint.verify(code, {rules: {}}, filename, true);
assert.isTrue(spy.calledOnce, "Event handler should be called.");
});

it("should get JSDoc comment for node when the node is a FunctionDeclaration", function() {

var code = [
Expand Down

0 comments on commit 5cd5429

Please sign in to comment.