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

enhance require-jsdoc to recognize function expressions #5867

Closed
vobu opened this issue Apr 15, 2016 · 23 comments · Fixed by #9395
Closed

enhance require-jsdoc to recognize function expressions #5867

vobu opened this issue Apr 15, 2016 · 23 comments · Fixed by #9395
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules

Comments

@vobu
Copy link

vobu commented Apr 15, 2016

ESLint-version: 1.10.3, default parser

configuration:

"require-jsdoc":
                [
                        2,
                        {
                                "require":
                                {
                                        "FunctionDeclaration": true,
                                        "MethodDefinition": false,
                                        "ClassDeclaration": false
                                }
                        }
                ],

Running eslint (from a grunt task), I would expect this to cause an error:

module.exports.foo = function foo() {
    return 'bar';
}

Instead, it passes through fine.

So that's why I would propose an enhancement to the require-jsdoc rule to also recognise missing JSDoc blocks in functional expressions such as the above.

All of the below should pass through fine:

/**
* some description
* 
* @returns {string}
*/
module.exports.foo = function foo() {
    return 'bar';
}
/**
 * some description
 *
 * @returns {string}
 */
foo: function() {
    return 'bar';
}
@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Apr 15, 2016
@BYK
Copy link
Member

BYK commented Apr 15, 2016

I'm 👍 to the suggestion.

@BYK BYK added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Apr 15, 2016
@nzakas
Copy link
Member

nzakas commented Apr 15, 2016

This is trickier than it seems, which is why it is not done yet. :)

We can't just flag any function expression, because it might not be considered a method. For instance, should this be flagged?

var foo = function() {};

How about this?

somethingFunction({
    apply: function() {}
});

I'm not opposed to exploring this, I just think we need to be very specific about the patterns we should and should not flag because it's not as simple as requested.

@BYK
Copy link
Member

BYK commented Apr 16, 2016

@nzakas - I guess if we find a reliable way to detect in-place callback definitions which I think is possible I think all others should be documented. We may have sub-options, say for methods to allow the rule behave a bit more relaxed. Makes sense?

@qfox
Copy link

qfox commented Apr 18, 2016

If I got it right, we should take a look at https://github.com/jscs-dev/jscs-jsdoc/blob/master/lib/rules/validate-jsdoc/enforce-existence.js with their options (since this rule was bundled into jscs package).

We got there 'exports' and 'paramless-procedures': jscs-dev/jscs-jsdoc#139.
'exports' stands for export some function(){} and module.exports = function(), and paramless-procedures for functions without params and return statements.

@nzakas nzakas added this to the JSCS Compatibility milestone Apr 18, 2016
@nzakas
Copy link
Member

nzakas commented Apr 18, 2016

@zxqfox nice! We should be able to reuse that detection logic. Can you list here (or point to docs) that explain the various options?

@BYK yes, I think if we can reuse some of what's in JSCS, we probably have a good shot.

@qfox
Copy link

qfox commented Apr 18, 2016

Yeah, sure.

By default it reports any function without bound jsdoc-block except anonymous functions. It tries to find comment-block on the previous (or skip one empty line, except file start) with the same indent as statement with target function.

E.g.:

/**
 * File overview
 */

var fn = function() {
}
/* nothing here */

/**
 * Function description
 */

var fn = function() {
}

And next to it there are several filters:

I'm not sure these are the best ones. But it's what we have in jscs-jsdoc atm.

@BYK
Copy link
Member

BYK commented Apr 21, 2016

@zxqfox - Wanna give this a shot? Otherwise I'll go ahead and try to implement it.

@qfox
Copy link

qfox commented Apr 21, 2016

Not now :-( Go ahead!

@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Apr 29, 2016
@nzakas
Copy link
Member

nzakas commented Apr 29, 2016

Marking as accepted because we need these changes for JSCS compat.

@doberkofler
Copy link
Contributor

+1

@joelbarker2011
Copy link

Somewhat related is the situation of functions within objects. I expected the following to be flagged by the require-jsdoc option, but it wasn't:

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

export default {
  foo: false,

  // these are not the docs you are looking for
  bar: function () {
    this.foo = true;
  },

  // sorry, but the docs are in another castle
  baz() {    // ES6-style method shortcut
    this.foo = false;
  },
};

@nzakas
Copy link
Member

nzakas commented Jul 29, 2016

@zxqfox are you planning on addressing this?

@qfox
Copy link

qfox commented Jul 29, 2016

Wow. Yeah, I'll try to give this a shot!

@mightypenguin
Copy link

Pinging this so people know there is still interest.
This would bring my documentation to 90+%.

All I care about is:
A)
var myfunc = function() {}

B)
var object = {
myfunc: function() {}
}

@chazmuzz
Copy link

chazmuzz commented Sep 11, 2016

+1 I'm looking for it to flag returning a named function so that it works with my AMD solution. Currently the following code is OK even though I have require-jsdoc:

define(function() {
  return function Foo() {
    // code
  }
})

However this similar code fails linting:

define(function() {
  function Foo() {
    // code
  }
  return Foo;
})

@sunjay
Copy link

sunjay commented Sep 27, 2016

The code I would like this to work for looks like this:

const Actions = {
  actions: new Set(),
  register(name) {
    ...
  },
};

Similar to what everyone else is presenting except instead of register: function(name) { I'm using register(name) { which is allowed in ES6.

@razbakov
Copy link

+1

@platinumazure
Copy link
Member

Could we augment the rule to support selectors? That way, people could enforce documentation on whatever function node types they want to (including only ones that are part of assignments, or part of object expressions, or...).

@not-an-aardvark
Copy link
Member

If I'm not mistaken, selectors wouldn't be an immediate solution to this problem because we would still have to match the JSDoc comment to the function expression itself. (This issue isn't asking for more configurability -- it's asking for functionality that isn't implemented yet.)

@kaicataldo
Copy link
Member

I started looking at this, but it doesn't look like we actually decided on an implementation (it was accepted because it was needed for JSCS compatibility). My understanding is that we want options to enforce JSDocs on the following:

/*
 * JSDoc Block
 */
const foo = function() {}

const foo = {
  /*
   * JSDoc Block
   */
  bar: function() {},

  /*
   * JSDoc Block
   */
  baz() {}
}

Assuming my understanding is correct (let me know if it's not!), it seems like these would need to be two new options (turning them on by default would be breaking and could potentially cause a lot of errors).

@eslint/eslint-team Let's hash this out and I'll get to work!

@not-an-aardvark
Copy link
Member

Could you clarify what the two options would do? I might be missing something, but I only see a need for one new option (to control whether function expressions are checked).

@kaicataldo
Copy link
Member

kaicataldo commented Oct 5, 2017

Sorry that wasn't clear. After my initial reading, seemed like the two asks are to be able to require JSDocs for function expressions in assignments and object methods (both function expressions and object method shorthand syntax). I feel like it would be counter-intuitive to lump the object shorthand syntax in with a function expression option, but if everyone else is fine with that, I won't object.

More concerned with getting this implemented for JSCS compatibility than anything else!

@kaicataldo
Copy link
Member

I'm going to implement this with a FunctionExpression option and we can decide later what we think.

ilyavolodin pushed a commit that referenced this issue Oct 14, 2017
* Update: Add FunctionExpression to require-jsdoc (fixes #5867)

* Bug fix: function expression as computed property
@aladdin-add aladdin-add moved this from Todo to Done in JSCS Compatibility Oct 15, 2017
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Apr 13, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules
Projects
No open projects
Development

Successfully merging a pull request may close this issue.