Skip to content

Rule proposal: Ensure yield on call to generator function #83

Open
@Bartel-C8

Description

@Bartel-C8

As proposed, but rejected due that Generator functions deemed a niche use-case: typescript-eslint/typescript-eslint#7749
And apparently proposed before: typescript-eslint/typescript-eslint#1068

Description

My suggested rule would check that every call to a generator, inside another generator is called with yield

Fail Cases

function* parentGenerator(){
  console.log("parentGenerator:: start");
  childGeneratorOne();
  yield* childGeneratorTwo();
  console.log("parentGenerator:: end");
}

function* childGeneratorOne(){
  console.log("childGeneratorOne:: start");
}

function* childGeneratorTwo(){
  console.log("childGeneratorTwo:: start");
}

const itr = parentGenerator();
console.log(itr.next());

Pass Cases

function* parentGenerator(){
  console.log("parentGenerator:: start");
  yield* childGeneratorOne();
  yield* childGeneratorTwo();
  console.log("parentGenerator:: end");
}

function* childGeneratorOne(){
  console.log("childGeneratorOne:: start");
}

function* childGeneratorTwo(){
  console.log("childGeneratorTwo:: start");
}

const itr = parentGenerator();
console.log(itr.next());

Additional Info

In the failing case, the childGeneratorOne is never called, which you won't expect if you miss that yield is lacking...

Was suggested before eslint/eslint#5193 but, as described in the issue, type info is needed to make this work.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions