Skip to content

Commit c43031f

Browse files
jacob314mprobst
authored andcommitted
Integrate Dart Formatter directly into ts2dart for cleaner output
and more readable test cases.
1 parent 4a23de9 commit c43031f

18 files changed

+654
-434
lines changed

gulpfile.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var sourcemaps = require('gulp-sourcemaps');
1212
var spawn = require('child_process').spawn;
1313
var ts = require('gulp-typescript');
1414
var typescript = require('typescript');
15+
var style = require('dart-style');
1516
var which = require('which');
1617

1718
gulp.task('test.check-format', function() {
@@ -54,7 +55,10 @@ gulp.task('test.compile', ['compile'], function(done) {
5455
done();
5556
return;
5657
}
57-
return gulp.src(['test/*.ts', 'typings/**/*.d.ts'], {base: '.'})
58+
return gulp
59+
.src(
60+
['test/*.ts', 'typings/**/*.d.ts', 'node_modules/dart-style/dart-style.d.ts'],
61+
{base: '.'})
5862
.pipe(sourcemaps.init())
5963
.pipe(ts(tsProject))
6064
.on('error', onError)
@@ -84,7 +88,7 @@ gulp.task('test.e2e', ['test.compile'], function(done) {
8488

8589
// run node with a shell so we can wildcard all the .ts files
8690
var cmd = 'node ../lib/main.js --translateBuiltins --basePath=. --destination=. ' +
87-
'*.ts angular2/src/facade/lang.d.ts';
91+
'*.ts angular2/src/facade/lang.d.ts';
8892
// Paths must be relative to our source root, so run with cwd == dir.
8993
spawn('sh', ['-c', cmd], {stdio: 'inherit', cwd: dir}).on('close', function(code, signal) {
9094
if (code > 0) {

lib/base.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ export class TranspilerBase {
6363

6464
isConst(decl: ClassLike) {
6565
return this.hasAnnotation(decl.decorators, 'CONST') ||
66-
(<ts.NodeArray<ts.Declaration>>decl.members).some((m) => {
67-
if (m.kind !== ts.SyntaxKind.Constructor) return false;
68-
return this.hasAnnotation(m.decorators, 'CONST');
69-
});
66+
(<ts.NodeArray<ts.Declaration>>decl.members).some((m) => {
67+
if (m.kind !== ts.SyntaxKind.Constructor) return false;
68+
return this.hasAnnotation(m.decorators, 'CONST');
69+
});
7070
}
7171

7272
getRelativeFileName(fileName: string): string {

lib/expression.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default class ExpressionTranspiler extends base.TranspilerBase {
2424
if (operatorKind === ts.SyntaxKind.InstanceOfKeyword) {
2525
this.emit('is');
2626
this.fc.visitTypeName(<ts.Identifier>binExpr.right);
27+
} else if (operatorKind == ts.SyntaxKind.InKeyword) {
28+
this.reportError(node, 'in operator is unsupported');
2729
} else {
2830
this.emit(ts.tokenToString(binExpr.operatorToken.kind));
2931
this.visit(binExpr.right);

lib/facade_converter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class FacadeConverter extends base.TranspilerBase {
197197
private reportMissingType(n: ts.Node, ident: string) {
198198
this.reportError(
199199
n, `Untyped property access to "${ident}" which could be ` + `a special ts2dart builtin. ` +
200-
`Please add type declarations to disambiguate.`);
200+
`Please add type declarations to disambiguate.`);
201201
}
202202

203203
isInsideConstExpr(node: ts.Node): boolean {

lib/main.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import StatementTranspiler from './statement';
1414
import TypeTranspiler from './type';
1515
import LiteralTranspiler from './literal';
1616
import {FacadeConverter} from './facade_converter';
17+
import * as dartStyle from 'dart-style';
1718

1819
export interface TranspilerOptions {
1920
/**
@@ -173,7 +174,16 @@ export class Transpiler {
173174
new Output(sourceFile, this.getRelativeFileName(), this.options.generateSourceMap);
174175
this.lastCommentIdx = -1;
175176
this.visit(sourceFile);
176-
return this.output.getResult();
177+
var result = this.output.getResult();
178+
return this.formatCode(result, sourceFile);
179+
}
180+
181+
private formatCode(code: string, context: ts.Node) {
182+
var result = dartStyle.formatCode(code);
183+
if (result.error) {
184+
this.reportError(context, result.error);
185+
}
186+
return result.code;
177187
}
178188

179189
private checkForErrors(program: ts.Program) {
@@ -325,7 +335,7 @@ class Output {
325335
}
326336
}
327337

328-
getResult(): string { return this.result + this.generateSourceMapComment(); }
338+
getResult(): string { return this.result; }
329339

330340
addSourceMapping(n: ts.Node) {
331341
if (!this.sourceMap) return; // source maps disabled.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
"test": "test"
99
},
1010
"dependencies": {
11+
"dart-style": "^0.2.6",
1112
"minimist": "^1.1.1",
1213
"source-map": "^0.4.2",
1314
"source-map-support": "^0.3.1",
1415
"typescript": "mprobst/TypeScript#pathMappingModuleResolution"
1516
},
1617
"devDependencies": {
1718
"chai": "^2.1.1",
18-
"clang-format": "^1.0.25",
19+
"clang-format": "^1.0.34",
1920
"fs-extra": "^0.18.0",
2021
"gulp": "^3.8.11",
2122
"gulp-clang-format": "^1.0.21",

test/call_test.ts

+36-27
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,59 @@ import {expectTranslate, expectErroneousCode} from './test_support';
44
describe('calls', () => {
55
it('translates destructuring parameters', () => {
66
expectTranslate('function x({p = null, d = false} = {}) {}')
7-
.to.equal(' x ( { p : null , d : false } ) { }');
7+
.to.equal('x({p: null, d: false}) {}');
88
expectErroneousCode('function x({a=false}={a:true})')
99
.to.throw('initializers for named parameters must be empty object literals');
1010
expectErroneousCode('function x({a=false}=true)')
1111
.to.throw('initializers for named parameters must be empty object literals');
12-
expectTranslate('class X { constructor() { super({p: 1}); } }')
13-
.to.equal(
14-
' class X { X ( ) : super ( p : 1 ) {' +
15-
' /* super call moved to initializer */ ; } }');
12+
expectTranslate('class X { constructor() { super({p: 1}); } }').to.equal(`class X {
13+
X() : super(p: 1) {
14+
/* super call moved to initializer */;
15+
}
16+
}`);
1617
});
1718
it('hacks last object literal parameters into named parameter', () => {
18-
expectTranslate('f(x, {a: 12, b: 4});').to.equal(' f ( x , a : 12 , b : 4 ) ;');
19-
expectTranslate('f({a: 12});').to.equal(' f ( a : 12 ) ;');
20-
expectTranslate('f({"a": 12});').to.equal(' f ( { "a" : 12 } ) ;');
21-
expectTranslate('new X(x, {a: 12, b: 4});').to.equal(' new X ( x , a : 12 , b : 4 ) ;');
22-
expectTranslate('f(x, {});').to.equal(' f ( x , { } ) ;');
19+
expectTranslate('f(x, {a: 12, b: 4});').to.equal('f(x, a: 12, b: 4);');
20+
expectTranslate('f({a: 12});').to.equal('f(a: 12);');
21+
expectTranslate('f({"a": 12});').to.equal('f({"a": 12});');
22+
expectTranslate('new X(x, {a: 12, b: 4});').to.equal('new X(x, a: 12, b: 4);');
23+
expectTranslate('f(x, {});').to.equal('f(x, {});');
2324
});
2425
it('translates calls', () => {
25-
expectTranslate('foo();').to.equal(' foo ( ) ;');
26-
expectTranslate('foo(1, 2);').to.equal(' foo ( 1 , 2 ) ;');
26+
expectTranslate('foo();').to.equal('foo();');
27+
expectTranslate('foo(1, 2);').to.equal('foo(1, 2);');
2728
});
2829
it('translates new calls', () => {
29-
expectTranslate('new Foo();').to.equal(' new Foo ( ) ;');
30-
expectTranslate('new Foo(1, 2);').to.equal(' new Foo ( 1 , 2 ) ;');
31-
expectTranslate('new Foo<number, string>(1, 2);')
32-
.to.equal(' new Foo < num , String > ( 1 , 2 ) ;');
30+
expectTranslate('new Foo();').to.equal('new Foo();');
31+
expectTranslate('new Foo(1, 2);').to.equal('new Foo(1, 2);');
32+
expectTranslate('new Foo<number, string>(1, 2);').to.equal('new Foo<num, String>(1, 2);');
3333
});
3434
it('throws away generic type parameters',
35-
() => { expectTranslate('var s = foo<string>();').to.equal(' var s = foo ( ) ;'); });
35+
() => { expectTranslate('var s = foo<string>();').to.equal('var s = foo();'); });
3636
it('translates "super()" constructor calls', () => {
37-
expectTranslate('class X { constructor() { super(1); } }')
38-
.to.equal(' class X { X ( ) : super ( 1 ) { /* super call moved to initializer */ ; } }');
37+
expectTranslate('class X { constructor() { super(1); } }').to.equal(`class X {
38+
X() : super(1) {
39+
/* super call moved to initializer */;
40+
}
41+
}`);
3942
expectErroneousCode('class X { constructor() { if (y) super(1, 2); } }')
4043
.to.throw('super calls must be immediate children of their constructors');
41-
expectTranslate('class X { constructor() { a(); super(1); b(); } }')
42-
.to.equal(
43-
' class X { X ( ) : super ( 1 ) {' +
44-
' a ( ) ; /* super call moved to initializer */ ; b ( ) ;' +
45-
' } }');
44+
expectTranslate('class X { constructor() { a(); super(1); b(); } }').to.equal(`class X {
45+
X() : super(1) {
46+
a();
47+
/* super call moved to initializer */
48+
;
49+
b();
50+
}
51+
}`);
4652
});
4753
it('translates "super.x()" super method calls', () => {
48-
expectTranslate('class X { y() { super.z(1); } }')
49-
.to.equal(' class X { y ( ) { super . z ( 1 ) ; } }');
54+
expectTranslate('class X { y() { super.z(1); } }').to.equal(`class X {
55+
y() {
56+
super.z(1);
57+
}
58+
}`);
5059
});
5160
it('transpiles new calls without arguments',
52-
() => { expectTranslate('new Foo;').to.equal(' new Foo ( ) ;'); });
61+
() => { expectTranslate('new Foo;').to.equal('new Foo();'); });
5362
});

0 commit comments

Comments
 (0)