Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Created January 16, 2015 06:28
Show Gist options
  • Save ctaggart/cceed3f8e74a890cb023 to your computer and use it in GitHub Desktop.
Save ctaggart/cceed3f8e74a890cb023 to your computer and use it in GitHub Desktop.
Format TypeScript with F#
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
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
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