fix parse and output of yield (#2690)

fixes #2689
This commit is contained in:
Alex Lam S.L
2017-12-30 03:27:26 +08:00
committed by GitHub
parent 53600e9869
commit 725aac8b46
4 changed files with 111 additions and 88 deletions

View File

@@ -8,18 +8,6 @@ describe("Yield", function() {
assert.strictEqual(result.code, 'function*foo(e){return yield 1,yield 2,3}');
});
it("Should not allow yield as labelIdentifier within generators", function() {
var js = "function* g() {yield: 1}"
var test = function() {
UglifyJS.parse(js);
}
var expect = function(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
e.message === "Yield cannot be used as label inside generators";
}
assert.throws(test, expect);
});
it("Should not allow yield* followed by a semicolon in generators", function() {
var js = "function* test() {yield*\n;}";
var test = function() {
@@ -65,42 +53,68 @@ describe("Yield", function() {
);
});
var identifiers = [
// Fail in as_symbol
'import yield from "bar";',
'yield = 123;',
'yield: "123";',
'for(;;){break yield;}',
'for(;;){continue yield;}',
'function yield(){}',
'try { new Error("")} catch (yield) {}',
'var yield = "foo";',
'class yield {}',
// Fail in as_property_name
'var foo = {yield};',
];
it("Should not allow yield to be used as symbol, identifier or shorthand property outside generators in strict mode", function() {
var tests = [
// Fail in as_symbol
'"use strict"; import yield from "bar";',
'"use strict"; yield = 123;',
'"use strict"; yield: "123";',
'"use strict"; for(;;){break yield;}',
'"use strict"; for(;;){continue yield;}',
'"use strict"; function yield(){}',
'"use strict"; function foo(...yield){}',
'"use strict"; try { new Error("")} catch (yield) {}',
'"use strict"; var yield = "foo";',
'"use strict"; class yield {}',
// Fail in maybe_assign
'"use strict"; var foo = yield;',
'"use strict"; var foo = bar = yield',
// Fail in as_property_name
'"use strict"; var foo = {yield};',
];
var fail = function(e) {
function fail(e) {
return e instanceof UglifyJS.JS_Parse_Error &&
/^Unexpected yield identifier (?:as parameter )?inside strict mode/.test(e.message);
}
var test = function(input) {
function test(input) {
return function() {
UglifyJS.parse(input);
}
}
for (var i = 0; i < tests.length; i++) {
assert.throws(test(tests[i]), fail, tests[i]);
identifiers.concat([
// Fail in as_symbol
"function foo(...yield){}",
// Fail in maybe_assign
'var foo = yield;',
'var foo = bar = yield',
]).map(function(code) {
return '"use strict"; ' + code;
}).forEach(function(code) {
assert.throws(test(code), fail, code);
});
});
it("Should not allow yield to be used as symbol, identifier or shorthand property inside generators", function() {
function fail(e) {
return e instanceof UglifyJS.JS_Parse_Error && [
"Unexpected token: operator (=)",
"Yield cannot be used as identifier inside generators",
].indexOf(e.message) >= 0;
}
function test(input) {
return function() {
UglifyJS.parse(input);
}
}
identifiers.map(function(code) {
return "function* f() { " + code + " }";
}).concat([
// Fail in as_symbol
"function* f(yield) {}",
]).forEach(function(code) {
assert.throws(test(code), fail, code);
});
});
it("Should allow yield to be used as class/object property name", function() {