Skip to content

Commit

Permalink
[[FIX]] Reset generator flag for each method definition
Browse files Browse the repository at this point in the history
This patch fixes the handling of non-generator method after generator method.
For examle:

    var a = {
      *gen() {
        yield 1;
      },
      non_gen() {
      }
    };

Here, non_gen was treated as a generator function, since generator flag `g`
wasn't reset to false when `*` wasn't found before method name.

Closes #2388
Closes #2389
  • Loading branch information
arai-a authored and caitp committed May 15, 2015
1 parent ab12dfb commit 2444a04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/jshint.js
Expand Up @@ -3216,7 +3216,7 @@ var JSHINT = (function() {

(function(x) {
x.nud = function() {
var b, f, i, p, t, g, nextVal;
var b, f, i, p, t, isGeneratorMethod = false, nextVal;
var props = {}; // All properties, including accessors

b = state.tokens.curr.line !== startLine(state.tokens.next);
Expand Down Expand Up @@ -3281,7 +3281,9 @@ var JSHINT = (function() {
warning("W104", state.tokens.next, "generator functions");
}
advance("*");
g = true;
isGeneratorMethod = true;
} else {
isGeneratorMethod = false;
}

if (state.tokens.next.id === "[") {
Expand All @@ -3301,7 +3303,7 @@ var JSHINT = (function() {
if (!state.inESNext()) {
warning("W104", state.tokens.curr, "concise methods");
}
doFunction({ type: g ? "generator" : null });
doFunction({ type: isGeneratorMethod ? "generator" : null });
} else {
advance(":");
expression(10);
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/parser.js
Expand Up @@ -6584,3 +6584,23 @@ exports.functionKeyword = function (test) {

test.done();
};

exports.nonGeneratorAfterGenerator = function (test) {
var run;
var code = [
'var obj = {',
' *gen() {',
' yield 1;',
' },',
// non_gen shouldn't be parsed as a generator method here, and parser
// shouldn't report an error about a generator without a yield expression.
' non_gen() {',
' }',
'};'
];

run = TestRun(test);
run.test(code, { esnext: true });

test.done();
};

0 comments on commit 2444a04

Please sign in to comment.