| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter'; | 
					
						
							|  |  |  | import * as o from '@angular/compiler/src/output/output_ast'; | 
					
						
							| 
									
										
										
										
											2019-04-25 10:41:45 -07:00
										 |  |  | import {JitEmitterVisitor, JitEvaluator} from '@angular/compiler/src/output/output_jit'; | 
					
						
							|  |  |  | import {R3JitReflector} from '@angular/compiler/src/render3/r3_jit'; | 
					
						
							| 
									
										
										
										
											2019-08-15 13:42:17 -07:00
										 |  |  | import {newArray} from '@angular/compiler/src/util'; | 
					
						
							| 
									
										
										
										
											2017-08-16 09:00:03 -07:00
										 |  |  | import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector'; | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const anotherModuleUrl = 'somePackage/someOtherPath'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  |   describe('Output JIT', () => { | 
					
						
							|  |  |  |     describe('regression', () => { | 
					
						
							|  |  |  |       it('should generate unique argument names', () => { | 
					
						
							| 
									
										
										
										
											2019-08-15 13:42:17 -07:00
										 |  |  |         const externalIds = newArray(10, 1).map( | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  |             (_, index) => | 
					
						
							|  |  |  |                 new o.ExternalReference(anotherModuleUrl, `id_${index}_`, {name: `id_${index}_`})); | 
					
						
							| 
									
										
										
										
											2019-08-15 13:42:17 -07:00
										 |  |  |         const externalIds1 = newArray(10, 1).map( | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  |             (_, index) => new o.ExternalReference( | 
					
						
							|  |  |  |                 anotherModuleUrl, `id_${index}_1`, {name: `id_${index}_1`})); | 
					
						
							|  |  |  |         const ctx = EmitterVisitorContext.createRoot(); | 
					
						
							| 
									
										
										
										
											2017-08-16 09:00:03 -07:00
										 |  |  |         const converter = new JitEmitterVisitor(new JitReflector()); | 
					
						
							| 
									
										
										
										
											2017-07-17 10:52:42 -07:00
										 |  |  |         converter.visitAllStatements( | 
					
						
							|  |  |  |             [o.literalArr([...externalIds1, ...externalIds].map(id => o.importExpr(id))).toStmt()], | 
					
						
							|  |  |  |             ctx); | 
					
						
							|  |  |  |         const args = converter.getArgs(); | 
					
						
							|  |  |  |         expect(Object.keys(args).length).toBe(20); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-04-25 10:41:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should use strict mode', () => { | 
					
						
							|  |  |  |       const evaluator = new JitEvaluator(); | 
					
						
							|  |  |  |       expect(() => { | 
					
						
							|  |  |  |         evaluator.evaluateStatements( | 
					
						
							|  |  |  |             'http://angular.io/something.ts', | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               // Set an undeclared variable
 | 
					
						
							|  |  |  |               // foo = "bar";
 | 
					
						
							|  |  |  |               o.variable('foo').equals(o.literal('bar')).toStmt(), | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             new R3JitReflector({}), false); | 
					
						
							|  |  |  |       }).toThrowError(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not add more than one strict mode statement if there is already one present', () => { | 
					
						
							|  |  |  |       const converter = new JitEmitterVisitor(new JitReflector()); | 
					
						
							|  |  |  |       const ctx = EmitterVisitorContext.createRoot(); | 
					
						
							|  |  |  |       converter.visitAllStatements( | 
					
						
							|  |  |  |           [ | 
					
						
							|  |  |  |             o.literal('use strict').toStmt(), | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |           ctx); | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |       const matches = ctx.toSource().match(/'use strict';/g)!; | 
					
						
							| 
									
										
										
										
											2019-04-25 10:41:45 -07:00
										 |  |  |       expect(matches.length).toBe(1); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-07-27 16:13:16 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-04-25 10:41:45 -07:00
										 |  |  | } |