mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:12:43 +00:00 
			
		
		
		
	 3194177dce
			
		
	
	
		3194177dce
		
	
	
	
	
		
			
			This commit implements parsing for `yield *expr`, and the multiple
ways something can or can't be parsed like that.
Also makes yield-from a TODO in the bytecode generator.
Behold, the glory of javascript syntax:
```js
// 'yield' = expression in generators.
function* foo() {
    yield
    *bar; // <- Syntax error here, expression can't start with *
}
// 'yield' = identifier anywhere else.
function foo() {
    yield
    *bar; // Perfectly fine, this is just `yield * bar`
}
```
		
	
			
		
			
				
	
	
		
			41 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| describe("parsing freestanding generators", () => {
 | |
|     test("simple", () => {
 | |
|         expect(`function* foo() {}`).toEval();
 | |
|         expect(`function *foo() {}`).toEval();
 | |
|         expect(`function
 | |
|             *foo() {}`).toEval();
 | |
|     });
 | |
|     test("yield expression", () => {
 | |
|         expect(`function* foo() { yield; }`).toEval();
 | |
|         expect(`function* foo() { yield (yield); }`).toEval();
 | |
|         expect(`function* foo() { yield (yield foo); }`).toEval();
 | |
|         expect(`function foo() { yield; }`).toEval();
 | |
|         expect(`function foo() { yield 3; }`).not.toEval();
 | |
|     });
 | |
|     test("yield-from expression", () => {
 | |
|         expect(`function* foo() { yield *bar; }`).toEval();
 | |
|         expect(`function* foo() { yield *(yield); }`).toEval();
 | |
|         expect(`function* foo() { yield
 | |
|             *bar; }`).not.toEval();
 | |
|         expect(`function foo() { yield
 | |
|             *bar; }`).toEval();
 | |
|     });
 | |
| });
 | |
| 
 | |
| describe("parsing object literal generator functions", () => {
 | |
|     test("simple", () => {
 | |
|         expect(`x = { *foo() { } }`).toEval();
 | |
|         expect(`x = { * foo() { } }`).toEval();
 | |
|         expect(`x = { *
 | |
|                 foo() { } }`).toEval();
 | |
|     });
 | |
|     test("yield", () => {
 | |
|         expect(`x = { foo() { yield; } }`).toEval();
 | |
|         expect(`x = { *foo() { yield; } }`).toEval();
 | |
|         expect(`x = { *foo() { yield 42; } }`).toEval();
 | |
|         expect(`x = { foo() { yield 42; } }`).not.toEval();
 | |
|         expect(`x = { *foo() { yield (yield); } }`).toEval();
 | |
|         expect(`x = { *
 | |
|                 foo() { yield (yield); } }`).toEval();
 | |
|     });
 | |
| });
 |