Permalink
Browse files

[[FIX]] Reset generator flag for each method definition

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 2444a0463e1a99d46e4afa50ed934c317265529d
Showing with 25 additions and 3 deletions.
  1. +5 −3 src/jshint.js
  2. +20 −0 tests/unit/parser.js
@@ -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);
@@ -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 === "[") {
@@ -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);
@@ -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.