Skip to content

Commit

Permalink
feat(transpiler): class fields for Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Nov 7, 2014
1 parent b4ff802 commit d16d6a0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};

Expand All @@ -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'
};

Expand Down
1 change: 1 addition & 0 deletions karma-dart.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = function(config) {
sourceMaps: true,
script: false,
modules: 'register',
memberVariables: true,
types: true,
// typeAssertions: true,
// typeAssertionModule: 'assert',
Expand Down
1 change: 1 addition & 0 deletions karma-js.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function(config) {
outputLanguage: 'es5',
sourceMaps: true,
script: false,
memberVariables: true,
modules: 'instantiate',
types: true,
typeAssertions: true,
Expand Down
2 changes: 2 additions & 0 deletions modules/test_lib/src/test_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions modules/test_lib/src/test_lib.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tools/transpiler/spec/classes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class HasGetters {
}
}

class WithFields {
name: string;
static id: number;
}

export function main() {
describe('classes', function() {
it('should work', function() {
Expand Down Expand Up @@ -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;
});
});
});

}
42 changes: 40 additions & 2 deletions tools/transpiler/spec/types_spec.js
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down Expand Up @@ -48,6 +48,12 @@ class Foo {
}
}

class WithFields {
name: string;
static id: number;
}


export function main() {
describe('types', function() {
it('should work', function() {
Expand All @@ -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'
);
});
});
});
}
}
13 changes: 13 additions & 0 deletions tools/transpiler/src/outputgeneration/DartParseTreeWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d16d6a0

Please sign in to comment.