| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {ReflectorHost, ReflectorHostContext} from '../src/reflector_host'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type Entry = string | Directory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | export interface Directory { [name: string]: Entry; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class MockContext implements ReflectorHostContext { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   constructor(public currentDirectory: string, private files: Entry) {} | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   exists(fileName: string): boolean { return this.getEntry(fileName) !== undefined; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   read(fileName: string): string|undefined { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     let data = this.getEntry(fileName); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     if (typeof data === 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       return data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   write(fileName: string, data: string): void { | 
					
						
							|  |  |  |     let parts = fileName.split('/'); | 
					
						
							|  |  |  |     let name = parts.pop(); | 
					
						
							|  |  |  |     let entry = this.getEntry(parts); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     if (entry && typeof entry !== 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       entry[name] = data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getEntry(fileName: string|string[]): Entry|undefined { | 
					
						
							|  |  |  |     let parts = typeof fileName === 'string' ? fileName.split('/') : fileName; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     if (parts[0]) { | 
					
						
							|  |  |  |       parts = this.currentDirectory.split('/').concat(parts); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     parts.shift(); | 
					
						
							|  |  |  |     parts = normalize(parts); | 
					
						
							|  |  |  |     let current = this.files; | 
					
						
							|  |  |  |     while (parts.length) { | 
					
						
							|  |  |  |       let part = parts.shift(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       if (typeof current === 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       let next = (<Directory>current)[part]; | 
					
						
							|  |  |  |       if (next === undefined) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       current = next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return current; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function normalize(parts: string[]): string[] { | 
					
						
							|  |  |  |   let result: string[] = []; | 
					
						
							|  |  |  |   while (parts.length) { | 
					
						
							|  |  |  |     let part = parts.shift(); | 
					
						
							|  |  |  |     switch (part) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       case '.': | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       case '..': | 
					
						
							|  |  |  |         result.pop(); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         result.push(part); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockCompilerHost implements ts.CompilerHost { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   constructor(private context: MockContext) {} | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   fileExists(fileName: string): boolean { return this.context.exists(fileName); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   readFile(fileName: string): string { return this.context.read(fileName); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   directoryExists(directoryName: string): boolean { return this.context.exists(directoryName); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getSourceFile( | 
					
						
							|  |  |  |       fileName: string, languageVersion: ts.ScriptTarget, | 
					
						
							|  |  |  |       onError?: (message: string) => void): ts.SourceFile { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     let sourceText = this.context.read(fileName); | 
					
						
							|  |  |  |     if (sourceText) { | 
					
						
							|  |  |  |       return ts.createSourceFile(fileName, sourceText, languageVersion); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getDefaultLibFileName(options: ts.CompilerOptions): string { | 
					
						
							|  |  |  |     return ts.getDefaultLibFileName(options); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   writeFile: ts.WriteFileCallback = (fileName, text) => { this.context.write(fileName, text); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getCurrentDirectory(): string { | 
					
						
							|  |  |  |     return this.context.currentDirectory; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getCanonicalFileName(fileName: string): string { return fileName; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   useCaseSensitiveFileNames(): boolean { return false; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getNewLine(): string { return '\n'; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | } |