eslint: Falsy error in `array-callback-return`

Returning something inside a try/catch block inside arr.map will sometimes falsely report an error. The following code should be perfectly valid (since there’s an process.exit() which should imply that it should return or exit). Currently I have to assign my return to a variable and return it outside the try/catch.

arr.map(() => {
    try {
        return require('foo');
    } catch (err) {
        console.error(err);
        process.exit(1);
    }
});

Using ESLint version 2.9.0 and the failing rule is array-callback-return.

Ref https://github.com/imagemin/imagemin-cli/pull/6#discussion_r61607411.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 19 (14 by maintainers)

Most upvoted comments

I have added node/process-exit-as-throw rule experimentally to eslint-plugin-node. If this rule is turned on, ESLint’s code path analysis will address process.exit() as throw. This might help this issue.

Just want to go on the record and clarify my position: I don’t believe a rule that isn’t already runtime-specific should become runtime-specific by handling runtime-specific cases. That was what I meant in my initial reply. On May 1, 2016 8:41 AM, “Sindre Sorhus” notifications@github.com wrote:

I don’t really see the problem with it being Node.js specific. ESLint already have Node.js and browser specific rules. The use of process.exit() is a very common thing in CLI apps and workers. Maybe if the ESLint plugin API exposed a method to mark something as unreachable, in similar manner to markVariableAsUsed(), we could fix this ourselves. The alternative is having to reimplement this rule in userland, which would be a shame for such a small tweak.

My personal recommendation would be to use `// eslint-disable-line array-callback-return for just this invocation.

This issue wouldn’t have been opened if that was something we wanted. Not interested in littering our code with a myriad of eslint-disable statements.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/eslint/eslint/issues/6014#issuecomment-216042752