| 
									
										
										
										
											2015-02-04 14:59:12 -08:00
										 |  |  | var compiler = require('../index'); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:40:29 -08:00
										 |  |  | var temp = require('temp'); | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | temp.track(); | 
					
						
							| 
									
										
										
										
											2015-02-04 14:59:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  | var DEFAULT_OPTIONS = { | 
					
						
							|  |  |  |   sourceMaps: false, | 
					
						
							| 
									
										
										
										
											2015-02-04 14:59:12 -08:00
										 |  |  |   annotations: true, // parse annotations
 | 
					
						
							|  |  |  |   types: true, // parse types
 | 
					
						
							|  |  |  |   script: false, // parse as a module
 | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  |   memberVariables: true // parse class fields
 | 
					
						
							| 
									
										
										
										
											2015-02-04 14:59:12 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  | describe('transpile to es6', function() { | 
					
						
							|  |  |  |   var options; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(function() { | 
					
						
							|  |  |  |     options = merge(DEFAULT_OPTIONS, {outputLanguage: 'es6', typeAssertions: 'true'}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should preserve generic type information', function() { | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							|  |  |  |       "function f(a:List<string>){}"); | 
					
						
							|  |  |  |     expect(result.js).toBe('function f(a) {\n'+ | 
					
						
							|  |  |  |       '  assert.argumentTypes(a, assert.genericType(List, assert.type.string));\n'+ | 
					
						
							|  |  |  |       '}\n'+ | 
					
						
							|  |  |  |       'Object.defineProperty(f, "parameters", {get: function() {\n'+ | 
					
						
							|  |  |  |       '    return [[assert.genericType(List, assert.type.string)]];\n'+ | 
					
						
							|  |  |  |       '  }});\n'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should allow super() calls when transpiling to ES6 with source maps', function() { | 
					
						
							|  |  |  |     options = merge(options, {sourceMaps: true}); | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							| 
									
										
										
										
											2015-03-24 11:01:26 -04:00
										 |  |  |       "class Base {}\n" + | 
					
						
							|  |  |  |       "class Test extends Base {" + | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  |       "  constructor() { super(); }" + | 
					
						
							|  |  |  |       "}"); | 
					
						
							| 
									
										
										
										
											2015-03-24 11:01:26 -04:00
										 |  |  |     expect(result.js).toBe("class Base {}\n" + | 
					
						
							|  |  |  |       "class Test extends Base {\n" + | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  |       "  constructor() {\n"+ | 
					
						
							|  |  |  |       "    super();\n"+ | 
					
						
							|  |  |  |       "  }\n"+ | 
					
						
							| 
									
										
										
										
											2015-03-24 11:01:26 -04:00
										 |  |  |       "}\n"+ | 
					
						
							|  |  |  |       "//# sourceMappingURL=test.js.map\n"); | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should convert types to expressions', function() { | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							|  |  |  |       "function f(a:string) {}"); | 
					
						
							|  |  |  |     expect(result.js).toBe('function f(a) {\n'+ | 
					
						
							|  |  |  |       '  assert.argumentTypes(a, assert.type.string);\n'+ | 
					
						
							|  |  |  |       '}\n' + | 
					
						
							|  |  |  |       'Object.defineProperty(f, "parameters", {get: function() {\n' + | 
					
						
							|  |  |  |       '    return [[assert.type.string]];\n' + | 
					
						
							|  |  |  |       '  }});\n'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should not convert type properties to getter/setters', function() { | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							|  |  |  |       "class Test {" + | 
					
						
							|  |  |  |       "  constructor() { this.a = 1; }" + | 
					
						
							|  |  |  |       "}"); | 
					
						
							|  |  |  |     expect(result.js).toBe("class Test {\n" + | 
					
						
							|  |  |  |       "  constructor() {\n"+ | 
					
						
							|  |  |  |       "    this.a = 1;\n"+ | 
					
						
							|  |  |  |       "  }\n"+ | 
					
						
							|  |  |  |       "}\n"); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should remove class field declarations', function() { | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							|  |  |  |       "class Test {" + | 
					
						
							|  |  |  |       "  a:number = 1;" + | 
					
						
							|  |  |  |       "}"); | 
					
						
							|  |  |  |     expect(result.js).toBe("class Test {}\n"); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should convert types to expressions on "assert" module', function() { | 
					
						
							|  |  |  |     var result = compiler.compile(options, "test.js", | 
					
						
							|  |  |  |       "function f(a:string, b) { return a+b; }"); | 
					
						
							|  |  |  |     expect(result.js).toBe('function f(a, b) {\n'+ | 
					
						
							|  |  |  |       '  assert.argumentTypes(a, assert.type.string, b, assert.type.any);\n'+ | 
					
						
							|  |  |  |       '  return a + b;\n'+ | 
					
						
							|  |  |  |       '}\n'+ | 
					
						
							|  |  |  |       'Object.defineProperty(f, "parameters", {get: function() {\n'+ | 
					
						
							|  |  |  |       '    return [[assert.type.string], []];\n'+ | 
					
						
							|  |  |  |       '  }});\n'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-04 14:59:12 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 11:40:29 -08:00
										 |  |  | describe('transpile to cjs', function() { | 
					
						
							|  |  |  |   var options; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(function() { | 
					
						
							|  |  |  |     options = merge(DEFAULT_OPTIONS, {modules: 'commonjs'}); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function compileAndWrite(input) { | 
					
						
							|  |  |  |     var transpiledCode = compiler.compile(options, "test.js", input).js; | 
					
						
							|  |  |  |     var tempFile = temp.openSync('ng2transpiler'); | 
					
						
							|  |  |  |     fs.writeSync(tempFile.fd, transpiledCode); | 
					
						
							|  |  |  |     return tempFile.path; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should transpile export *', function() { | 
					
						
							|  |  |  |     var file1 = compileAndWrite('export var a = 1'); | 
					
						
							|  |  |  |     var file2 = compileAndWrite('export * from "' + file1 + '"'); | 
					
						
							|  |  |  |     expect(require(file2).a).toBe(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should transpile export {name}', function() { | 
					
						
							|  |  |  |     var file1 = compileAndWrite('export var a = 1'); | 
					
						
							|  |  |  |     var file2 = compileAndWrite('export {a} from "' + file1 + '"'); | 
					
						
							|  |  |  |     expect(require(file2).a).toBe(1); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 13:38:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function merge(a, b) { | 
					
						
							|  |  |  |   var result = {}; | 
					
						
							|  |  |  |   for (var prop in a) { | 
					
						
							|  |  |  |     result[prop] = a[prop]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (var prop in b) { | 
					
						
							|  |  |  |     result[prop] = b[prop]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } |