fix(setup): use upstream traceur with explicit patches

Also correct the transpile to ES6

Also support generics correctly

All patches are hooked in via `/tools/transpiler/index.js`
https://github.com/google/traceur-compiler/issues/1700
https://github.com/google/traceur-compiler/issues/1699
https://github.com/google/traceur-compiler/issues/1708
https://github.com/google/traceur-compiler/issues/1625
https://github.com/google/traceur-compiler/issues/1706
This commit is contained in:
Tobias Bosch
2015-02-06 13:38:52 -08:00
parent 63d8107d1c
commit f39c6dc2c7
46 changed files with 515 additions and 125 deletions

View File

@ -10,6 +10,7 @@ var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.j
var SELF_SOURCE_REGEX = /transpiler\/src/;
var SELF_COMPILE_OPTIONS = {
modules: 'register',
memberVariables: false,
moduleName: true,
script: false // parse as a module
};
@ -74,6 +75,12 @@ function reloadCompiler() {
}
return m;
};
useRttsAssertModuleForConvertingTypesToExpressions();
supportSuperCallsInEs6Patch();
convertTypesToExpressionsInEs6Patch();
removeNonStaticFieldDeclarationsInEs6Patch();
disableGetterSetterAssertionPatch();
}
function loadModule(filepath, transpile) {
@ -106,3 +113,105 @@ function extend(source, props) {
}
return res;
}
// TODO(tbosch): remove when traceur is fixed.
// see https://github.com/google/traceur-compiler/issues/1700
function supportSuperCallsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var ParseTreeMapWriter = System.get(traceurVersion+'/src/outputgeneration/ParseTreeMapWriter').ParseTreeMapWriter;
var _enterBranch = ParseTreeMapWriter.prototype.enterBranch;
ParseTreeMapWriter.prototype.enterBranch = function(location) {
if (!location.start) {
// This would throw...
return;
}
return _enterBranch.apply(this, arguments);
}
}
// TODO(tbosch): Remove when traceur is fixed.
// see https://github.com/google/traceur-compiler/issues/1699
function convertTypesToExpressionsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var TypeToExpressionTransformer = System.get(traceurVersion+'/src/codegeneration/TypeToExpressionTransformer').TypeToExpressionTransformer;
var PureES6Transformer = System.get(traceurVersion+'/src/codegeneration/PureES6Transformer').PureES6Transformer;
var UniqueIdentifierGenerator = System.get(traceurVersion+'/src/codegeneration/UniqueIdentifierGenerator').UniqueIdentifierGenerator;
var _transform = PureES6Transformer.prototype.transform;
PureES6Transformer.prototype.transform = function() {
if (!this._patched) {
this._patched = true;
this.treeTransformers_.splice(0,0, function(tree) {
return new TypeToExpressionTransformer(new UniqueIdentifierGenerator(), this.reporter_).transformAny(tree);
});
}
return _transform.apply(this, arguments);
};
}
// TODO(tbosch): Don't write field declarations in classes when we output to ES6.
// This just patches the writer and does not support moving initializers to the constructor.
// See src/codegeneration/ClassTransformer.js for how to support initializers as well.
// see https://github.com/google/traceur-compiler/issues/1708
function removeNonStaticFieldDeclarationsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var ParseTreeWriter = System.get(traceurVersion+'/src/outputgeneration/ParseTreeWriter').ParseTreeWriter;
var options = System.get(traceurVersion + "/src/Options.js").options;
var _visitPropertyVariableDeclaration = ParseTreeWriter.prototype.visitPropertyVariableDeclaration;
ParseTreeWriter.prototype.visitPropertyVariableDeclaration = function() {
if (options.outputLanguage !== 'es6') {
return _visitPropertyVariableDeclaration.apply(this, arguments);
}
};
}
// TODO(tbosch): Disable getter/setters for assertions until traceur has a flag
// that allows to disable them while keeping assertions and member fields enabled.
// see https://github.com/google/traceur-compiler/issues/1625
// Why:
// - traceur uses field names based on numbers, which can lead to collisions when creating a subclass in a separate compiler run.
// - this rename of fields makes debugging via the repl harder (e.g. via DevTools console)
// - this rename can break JSON conversion of instances
function disableGetterSetterAssertionPatch() {
var traceurVersion = System.map['traceur'];
var MemberVariableTransformer = System.get(traceurVersion+'/src/codegeneration/MemberVariableTransformer').MemberVariableTransformer;
var AnonBlock = System.get(traceurVersion+'/src/syntax/trees/ParseTrees.js').AnonBlock;
MemberVariableTransformer.prototype.transformPropertyVariableDeclaration = function(tree) {
return new AnonBlock(tree.location, []);
}
}
// TODO(tbosch): Get all types from `assert` module and not from `$traceurRuntime`.
// With this a transpile to ES6 does no more include the `$traceurRuntime`.
// see https://github.com/google/traceur-compiler/issues/1706
function useRttsAssertModuleForConvertingTypesToExpressions() {
var traceurVersion = System.map['traceur'];
var original = System.get(traceurVersion+'/src/codegeneration/TypeToExpressionTransformer').TypeToExpressionTransformer;
var patch = System.get('transpiler/src/patch/TypeToExpressionTransformer').TypeToExpressionTransformer;
for (var prop in patch.prototype) {
original.prototype[prop] = patch.prototype[prop];
}
var TypeAssertionTransformer = System.get(traceurVersion+'/src/codegeneration/TypeAssertionTransformer').TypeAssertionTransformer;
var createIdentifierExpression = System.get(traceurVersion+'/src/codegeneration/ParseTreeFactory').createIdentifierExpression;
var parseExpression = System.get(traceurVersion+'/src/codegeneration/PlaceholderParser.js').parseExpression;
TypeAssertionTransformer.prototype.transformBindingElementParameter_ = function(element, typeAnnotation) {
// Copied from https://github.com/google/traceur-compiler/commits/master/src/codegeneration/TypeAssertionTransformer.js
if (!element.binding.isPattern()) {
if (typeAnnotation) {
this.paramTypes_.atLeastOneParameterTyped = true;
} else {
// PATCH start
typeAnnotation = parseExpression(["assert.type.any"]);
// PATCH end
}
this.paramTypes_.arguments.push(
createIdentifierExpression(element.binding.identifierToken),
typeAnnotation);
return;
}
// NYI
}
}

View File

@ -3,49 +3,53 @@ import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {IterableList} from './fixtures/facade';
export function main() {
describe('for..of', function() {
it('should iterate iterable', function() {
var values = ['a', 'b', 'c'];
var result = ListWrapper.create();
for (var value of new IterableList(values)) {
ListWrapper.push(result, value);
}
expect(result).toEqual(values);
});
// TODO(tbosch): For ... of is not supported by our Dart transpiler right now.
// Vojta had a traceur branch that reverted https://github.com/google/traceur-compiler/commit/2d12b2f9cea86e4f234c90dcc188b4c7a2881359
// to make it work, but we should first implement this in a proper way before we start using it!
it('should iterate iterable without var declaration list', function() {
var values = ['a', 'b', 'c'];
var result = ListWrapper.create();
var value;
for (value of new IterableList(values)) {
ListWrapper.push(result, value);
}
expect(value).toEqual('c');
expect(result).toEqual(values);
});
// describe('for..of', function() {
// it('should iterate iterable', function() {
// var values = ['a', 'b', 'c'];
// var result = ListWrapper.create();
// for (var value of new IterableList(values)) {
// ListWrapper.push(result, value);
// }
// expect(result).toEqual(values);
// });
it('should iterate maps', function() {
var values = [['a', 1], ['b', 2], ['c', 3]];
var result = ListWrapper.create();
var map = MapWrapper.createFromPairs(values);
for (var [key, value] of MapWrapper.iterable(map)) {
ListWrapper.push(result, [key, value]);
}
expect(result).toEqual(values);
});
// it('should iterate iterable without var declaration list', function() {
// var values = ['a', 'b', 'c'];
// var result = ListWrapper.create();
// var value;
// for (value of new IterableList(values)) {
// ListWrapper.push(result, value);
// }
// expect(value).toEqual('c');
// expect(result).toEqual(values);
// });
it('should iterate maps without var declaration list', function() {
var values = [['a', 1], ['b', 2], ['c', 3]];
var result = ListWrapper.create();
var map = MapWrapper.createFromPairs(values);
var key, value;
for ([key, value] of MapWrapper.iterable(map)) {
ListWrapper.push(result, [key, value]);
}
expect(key).toEqual('c');
expect(value).toEqual(3);
expect(result).toEqual(values);
});
});
// it('should iterate maps', function() {
// var values = [['a', 1], ['b', 2], ['c', 3]];
// var result = ListWrapper.create();
// var map = MapWrapper.createFromPairs(values);
// for (var [key, value] of MapWrapper.iterable(map)) {
// ListWrapper.push(result, [key, value]);
// }
// expect(result).toEqual(values);
// });
// it('should iterate maps without var declaration list', function() {
// var values = [['a', 1], ['b', 2], ['c', 3]];
// var result = ListWrapper.create();
// var map = MapWrapper.createFromPairs(values);
// var key, value;
// for ([key, value] of MapWrapper.iterable(map)) {
// ListWrapper.push(result, [key, value]);
// }
// expect(key).toEqual('c');
// expect(value).toEqual(3);
// expect(result).toEqual(values);
// });
// });
}

View File

@ -19,7 +19,9 @@ import {
SEMI_COLON,
STAR,
STATIC,
VAR
VAR,
OPEN_ANGLE,
CLOSE_ANGLE
} from 'traceur/src/syntax/TokenType';
import {
@ -53,7 +55,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
if (tree.isFinal) {
this.write_('final');
}
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
}
this.writeSpace_();
@ -78,7 +80,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
}
visitVariableDeclaration(tree) {
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
this.visitAny(tree.lvalue);
if (tree.initializer !== null) {
@ -123,7 +125,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
}
if (tree.name) {
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
this.visitAny(tree.name);
}
@ -152,7 +154,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
this.write_(ASYNC);
}
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
this.visitAny(tree.name);
this.write_(OPEN_PAREN);
this.visitAny(tree.parameterList);
@ -200,7 +202,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
this.writeSpace_();
}
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
this.visitAny(tree.name);
this.write_(OPEN_PAREN);
this.visitAny(tree.parameterList);
@ -246,7 +248,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
// resetting type annotation so it doesn't filter down recursively
this.currentParameterTypeAnnotation_ = null;
this.writeType_(typeAnnotation);
this.writeTypeAndSpace_(typeAnnotation);
this.visitAny(tree.binding);
if (tree.initializer) {
@ -257,22 +259,44 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
}
}
writeTypeAndSpace_(typeAnnotation) {
this.writeType_(typeAnnotation);
this.writeSpace_();
}
writeType_(typeAnnotation) {
if (!typeAnnotation) {
return;
}
var typeNameNode;
var args = [];
if (typeAnnotation.typeName) {
typeNameNode = typeAnnotation.typeName;
args = typeAnnotation.args.args;
} else {
typeNameNode = typeAnnotation;
args = [];
}
// TODO(vojta): Figure out why `typeAnnotation` has different structure when used with a variable.
// TODO(vojta): Figure out why `typeNameNode` has different structure when used with a variable.
// This should probably be fixed in Traceur.
var typeName = typeAnnotation.typeToken && typeAnnotation.typeToken.value || (typeAnnotation.name && typeAnnotation.name.value) || null;
var typeName = typeNameNode.typeToken && typeNameNode.typeToken.value || (typeNameNode.name && typeNameNode.name.value) || null;
if (!typeName) {
return;
}
this.write_(this.normalizeType_(typeName));
this.writeSpace_();
if (args.length) {
this.write_(OPEN_ANGLE);
this.writeType_(args[0]);
for (var i=1; i<args.length; i++) {
this.write_(COMMA);
this.writeSpace_();
this.writeType_(args[i]);
}
this.write_(CLOSE_ANGLE);
}
}
// EXPORTS
@ -427,7 +451,7 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
this.write_(STATIC);
this.writeSpace_();
}
this.writeType_(tree.typeAnnotation);
this.writeTypeAndSpace_(tree.typeAnnotation);
this.writeSpace_();
this.write_(GET);
this.writeSpace_();

View File

@ -8,28 +8,15 @@ export class Parser extends TraceurParser {
super(file, errorReporter);
}
parseTypeName_() {
// Copy of original implementation
var typeName = super.parseTypeName_();
// Generics support
if (this.eatIf_(OPEN_ANGLE)) {
var generics = [];
do {
generics.push(this.eatId_());
} while(this.eatIf_(COMMA));
this.eat_(CLOSE_ANGLE);
// TODO: save the generics into the typeName and use them e.g. for assertions, ...
}
return typeName;
}
// TODO: add support for object type literals to traceur!
parseObjectType_() {
//TODO(misko): save the type information
this.eat_(OPEN_CURLY);
do {
var identifier = this.eatId_();
this.eat_(COLON);
var type = this.parseNamedOrPredefinedType_();
var typeParameters = this.parseTypeParametersOpt_();
// TODO(misko): save the type information
} while (this.eatIf_(COMMA));
this.eat_(CLOSE_CURLY);
}

View File

@ -0,0 +1,43 @@
// Based on https://github.com/google/traceur-compiler/blob/master/src/codegeneration/TypeToExpressionTransformer.js
// Copyright 2012 Traceur Authors.
// Licensed under the Apache License, Version 2.0 (the 'License');
//
// Modifications:
// - use `assert` import, instead of `$traceurRuntime....` so
// that a transpilation to ES6 does not contain any traceur references.
import {ParseTreeTransformer} from 'traceur/src/codegeneration/ParseTreeTransformer.js';
import {
ArgumentList,
IdentifierExpression,
MemberExpression
} from 'traceur/src/syntax/trees/ParseTrees.js';
import {
parseExpression
} from 'traceur/src/codegeneration/PlaceholderParser.js';
export class TypeToExpressionTransformer extends ParseTreeTransformer {
transformTypeName(tree) {
if (tree.moduleName) {
var operand = this.transformAny(tree.moduleName);
return new MemberExpression(tree.location, operand, tree.name);
}
return new IdentifierExpression(tree.location, tree.name);
}
transformPredefinedType(tree) {
return parseExpression `assert.type.${tree.typeToken})`;
}
transformTypeReference(tree) {
var typeName = this.transformAny(tree.typeName);
var args = this.transformAny(tree.args);
var argumentList = new ArgumentList(tree.location, [typeName, ...args]);
return parseExpression `assert.genericType(${argumentList})`;
}
transformTypeArguments(tree) {
return this.transformList(tree.args);
}
}

View File

@ -1,21 +1,24 @@
var compiler = require('../index');
// fixme: copied from top of gulpfile
var OPTIONS = {
sourceMaps: true,
var DEFAULT_OPTIONS = {
sourceMaps: false,
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
memberVariables: true, // parse class fields
outputLanguage: 'dart'
memberVariables: true // parse class fields
};
describe('transpile to dart', function(){
var options;
beforeEach(function() {
options = merge(DEFAULT_OPTIONS, {outputLanguage: 'dart'});
});
// https://github.com/angular/angular/issues/509
describe('string interpolation', function() {
it('should not interpolate inside old quotes', function(){
var result = compiler.compile(OPTIONS, "test.js",
var result = compiler.compile(options, "test.js",
"var a:number = 1;" +
"var s1:string = \"${a}\";" +
"var s2:string = '\\${a}';" +
@ -28,7 +31,7 @@ describe('transpile to dart', function(){
});
it('should not interpolate without curly braces', function() {
var result = compiler.compile(OPTIONS, "test.js",
var result = compiler.compile(options, "test.js",
"var a:number = 1;" +
"var s1:string = `$a`;" +
"var s2:string = `\\$a`;");
@ -39,7 +42,7 @@ describe('transpile to dart', function(){
});
it('should interpolate inside template quotes', function() {
var result = compiler.compile(OPTIONS, "test.js",
var result = compiler.compile(options, "test.js",
"var a:number = 1;" +
"var s1:string = `${a}`;");
expect(result.js).toBe("library test_dart;\n" +
@ -47,4 +50,121 @@ describe('transpile to dart', function(){
"String s1 = '''${a}''';\n");
});
});
describe('generic', function() {
it('should support types without generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List = [];");
expect(result.js).toBe("library test_dart;\nList a = [];\n");
});
it('should support one level generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<string> = [];");
expect(result.js).toBe("library test_dart;\nList<String> a = [];\n");
});
it('should support multiple one level generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<A,B> = [];");
expect(result.js).toBe("library test_dart;\nList<A, B> a = [];\n");
});
it('should support nested generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<A<B>> = [];");
expect(result.js).toBe("library test_dart;\nList<A<B>> a = [];\n");
});
});
});
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",
"class Test {" +
" constructor() { super(); }" +
"}");
expect(result.js).toBe("class Test {\n" +
" constructor() {\n"+
" super();\n"+
" }\n"+
"}\n\n"+
"//# sourceMappingURL=test.map\n");
});
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');
});
});
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;
}