Skip to content

Commit 035dc5b

Browse files
committed
feat(transpiler): add support for getters
1 parent 2f732c6 commit 035dc5b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

tools/transpiler/spec/classes_spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, it, expect} from 'test_lib/test_lib';
1+
import {ddescribe, describe, it, expect} from 'test_lib/test_lib';
22
import {CONST} from './fixtures/annotations';
33

44
// Constructor
@@ -36,6 +36,16 @@ class SubConst extends Const {
3636
}
3737
}
3838

39+
class HasGetters {
40+
get getter():string {
41+
return 'getter';
42+
}
43+
44+
static get staticGetter():string {
45+
return 'getter';
46+
}
47+
}
48+
3949
export function main() {
4050
describe('classes', function() {
4151
it('should work', function() {
@@ -60,6 +70,17 @@ export function main() {
6070
expect(subFoo.c).toBe(3);
6171
});
6272
});
73+
74+
describe("getters", function () {
75+
it("should call instance getters", function () {
76+
var obj = new HasGetters();
77+
expect(obj.getter).toEqual('getter');
78+
});
79+
80+
it("should call static getters", function () {
81+
expect(HasGetters.staticGetter).toEqual('getter');
82+
});
83+
});
6384
});
6485

6586
}

tools/transpiler/src/outputgeneration/DartParseTreeWriter.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import {
1818
STATIC
1919
} from 'traceur/src/syntax/TokenType';
2020

21+
import {
22+
GET
23+
} from 'traceur/src/syntax/PredefinedName';
24+
2125
import {ParseTreeWriter as JavaScriptParseTreeWriter, ObjectLiteralExpression} from 'traceur/src/outputgeneration/ParseTreeWriter';
2226
import {ImportedBinding, BindingIdentifier} from 'traceur/src/syntax/trees/ParseTrees';
2327
import {IdentifierToken} from 'traceur/src/syntax/IdentifierToken';
@@ -369,6 +373,21 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
369373
this.writeSpace_()
370374
}
371375

376+
visitGetAccessor(tree) {
377+
this.writeAnnotations_(tree.annotations);
378+
if (tree.isStatic) {
379+
this.write_(STATIC);
380+
this.writeSpace_();
381+
}
382+
this.writeType_(tree.typeAnnotation);
383+
this.writeSpace_();
384+
this.write_(GET);
385+
this.writeSpace_();
386+
this.visitAny(tree.name);
387+
this.writeSpace_();
388+
this.visitAny(tree.body);
389+
}
390+
372391
visitNamedParameterList(tree) {
373392
this.writeList_(tree.parameterNameAndValues, COMMA, false);
374393
}

0 commit comments

Comments
 (0)