Skip to content
This repository was archived by the owner on Feb 8, 2022. It is now read-only.

Commit a465f8b

Browse files
committed
attempted creating a SourceFile from scratch
microsoft/TypeScript#3241
1 parent d75136b commit a465f8b

File tree

1 file changed

+70
-22
lines changed

1 file changed

+70
-22
lines changed

app.ts

+70-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
11
///<reference path='node_modules/typescript/lib/typescript.d.ts' />
2+
/////<reference path='../TypeScript/lib/typescript.d.ts' />
23
///<reference path='node.d.ts' />
34

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";
78

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
1311

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);
2041
}
2142

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+
}
2550

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+
}
2762

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+
};
3280
}
3381

3482
main();

0 commit comments

Comments
 (0)