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

@ -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;