1
1
///<reference path='node_modules/typescript/lib/typescript.d.ts' />
2
+ /////<reference path='../TypeScript/lib/typescript.d.ts' />
2
3
///<reference path='node.d.ts' />
3
4
4
- import ts = require ( ' typescript' ) ;
5
- import fs = require ( 'fs' ) ;
6
- import path = require ( ' path' ) ;
5
+ import * as ts from " typescript" ;
6
+ import * as fs from "fs" ;
7
+ import * as path from " path" ;
7
8
8
- // derived from getAllBinaryExpressions https://github.com/Microsoft/TypeScript/issues/254
9
- function getAllInterfaces ( root : ts . Node ) {
10
- var result : ts . InterfaceDeclaration [ ] = [ ] ;
11
- aggregate ( root ) ;
12
- return result ;
9
+ // https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#pretty-printer-using-the-ls-formatter
10
+ // https://github.com/Microsoft/TypeScript/blob/master/src/compiler/parser.ts
13
11
14
- function aggregate ( node : ts . Node ) : void {
15
- if ( node . kind === ts . SyntaxKind . InterfaceDeclaration ) {
16
- result . push ( < ts . InterfaceDeclaration > node ) ;
17
- }
18
- ts . forEachChild ( node , aggregate ) ;
19
- }
12
+ export function main ( ) {
13
+ let source = "var a=function(v:number){return 0+1+2+3;}" ;
14
+ let sourceFile = ts . createSourceFile ( "file.ts" , source , ts . ScriptTarget . Latest , true ) ;
15
+
16
+ // It would be nice if I could create it from scratch.
17
+ //let sourceFile = <ts.SourceFile>ts.createNode(ts.SyntaxKind.SourceFile);
18
+ //sourceFile.text = "";
19
+
20
+ let stmt = < ts . VariableStatement > ts . createNode ( ts . SyntaxKind . VariableStatement ) ;
21
+ let vdl = < ts . VariableDeclarationList > ts . createNode ( ts . SyntaxKind . VariableDeclarationList ) ;
22
+ let vd = < ts . VariableDeclaration > ts . createNode ( ts . SyntaxKind . VariableDeclaration ) ;
23
+ let id = < ts . Identifier > ts . createNode ( ts . SyntaxKind . Identifier ) ;
24
+ let t = < ts . LiteralExpression > ts . createNode ( ts . SyntaxKind . FirstLiteralToken ) ;
25
+ t . text = "1" ;
26
+ id . text = "a" ;
27
+ vd . name = id ;
28
+ vdl . declarations = < ts . NodeArray < ts . VariableDeclaration > > [ vd ] ;
29
+ stmt . declarationList = vdl ;
30
+
31
+ //sourceFile.statements = <ts.NodeArray<ts.Statement>>[];
32
+ //sourceFile.statements[0] = stmt;
33
+ sourceFile . statements = < ts . NodeArray < ts . Statement > > [ < ts . Statement > stmt ] ;
34
+
35
+ let formatCodeOptions = getDefaultOptions ( ) ;
36
+ let rulesProvider = getRuleProvider ( formatCodeOptions ) ;
37
+
38
+ let edits = ( < any > ts ) . formatting . formatDocument ( sourceFile , rulesProvider , formatCodeOptions ) ;
39
+ let code = applyEdits ( sourceFile . text , edits ) ;
40
+ console . log ( code ) ;
20
41
}
21
42
22
- export function main ( ) {
23
- var filename = process . cwd ( ) + '/node_modules/typescript/lib/typescript.d.ts'
24
- var source = String ( fs . readFileSync ( filename ) ) ;
43
+ function getRuleProvider ( options : ts . FormatCodeOptions ) {
44
+ // Share this between multiple formatters using the same options.
45
+ // This represents the bulk of the space the formatter uses.
46
+ let ruleProvider = new ( < any > ts ) . formatting . RulesProvider ( ) ;
47
+ ruleProvider . ensureUpToDate ( options ) ;
48
+ return ruleProvider ;
49
+ }
25
50
26
- var sf = ts . createSourceFile ( filename , source , ts . ScriptTarget . Latest ) ;
51
+ function applyEdits ( text : string , edits : ts . TextChange [ ] ) : string {
52
+ // Apply edits in reverse on the existing text
53
+ let result = text ;
54
+ for ( let i = edits . length - 1 ; i >= 0 ; i -- ) {
55
+ let change = edits [ i ] ;
56
+ let head = result . slice ( 0 , change . span . start ) ;
57
+ let tail = result . slice ( change . span . start + change . span . length )
58
+ result = head + change . newText + tail ;
59
+ }
60
+ return result ;
61
+ }
27
62
28
- getAllInterfaces ( sf )
29
- . map ( ifd => ifd . name . text )
30
- . sort ( )
31
- . forEach ( nm => console . log ( nm ) ) ;
63
+ function getDefaultOptions ( ) : ts . FormatCodeOptions {
64
+ return {
65
+ IndentSize : 4 ,
66
+ TabSize : 4 ,
67
+ NewLineCharacter : '\r' ,
68
+ ConvertTabsToSpaces : true ,
69
+ InsertSpaceAfterCommaDelimiter : true ,
70
+ InsertSpaceAfterSemicolonInForStatements : true ,
71
+ InsertSpaceBeforeAndAfterBinaryOperators : true ,
72
+ InsertSpaceAfterKeywordsInControlFlowStatements : true ,
73
+ InsertSpaceAfterFunctionKeywordForAnonymousFunctions : false ,
74
+ InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis : false ,
75
+ InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets : false , // added
76
+ PlaceOpenBraceOnNewLineForFunctions : false ,
77
+ PlaceOpenBraceOnNewLineForControlBlocks : false ,
78
+ IndentStyle : ts . IndentStyle . Smart // added
79
+ } ;
32
80
}
33
81
34
82
main ( ) ;
0 commit comments