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.
This commit is contained in:
Rado Kirov 2014-11-07 15:20:06 -08:00
parent 90fd1a9227
commit 4f416694a5
4 changed files with 35 additions and 32 deletions

View File

@ -1,9 +1,25 @@
import {describe, it, expect} from 'test_lib/test_lib';
import {Provide, readFirstAnnotation} from './fixtures/annotations';
import {readFirstAnnotation} from './fixtures/annotations';
import {CONST} from 'facade/lang';
class Inject {}
class Bar {}
class Provide {
@CONST()
constructor(token) {
this.token = token;
}
}
class AnnotateMe {
@CONST()
constructor({maybe = 'default'} = {}) {
this.maybe = maybe;
}
}
@Provide('Foo')
class Foo {
@Inject
@ -13,6 +29,12 @@ class Foo {
@Provide(Foo)
function baz() {}
@AnnotateMe()
class A {}
@AnnotateMe({maybe: 'yes'})
class B {}
function annotatedParams(@Inject(Foo) f, @Inject(Bar) b) {}
export function main() {
@ -22,5 +44,10 @@ export function main() {
var clazz = readFirstAnnotation(Foo);
expect(clazz instanceof Provide).toBe(true);
});
it('should work with named arguments', function() {
expect(readFirstAnnotation(A).maybe).toBe('default');
expect(readFirstAnnotation(B).maybe).toBe('yes');
});
});
}
}

View File

@ -1,20 +1,5 @@
import 'dart:mirrors';
// This class is not generated,
// but should be in the future.
//
// Problems:
// - Dart requires annotations to be const (which makes sense).
// Right now, I can't describe that in ES6.
class Provide {
final token;
const Provide(this.token);
}
class CONST {
const CONST();
}
// TODO: this api does not yet return an array as we don't have
// a nice array wrapper for Dart
readFirstAnnotation(clazz) {

View File

@ -1,18 +1,3 @@
// This class is not generated,
// but should be in the future.
//
// Problems:
// - Dart requires annotations to be const (which makes sense).
// Right now, I can't describe that in ES6.
export class Provide {
constructor(token) {
this.token = token;
}
}
export class CONST {
}
// TODO: this api does not yet return an array as we don't have
// a nice array wrapper for Dart
export function readFirstAnnotation(clazz) {

View File

@ -48,6 +48,12 @@ export class NamedParamsTransformer extends ParseTreeTransformer {
return tree;
}
transformAnnotation(tree) {
tree = super.transformAnnotation(tree);
if (tree.args) this._handleNamedParams(tree);
return tree;
}
_handleNamedParams(tree) {
if (this._isLastArgAnNonEmptyObjectLiteral(tree) &&
! this._isLastArgObjectLiteralWithQuotedKeys(tree)) {