generators.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. describe("parsing freestanding generators", () => {
  2. test("simple", () => {
  3. expect(`function* foo() {}`).toEval();
  4. expect(`function *foo() {}`).toEval();
  5. expect(`function
  6. *foo() {}`).toEval();
  7. });
  8. test("yield expression", () => {
  9. expect(`function* foo() { yield; }`).toEval();
  10. expect(`function* foo() { yield (yield); }`).toEval();
  11. expect(`function* foo() { yield (yield foo); }`).toEval();
  12. expect(`function foo() { yield; }`).toEval();
  13. expect(`function foo() { yield 3; }`).not.toEval();
  14. });
  15. test("yield expression only gets the first expression", () => {
  16. expect("function* foo() { yield 2,3 }").toEval();
  17. expect("function* foo() { ({...yield yield, }) }").toEval();
  18. });
  19. test("yield-from expression", () => {
  20. expect(`function* foo() { yield *bar; }`).toEval();
  21. expect(`function* foo() { yield *(yield); }`).toEval();
  22. expect(`function* foo() { yield
  23. *bar; }`).not.toEval();
  24. expect(`function foo() { yield
  25. *bar; }`).toEval();
  26. });
  27. });
  28. describe("parsing object literal generator functions", () => {
  29. test("simple", () => {
  30. expect(`x = { *foo() { } }`).toEval();
  31. expect(`x = { * foo() { } }`).toEval();
  32. expect(`x = { *
  33. foo() { } }`).toEval();
  34. });
  35. test("yield", () => {
  36. expect(`x = { foo() { yield; } }`).toEval();
  37. expect(`x = { *foo() { yield; } }`).toEval();
  38. expect(`x = { *foo() { yield 42; } }`).toEval();
  39. expect(`x = { foo() { yield 42; } }`).not.toEval();
  40. expect(`x = { *foo() { yield (yield); } }`).toEval();
  41. expect(`x = { *
  42. foo() { yield (yield); } }`).toEval();
  43. });
  44. });
  45. describe("parsing classes with generator methods", () => {
  46. test("simple", () => {
  47. expect(`class Foo { *foo() {} }`).toEval();
  48. expect(`class Foo { static *foo() {} }`).toEval();
  49. expect(`class Foo { *foo() { yield; } }`).toEval();
  50. expect(`class Foo { *foo() { yield 42; } }`).toEval();
  51. expect(`class Foo { *constructor() { yield 42; } }`).not.toEval();
  52. });
  53. });
  54. test("function expression names equal to 'yield'", () => {
  55. expect(`function *foo() { (function yield() {}); }`).toEval();
  56. expect(`function *foo() { function yield() {} }`).not.toEval();
  57. });