This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempted creating a SourceFile from scratch
- Loading branch information
Showing
1 changed file
with
70 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,82 @@ | ||
///<reference path='node_modules/typescript/lib/typescript.d.ts' /> | ||
/////<reference path='../TypeScript/lib/typescript.d.ts' /> | ||
///<reference path='node.d.ts' /> | ||
|
||
import ts = require('typescript'); | ||
import fs = require('fs'); | ||
import path = require('path'); | ||
import * as ts from "typescript"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
// derived from getAllBinaryExpressions https://github.com/Microsoft/TypeScript/issues/254 | ||
function getAllInterfaces(root: ts.Node) { | ||
var result: ts.InterfaceDeclaration[] = []; | ||
aggregate(root); | ||
return result; | ||
// https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#pretty-printer-using-the-ls-formatter | ||
// https://github.com/Microsoft/TypeScript/blob/master/src/compiler/parser.ts | ||
|
||
function aggregate(node: ts.Node): void { | ||
if (node.kind === ts.SyntaxKind.InterfaceDeclaration) { | ||
result.push(<ts.InterfaceDeclaration>node); | ||
} | ||
ts.forEachChild(node, aggregate); | ||
} | ||
export function main() { | ||
let source = "var a=function(v:number){return 0+1+2+3;}"; | ||
let sourceFile = ts.createSourceFile("file.ts", source, ts.ScriptTarget.Latest, true); | ||
|
||
// It would be nice if I could create it from scratch. | ||
//let sourceFile = <ts.SourceFile>ts.createNode(ts.SyntaxKind.SourceFile); | ||
//sourceFile.text = ""; | ||
|
||
let stmt = <ts.VariableStatement>ts.createNode(ts.SyntaxKind.VariableStatement); | ||
let vdl = <ts.VariableDeclarationList>ts.createNode(ts.SyntaxKind.VariableDeclarationList); | ||
let vd = <ts.VariableDeclaration>ts.createNode(ts.SyntaxKind.VariableDeclaration); | ||
let id = <ts.Identifier>ts.createNode(ts.SyntaxKind.Identifier); | ||
let t = <ts.LiteralExpression>ts.createNode(ts.SyntaxKind.FirstLiteralToken); | ||
t.text = "1"; | ||
id.text = "a"; | ||
vd.name = id; | ||
vdl.declarations = <ts.NodeArray<ts.VariableDeclaration>>[vd]; | ||
stmt.declarationList = vdl; | ||
|
||
//sourceFile.statements = <ts.NodeArray<ts.Statement>>[]; | ||
//sourceFile.statements[0] = stmt; | ||
sourceFile.statements = <ts.NodeArray<ts.Statement>>[<ts.Statement>stmt]; | ||
|
||
let formatCodeOptions = getDefaultOptions(); | ||
let rulesProvider = getRuleProvider(formatCodeOptions); | ||
|
||
let edits = (<any>ts).formatting.formatDocument(sourceFile, rulesProvider, formatCodeOptions); | ||
let code = applyEdits(sourceFile.text, edits); | ||
console.log(code); | ||
} | ||
|
||
export function main() { | ||
var filename = process.cwd() + '/node_modules/typescript/lib/typescript.d.ts' | ||
var source = String(fs.readFileSync(filename)); | ||
function getRuleProvider(options: ts.FormatCodeOptions) { | ||
// Share this between multiple formatters using the same options. | ||
// This represents the bulk of the space the formatter uses. | ||
let ruleProvider = new (<any>ts).formatting.RulesProvider(); | ||
ruleProvider.ensureUpToDate(options); | ||
return ruleProvider; | ||
} | ||
|
||
var sf = ts.createSourceFile(filename, source, ts.ScriptTarget.Latest); | ||
function applyEdits(text: string, edits: ts.TextChange[]): string { | ||
// Apply edits in reverse on the existing text | ||
let result = text; | ||
for (let i = edits.length - 1; i >= 0; i--) { | ||
let change = edits[i]; | ||
let head = result.slice(0, change.span.start); | ||
let tail = result.slice(change.span.start + change.span.length) | ||
result = head + change.newText + tail; | ||
} | ||
return result; | ||
} | ||
|
||
getAllInterfaces(sf) | ||
.map(ifd => ifd.name.text) | ||
.sort() | ||
.forEach(nm => console.log(nm)); | ||
function getDefaultOptions(): ts.FormatCodeOptions { | ||
return { | ||
IndentSize: 4, | ||
TabSize: 4, | ||
NewLineCharacter: '\r', | ||
ConvertTabsToSpaces: true, | ||
InsertSpaceAfterCommaDelimiter: true, | ||
InsertSpaceAfterSemicolonInForStatements: true, | ||
InsertSpaceBeforeAndAfterBinaryOperators: true, | ||
InsertSpaceAfterKeywordsInControlFlowStatements: true, | ||
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false, | ||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, | ||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, // added | ||
PlaceOpenBraceOnNewLineForFunctions: false, | ||
PlaceOpenBraceOnNewLineForControlBlocks: false, | ||
IndentStyle: ts.IndentStyle.Smart // added | ||
}; | ||
} | ||
|
||
main(); |