| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | import * as compiler from '@angular/compiler'; | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | import {MetadataCollector} from '../../src/metadata/collector'; | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  | import {CompilerHost, CompilerOptions, LibrarySummary} from '../../src/transformers/api'; | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | import {TsCompilerAotCompilerTypeCheckHostAdapter, createCompilerHost} from '../../src/transformers/compiler_host'; | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  | import {Directory, Entry, MockAotContext, MockCompilerHost} from '../mocks'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dummyModule = 'export let foo: any[];'; | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | const aGeneratedFile = new compiler.GeneratedFile( | 
					
						
							|  |  |  |     '/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', | 
					
						
							|  |  |  |     [new compiler.DeclareVarStmt('x', new compiler.LiteralExpr(1))]); | 
					
						
							|  |  |  | const aGeneratedFileText = `var x:any = 1;\n`; | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('NgCompilerHost', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |   let codeGenerator: {generateFile: jasmine.Spy; findGeneratedFileNames: jasmine.Spy;}; | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     codeGenerator = { | 
					
						
							|  |  |  |       generateFile: jasmine.createSpy('generateFile').and.returnValue(null), | 
					
						
							|  |  |  |       findGeneratedFileNames: jasmine.createSpy('findGeneratedFileNames').and.returnValue([]), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function createNgHost({files = {}}: {files?: Directory} = {}): CompilerHost { | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |     const context = new MockAotContext('/tmp/', files); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     return new MockCompilerHost(context) as ts.CompilerHost; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function createHost({ | 
					
						
							|  |  |  |     files = {}, | 
					
						
							|  |  |  |     options = { | 
					
						
							|  |  |  |       basePath: '/tmp', | 
					
						
							|  |  |  |       moduleResolution: ts.ModuleResolutionKind.NodeJs, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-03 13:47:39 -07:00
										 |  |  |     rootNames = ['/tmp/index.ts'], | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     ngHost = createNgHost({files}), | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |     librarySummaries = [], | 
					
						
							| 
									
										
										
										
											2017-09-20 16:31:32 -07:00
										 |  |  |   }: { | 
					
						
							|  |  |  |     files?: Directory, | 
					
						
							|  |  |  |     options?: CompilerOptions, | 
					
						
							| 
									
										
										
										
											2017-10-03 13:47:39 -07:00
										 |  |  |     rootNames?: string[], | 
					
						
							| 
									
										
										
										
											2017-09-20 16:31:32 -07:00
										 |  |  |     ngHost?: CompilerHost, | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |     librarySummaries?: LibrarySummary[] | 
					
						
							| 
									
										
										
										
											2017-09-20 16:31:32 -07:00
										 |  |  |   } = {}) { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     return new TsCompilerAotCompilerTypeCheckHostAdapter( | 
					
						
							| 
									
										
										
										
											2017-10-03 13:47:39 -07:00
										 |  |  |         rootNames, options, ngHost, new MetadataCollector(), codeGenerator, | 
					
						
							| 
									
										
										
										
											2017-09-29 15:02:11 -07:00
										 |  |  |         new Map(librarySummaries.map(entry => [entry.fileName, entry] as[string, LibrarySummary]))); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('fileNameToModuleName', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     let host: TsCompilerAotCompilerTypeCheckHostAdapter; | 
					
						
							|  |  |  |     beforeEach(() => { host = createHost(); }); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should use a package import when accessing a package from a source file', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName('/tmp/node_modules/@angular/core.d.ts', '/tmp/main.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |           .toBe('@angular/core'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use a package import when accessing a package from another package', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |                  '/tmp/node_modules/mod1/index.d.ts', '/tmp/node_modules/mod2/index.d.ts')) | 
					
						
							|  |  |  |           .toBe('mod1/index'); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |                  '/tmp/node_modules/@angular/core/index.d.ts', | 
					
						
							|  |  |  |                  '/tmp/node_modules/@angular/common/index.d.ts')) | 
					
						
							|  |  |  |           .toBe('@angular/core/index'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use a relative import when accessing a file in the same package', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |                  '/tmp/node_modules/mod/a/child.d.ts', '/tmp/node_modules/mod/index.d.ts')) | 
					
						
							|  |  |  |           .toBe('./a/child'); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |                  '/tmp/node_modules/@angular/core/src/core.d.ts', | 
					
						
							|  |  |  |                  '/tmp/node_modules/@angular/core/index.d.ts')) | 
					
						
							|  |  |  |           .toBe('./src/core'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use a relative import when accessing a source file from a source file', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.fileNameToModuleName('/tmp/src/a/child.ts', '/tmp/src/index.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |           .toBe('./a/child'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 11:54:40 -07:00
										 |  |  |     it('should use a relative import when accessing generated files, even if crossing packages', | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          expect(host.fileNameToModuleName( | 
					
						
							|  |  |  |                     '/tmp/node_modules/mod2/b.ngfactory.d.ts', | 
					
						
							|  |  |  |                     '/tmp/node_modules/mod1/a.ngfactory.d.ts')) | 
					
						
							|  |  |  |              .toBe('../mod2/b.ngfactory'); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |     it('should support multiple rootDirs when accessing a source file form a source file', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const hostWithMultipleRoots = createHost({ | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |         options: { | 
					
						
							|  |  |  |           basePath: '/tmp/', | 
					
						
							|  |  |  |           rootDirs: [ | 
					
						
							|  |  |  |             'src/a', | 
					
						
							|  |  |  |             'src/b', | 
					
						
							|  |  |  |           ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-08-15 14:41:48 -07:00
										 |  |  |       // both files are in the rootDirs
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(hostWithMultipleRoots.fileNameToModuleName('/tmp/src/b/b.ts', '/tmp/src/a/a.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |           .toBe('./b'); | 
					
						
							| 
									
										
										
										
											2017-08-15 14:41:48 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // one file is not in the rootDirs
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(hostWithMultipleRoots.fileNameToModuleName('/tmp/src/c/c.ts', '/tmp/src/a/a.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-15 14:41:48 -07:00
										 |  |  |           .toBe('../c/c'); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should error if accessing a source file from a package', () => { | 
					
						
							|  |  |  |       expect( | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |           () => host.fileNameToModuleName( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |               '/tmp/src/a/child.ts', '/tmp/node_modules/@angular/core.d.ts')) | 
					
						
							|  |  |  |           .toThrowError( | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |               'Trying to import a source file from a node_modules package: ' + | 
					
						
							|  |  |  |               'import /tmp/src/a/child.ts from /tmp/node_modules/@angular/core.d.ts'); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     it('should use the provided implementation if any', () => { | 
					
						
							|  |  |  |       const ngHost = createNgHost(); | 
					
						
							|  |  |  |       ngHost.fileNameToModuleName = () => 'someResult'; | 
					
						
							|  |  |  |       const host = createHost({ngHost}); | 
					
						
							|  |  |  |       expect(host.fileNameToModuleName('a', 'b')).toBe('someResult'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('moduleNameToFileName', () => { | 
					
						
							|  |  |  |     it('should resolve an import using the containing file', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'src': {'a': {'child.d.ts': dummyModule}}}}}); | 
					
						
							|  |  |  |       expect(host.moduleNameToFileName('./a/child', '/tmp/src/index.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |           .toBe('/tmp/src/a/child.d.ts'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-10 17:14:58 +00:00
										 |  |  |     it('should allow to skip the containing file for package imports', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = | 
					
						
							|  |  |  |           createHost({files: {'tmp': {'node_modules': {'@core': {'index.d.ts': dummyModule}}}}}); | 
					
						
							|  |  |  |       expect(host.moduleNameToFileName('@core/index')).toBe('/tmp/node_modules/@core/index.d.ts'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use the provided implementation if any', () => { | 
					
						
							|  |  |  |       const ngHost = createNgHost(); | 
					
						
							|  |  |  |       ngHost.moduleNameToFileName = () => 'someResult'; | 
					
						
							|  |  |  |       const host = createHost({ngHost}); | 
					
						
							|  |  |  |       expect(host.moduleNameToFileName('a', 'b')).toBe('someResult'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-03 13:47:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should work well with windows paths', () => { | 
					
						
							|  |  |  |       const host = createHost({ | 
					
						
							|  |  |  |         rootNames: ['\\tmp\\index.ts'], | 
					
						
							|  |  |  |         options: {basePath: '\\tmp'}, | 
					
						
							|  |  |  |         files: {'tmp': {'node_modules': {'@core': {'index.d.ts': dummyModule}}}} | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       expect(host.moduleNameToFileName('@core/index')).toBe('/tmp/node_modules/@core/index.d.ts'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-14 11:04:55 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('resourceNameToFileName', () => { | 
					
						
							|  |  |  |     it('should resolve a relative import', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'src': {'a': {'child.html': '<div>'}}}}}); | 
					
						
							|  |  |  |       expect(host.resourceNameToFileName('./a/child.html', '/tmp/src/index.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  |           .toBe('/tmp/src/a/child.html'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       expect(host.resourceNameToFileName('./a/non-existing.html', '/tmp/src/index.ts')).toBe(null); | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve package paths as relative paths', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'src': {'a': {'child.html': '<div>'}}}}}); | 
					
						
							|  |  |  |       expect(host.resourceNameToFileName('a/child.html', '/tmp/src/index.ts')) | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  |           .toBe('/tmp/src/a/child.html'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 16:08:55 -07:00
										 |  |  |     it('should resolve absolute paths as package paths', () => { | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'node_modules': {'a': {'child.html': '<div>'}}}}}); | 
					
						
							|  |  |  |       expect(host.resourceNameToFileName('/a/child.html', '')) | 
					
						
							| 
									
										
										
										
											2017-08-30 16:08:55 -07:00
										 |  |  |           .toBe('/tmp/node_modules/a/child.html'); | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should use the provided implementation if any', () => { | 
					
						
							|  |  |  |       const ngHost = createNgHost(); | 
					
						
							|  |  |  |       ngHost.resourceNameToFileName = () => 'someResult'; | 
					
						
							|  |  |  |       const host = createHost({ngHost}); | 
					
						
							|  |  |  |       expect(host.resourceNameToFileName('a', 'b')).toBe('someResult'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getSourceFile', () => { | 
					
						
							|  |  |  |     it('should cache source files by name', () => { | 
					
						
							|  |  |  |       const host = createHost({files: {'tmp': {'src': {'index.ts': ``}}}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const sf1 = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       const sf2 = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf1).toBe(sf2); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should generate code when asking for the base name and add it as referencedFiles', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(aGeneratedFile); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({ | 
					
						
							|  |  |  |         files: { | 
					
						
							|  |  |  |           'tmp': { | 
					
						
							|  |  |  |             'src': { | 
					
						
							|  |  |  |               'index.ts': `
 | 
					
						
							|  |  |  |               /// <reference path="main.ts"/>
 | 
					
						
							|  |  |  |             `
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const sf = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[0].fileName).toBe('main.ts'); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(genSf.text).toBe(aGeneratedFileText); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // the codegen should have been cached
 | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |       expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should generate code when asking for the generated name first', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(aGeneratedFile); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({ | 
					
						
							|  |  |  |         files: { | 
					
						
							|  |  |  |           'tmp': { | 
					
						
							|  |  |  |             'src': { | 
					
						
							|  |  |  |               'index.ts': `
 | 
					
						
							|  |  |  |               /// <reference path="main.ts"/>
 | 
					
						
							|  |  |  |             `
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(genSf.text).toBe(aGeneratedFileText); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const sf = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[0].fileName).toBe('main.ts'); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // the codegen should have been cached
 | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |       expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should clear old generated references if the original host cached them', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const sfText = `
 | 
					
						
							|  |  |  |           /// <reference path="main.ts"/>
 | 
					
						
							|  |  |  |       `;
 | 
					
						
							| 
									
										
										
										
											2017-10-17 16:10:15 -07:00
										 |  |  |       const ngHost = createNgHost({files: {'tmp': {'src': {'index.ts': sfText}}}}); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const sf = ts.createSourceFile('/tmp/src/index.ts', sfText, ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       ngHost.getSourceFile = () => sf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue( | 
					
						
							|  |  |  |           new compiler.GeneratedFile('/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', [])); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host1 = createHost({ngHost}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       host1.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf.referencedFiles.length).toBe(2); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[0].fileName).toBe('main.ts'); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue([]); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(null); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host2 = createHost({ngHost}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       host2.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf.referencedFiles.length).toBe(1); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[0].fileName).toBe('main.ts'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-29 15:27:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should generate for tsx files', () => { | 
					
						
							|  |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(aGeneratedFile); | 
					
						
							|  |  |  |       const host = createHost({files: {'tmp': {'src': {'index.tsx': ``}}}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(genSf.text).toBe(aGeneratedFileText); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const sf = host.getSourceFile('/tmp/src/index.tsx', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(sf.referencedFiles[0].fileName).toBe('/tmp/src/index.ngfactory.ts'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // the codegen should have been cached
 | 
					
						
							|  |  |  |       expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |       expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('updateSourceFile', () => { | 
					
						
							|  |  |  |     it('should update source files', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(aGeneratedFile); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'src': {'index.ts': ''}}}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(genSf.text).toBe(aGeneratedFileText); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       host.updateGeneratedFile(new compiler.GeneratedFile( | 
					
						
							|  |  |  |           '/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', | 
					
						
							|  |  |  |           [new compiler.DeclareVarStmt('x', new compiler.LiteralExpr(2))])); | 
					
						
							|  |  |  |       genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  |       expect(genSf.text).toBe(`var x:any = 2;\n`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should error if the imports changed', () => { | 
					
						
							| 
									
										
										
										
											2017-09-21 18:05:07 -07:00
										 |  |  |       codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']); | 
					
						
							|  |  |  |       codeGenerator.generateFile.and.returnValue(new compiler.GeneratedFile( | 
					
						
							|  |  |  |           '/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', | 
					
						
							|  |  |  |           [new compiler.DeclareVarStmt( | 
					
						
							|  |  |  |               'x', | 
					
						
							|  |  |  |               new compiler.ExternalExpr(new compiler.ExternalReference('aModule', 'aName')))])); | 
					
						
							| 
									
										
										
										
											2017-09-12 09:40:28 -07:00
										 |  |  |       const host = createHost({files: {'tmp': {'src': {'index.ts': ''}}}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect( | 
					
						
							|  |  |  |           () => host.updateGeneratedFile(new compiler.GeneratedFile( | 
					
						
							|  |  |  |               '/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', | 
					
						
							|  |  |  |               [new compiler.DeclareVarStmt( | 
					
						
							|  |  |  |                   'x', new compiler.ExternalExpr( | 
					
						
							|  |  |  |                            new compiler.ExternalReference('otherModule', 'aName')))]))) | 
					
						
							|  |  |  |           .toThrowError([ | 
					
						
							|  |  |  |             `Illegal State: external references changed in /tmp/src/index.ngfactory.ts.`, | 
					
						
							|  |  |  |             `Old: aModule.`, `New: otherModule` | 
					
						
							|  |  |  |           ].join('\n')); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-15 17:06:09 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |