-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tsfmt): using Language Service API instead of Old Compiler API
- Loading branch information
Showing
1 changed file
with
29 additions
and
39 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,47 +1,37 @@ | ||
import * as ts from "typescript"; | ||
import { createDefaultFormatCodeSettings } from "./utils"; | ||
|
||
// from https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#pretty-printer-using-the-ls-formatter | ||
|
||
// Note: this uses ts.formatting which is part of the typescript 1.4 package but is not currently | ||
// exposed in the public typescript.d.ts. The typings should be exposed in the next release. | ||
export default function format(fileName: string, text: string, options = createDefaultFormatCodeSettings()) { | ||
|
||
// Parse the source text | ||
let sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true); | ||
|
||
// Get the formatting edits on the input sources | ||
let edits = (ts as any).formatting.formatDocument(sourceFile, getRuleProvider(options), options); | ||
|
||
// Apply the edits on the input code | ||
return applyEdits(text, edits); | ||
import { createDefaultFormatCodeSettings } from "./utils"; | ||
|
||
function getRuleProvider(settings: ts.FormatCodeSettings) { | ||
// Share this between multiple formatters using the same options. | ||
// This represents the bulk of the space the formatter uses. | ||
let ruleProvider = new (ts as any).formatting.RulesProvider(); | ||
ruleProvider.ensureUpToDate(settings); | ||
return ruleProvider; | ||
class LanguageServiceHost implements ts.LanguageServiceHost { | ||
files: { [fileName: string]: ts.IScriptSnapshot; } = {}; | ||
addFile(fileName: string, text: string) { | ||
this.files[fileName] = ts.ScriptSnapshot.fromString(text); | ||
} | ||
|
||
function applyEdits(text: string, edits: ts.TextChange[]): string { | ||
// Apply edits in reverse on the existing text | ||
let result = text; | ||
// for ts.LanguageServiceHost | ||
|
||
// An issue with `ts.formatting.formatDocument` is that it does | ||
// not always give the edits array in ascending order of change start | ||
// point. This can result that we add or remove some character in | ||
// the begining of the document, making the all the other edits | ||
// offsets invalid. | ||
getCompilationSettings = () => ts.getDefaultCompilerOptions(); | ||
getScriptFileNames = () => Object.keys(this.files); | ||
getScriptVersion = (_fileName: string) => "1"; | ||
getScriptSnapshot = (fileName: string) => this.files[fileName]; | ||
getCurrentDirectory = () => process.cwd(); | ||
getDefaultLibFileName = (_options: ts.CompilerOptions) => "lib"; | ||
} | ||
|
||
// We resolve this by sorting edits by ascending start point | ||
edits.sort((a, b) => a.span.start - b.span.start); | ||
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; | ||
} | ||
export default function format(fileName: string, text: string, options = createDefaultFormatCodeSettings()) { | ||
const host = new LanguageServiceHost(); | ||
host.addFile(fileName, text); | ||
|
||
const languageService = ts.createLanguageService(host, ts.createDocumentRegistry()); | ||
const edits = languageService.getFormattingEditsForDocument(fileName, options); | ||
edits | ||
.sort((a, b) => a.span.start - b.span.start) | ||
.reverse() | ||
.forEach(edit => { | ||
const head = text.slice(0, edit.span.start); | ||
const tail = text.slice(edit.span.start + edit.span.length); | ||
text = `${head}${edit.newText}${tail}`; | ||
}); | ||
|
||
return text; | ||
} |