feat(transpiler): class fields for Dart

This commit is contained in:
Vojta Jina 2014-11-07 09:31:51 -08:00
parent b4ff802e28
commit d16d6a02ab
8 changed files with 73 additions and 2 deletions

View File

@ -25,6 +25,7 @@ var js2es5Options = {
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
memberVariables: true, // parse class fields
modules: 'instantiate'
};
@ -42,6 +43,7 @@ var js2dartOptions = {
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
memberVariables: true, // parse class fields
outputLanguage: 'dart'
};

View File

@ -60,6 +60,7 @@ module.exports = function(config) {
sourceMaps: true,
script: false,
modules: 'register',
memberVariables: true,
types: true,
// typeAssertions: true,
// typeAssertionModule: 'assert',

View File

@ -36,6 +36,7 @@ module.exports = function(config) {
outputLanguage: 'es5',
sourceMaps: true,
script: false,
memberVariables: true,
modules: 'instantiate',
types: true,
typeAssertions: true,

View File

@ -6,6 +6,8 @@ import 'package:unittest/unittest.dart' hide expect;
import 'dart:mirrors';
import 'dart:async';
bool IS_DARTIUM = true;
Expect expect(actual, [matcher]) {
final expect = new Expect(actual);
if (matcher != null) expect.to(matcher);

View File

@ -7,6 +7,7 @@ export var iit = window.iit;
export var beforeEach = window.beforeEach;
export var afterEach = window.afterEach;
export var expect = window.expect;
export var IS_DARTIUM = false;
// To make testing consistent between dart and js
window.print = function(msg) {

View File

@ -49,6 +49,11 @@ class HasGetters {
}
}
class WithFields {
name: string;
static id: number;
}
export function main() {
describe('classes', function() {
it('should work', function() {
@ -89,6 +94,14 @@ export function main() {
expect(HasGetters.staticGetter).toEqual('getter');
});
});
describe('fields', function() {
it('should work', function() {
var obj = new WithFields();
obj.name = 'Vojta';
WithFields.id = 12;
});
});
});
}

View File

@ -1,4 +1,4 @@
import {describe, it, expect} from 'test_lib/test_lib';
import {describe, it, expect, IS_DARTIUM} from 'test_lib/test_lib';
class A {}
class B {}
@ -48,6 +48,12 @@ class Foo {
}
}
class WithFields {
name: string;
static id: number;
}
export function main() {
describe('types', function() {
it('should work', function() {
@ -58,5 +64,37 @@ export function main() {
f.typedVariables();
});
describe('class fields', function() {
it('should fail when setting wrong type value', function() {
var wf = new WithFields();
expect(function() {
wf.name = true;
}).toThrowError(IS_DARTIUM ?
// Dart
"type 'bool' is not a subtype of type 'String' of 'value'" :
// JavaScript
// TODO(vojta): Better error, it's not first argument, it's setting a field.
'Invalid arguments given!\n' +
' - 1st argument has to be an instance of string, got true'
);
});
});
describe('static class fields', function() {
it('should fail when setting wrong type value', function() {
expect(function() {
WithFields.id = true;
}).toThrowError(IS_DARTIUM ?
// Dart
"type 'bool' is not a subtype of type 'num' of 'id'" :
// JavaScript
// TODO(vojta): Better error, it's not first argument, it's setting a field.
'Invalid arguments given!\n' +
' - 1st argument has to be an instance of number, got true'
);
});
});
});
}
}

View File

@ -32,6 +32,19 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
this.libName = moduleName.replace(/\//g, '.').replace(/[^\w.\/]/g, '_');
}
// CLASS FIELDS
visitPropertyVariableDeclaration(tree) {
if (tree.isStatic) {
this.write_(STATIC);
this.writeSpace_();
}
this.writeType_(tree.typeAnnotation);
this.writeSpace_();
this.visitAny(tree.name);
this.write_(SEMI_COLON);
}
// VARIABLES - types
// ```
// var foo:bool = true;