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

require-throws triggered by arrow function without body #597

Closed
homer0 opened this issue Jul 1, 2020 · 3 comments
Closed

require-throws triggered by arrow function without body #597

homer0 opened this issue Jul 1, 2020 · 3 comments

Comments

@homer0
Copy link

homer0 commented Jul 1, 2020

Expected behavior

There shouldn't be any errors on a fully documented function (arrow expression) without a body.

Actual behavior

The plugin is telling me that I'm missing a @throws declaration when there's no throw.

ESLint Config

'jsdoc/require-throws': 'error',

(I have a lot of other rules for the Airbnb preset, but I'm almost sure it's not affecting this issue)

ESLint sample

/**
 * @typedef {Function} Add
 * @param {number} n Something.
 * @returns {number}
 */

/**
 * Something.
 *
 * @param {number} base  Something.
 * @returns {Add}
 */
const sum = (base) => (n) => base + n;

Environment

  • Node version: 10.20
  • ESLint version 7.3.1
  • eslint-plugin-jsdoc version: 28.6.0

I believe the issue is on hasThrowValue, when the node is an ArrowFunctionExpression, it checks for expression... that's always true, and unrelated to a throw, if the function doesn't have a body.

Writing sum this way doesn't trigger the rule:

const sum = (base) => {
  return (n) => base + n;
};

But it triggers arrow-body-style :P.

@gajus
Copy link
Owner

gajus commented Jul 1, 2020

🎉 This issue has been resolved in version 28.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label Jul 1, 2020
@brettz9
Copy link
Collaborator

brettz9 commented Jul 1, 2020

Was apparently a copy-paste bug (from the hasReturnValue function where expression on a function successfully denotes an (arrow) return value, but does not denote a throw statement).

Now fixed.

I also tweaked the rule to avoid something like the following reporting errors:

const nested = () => () => {throw new Error('oops');};

Since nested does not directly throw, its docs should not be indicating that it throws.

Note that we do not currently catch:

      const nested = () => {
        function a () {
          throw new Error('oops');
        }
        a();
      };

...nor does our current comment-detection approach (getJSDocComment in source) check comment blocks directly above expressions, so one couldn't get the nested block to be reported (though we probably should be checking for such blocks):

const nested = () => 
  /**
   *
   */
  () => {throw new Error('oops');};

@homer0
Copy link
Author

homer0 commented Jul 2, 2020

@brettz9 thank you so much :D!! Everything works now, and the change you made for the nested scenario makes a lot of sense too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants