Restrict yield outside generators in strict mode

* Move some yield/generic tests from compress/harmony.js to
  compress/yield.js
 * Adjust error messages to conform ecmascript standards
This commit is contained in:
Anthony Van de Gejuchte
2016-06-17 20:39:03 +02:00
committed by Richard van Velzen
parent 6b03b800b3
commit ca04508cd1
7 changed files with 208 additions and 123 deletions

View File

@@ -795,14 +795,14 @@ function parse($TEXT, options) {
function unexpected(token) {
if (token == null)
token = S.token;
token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
token_error(token, "SyntaxError: Unexpected token: " + token.type + " (" + token.value + ")");
};
function expect_token(type, val) {
if (is(type, val)) {
return next();
}
token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
token_error(S.token, "SyntaxError: Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
};
function expect(punc) { return expect_token("punc", punc); };
@@ -993,7 +993,7 @@ function parse($TEXT, options) {
var label = as_symbol(AST_Label);
if (label.name === "yield" && is_in_generator()) {
// Ecma-262, 12.1.1 Static Semantics: Early Errors
croak("Yield cannot be used as label inside generators");
token_error(S.prev, "SyntaxError: Yield cannot be used as label inside generators");
}
if (find_if(function(l){ return l.name == label.name }, S.labels)) {
// ECMA-262, 12.12: An ECMAScript program is considered
@@ -1873,9 +1873,12 @@ function parse($TEXT, options) {
expect("]");
return ex;
} else unexpected();
case "num":
case "string":
case "name":
if (tmp.value === "yield" && S.input.has_directive("use strict") && !is_in_generator()) {
token_error(tmp, "SyntaxError: Unexpected yield identifier inside strict mode");
}
case "string":
case "num":
case "operator":
case "keyword":
case "atom":
@@ -1915,6 +1918,9 @@ function parse($TEXT, options) {
if (!noerror) croak("Name expected");
return null;
}
if (is("name", "yield") && S.input.has_directive("use strict")) {
token_error(S.prev, "SyntaxError: Unexpected yield identifier inside strict mode");
}
var sym = _make_symbol(type);
next();
return sym;
@@ -2050,9 +2056,13 @@ function parse($TEXT, options) {
var maybe_assign = function(no_in) {
var start = S.token;
if (start.type == "name" && start.value == "yield" && is_in_generator()) {
next();
return _yield_expression();
if (start.type == "name" && start.value == "yield") {
if (is_in_generator()) {
next();
return _yield_expression();
} else if (S.input.has_directive("use strict")) {
token_error(S.token, "SyntaxError: Unexpected yield identifier inside strict mode")
}
}
if (start.type == "punc" && start.value == "(" && peek().value == ")") {