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

Warning when writing a generator function without any yield #13847

Closed
krryan opened this issue Feb 2, 2017 · 4 comments · Fixed by #30790
Closed

Warning when writing a generator function without any yield #13847

krryan opened this issue Feb 2, 2017 · 4 comments · Fixed by #30790
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@krryan
Copy link

krryan commented Feb 2, 2017

TypeScript Version: 2.2.0-dev.20170126

Code

export function* keyed<K, V>(values: Iterable<V>, toKey: (value: V) => K): IterableIterator<[K, V]> {
    for (const value of values) {
        return [toKey(value), value];
    }
}

Expected behavior:
Some kind of notice that this generator function never actually yields anything.

Actual behavior:
Compiles, and runs for that matter, without error, just silently yielding nothing.

I just spent a couple of hours tracking down a bug here, where return was accidentally used instead of yield. I'll admit I'm only starting to learn about generator functions and their uses, so it may be entirely possible that there are generator functions that want to return, rather than yield, in some cases, but my understanding is that every generator function should yield at some point.

Could we get a compiler warning for something like this? I'm not looking for a halt detector or anything :P just some kind of warning like "No 'yield' in reachable code," or something.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 2, 2017
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Mar 14, 2017
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Mar 14, 2017
@RyanCavanaugh
Copy link
Member

Accepting PRs for the rule: a generator function with a return statement must not have zero yield return statements (please make the error message less spec-like!)

@mhegazy
Copy link
Contributor

mhegazy commented Mar 20, 2017

#14043 has a valid example, a default implementation for a generator. the alternative would be to implement a function that returns the generator protocol, but that seems overly verbose.

@RyanCavanaugh RyanCavanaugh added the Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". label Aug 12, 2017
@mhegazy mhegazy removed the Help Wanted You can do this label Feb 20, 2018
@mhegazy mhegazy removed this from the Community milestone Feb 20, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2018

In light of the discussion about errors vs warnings we should instead move this to tslint.

@mhegazy mhegazy added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". labels Feb 20, 2018
@rbuckton
Copy link
Member

rbuckton commented Apr 6, 2019

Note: #30790 will address this only in cases where the return type is explicitly specified as Generator<T, void>, as returning the value above would not be assignable to void.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants