A generator function shall contain a yield statement. #1123

Closed
Raynos opened this Issue Jun 6, 2013 · 16 comments

Projects

None yet

10 participants

@Raynos
Raynos commented Jun 6, 2013

a generator that only contains a return statement is probably a valid generator

/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, undef:true, unused:true, curly:true, browser:true, devel:true, esnext:true, indent:4, maxerr:50 */
var of, run;

run(function *() {
    return of(5);
});

The above currently errors complaining about lack of yields

@rwaldron
Member
rwaldron commented Jun 6, 2013

GeneratorMethod : * PropertyName( StrictFormalParameters ) { FunctionBody }
and
GeneratorDeclaration :function * BindingIdentifier( FormalParameters ) { FunctionBody }
and
GeneratorExpression : function * BindingIdentifier_opt( FormalParameters ) { FunctionBody }

  • It is a Syntax Error if FunctionBody Contains YieldExpression is false.

The static semantic rule "Contains" is defined in 5.3 Static Semantic Rules of ES6 rev15

@Raynos
Raynos commented Jun 6, 2013

@rwldrn I checked the above code using just return and it works in v8 used in node v0.11.2.

Is there an explanation of why generators that only contain return is a bad thing?

For reference the "It is a Syntax Error if FunctionBody Contains YieldExpression is false." is defined in 13.4 in ES6 rev15

@dherman
dherman commented Jun 6, 2013

That is a bug in the ES6 spec. I'll make sure we get it fixed. There's nothing wrong with a generator function with no yields in it.

@rwaldron
Member
rwaldron commented Jun 6, 2013

Correction filed here: https://bugs.ecmascript.org/show_bug.cgi?id=1551

Thanks for pressing the issue!

@Raynos
Raynos commented Jun 6, 2013

@dherman sweet !

@usrbincc
usrbincc commented Jun 8, 2013

Sorry to piggyback on this, but I thought I'd ask about the other static semantic check in the same section:

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-13.4

YieldExpression : yield YieldDelegator opt AssignmentExpression

It is a Syntax Error if AssignmentExpression Contains YieldExpression.

This seems like a little bit of an artificial limitation as well, since the semantics are unambiguous and well-defined.

node --harmony <<"END"
function* G() {
  yield yield 42;
}
var g = G();
console.log(g.next());
console.log(g.send(24));
console.log(g.send('will return { done: true }'));
END

# spidermonkey jsshell
js <<"END"
function G() {
  yield (yield 42);
}
var g = G();
print(g.next());
print(g.send(24));
print(g.send('will throw StopIteration'));
END
@rwaldron
Member
rwaldron commented Jun 8, 2013

@usrbincc can you file something here: https://bugs.ecmascript.org/post_bug.cgi as I'm not sure how to answer this question

@usrbincc

I ended up just adding an extra message to https://bugs.ecmascript.org/show_bug.cgi?id=1283 .

I don't know if I really added anything, but I went ahead and added something anyway.


I'm really thinking that there would be a lot less friction to handling bugs in the spec (both for bug reporters and the spec maintainers) if it were hosted on github. Since the edit format of the official spec is .docx (I think?), I can understand the difficulties that might be involved.

But switching to a more github-friendly format certainly isn't a complete pipe dream. I mean, you could always convert the source xml (hopefully with minimal formatting loss) and use that (maybe TeX? or some lightweight markup language?) as the base format for converting to pdf and html.

@qubyte
qubyte commented Dec 22, 2013

I'm seeing this a lot with Koa using Regenerator via gnode. In Koa middleware, it seems very common to contain only implicit returns and no yields.

@qubyte qubyte added a commit to qubyte/jshint that referenced this issue Dec 24, 2013
@qubyte qubyte Remove checks for generator functions without yield.
Generators without a yield are valid, making this check redundant.

For example:

    function *() {} // This is useless but valid.

This commit simply removes the check, updates messages, and removes
a unit test for the check.

References:

    Closes #1123
d682508
@gebrits
gebrits commented Dec 30, 2013

+1, Getting this a lot in Koa

@qubyte qubyte added a commit to qubyte/jshint that referenced this issue Jan 3, 2014
@qubyte qubyte Remove checks for generator functions without yield.
Generators without a yield are valid, making this check too strict.

For example:

    function *() {} // This is useless but valid.

This commit makes the check optional (`noyield` switches the check
off), adds a unit test for the new switch, and makes the error for
unyielding generators a warning.

References:

    Closes #1123
d68162c
@qubyte qubyte added a commit to qubyte/jshint that referenced this issue Jan 3, 2014
@qubyte qubyte Make checks for generator functions without yield optional.
Generators without a yield are valid, making this check too strict.

For example:

    function *() {} // This is useless but valid.

This commit makes the check optional (`noyield` switches the check
off), adds a unit test for the new switch, and makes the error for
unyielding generators a warning.

References:

    Closes #1123
a92b093
@valueof valueof added a commit that referenced this issue Jan 3, 2014
@qubyte qubyte Issue #1123: Make checks for generator functions without yield optional
Signed-off-by: Anton Kovalyov <anton@kovalyov.net>
85b5b59
@valueof
Member
valueof commented Jan 3, 2014

Fixed.

@valueof valueof closed this Jan 3, 2014
@jugglinmike jugglinmike added a commit to jugglinmike/jshint that referenced this issue Oct 21, 2014
@qubyte qubyte Issue #1123: Make checks for generator functions without yield optional
Signed-off-by: Anton Kovalyov <anton@kovalyov.net>
ad343ab
@monolithed
'use strict';

export default {
    /**
     * entries
     *
     * @param {Object} object
     * @return {Generator}
     */
     * entries (object) {
        for (let key of Object.keys(object)) {
            yield [key, object[key]];
        }
    }
};
   tools/functional.js
     37 |    },
             ^ A generator function shall contain a yield statement.
     65 |    }
             ^ A generator function shall contain a yield statement.

"grunt-contrib-jshint": "^0.11.2",

@artin-phares

Receiving 'A generator function shall contain a yield statement' in jshint@2.8.0
Regression?

@kilianc
kilianc commented Jun 26, 2015

v2.0.8 here still getting the warning

@kilianc
kilianc commented Jun 26, 2015

found: 37757f0#diff-91ba5f4df12e1b6b0629662164b3f14eR17

we need to use noyeld option, not sure why but at least I can turn it off.

@artin-phares

yep, I'm using "noyield": true to get rid of this warning too
no idea why generator function with no yields treat as a problem again...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment