Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(typescript): syntax only service context isolation
- Loading branch information
1 parent
e05c357
commit 169ffb4
Showing
3 changed files
with
82 additions
and
66 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type * as ts from 'typescript'; | ||
|
||
export function createSyntaxOnlyService(ts: typeof import('typescript'), syntaxOnly: boolean) { | ||
let currentProjectVersion = -1; | ||
let fileNames: string[] = []; | ||
|
||
const scriptInfos = new Map<string, { | ||
snapshot: ts.IScriptSnapshot; | ||
kind: ts.ScriptKind; | ||
version: number; | ||
}>(); | ||
const host: ts.LanguageServiceHost = { | ||
getProjectVersion: () => currentProjectVersion.toString(), | ||
getScriptFileNames: () => fileNames, | ||
getScriptVersion: () => currentProjectVersion.toString(), | ||
getScriptSnapshot: fileName => scriptInfos.get(fileName)!.snapshot, | ||
getScriptKind: fileName => scriptInfos.get(fileName)!.kind, | ||
getCompilationSettings: () => ({}), | ||
getCurrentDirectory: () => '', | ||
getDefaultLibFileName: () => '', | ||
readFile: () => undefined, | ||
fileExists: fileName => scriptInfos.has(fileName), | ||
}; | ||
|
||
return { | ||
languageService: syntaxOnly | ||
? ts.createLanguageService(host, undefined, ts.LanguageServiceMode.Syntactic) | ||
: ts.createLanguageService(host), | ||
updateFile, | ||
}; | ||
|
||
function updateFile(fileName: string, snapshot: ts.IScriptSnapshot, scriptKind: ts.ScriptKind) { | ||
let scriptInfo = scriptInfos.get(fileName); | ||
if (scriptInfo?.snapshot === snapshot && scriptInfo.kind === scriptKind) { | ||
return; | ||
} | ||
currentProjectVersion++; | ||
scriptInfo = { | ||
snapshot, | ||
kind: scriptKind, | ||
version: (scriptInfo?.version ?? 0) + 1, | ||
}; | ||
const filesChanged = !scriptInfos.has(fileName); | ||
scriptInfos.set(fileName, scriptInfo); | ||
if (filesChanged) { | ||
fileNames = [...scriptInfos.keys()]; | ||
} | ||
} | ||
} |