diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-07 22:44:20 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-08 06:44:20 +0800 |
commit | fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a (patch) | |
tree | 75e5f2eedf3dbf809d76e419ab03d4dc62363a79 /test/mocha/yields.js | |
parent | c44b6399c30b043f9bf687ce9b44458abc211d80 (diff) | |
download | tracifyjs-fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a.tar.gz tracifyjs-fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a.zip |
support generator functions (#4620)
Diffstat (limited to 'test/mocha/yields.js')
-rw-r--r-- | test/mocha/yields.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/test/mocha/yields.js b/test/mocha/yields.js new file mode 100644 index 00000000..d995d61d --- /dev/null +++ b/test/mocha/yields.js @@ -0,0 +1,82 @@ +var assert = require("assert"); +var UglifyJS = require("../node"); + +describe("generator", function() { + it("Should reject `yield` as symbol name within generator functions only", function() { + [ + "function yield() {}", + "function(yield) {}", + "function() { yield:{} }", + "function() { var yield; }", + "function() { function yield() {} }", + "function() { try {} catch (yield) {} }", + ].forEach(function(code) { + var ast = UglifyJS.parse("(" + code + ")();"); + assert.strictEqual(ast.TYPE, "Toplevel"); + assert.strictEqual(ast.body.length, 1); + assert.strictEqual(ast.body[0].TYPE, "SimpleStatement"); + assert.strictEqual(ast.body[0].body.TYPE, "Call"); + assert.strictEqual(ast.body[0].body.expression.TYPE, "Function"); + assert.throws(function() { + UglifyJS.parse("(" + code.replace(/^function/, "function*") + ")();"); + }, function(e) { + return e instanceof UglifyJS.JS_Parse_Error; + }, code); + }); + }); + it("Should reject `yield` expression outside of generator functions", function() { + [ + "yield 42;", + "function f() { yield 42; }", + "function* f() { function g() { yield 42; } }", + ].forEach(function(code) { + assert.throws(function() { + UglifyJS.parse(code); + }, function(e) { + return e instanceof UglifyJS.JS_Parse_Error; + }, code); + }); + }); + it("Should reject `yield` expression directly on computed key of function argument", function() { + [ + "function f({ [yield 42]: a }) {}", + "function* f({ [yield 42]: a }) {}", + ].forEach(function(code) { + assert.throws(function() { + UglifyJS.parse(code); + }, function(e) { + return e instanceof UglifyJS.JS_Parse_Error; + }, code); + }); + }); + it("Should accept `yield` expression nested within computed key of function argument", function() { + [ + "function f({ [function*() { yield 42; }()]: a }) {}", + "function* f({ [function*() { yield 42; }()]: a }) {}", + ].forEach(function(code) { + var ast = UglifyJS.parse(code); + assert.strictEqual(ast.TYPE, "Toplevel"); + assert.strictEqual(ast.body.length, 1); + assert.strictEqual(ast.body[0].argnames.length, 1); + assert.strictEqual(ast.body[0].argnames[0].TYPE, "DestructuredObject"); + }); + }); + it("Should reject `yield*` without an expression", function() { + [ + "yield*", + "yield*;", + "yield*,", + "(yield*)", + "[ yield* ]", + "42[yield*]", + "yield* && 42", + ].forEach(function(code) { + code = "function* f() { " + code + " }"; + assert.throws(function() { + UglifyJS.parse(code); + }, function(e) { + return e instanceof UglifyJS.JS_Parse_Error; + }, code); + }); + }); +}); |