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

bug: yield is not an unused expression #1672

Closed
remitbri opened this issue Jan 19, 2015 · 8 comments

Comments

Projects
None yet
3 participants
@remitbri
Copy link
Contributor

commented Jan 19, 2015

ESLint 0.12.0
with .eslintrc

{
  "ecmaFeatures" : {
    "generators": true
  }
}

If no-unused-expressions is enabled, the following is considered an error

function *generator() {
    yield "44";
    yield "55";
}

From what the newbie me sees, it seems it would be just a matter of modifying line 63 of lib/rules/no-unused-expressions.js to

!/^(?:Assignment|Call|New|Update|Yield)Expression$/.test(type) &&
@michaelficarra

This comment has been minimized.

Copy link
Member

commented Jan 19, 2015

Yeah, that should do it.

@remitbri

This comment has been minimized.

Copy link
Contributor Author

commented Jan 19, 2015

How should I proceed from there? I'm really new to proper testing, and if I update tests/lib/rules/no-unused-expressions.js to add "yield someValue" as a valid test, npm testfails ("Unexpected token yield") ; it seems odd to PR the rule fix only without the test also.

@michaelficarra

This comment has been minimized.

Copy link
Member

commented Jan 19, 2015

Since yield isn't allowed in ES5, you'll need to enable ES6 features in the parser for those tests with ecmaFeatures: { "generators": true }. See other tests for examples.

@remitbri

This comment has been minimized.

Copy link
Contributor Author

commented Jan 19, 2015

Thanks.
That I did,

        {
            code: "yield \"2\"",
            ecmaFeatures: { "generators": true }
        },

… but now, depending on what I add to the valid section (yield "2", yield someValue), I get an error 'Unexpected string' / 'Unexpected identifier'

@michaelficarra

This comment has been minimized.

Copy link
Member

commented Jan 19, 2015

Try using it in a generator. (function*(){ yield 0; }).

@remitbri

This comment has been minimized.

Copy link
Contributor Author

commented Jan 20, 2015

Thanks for your patient help.
Actually the anonymous generator didn't work (Unexpected token (), I had to use function* foo(){ yield 0; }

@michaelficarra

This comment has been minimized.

Copy link
Member

commented Jan 20, 2015

That's because the generator was in statement position. You could wrap the generator in parentheses to avoid the name if you wanted.

@remitbri

This comment has been minimized.

Copy link
Contributor Author

commented Jan 20, 2015

Hmm. It doesn't work ("Expected an assignment or function call and instead saw an expression."), unless I use (function*(){ yield 0; })(). I think it "looks" simpler if I just use a name.

@nzakas nzakas closed this in 42255d9 Jan 20, 2015

nzakas added a commit that referenced this issue Jan 20, 2015

Merge pull request #1684 from remitbri/patch-no-unused-expressions
Fix: no-unused-expressions rule incorrectly flagging yield (fixes #1672)

@eslint eslint bot locked and limited conversation to collaborators Feb 7, 2018

@eslint eslint bot added the archived due to age label Feb 7, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.