Skip to content

Commit 00e23aa

Browse files
committed
Install the clang-format gulp task and run it on the main file
1 parent eace367 commit 00e23aa

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require('source-map-support').install();
22

3-
var formatter = require('gulp-tsformat');
3+
var formatter = require('gulp-clang-format');
44
var fs = require('fs');
55
var gulp = require('gulp');
66
var merge = require('merge2');

main.ts

+53-46
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import ts = require("typescript");
77
export function translateProgram(program: ts.Program): string {
88
var result: string = "";
99
program.getSourceFiles()
10-
.filter((sourceFile: ts.SourceFile) => sourceFile.fileName.indexOf(".d.ts") < 0)
10+
.filter((sourceFile: ts.SourceFile) =>
11+
sourceFile.fileName.indexOf(".d.ts") < 0)
1112
.forEach(emitDart);
1213
return result;
1314

@@ -16,9 +17,7 @@ export function translateProgram(program: ts.Program): string {
1617
result += str;
1718
}
1819

19-
function visitEach(nodes) {
20-
nodes.forEach(visit);
21-
}
20+
function visitEach(nodes) { nodes.forEach(visit); }
2221

2322
function visitList(nodes: ts.NodeArray<ts.Node>) {
2423
for (var i = 0; i < nodes.length; i++) {
@@ -48,14 +47,16 @@ export function translateProgram(program: ts.Program): string {
4847
throw new Error(`${file.fileName}:${pos.line}:${pos.character}: ${message}`);
4948
}
5049

51-
// Comments attach to all following AST nodes before the next 'physical' token. Track the earliest
50+
// Comments attach to all following AST nodes before the next 'physical'
51+
// token. Track the earliest
5252
// offset to avoid printing comments multiple times.
5353
// TODO(martinprobst): Refactor this.
5454
var lastCommentIdx;
5555

5656
function visit(node: ts.Node) {
5757
// console.log(`Node kind: ${node.kind} ${node.getText()}`);
58-
var comments = ts.getLeadingCommentRanges(node.getSourceFile().text, node.getFullStart());
58+
var comments = ts.getLeadingCommentRanges(node.getSourceFile().text,
59+
node.getFullStart());
5960
if (comments) {
6061
comments.forEach((c) => {
6162
if (c.pos <= lastCommentIdx) return;
@@ -73,12 +74,12 @@ export function translateProgram(program: ts.Program): string {
7374
break;
7475

7576
case ts.SyntaxKind.VariableDeclarationList:
76-
var varDeclList = <ts.VariableDeclarationList>node;
77+
var varDeclList = <ts.VariableDeclarationList> node;
7778
visitEach(varDeclList.declarations);
7879
break;
7980

8081
case ts.SyntaxKind.VariableDeclaration:
81-
var varDecl = <ts.VariableDeclaration>node;
82+
var varDecl = <ts.VariableDeclaration> node;
8283
if (varDecl.type) {
8384
visit(varDecl.type);
8485
} else {
@@ -102,7 +103,7 @@ export function translateProgram(program: ts.Program): string {
102103
break;
103104

104105
case ts.SyntaxKind.ParenthesizedExpression:
105-
var parenExpr = <ts.ParenthesizedExpression>node;
106+
var parenExpr = <ts.ParenthesizedExpression> node;
106107
emit('(');
107108
visit(parenExpr.expression);
108109
emit(')');
@@ -113,31 +114,31 @@ export function translateProgram(program: ts.Program): string {
113114
emit(';\n');
114115
break;
115116
case ts.SyntaxKind.ExpressionStatement:
116-
var expr = <ts.ExpressionStatement>node;
117+
var expr = <ts.ExpressionStatement> node;
117118
visit(expr.expression);
118119
emit(';');
119120
break;
120121
case ts.SyntaxKind.SwitchStatement:
121-
var switchStmt = <ts.SwitchStatement>node;
122+
var switchStmt = <ts.SwitchStatement> node;
122123
emit('switch (');
123124
visit(switchStmt.expression);
124125
emit(') {');
125126
visitEach(switchStmt.clauses);
126127
emit('}');
127128
break;
128129
case ts.SyntaxKind.CaseClause:
129-
var caseClause = <ts.CaseClause>node;
130+
var caseClause = <ts.CaseClause> node;
130131
emit('case');
131132
visit(caseClause.expression);
132133
emit(':');
133134
visitEach(caseClause.statements);
134135
break;
135136
case ts.SyntaxKind.DefaultClause:
136137
emit('default :');
137-
visitEach((<ts.DefaultClause>node).statements);
138+
visitEach((<ts.DefaultClause> node).statements);
138139
break;
139140
case ts.SyntaxKind.IfStatement:
140-
var ifStmt = <ts.IfStatement>node;
141+
var ifStmt = <ts.IfStatement> node;
141142
emit('if (');
142143
visit(ifStmt.expression);
143144
emit(')');
@@ -149,7 +150,7 @@ export function translateProgram(program: ts.Program): string {
149150
break;
150151

151152
case ts.SyntaxKind.ForStatement:
152-
var forStmt = <ts.ForStatement>node;
153+
var forStmt = <ts.ForStatement> node;
153154
emit('for (');
154155
if (forStmt.initializer) visit(forStmt.initializer);
155156
emit(';');
@@ -160,25 +161,26 @@ export function translateProgram(program: ts.Program): string {
160161
visit(forStmt.statement);
161162
break;
162163
case ts.SyntaxKind.ForInStatement:
163-
// TODO(martinprobst): Dart's for-in loops actually have different semantics, they are more
164+
// TODO(martinprobst): Dart's for-in loops actually have different
165+
// semantics, they are more
164166
// like for-of loops, iterating over collections.
165-
var forInStmt = <ts.ForInStatement>node;
166-
emit ('for (');
167+
var forInStmt = <ts.ForInStatement> node;
168+
emit('for (');
167169
if (forInStmt.initializer) visit(forInStmt.initializer);
168170
emit('in');
169171
visit(forInStmt.expression);
170172
emit(')');
171173
visit(forInStmt.statement);
172174
break;
173175
case ts.SyntaxKind.WhileStatement:
174-
var whileStmt = <ts.WhileStatement>node;
176+
var whileStmt = <ts.WhileStatement> node;
175177
emit('while (');
176178
visit(whileStmt.expression);
177179
emit(')');
178180
visit(whileStmt.statement);
179181
break;
180182
case ts.SyntaxKind.DoStatement:
181-
var doStmt = <ts.DoStatement>node;
183+
var doStmt = <ts.DoStatement> node;
182184
emit('do');
183185
visit(doStmt.statement);
184186
emit('while (');
@@ -192,11 +194,11 @@ export function translateProgram(program: ts.Program): string {
192194

193195
// Literals.
194196
case ts.SyntaxKind.NumericLiteral:
195-
var sLit = <ts.LiteralExpression>node;
197+
var sLit = <ts.LiteralExpression> node;
196198
emit(sLit.getText());
197199
break;
198200
case ts.SyntaxKind.StringLiteral:
199-
var sLit = <ts.LiteralExpression>node;
201+
var sLit = <ts.LiteralExpression> node;
200202
emit(JSON.stringify(sLit.text));
201203
break;
202204
case ts.SyntaxKind.TrueKeyword:
@@ -209,50 +211,50 @@ export function translateProgram(program: ts.Program): string {
209211
emit('null');
210212
break;
211213
case ts.SyntaxKind.RegularExpressionLiteral:
212-
emit((<ts.LiteralExpression>node).text);
214+
emit((<ts.LiteralExpression> node).text);
213215
break;
214216
case ts.SyntaxKind.ThisKeyword:
215217
emit('this');
216218
break;
217219

218220
case ts.SyntaxKind.PropertyAccessExpression:
219-
var propAccess = <ts.PropertyAccessExpression>node;
221+
var propAccess = <ts.PropertyAccessExpression> node;
220222
visit(propAccess.expression);
221223
emit('.');
222224
visit(propAccess.name);
223225
break;
224226
case ts.SyntaxKind.ElementAccessExpression:
225-
var elemAccess = <ts.ElementAccessExpression>node;
227+
var elemAccess = <ts.ElementAccessExpression> node;
226228
visit(elemAccess.expression);
227229
emit('[');
228230
visit(elemAccess.argumentExpression);
229231
emit(']');
230232
break;
231233
case ts.SyntaxKind.NewExpression:
232234
emit('new');
233-
visitCall(<ts.NewExpression>node);
235+
visitCall(<ts.NewExpression> node);
234236
break;
235237
case ts.SyntaxKind.CallExpression:
236-
visitCall(<ts.CallExpression>node);
238+
visitCall(<ts.CallExpression> node);
237239
break;
238240
case ts.SyntaxKind.BinaryExpression:
239-
var binExpr = <ts.BinaryExpression>node;
241+
var binExpr = <ts.BinaryExpression> node;
240242
visit(binExpr.left);
241243
emit(ts.tokenToString(binExpr.operatorToken.kind));
242244
visit(binExpr.right);
243245
break;
244246
case ts.SyntaxKind.PrefixUnaryExpression:
245-
var prefixUnary = <ts.PrefixUnaryExpression>node;
247+
var prefixUnary = <ts.PrefixUnaryExpression> node;
246248
emit(ts.tokenToString(prefixUnary.operator));
247249
visit(prefixUnary.operand);
248250
break;
249251
case ts.SyntaxKind.PostfixUnaryExpression:
250-
var postfixUnary = <ts.PostfixUnaryExpression>node;
252+
var postfixUnary = <ts.PostfixUnaryExpression> node;
251253
visit(postfixUnary.operand);
252254
emit(ts.tokenToString(postfixUnary.operator));
253255
break;
254256
case ts.SyntaxKind.ConditionalExpression:
255-
var conditional = <ts.ConditionalExpression>node;
257+
var conditional = <ts.ConditionalExpression> node;
256258
visit(conditional.condition);
257259
emit('?');
258260
visit(conditional.whenTrue);
@@ -274,15 +276,15 @@ export function translateProgram(program: ts.Program): string {
274276
break;
275277

276278
case ts.SyntaxKind.TypeReference:
277-
var typeRef = <ts.TypeReferenceNode>node;
279+
var typeRef = <ts.TypeReferenceNode> node;
278280
visit(typeRef.typeName);
279281
if (typeRef.typeArguments) {
280282
visitEach(typeRef.typeArguments);
281283
}
282284
break;
283285

284286
case ts.SyntaxKind.ClassDeclaration:
285-
var classDecl = <ts.ClassDeclaration>node;
287+
var classDecl = <ts.ClassDeclaration> node;
286288
emit('class');
287289
visit(classDecl.name);
288290
if (classDecl.typeParameters) {
@@ -300,7 +302,7 @@ export function translateProgram(program: ts.Program): string {
300302
break;
301303

302304
case ts.SyntaxKind.HeritageClause:
303-
var heritageClause = <ts.HeritageClause>node;
305+
var heritageClause = <ts.HeritageClause> node;
304306
if (heritageClause.token === ts.SyntaxKind.ExtendsKeyword) {
305307
emit('extends');
306308
} else {
@@ -311,12 +313,12 @@ export function translateProgram(program: ts.Program): string {
311313
break;
312314

313315
case ts.SyntaxKind.Constructor:
314-
var ctorDecl = <ts.ConstructorDeclaration>node;
316+
var ctorDecl = <ts.ConstructorDeclaration> node;
315317
// Find containing class name.
316318
var className;
317319
for (var parent = ctorDecl.parent; parent; parent = parent.parent) {
318320
if (parent.kind == ts.SyntaxKind.ClassDeclaration) {
319-
className = (<ts.ClassDeclaration>parent).name;
321+
className = (<ts.ClassDeclaration> parent).name;
320322
break;
321323
}
322324
}
@@ -326,7 +328,7 @@ export function translateProgram(program: ts.Program): string {
326328
break;
327329

328330
case ts.SyntaxKind.PropertyDeclaration:
329-
var propertyDecl = <ts.PropertyDeclaration>node;
331+
var propertyDecl = <ts.PropertyDeclaration> node;
330332
visit(propertyDecl.type);
331333
visit(propertyDecl.name);
332334
if (propertyDecl.initializer) {
@@ -337,22 +339,23 @@ export function translateProgram(program: ts.Program): string {
337339
break;
338340

339341
case ts.SyntaxKind.MethodDeclaration:
340-
var methodDecl = <ts.MethodDeclaration>node;
342+
var methodDecl = <ts.MethodDeclaration> node;
341343
if (methodDecl.type) visit(methodDecl.type);
342344
visit(methodDecl.name);
343345
visitFunctionLike(methodDecl);
344346
break;
345347

346348
case ts.SyntaxKind.FunctionDeclaration:
347-
var funcDecl = <ts.FunctionDeclaration>node;
349+
var funcDecl = <ts.FunctionDeclaration> node;
348350
if (funcDecl.type) visit(funcDecl.type);
349351
visit(funcDecl.name);
350352
visitFunctionLike(funcDecl);
351353
break;
352354

353355
case ts.SyntaxKind.Parameter:
354-
var paramDecl = <ts.ParameterDeclaration>node;
355-
if (paramDecl.dotDotDotToken) reportError(node, 'rest parameters are unsupported');
356+
var paramDecl = <ts.ParameterDeclaration> node;
357+
if (paramDecl.dotDotDotToken)
358+
reportError(node, 'rest parameters are unsupported');
356359
if (paramDecl.initializer) emit('[');
357360
if (paramDecl.type) visit(paramDecl.type);
358361
visit(paramDecl.name);
@@ -365,18 +368,19 @@ export function translateProgram(program: ts.Program): string {
365368

366369
case ts.SyntaxKind.ReturnStatement:
367370
emit('return');
368-
visit((<ts.ReturnStatement>node).expression);
371+
visit((<ts.ReturnStatement> node).expression);
369372
emit(';');
370373
break;
371374

372375
case ts.SyntaxKind.Block:
373376
emit('{');
374-
visitEach((<ts.Block>node).statements);
377+
visitEach((<ts.Block> node).statements);
375378
emit('}');
376379
break;
377380

378381
default:
379-
reportError(node, "Unsupported node type " + (<any>ts).SyntaxKind[node.kind]);
382+
reportError(
383+
node, "Unsupported node type " + (<any> ts).SyntaxKind[node.kind]);
380384
break;
381385
}
382386
}
@@ -387,11 +391,14 @@ export function translateProgram(program: ts.Program): string {
387391
}
388392

389393
export function translateFiles(fileNames: string[]): string {
390-
var options: ts.CompilerOptions = { target: ts.ScriptTarget.ES6, module: ts.ModuleKind.CommonJS };
394+
var options: ts.CompilerOptions = {
395+
target: ts.ScriptTarget.ES6,
396+
module: ts.ModuleKind.CommonJS
397+
};
391398
var host = ts.createCompilerHost(options);
392399
var program = ts.createProgram(fileNames, options, host);
393400
return translateProgram(program);
394401
}
395402

396403
// CLI entry point
397-
translateFiles(process.argv.slice(2));
404+
translateFiles(process.argv.slice(2));

0 commit comments

Comments
 (0)