mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 13:12:46 +00:00 
			
		
		
		
	 38fd980b0c
			
		
	
	
		38fd980b0c
		
	
	
	
	
		
			
			The parser now keeps track of a scope chain so that it can hoist function declarations to the closest function scope.
		
			
				
	
	
		
			39 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| test("basic functionality", () => {
 | |
|     let callHoisted = hoisted();
 | |
|     function hoisted() {
 | |
|         return "foo";
 | |
|     }
 | |
|     expect(hoisted()).toBe("foo");
 | |
|     expect(callHoisted).toBe("foo");
 | |
| });
 | |
| 
 | |
| // First two calls produce a ReferenceError, but the declarations should be hoisted
 | |
| test("functions are hoisted across non-lexical scopes", () => {
 | |
|     expect(scopedHoisted).toBeUndefined();
 | |
|     expect(callScopedHoisted).toBeUndefined();
 | |
|     {
 | |
|         var callScopedHoisted = scopedHoisted();
 | |
|         function scopedHoisted() {
 | |
|             return "foo";
 | |
|         }
 | |
|         expect(scopedHoisted()).toBe("foo");
 | |
|         expect(callScopedHoisted).toBe("foo");
 | |
|     }
 | |
|     expect(scopedHoisted()).toBe("foo");
 | |
|     expect(callScopedHoisted).toBe("foo");
 | |
| });
 | |
| 
 | |
| test("functions are not hoisted across lexical scopes", () => {
 | |
|     const test = () => {
 | |
|         var iife = (function () {
 | |
|             return declaredLater();
 | |
|         })();
 | |
|         function declaredLater() {
 | |
|             return "yay";
 | |
|         }
 | |
|         return iife;
 | |
|     };
 | |
| 
 | |
|     expect(() => declaredLater).toThrow(ReferenceError);
 | |
|     expect(test()).toBe("yay");
 | |
| });
 |