Skip to content

Commit

Permalink
[[FIX]] Allow RegExp literal as yield operand (#3011)
Browse files Browse the repository at this point in the history
Extend the lexer to recognize RegExp literals which immediately follow
the `yield` operator.
  • Loading branch information
jugglinmike authored and rwaldron committed Sep 22, 2016
1 parent a2b3881 commit b646aea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lex.js
Expand Up @@ -1696,7 +1696,7 @@ Lexer.prototype = {
}

if (type === "(identifier)") {
if (value === "return" || value === "case" ||
if (value === "return" || value === "case" || value === "yield" ||
value === "typeof" || value === "instanceof") {
this.prereg = true;
}
Expand Down
52 changes: 51 additions & 1 deletion tests/unit/parser.js
Expand Up @@ -6465,7 +6465,6 @@ exports["test 'yield' in invalid positions"] = function (test) {
testRun.test("function* g() { yield* ? null : null; }", { esversion: 6, expr: true });
testRun.test("function* g() { (yield ? 1 : 1); }", { esversion: 6, expr: true });
testRun.test("function* g() { (yield* ? 1 : 1); }", { esversion: 6, expr: true });
testRun.test("function* g() { yield / 1; }", { esversion: 6, expr: true });
TestRun(test)
.addError(1, "Unclosed regular expression.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
Expand Down Expand Up @@ -6647,6 +6646,57 @@ exports["test for line breaks with 'yield'"] = function (test) {
test.done();
};

// Regression test for gh-2956
exports.yieldRegExp = function (test) {
var code = [
"function* g() {",
" yield /./;",
" yield/./;",
" yield",
" /./;",
" yield /* comment */;",
" yield /* comment *//./;",
" yield 1 / 1;",
"}"
];

TestRun(test)
.addError(1, "'function*' is only available in ES6 (use 'esversion: 6').")
.addError(2, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(3, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(4, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(4, "Missing semicolon.")
.addError(5, "Expected an assignment or function call and instead saw an expression.")
.addError(6, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(7, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(8, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.test(code);

TestRun(test)
.addError(4, "Missing semicolon.")
.addError(5, "Expected an assignment or function call and instead saw an expression.")
.test(code, { esversion: 6 });

code = [
"function* g() {",
" yield / 2;",
"}"
];

TestRun(test)
.addError(1, "'function*' is only available in ES6 (use 'esversion: 6').")
.addError(2, "Unclosed regular expression.")
.addError(2, "Unrecoverable syntax error. (66% scanned).")
.test(code);

TestRun(test)
.addError(2, "Unclosed regular expression.")
.addError(2, "Unrecoverable syntax error. (66% scanned).")
.test(code, { esversion: 6 });

test.done();
};

exports.unreachable = {
"regression for GH-1227": function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1227.js", "utf8");
Expand Down

0 comments on commit b646aea

Please sign in to comment.