Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function createVirtualTypeScriptEnvironment(
getSourceFile: fileName => languageService.getProgram()?.getSourceFile(fileName),

createFile: (fileName, content) => {
updateFile(ts.createSourceFile(fileName, content, mergedCompilerOpts.target!, false))
updateFile({ fileName, text: content } as SourceFile)
},
updateFile: (fileName, content, optPrevTextSpan) => {
const prevSourceFile = languageService.getProgram()!.getSourceFile(fileName)
Expand All @@ -105,12 +105,8 @@ export function createVirtualTypeScriptEnvironment(
prevFullContents.slice(0, prevTextSpan.start) +
content +
prevFullContents.slice(prevTextSpan.start + prevTextSpan.length)
const newSourceFile = ts.updateSourceFile(prevSourceFile, newText, {
span: prevTextSpan,
newLength: content.length,
})

updateFile(newSourceFile)
updateFile({ fileName, text: newText } as SourceFile)
},
deleteFile(fileName) {
const sourceFile = languageService.getProgram()!.getSourceFile(fileName)
Expand Down Expand Up @@ -681,11 +677,13 @@ export function createVirtualLanguageServiceHost(
customTransformers?: CustomTransformers
) {
const fileNames = [...rootFiles]
const { compilerHost, updateFile, deleteFile } = createVirtualCompilerHost(sys, compilerOptions, ts)
const fileVersions = new Map<string, string>()
let projectVersion = 0
const languageServiceHost: LanguageServiceHost = {
...compilerHost,
...sys,
getDefaultLibFileName: () => "/" + ts.getDefaultLibFileName(compilerOptions),
getNewLine: () => sys.newLine,
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
getProjectVersion: () => projectVersion.toString(),
getCompilationSettings: () => compilerOptions,
getCustomTransformers: () => customTransformers,
Expand Down Expand Up @@ -725,7 +723,7 @@ export function createVirtualLanguageServiceHost(
if (!fileNames.includes(sourceFile.fileName)) {
fileNames.push(sourceFile.fileName)
}
updateFile(sourceFile)
sys.writeFile(sourceFile.fileName, sourceFile.text)
},
deleteFile: sourceFile => {
projectVersion++
Expand All @@ -734,7 +732,7 @@ export function createVirtualLanguageServiceHost(
if (index !== -1) {
fileNames.splice(index, 1)
}
deleteFile(sourceFile)
sys.deleteFile!(sourceFile.fileName)
}
}
return lsHost
Expand Down
22 changes: 22 additions & 0 deletions packages/typescript-vfs/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,25 @@ it("moduleDetection options", async () => {
program.emit()
expect(fsMap.get("index.js")).toEqual(`define(["require", "exports"], function (require, exports) {\n "use strict";\n Object.defineProperty(exports, "__esModule", { value: true });\n let foo = 'foo';\n});\n`)
})

it("createFile and updateFile do not parse duplicate SourceFiles outside the language service", () => {
const fsMap = createDefaultMapFromNodeModules({})
const system = createSystem(fsMap)
const createSourceFile = jest.fn(ts.createSourceFile)
const updateSourceFile = jest.fn(ts.updateSourceFile)
const tsProxy = { ...ts, createSourceFile, updateSourceFile }
const env = createVirtualTypeScriptEnvironment(system, [], tsProxy, {})

env.createFile("a.ts", "const x: number = 1")
expect(createSourceFile).not.toHaveBeenCalled()
expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 1")

env.updateFile("a.ts", "const x: number = 2")
expect(updateSourceFile).not.toHaveBeenCalled()
expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 2")

env.updateFile("a.ts", "3", ts.createTextSpan("const x: number = ".length, 1))
expect(updateSourceFile).not.toHaveBeenCalled()
expect(env.getSourceFile("a.ts")?.text).toBe("const x: number = 3")
expect(env.languageService.getSemanticDiagnostics("a.ts")).toHaveLength(0)
})