Created
January 16, 2015 06:28
-
-
Save ctaggart/cceed3f8e74a890cb023 to your computer and use it in GitHub Desktop.
Format TypeScript with F#
This file contains hidden or 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
module App | |
// an F# wrapper for the TypeScript module | |
module tsast = | |
open EdgeJs | |
let format (ts:string): Async<string> = | |
let f = | |
Edge.Async | |
""" | |
var tsast = require('tsast'); | |
return function(ts, cb) { | |
cb(null, tsast.format(ts)); | |
} | |
""" | |
f ts |> Async.Unbox | |
[<EntryPoint>] | |
let main argv = | |
async { | |
let unformatted = "class Triangle { edges() { return 3; }}" | |
let! formatted = tsast.format unformatted | |
printfn "unformatted: %s" unformatted | |
printfn "formatted: %s" formatted | |
} | |
|> Async.RunSynchronously | |
0 |
This file contains hidden or 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
git clone https://github.com/ctaggart/TsAst.git -b format | |
cd TsAst | |
git clone https://github.com/Microsoft/TypeScript.git | |
cd TsAst | |
npm install ..\TypeScript | |
msbuild .\TsAst.njsproj | |
cd ..\TsAstFs | |
npm install ..\TypeScript | |
npm install ..\TsAst | |
msbuild .\TsAstFs.fsproj | |
.\bin\Debug\TsAstFs.exe |
This file contains hidden or 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
export function format(ts: string): string { | |
var f: SourceFileVersion = { filename: "file.ts", version: 0, text: ts }; | |
var langSvc = inMemoryLanguageService([f]); | |
var textChanges = langSvc.getFormattingEditsForDocument(f.filename, defaultFormatCodeOptions()); | |
return formatCode(f.text, textChanges); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment