Skip to content

Commit

Permalink
Transpile console.log to print (dart-archive#352)
Browse files Browse the repository at this point in the history
Note: as suggested in the issue, we could transpile to
window.console.log if it had a varargs signature. However, that would
introduce a dependency to dart:html, which seems overkill.
  • Loading branch information
ochafik committed Apr 22, 2016
1 parent 93ccbb0 commit 0724499
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lib/facade_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,17 @@ export class FacadeConverter extends base.TranspilerBase {
};

private stdlibHandlers: ts.Map<CallHandler> = merge(this.es6Promises, {
'Console.log': (c: ts.CallExpression, context: ts.Expression) => {
this.emit('print(');
if (c.arguments.length === 1) {
this.visit(c.arguments[0]);
} else {
this.emit('[');
this.visitList(c.arguments);
this.emit('].join(" ")');
}
this.emit(')');
},
'Array.push': (c: ts.CallExpression, context: ts.Expression) => {
this.visit(context);
this.emitMethodCall('add', c.arguments);
Expand Down
17 changes: 11 additions & 6 deletions test/facade_converter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ num y = x.fold(0, (a, b) => a + b);`);
num y = x.fold(null, (a, b) => a + b);`);
});

it('translates console.log', () => {
expectWithTypes(`console.log(1);`).to.equal('print(1);');
expectWithTypes(`console.log(1, 2);`).to.equal('print([1, 2].join(" "));');
});

it('translates string methoids',
() => { expectErroneousWithType(`var x = 'asd'.substr(0, 1);`).to.throw(/use substring/); });

Expand Down Expand Up @@ -293,9 +298,9 @@ Future<dynamic> x = (() {
})();
void fn() {
x.then((v) {
console.log(v);
print(v);
}).catchError((err) {
console.log(err);
print(err);
});
}`);
expectWithTypes(
Expand All @@ -306,9 +311,9 @@ void fn() {
dynamic /* () => Promise<number> */ fn;
main() {
fn().then((v) {
console.log(v);
print(v);
}).catchError((err) {
console.log(err);
print(err);
});
}`);
expectWithTypes(
Expand All @@ -319,9 +324,9 @@ main() {
dynamic /* () => Promise<number> */ fn;
main() {
fn().then((v) {
console.log(v);
print(v);
}).catchError((err) {
console.log(err);
print(err);
});
}`);
});
Expand Down

0 comments on commit 0724499

Please sign in to comment.