dart-lang / dart_style

An opinionated formatter/linter for Dart code
https://pub.dev/packages/dart_style
BSD 3-Clause "New" or "Revised" License
645 stars 118 forks source link

Indentation of curried functions #957

Open ghivert opened 3 years ago

ghivert commented 3 years ago

Hi !

I really like the formatter, but there’s some cases where I wonder if it’s by design or not. I wonder mainly about curried functions. I’m coming mainly from functional programming (Haskell and stuff) and it’s usual to have something like this:

// In JavaScript for example
const fun = arg1 => arg2 => arg3 => {
  // body...
}
// In Dart for example
fun(arg1) => (arg2) => (arg3) {
  // body...
};

But when doing the latter, it’s indented like this:

fun(arg1) => (arg2) => (arg3) {
    final var = 1;
    return var;
  };

It feels like there’s two unneeded spaces in the function body. With indent guides and other stuff, it renders strangely. Is it on purpose or because of some rules on closures? I know we can do it differently, like:

fun(arg1, arg2, arg3) {}
// And then use a closure at the appropriate place
onEvent: (event) => fun('fromScope', 'alsoFromScope', event);

// Instead of:

fun(arg1, arg2) => (arg3) {};
// And then
onEvent: fun('fromScope', 'alsoFromScope');

It’s a similar result in the end, but it’s a slightly different code. What do you think about this?

munificent commented 3 years ago

Is it on purpose or because of some rules on closures?

It's a consequence of the general rule that the body of a function declaration using => is indented relative to the declaration itself. Curried style is really uncommon in Dart, so the formatter doesn't have any special rules to handle it better.

ghivert commented 3 years ago

Do you think we could add this special case to the formatter ?

munificent commented 3 years ago

I think it's unlikely. Nested functions are a difficult corner of the formatter's rules and it's hard to change how they are handled without producing worse output in other cases. If this was a common style, it would be worth the effort, but I almost never see Dart code like this. I'll leave the issue open and if I get time I can investigate it, but it likely won't be a high priority.

yelliver commented 2 years ago

I think this issue is a special case of my general issue https://github.com/dart-lang/dart_style/issues/1083