-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generator methods in object literals, e.g.
o = {*g() {...}}
- Loading branch information
1 parent
8b45873
commit 6d7b642
Showing
7 changed files
with
168 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
rhino/src/main/java/org/mozilla/javascript/ast/GeneratorMethodDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.ast; | ||
|
||
import org.mozilla.javascript.Token; | ||
|
||
/** | ||
* AST node for a generator method definition in an object literal, i.e. `*key() {}` in an object | ||
* literal. | ||
*/ | ||
public class GeneratorMethodDefinition extends AstNode { | ||
|
||
private AstNode methodName; | ||
|
||
public GeneratorMethodDefinition(int pos, int len, AstNode methodName) { | ||
super(pos, len); | ||
setType(Token.MUL); | ||
setMethodName(methodName); | ||
} | ||
|
||
public AstNode getMethodName() { | ||
return methodName; | ||
} | ||
|
||
public void setMethodName(AstNode methodName) { | ||
assertNotNull(methodName); | ||
this.methodName = methodName; | ||
methodName.setParent(this); | ||
} | ||
|
||
@Override | ||
public String toSource(int depth) { | ||
return makeIndent(depth) + '*' + methodName.toSource(depth); | ||
} | ||
|
||
/** Visits this node, then the name. */ | ||
@Override | ||
public void visit(NodeVisitor v) { | ||
if (v.visit(this)) { | ||
methodName.visit(v); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/src/test/java/org/mozilla/javascript/tests/es6/GeneratorMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.mozilla.javascript.tests.es6; | ||
|
||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.drivers.LanguageVersion; | ||
import org.mozilla.javascript.drivers.RhinoTest; | ||
import org.mozilla.javascript.drivers.ScriptTestsBase; | ||
|
||
@RhinoTest("testsrc/jstests/es6/generator-method.js") | ||
@LanguageVersion(Context.VERSION_ES6) | ||
public class GeneratorMethodTest extends ScriptTestsBase {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
load("testsrc/assert.js"); | ||
|
||
(function generatorAsShortHandMethod() { | ||
const o = { | ||
h() {}, | ||
*g() { | ||
yield 1; | ||
yield 2; | ||
} | ||
}; | ||
|
||
const iter = o.g(); | ||
|
||
let next = iter.next(); | ||
assertEquals(1, next.value); | ||
assertEquals(false, next.done); | ||
|
||
next = iter.next(); | ||
assertEquals(2, next.value); | ||
assertEquals(false, next.done); | ||
|
||
next = iter.next(); | ||
assertEquals(undefined, next.value); | ||
assertEquals(true, next.done); | ||
})(); | ||
|
||
"success"; |
Oops, something went wrong.