generators.js 2.4 KB

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