diff --git a/gulpfile.js b/gulpfile.js index f605ece8d3..8224aedb21 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' }; diff --git a/karma-dart.conf.js b/karma-dart.conf.js index 8980f26c93..43f4ba02d5 100644 --- a/karma-dart.conf.js +++ b/karma-dart.conf.js @@ -60,6 +60,7 @@ module.exports = function(config) { sourceMaps: true, script: false, modules: 'register', + memberVariables: true, types: true, // typeAssertions: true, // typeAssertionModule: 'assert', diff --git a/karma-js.conf.js b/karma-js.conf.js index 22fbe33376..96a2d23628 100644 --- a/karma-js.conf.js +++ b/karma-js.conf.js @@ -36,6 +36,7 @@ module.exports = function(config) { outputLanguage: 'es5', sourceMaps: true, script: false, + memberVariables: true, modules: 'instantiate', types: true, typeAssertions: true, diff --git a/modules/test_lib/src/test_lib.dart b/modules/test_lib/src/test_lib.dart index 4709721732..ac94ec19d5 100644 --- a/modules/test_lib/src/test_lib.dart +++ b/modules/test_lib/src/test_lib.dart @@ -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); diff --git a/modules/test_lib/src/test_lib.es6 b/modules/test_lib/src/test_lib.es6 index fb420eb0ad..e3ddd49b21 100644 --- a/modules/test_lib/src/test_lib.es6 +++ b/modules/test_lib/src/test_lib.es6 @@ -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) { diff --git a/tools/transpiler/spec/classes_spec.js b/tools/transpiler/spec/classes_spec.js index da87bb2507..a9dcb1dd53 100644 --- a/tools/transpiler/spec/classes_spec.js +++ b/tools/transpiler/spec/classes_spec.js @@ -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; + }); + }); }); } diff --git a/tools/transpiler/spec/types_spec.js b/tools/transpiler/spec/types_spec.js index dd192ff9d9..998454fc98 100644 --- a/tools/transpiler/spec/types_spec.js +++ b/tools/transpiler/spec/types_spec.js @@ -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' + ); + }); + }); }); -} \ No newline at end of file +} diff --git a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js index 2f3e083d7f..cad44e4013 100644 --- a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js +++ b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js @@ -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;