Fix newline handling after yield

YieldExpressions can only be defined as:
 * `yield`
 * `yield` [no nlb] AssignmentExpression
 * `yield` [no nlb] `*` AssignmentExpression
This commit is contained in:
Anthony Van de Gejuchte
2016-06-11 21:41:16 +02:00
parent fa29344781
commit b0555a123a
2 changed files with 29 additions and 11 deletions

View File

@@ -1174,17 +1174,23 @@ function parse($TEXT, options) {
var has_expression = true; var has_expression = true;
var tmp; var tmp;
// Get expression behind yield, default to value `undefined` stored as `null` in ast // Attempt to get expression or star (and then the mandatory expression)
// Expression must start on same line, yield* has a mandatory expression // behind yield on the same line.
if (is("operator", "*")) { //
star = true; // If nothing follows on the same line of the yieldExpression,
next(); // it should default to the value `undefined` for yield to return.
if (S.token.nlb) { // In that case, the `undefined` stored as `null` in ast.
unexpected(S.prev); //
} // Note 1: It isn't allowed for yield* to close without an expression
} else if (can_insert_semicolon() || // Note 2: If there is a nlb between yield and star, it is interpret as
// yield <explicit undefined> <inserted automatic semicolon> *
if (can_insert_semicolon() ||
(is("punc") && PUNC_AFTER_EXPRESSION(S.token.value))) { (is("punc") && PUNC_AFTER_EXPRESSION(S.token.value))) {
has_expression = false; has_expression = false;
} else if (is("operator", "*")) {
star = true;
next();
} }
return new AST_Yield({ return new AST_Yield({

View File

@@ -20,8 +20,20 @@ describe("Yield", function() {
assert.throws(test, expect); assert.throws(test, expect);
}); });
it("Should not allow yield* followed by a newline in generators", function() { it("Should not allow yield* followed by a semicolon in generators", function() {
var js = "function* test() {yield*\n123;}"; var js = "function* test() {yield*\n;}";
var test = function() {
UglifyJS.parse(js);
}
var expect = function(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "Unexpected token: punc (;)";
}
assert.throws(test, expect);
});
it("Should not allow yield with next token star on next line", function() {
var js = "function* test() {yield\n*123;}";
var test = function() { var test = function() {
UglifyJS.parse(js); UglifyJS.parse(js);
} }