Search code examples
javascriptlambdaanonymous-functionjslinteslint

Javascript lambda/anonymous function length warning in eslint or jslint


For javascript, is there an eslint (or even jslint) setting to give a warning for a given length (eg: longer than 5 lines) of anonymous/lambda functions, especially callbacks?

I'm trying to see if there is a way to enforce cleaner code than this using lint:

  ...
  var a = foo(b, function(c, cb1) {
    // dozens of lines of code
    cb1(d, function(e, cb2){
      // dozens of lines of code
      cb2(f, function(g, cb3) {
        // dozens of lines of code
        cb3(...);
        // dozens of lines of code
      });
    });
  });
  ...

I do know that the number of nested functions can be limited, but I think the length of these could also be as short as possible.


Solution

  • You can use the eslint max-statements option to enforce a certain number of statements per function.

    The max-depth and max-nested-callbacks that Jordan mentioned are useful to prevent nesting conditions and callbacks too deeply. You may also be interested in limiting complexity, meaning how many different results a function can have.