Files
sdk/pkg/dev_compiler/test/codegen/closure.dart
T
Olivier Chafik 4ad758998a Emit TypeScript / Closure ES6_TYPED type annotations in --closure mode (instead of closure comments).
- Replaced ClosureType by a general JS.TypeRef, used in AST for identifier types, return types
- Convert DartType to JS.TypeRef (in mixin JsTypeRefCodegen), including type parameters (also added to AST in Fun & ClassExpression)
- Emit field declarations expected by TS
- Wrote a TypeScriptTypePrinter (mixed in by Printer) and a ClosureTypePrinter (might disappear soon)
- Simplified annotation code, called in more places (seems to gives more source info)

Example input:

  List/*<T>*/ func/*<T>*/(List/*<T>*/ items, dynamic/*=T*/ seed) {}

  class Foo<T> {
    int i;
    static var x;
    Foo(this.i, o, {String v : "?"}) {}
  }

Output:

  function func<T>(items: core.List<T>, seed: T): core.List<T> {}

  const Foo$ = dart.generic(function(T) {
    class Foo<T> extends core.Object {
      i: number;
      static x;
      Foo(i: number, o, {v = "?"}: {v?: string} = {}) {
        this.i = i;
      }
    }
    ...
    Foo.x = null;
    return Foo;
  });

Known remaining issues:
- typedefs expect a `type Callback = (...) => ...;` statement
- `exports` is a reserved keyword in TS (either we change the way we do exports, or we'll need a different temp + extra type annotations of the default-exported object).
- Generic type is currently locked inside the generic call. Might be able to solve by exporting signatures in .d.ts file, or changing the way we do generics.

BUG=
R=jmesserly@google.com

Review URL: https://codereview.chromium.org/1676463002 .
2016-02-10 18:22:21 +00:00

66 lines
1.3 KiB
Dart

library test;
import 'dart:js';
List/*<T>*/ generic_function/*<T>*/(List/*<T>*/ items, dynamic/*=T*/ seed) {
var strings = items.map((i) => "$i").toList();
return items;
}
typedef void Callback({int i});
class Foo<T> {
final int i;
bool b;
String s;
T v;
Foo(this.i, this.v);
factory Foo.build() => new Foo(1, null);
untyped_method(a, b) {}
T pass(T t) => t;
String typed_method(
Foo foo, List list,
int i, num n, double d, bool b, String s,
JsArray a, JsObject o, JsFunction f) {
return '';
}
optional_params(a, [b, int c]) {}
static named_params(a, {b, int c}) {}
nullary_method() {}
function_params(int f(x, [y]), g(x, {String y, z}), Callback cb) {
cb(i: i);
}
run(List a, String b, List c(String d), List<int> e(f(g)), {Map<Map, Map> h}) {}
String get prop => null;
set prop(String value) {}
static String get staticProp => null;
static set staticProp(String value) {}
static const String some_static_constant = "abc";
static final String some_static_final = "abc";
static String some_static_var = "abc";
}
class Bar {}
class Baz extends Foo<int> with Bar {
Baz(int i) : super(i, 123);
}
void main(args) {}
const String some_top_level_constant = "abc";
final String some_top_level_final = "abc";
String some_top_level_var = "abc";