site-www icon indicating copy to clipboard operation
site-www copied to clipboard

Tour page has examples that get lint warning: Avoid using `forEach` with a function literal.dartavoid_function_literals_in_foreach_calls

Open jpangburn opened this issue 4 years ago • 6 comments

Page URL

https://dart.dev/guides/language/language-tour

Page source

No response

Describe the problem

I use the recommended lints on VSCode for Dart (default setting) and I see this lint Avoid using 'forEach' with a function literal.dartavoid_function_literals_in_foreach_calls for the following code:

const list = ['apples', 'bananas', 'oranges']; list.forEach((item) { print('${list.indexOf(item)}: $item'); });

And in the same section, the following code gets the same warning:

list.forEach( (item) => print('${list.indexOf(item)}: $item'));

Expected fix

I think recommended code should pass the recommended linter. For these examples, maybe change to for..in?

Additional context

No response

jpangburn avatar Nov 09 '21 00:11 jpangburn

Thanks for opening an issue @jpangburn. I agree it would be nice for the code present to follow the recommended lints.

Some of the uses of forEach in the language tour are pretty engrained so this won't be exactly straightforward. In this particular example, it is used to illustrate anonymous functions, so for ... in would not suffice. That isn't to say we can't think of alternative examples for some of the use cases which also illustrate the intended features well, but we'll need to make sure they also work well for explanatory purposes.

Thanks again! I'll make sure to take a look at the affected examples and give them some extra though.

parlough avatar Nov 09 '21 08:11 parlough

Yeah, as I read further through the tour, I see there's a lot of these. Whoever wrote the tour likes anonymous functions. Personally, I like them too and don't see what the harm is for appropriate use cases (other than obvious things like no early exit, no return values, etc.) but I also like linters and following style for a language for more consistent code in a project with multiple developers.

At this point, I'm not sure what the right way forward is either. The tour author apparently disagrees with the recommended linter about style so... 🤷‍♂️

jpangburn avatar Nov 09 '21 16:11 jpangburn

Many of the code samples in the tour predate the style guide by years, so I think we should follow the recommended style (except when it interferes with legibility or the point the tour is trying to make).

kwalrath avatar Nov 09 '21 21:11 kwalrath

Is this issue still open? May I work on it? Can someone help me as I am new to open source contribution?

pree-T avatar Jan 13 '22 19:01 pree-T

@pree-T The issue is still open. The problem seems to be that it's not super clear what the right answer is. As @kwalrath said, many tour examples are pretty old and were created long before the linter's recommended style. So is the right answer to remove these examples that violate style? Change them to other things that don't violate style? Or leave them as-is because they demonstrate some useful concept that might be out of favor by the current recommended style?

Not being an expert in Dart, I felt like I should leave it to people who know more about it. If that's you, but you're just new to open source contribution, these links may help. I got this from the top of the README.md for this repository "We welcome contributions, and we're first-timer friendly!"

jpangburn avatar Jan 13 '22 20:01 jpangburn

@pree-T @jpangburn's assessment is accurate. Plus you might want to start on a simpler issue (one that doesn't require you to edit multiple files) for your first site-www contribution.

kwalrath avatar Jan 24 '22 22:01 kwalrath