2015-02-04 17:59:12 -05:00
|
|
|
var compiler = require('../index');
|
2015-02-11 14:40:29 -05:00
|
|
|
var temp = require('temp');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
temp.track();
|
2015-02-04 17:59:12 -05:00
|
|
|
|
2015-02-06 16:38:52 -05:00
|
|
|
var DEFAULT_OPTIONS = {
|
|
|
|
sourceMaps: false,
|
2015-02-04 17:59:12 -05:00
|
|
|
annotations: true, // parse annotations
|
|
|
|
types: true, // parse types
|
|
|
|
script: false, // parse as a module
|
2015-02-06 16:38:52 -05:00
|
|
|
memberVariables: true // parse class fields
|
2015-02-04 17:59:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('transpile to dart', function(){
|
|
|
|
|
2015-02-06 16:38:52 -05:00
|
|
|
var options;
|
|
|
|
beforeEach(function() {
|
|
|
|
options = merge(DEFAULT_OPTIONS, {outputLanguage: 'dart'});
|
|
|
|
});
|
|
|
|
|
2015-02-04 17:59:12 -05:00
|
|
|
// https://github.com/angular/angular/issues/509
|
2015-02-04 19:40:04 -05:00
|
|
|
describe('string interpolation', function() {
|
|
|
|
it('should not interpolate inside old quotes', function(){
|
2015-02-06 16:38:52 -05:00
|
|
|
var result = compiler.compile(options, "test.js",
|
2015-02-04 19:40:04 -05:00
|
|
|
"var a:number = 1;" +
|
|
|
|
"var s1:string = \"${a}\";" +
|
|
|
|
"var s2:string = '\\${a}';" +
|
|
|
|
"var s3:string = '$a';");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\n" +
|
2015-02-04 19:40:04 -05:00
|
|
|
"num a = 1;\n" +
|
|
|
|
"String s1 = \"\\${a}\";\n" +
|
|
|
|
"String s2 = '\\${a}';\n" +
|
|
|
|
"String s3 = '\\$a';\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not interpolate without curly braces', function() {
|
2015-02-06 16:38:52 -05:00
|
|
|
var result = compiler.compile(options, "test.js",
|
2015-02-04 19:40:04 -05:00
|
|
|
"var a:number = 1;" +
|
|
|
|
"var s1:string = `$a`;" +
|
|
|
|
"var s2:string = `\\$a`;");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\n" +
|
2015-02-04 19:40:04 -05:00
|
|
|
"num a = 1;\n" +
|
|
|
|
"String s1 = '''\\$a''';\n" +
|
|
|
|
"String s2 = '''\\$a''';\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should interpolate inside template quotes', function() {
|
2015-02-06 16:38:52 -05:00
|
|
|
var result = compiler.compile(options, "test.js",
|
2015-02-04 19:40:04 -05:00
|
|
|
"var a:number = 1;" +
|
|
|
|
"var s1:string = `${a}`;");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\n" +
|
2015-02-04 19:40:04 -05:00
|
|
|
"num a = 1;\n" +
|
|
|
|
"String s1 = '''${a}''';\n");
|
|
|
|
});
|
|
|
|
});
|
2015-02-06 16:38:52 -05:00
|
|
|
|
|
|
|
describe('generic', function() {
|
|
|
|
|
|
|
|
it('should support types without generics', function() {
|
|
|
|
var result = compiler.compile(options, "test.js",
|
|
|
|
"var a:List = [];");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\nList a = [];\n");
|
2015-02-06 16:38:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support one level generics', function() {
|
|
|
|
var result = compiler.compile(options, "test.js",
|
|
|
|
"var a:List<string> = [];");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\nList<String> a = [];\n");
|
2015-02-06 16:38:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support multiple one level generics', function() {
|
|
|
|
var result = compiler.compile(options, "test.js",
|
|
|
|
"var a:List<A,B> = [];");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\nList<A, B> a = [];\n");
|
2015-02-06 16:38:52 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support nested generics', function() {
|
|
|
|
var result = compiler.compile(options, "test.js",
|
|
|
|
"var a:List<A<B>> = [];");
|
2015-03-11 21:07:29 -04:00
|
|
|
expect(result.js).toBe("library test;\nList<A<B>> a = [];\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add dart suffix to reserved words', function() {
|
|
|
|
var result = compiler.compile(options, "project/if.js",
|
|
|
|
"var a;");
|
|
|
|
expect(result.js).toBe("library project.if_dart;\nvar a;\n");
|
2015-02-06 16:38:52 -05: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 16:38:52 -05: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 16:38:52 -05:00
|
|
|
" constructor() {\n"+
|
|
|
|
" super();\n"+
|
|
|
|
" }\n"+
|
2015-03-24 11:01:26 -04:00
|
|
|
"}\n"+
|
|
|
|
"//# sourceMappingURL=test.js.map\n");
|
2015-02-06 16:38:52 -05: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 17:59:12 -05:00
|
|
|
});
|
2015-02-06 16:38:52 -05:00
|
|
|
|
2015-02-11 14:40:29 -05: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 16:38:52 -05: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;
|
|
|
|
}
|