Skip to content

Commit 4f41669

Browse files
committed
bug(transpiler): Support optional arguments in annotations.
Clean-up: move annotaitons out of fixtures/annotations, since we have the @const annotation in the transpiler already.
1 parent 90fd1a9 commit 4f41669

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

tools/transpiler/spec/annotations_spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import {describe, it, expect} from 'test_lib/test_lib';
2-
import {Provide, readFirstAnnotation} from './fixtures/annotations';
2+
import {readFirstAnnotation} from './fixtures/annotations';
3+
import {CONST} from 'facade/lang';
34

45
class Inject {}
56
class Bar {}
67

8+
class Provide {
9+
@CONST()
10+
constructor(token) {
11+
this.token = token;
12+
}
13+
}
14+
15+
class AnnotateMe {
16+
@CONST()
17+
constructor({maybe = 'default'} = {}) {
18+
this.maybe = maybe;
19+
}
20+
}
21+
22+
723
@Provide('Foo')
824
class Foo {
925
@Inject
@@ -13,6 +29,12 @@ class Foo {
1329
@Provide(Foo)
1430
function baz() {}
1531

32+
@AnnotateMe()
33+
class A {}
34+
35+
@AnnotateMe({maybe: 'yes'})
36+
class B {}
37+
1638
function annotatedParams(@Inject(Foo) f, @Inject(Bar) b) {}
1739

1840
export function main() {
@@ -22,5 +44,10 @@ export function main() {
2244
var clazz = readFirstAnnotation(Foo);
2345
expect(clazz instanceof Provide).toBe(true);
2446
});
47+
48+
it('should work with named arguments', function() {
49+
expect(readFirstAnnotation(A).maybe).toBe('default');
50+
expect(readFirstAnnotation(B).maybe).toBe('yes');
51+
});
2552
});
26-
}
53+
}

tools/transpiler/spec/fixtures/annotations.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import 'dart:mirrors';
22

3-
// This class is not generated,
4-
// but should be in the future.
5-
//
6-
// Problems:
7-
// - Dart requires annotations to be const (which makes sense).
8-
// Right now, I can't describe that in ES6.
9-
class Provide {
10-
final token;
11-
const Provide(this.token);
12-
}
13-
14-
class CONST {
15-
const CONST();
16-
}
17-
183
// TODO: this api does not yet return an array as we don't have
194
// a nice array wrapper for Dart
205
readFirstAnnotation(clazz) {

tools/transpiler/spec/fixtures/annotations.es6

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
// This class is not generated,
2-
// but should be in the future.
3-
//
4-
// Problems:
5-
// - Dart requires annotations to be const (which makes sense).
6-
// Right now, I can't describe that in ES6.
7-
export class Provide {
8-
constructor(token) {
9-
this.token = token;
10-
}
11-
}
12-
13-
export class CONST {
14-
}
15-
161
// TODO: this api does not yet return an array as we don't have
172
// a nice array wrapper for Dart
183
export function readFirstAnnotation(clazz) {

tools/transpiler/src/codegeneration/NamedParamsTransformer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export class NamedParamsTransformer extends ParseTreeTransformer {
4848
return tree;
4949
}
5050

51+
transformAnnotation(tree) {
52+
tree = super.transformAnnotation(tree);
53+
if (tree.args) this._handleNamedParams(tree);
54+
return tree;
55+
}
56+
5157
_handleNamedParams(tree) {
5258
if (this._isLastArgAnNonEmptyObjectLiteral(tree) &&
5359
! this._isLastArgObjectLiteralWithQuotedKeys(tree)) {

0 commit comments

Comments
 (0)