@ts-morph/bootstrap
Advanced tools
Comparing version 0.21.0 to 0.22.0
@@ -18,3 +18,5 @@ import { DocumentRegistry, StringUtils, ts, FileUtils, TransactionalFileSystem, TsConfigResolver, errors, InMemoryFileSystemHost, RealFileSystemHost, CompilerOptionsContainer, createHosts, runtime, createModuleResolutionHost, Memoize } from '@ts-morph/common'; | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
function __decorate(decorators, target, key, desc) { | ||
@@ -27,21 +29,29 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { | ||
var e = new Error(message); | ||
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; | ||
}; | ||
class SourceFileCache { | ||
#sourceFilesByFilePath = new Map(); | ||
#projectVersion = 0; | ||
#fileSystemWrapper; | ||
#compilerOptions; | ||
documentRegistry; | ||
constructor(fileSystemWrapper, compilerOptions) { | ||
this.fileSystemWrapper = fileSystemWrapper; | ||
this.compilerOptions = compilerOptions; | ||
this.sourceFilesByFilePath = new Map(); | ||
this.projectVersion = 0; | ||
this.documentRegistry = new DocumentRegistry(fileSystemWrapper); | ||
this.#fileSystemWrapper = fileSystemWrapper; | ||
this.#compilerOptions = compilerOptions; | ||
} | ||
containsSourceFileAtPath(filePath) { | ||
return this.sourceFilesByFilePath.has(filePath); | ||
return this.#sourceFilesByFilePath.has(filePath); | ||
} | ||
getSourceFilePaths() { | ||
return this.sourceFilesByFilePath.keys(); | ||
return this.#sourceFilesByFilePath.keys(); | ||
} | ||
getSourceFiles() { | ||
return this.sourceFilesByFilePath.values(); | ||
return this.#sourceFilesByFilePath.values(); | ||
} | ||
getProjectVersion() { | ||
return this.projectVersion; | ||
return this.#projectVersion; | ||
} | ||
@@ -52,8 +62,8 @@ getSourceFileVersion(sourceFile) { | ||
getSourceFileFromCacheFromFilePath(filePath) { | ||
return this.sourceFilesByFilePath.get(filePath); | ||
return this.#sourceFilesByFilePath.get(filePath); | ||
} | ||
async addOrGetSourceFileFromFilePath(filePath, options) { | ||
let sourceFile = this.sourceFilesByFilePath.get(filePath); | ||
let sourceFile = this.#sourceFilesByFilePath.get(filePath); | ||
if (sourceFile == null) { | ||
const fileText = await this.fileSystemWrapper.readFileIfExists(filePath, this.compilerOptions.getEncoding()); | ||
const fileText = await this.#fileSystemWrapper.readFileIfExists(filePath, this.#compilerOptions.getEncoding()); | ||
if (fileText != null) { | ||
@@ -66,5 +76,5 @@ sourceFile = this.createSourceFileFromText(filePath, fileText, options); | ||
addOrGetSourceFileFromFilePathSync(filePath, options) { | ||
let sourceFile = this.sourceFilesByFilePath.get(filePath); | ||
let sourceFile = this.#sourceFilesByFilePath.get(filePath); | ||
if (sourceFile == null) { | ||
const fileText = this.fileSystemWrapper.readFileIfExistsSync(filePath, this.compilerOptions.getEncoding()); | ||
const fileText = this.#fileSystemWrapper.readFileIfExistsSync(filePath, this.#compilerOptions.getEncoding()); | ||
if (fileText != null) { | ||
@@ -77,7 +87,7 @@ sourceFile = this.createSourceFileFromText(filePath, fileText, options); | ||
createSourceFileFromText(filePath, text, options) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
filePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
const hasBom = StringUtils.hasBom(text); | ||
if (hasBom) | ||
text = StringUtils.stripBom(text); | ||
const sourceFile = this.documentRegistry.createOrUpdateSourceFile(filePath, this.compilerOptions.get(), ts.ScriptSnapshot.fromString(text), options.scriptKind); | ||
const sourceFile = this.documentRegistry.createOrUpdateSourceFile(filePath, this.#compilerOptions.get(), ts.ScriptSnapshot.fromString(text), options.scriptKind); | ||
this.setSourceFile(sourceFile); | ||
@@ -87,19 +97,19 @@ return sourceFile; | ||
setSourceFile(sourceFile) { | ||
const standardizedFilePath = this.fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
const standardizedFilePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
sourceFile.fileName = standardizedFilePath; | ||
this.documentRegistry.updateDocument(standardizedFilePath, this.compilerOptions.get(), ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
this.documentRegistry.updateDocument(standardizedFilePath, this.#compilerOptions.get(), ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
const dirPath = FileUtils.getDirPath(standardizedFilePath); | ||
if (!this.fileSystemWrapper.directoryExistsSync(dirPath)) | ||
this.fileSystemWrapper.queueMkdir(dirPath); | ||
this.sourceFilesByFilePath.set(standardizedFilePath, sourceFile); | ||
this.projectVersion++; | ||
if (!this.#fileSystemWrapper.directoryExistsSync(dirPath)) | ||
this.#fileSystemWrapper.queueMkdir(dirPath); | ||
this.#sourceFilesByFilePath.set(standardizedFilePath, sourceFile); | ||
this.#projectVersion++; | ||
} | ||
removeSourceFile(filePath) { | ||
this.sourceFilesByFilePath.delete(filePath); | ||
this.#sourceFilesByFilePath.delete(filePath); | ||
} | ||
containsDirectoryAtPath(dirPath) { | ||
return this.fileSystemWrapper.directoryExistsSync(dirPath); | ||
return this.#fileSystemWrapper.directoryExistsSync(dirPath); | ||
} | ||
getChildDirectoriesOfDirectory(dirPath) { | ||
return this.fileSystemWrapper.getDirectories(dirPath); | ||
return this.#fileSystemWrapper.getDirectories(dirPath); | ||
} | ||
@@ -111,3 +121,3 @@ } | ||
if (tsConfigResolver != null && options.skipAddingFilesFromTsConfig !== true) { | ||
await project._addSourceFilesForTsConfigResolver(tsConfigResolver, project.compilerOptions.get()); | ||
await addSourceFilesForTsConfigResolver(project, tsConfigResolver, project.compilerOptions.get()); | ||
if (!options.skipFileDependencyResolution) | ||
@@ -121,3 +131,3 @@ project.resolveSourceFileDependencies(); | ||
if (tsConfigResolver != null && options.skipAddingFilesFromTsConfig !== true) { | ||
project._addSourceFilesForTsConfigResolverSync(tsConfigResolver, project.compilerOptions.get()); | ||
addSourceFilesForTsConfigResolverSync(project, tsConfigResolver, project.compilerOptions.get()); | ||
if (!options.skipFileDependencyResolution) | ||
@@ -150,6 +160,5 @@ project.resolveSourceFileDependencies(); | ||
function getFileSystem() { | ||
var _a; | ||
if (options.useInMemoryFileSystem) | ||
return new InMemoryFileSystemHost(); | ||
return (_a = options.fileSystem) !== null && _a !== void 0 ? _a : new RealFileSystemHost(); | ||
return options.fileSystem ?? new RealFileSystemHost(); | ||
} | ||
@@ -164,11 +173,15 @@ function getEncodingFromProvidedOptions() { | ||
class Project { | ||
#sourceFileCache; | ||
#fileSystemWrapper; | ||
#languageServiceHost; | ||
#compilerHost; | ||
#configFileParsingDiagnostics; | ||
constructor(objs, options) { | ||
var _a; | ||
const { tsConfigResolver } = objs; | ||
this.fileSystem = objs.fileSystem; | ||
this._fileSystemWrapper = objs.fileSystemWrapper; | ||
this.#fileSystemWrapper = objs.fileSystemWrapper; | ||
const tsCompilerOptions = getCompilerOptions(); | ||
this.compilerOptions = new CompilerOptionsContainer(); | ||
this.compilerOptions.set(tsCompilerOptions); | ||
this._sourceFileCache = new SourceFileCache(this._fileSystemWrapper, this.compilerOptions); | ||
this.#sourceFileCache = new SourceFileCache(this.#fileSystemWrapper, this.compilerOptions); | ||
const resolutionHost = !options.resolutionHost | ||
@@ -179,8 +192,8 @@ ? undefined | ||
const { languageServiceHost, compilerHost } = createHosts({ | ||
transactionalFileSystem: this._fileSystemWrapper, | ||
sourceFileContainer: this._sourceFileCache, | ||
transactionalFileSystem: this.#fileSystemWrapper, | ||
sourceFileContainer: this.#sourceFileCache, | ||
compilerOptions: this.compilerOptions, | ||
getNewLine: () => newLineKind, | ||
resolutionHost: resolutionHost || {}, | ||
getProjectVersion: () => this._sourceFileCache.getProjectVersion().toString(), | ||
getProjectVersion: () => this.#sourceFileCache.getProjectVersion().toString(), | ||
isKnownTypesPackageName: options.isKnownTypesPackageName, | ||
@@ -190,5 +203,5 @@ libFolderPath: options.libFolderPath, | ||
}); | ||
this.languageServiceHost = languageServiceHost; | ||
this.compilerHost = compilerHost; | ||
this.configFileParsingDiagnostics = (_a = tsConfigResolver === null || tsConfigResolver === void 0 ? void 0 : tsConfigResolver.getErrors()) !== null && _a !== void 0 ? _a : []; | ||
this.#languageServiceHost = languageServiceHost; | ||
this.#compilerHost = compilerHost; | ||
this.#configFileParsingDiagnostics = tsConfigResolver?.getErrors() ?? []; | ||
function getCompilerOptions() { | ||
@@ -206,6 +219,8 @@ return { | ||
} | ||
compilerOptions; | ||
fileSystem; | ||
async addSourceFileAtPath(filePath, options) { | ||
const sourceFile = await this.addSourceFileAtPathIfExists(filePath, options); | ||
if (sourceFile == null) | ||
throw new errors.FileNotFoundError(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
throw new errors.FileNotFoundError(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
return sourceFile; | ||
@@ -216,7 +231,7 @@ } | ||
if (sourceFile == null) | ||
throw new errors.FileNotFoundError(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
throw new errors.FileNotFoundError(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
return sourceFile; | ||
} | ||
addSourceFileAtPathIfExists(filePath, options) { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePath(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
return this.#sourceFileCache.addOrGetSourceFileFromFilePath(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
scriptKind: options && options.scriptKind, | ||
@@ -226,3 +241,3 @@ }); | ||
addSourceFileAtPathIfExistsSync(filePath, options) { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePathSync(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
return this.#sourceFileCache.addOrGetSourceFileFromFilePathSync(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
scriptKind: options && options.scriptKind, | ||
@@ -236,3 +251,3 @@ }); | ||
const sourceFiles = []; | ||
for (const filePath of await this._fileSystemWrapper.glob(fileGlobs)) { | ||
for (const filePath of await this.#fileSystemWrapper.glob(fileGlobs)) { | ||
sourceFilePromises.push(this.addSourceFileAtPathIfExists(filePath).then(sourceFile => { | ||
@@ -250,3 +265,3 @@ if (sourceFile != null) | ||
const sourceFiles = []; | ||
for (const filePath of this._fileSystemWrapper.globSync(fileGlobs)) { | ||
for (const filePath of this.#fileSystemWrapper.globSync(fileGlobs)) { | ||
const sourceFile = this.addSourceFileAtPathIfExistsSync(filePath); | ||
@@ -259,15 +274,15 @@ if (sourceFile != null) | ||
addSourceFilesFromTsConfig(tsConfigFilePath) { | ||
const resolver = this._getTsConfigResolver(tsConfigFilePath); | ||
return this._addSourceFilesForTsConfigResolver(resolver, resolver.getCompilerOptions()); | ||
const resolver = this.#getTsConfigResolver(tsConfigFilePath); | ||
return addSourceFilesForTsConfigResolver(this, resolver, resolver.getCompilerOptions()); | ||
} | ||
addSourceFilesFromTsConfigSync(tsConfigFilePath) { | ||
const resolver = this._getTsConfigResolver(tsConfigFilePath); | ||
return this._addSourceFilesForTsConfigResolverSync(resolver, resolver.getCompilerOptions()); | ||
const resolver = this.#getTsConfigResolver(tsConfigFilePath); | ||
return addSourceFilesForTsConfigResolverSync(this, resolver, resolver.getCompilerOptions()); | ||
} | ||
_getTsConfigResolver(tsConfigFilePath) { | ||
const standardizedFilePath = this._fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
return new TsConfigResolver(this._fileSystemWrapper, standardizedFilePath, this.compilerOptions.getEncoding()); | ||
#getTsConfigResolver(tsConfigFilePath) { | ||
const standardizedFilePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
return new TsConfigResolver(this.#fileSystemWrapper, standardizedFilePath, this.compilerOptions.getEncoding()); | ||
} | ||
createSourceFile(filePath, sourceFileText, options) { | ||
return this._sourceFileCache.createSourceFileFromText(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
return this.#sourceFileCache.createSourceFileFromText(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
} | ||
@@ -279,3 +294,3 @@ updateSourceFile(filePathOrSourceFile, sourceFileText, options) { | ||
ensureScriptSnapshot(filePathOrSourceFile); | ||
return this._sourceFileCache.setSourceFile(filePathOrSourceFile); | ||
return this.#sourceFileCache.setSourceFile(filePathOrSourceFile); | ||
function incrementVersion(sourceFile) { | ||
@@ -296,3 +311,3 @@ let version = sourceFile.version || "-1"; | ||
removeSourceFile(filePathOrSourceFile) { | ||
this._sourceFileCache.removeSourceFile(this._fileSystemWrapper.getStandardizedAbsolutePath(typeof filePathOrSourceFile === "string" ? filePathOrSourceFile : filePathOrSourceFile.fileName)); | ||
this.#sourceFileCache.removeSourceFile(this.#fileSystemWrapper.getStandardizedAbsolutePath(typeof filePathOrSourceFile === "string" ? filePathOrSourceFile : filePathOrSourceFile.fileName)); | ||
} | ||
@@ -302,29 +317,21 @@ resolveSourceFileDependencies() { | ||
} | ||
async _addSourceFilesForTsConfigResolver(tsConfigResolver, compilerOptions) { | ||
const sourceFiles = []; | ||
await Promise.all(tsConfigResolver.getPaths(compilerOptions).filePaths | ||
.map(p => this.addSourceFileAtPath(p).then(s => sourceFiles.push(s)))); | ||
return sourceFiles; | ||
} | ||
_addSourceFilesForTsConfigResolverSync(tsConfigResolver, compilerOptions) { | ||
return tsConfigResolver.getPaths(compilerOptions).filePaths.map(p => this.addSourceFileAtPathSync(p)); | ||
} | ||
#oldProgram; | ||
createProgram(options) { | ||
const oldProgram = this._oldProgram; | ||
const oldProgram = this.#oldProgram; | ||
const program = ts.createProgram({ | ||
rootNames: Array.from(this._sourceFileCache.getSourceFilePaths()), | ||
rootNames: Array.from(this.#sourceFileCache.getSourceFilePaths()), | ||
options: this.compilerOptions.get(), | ||
host: this.compilerHost, | ||
host: this.#compilerHost, | ||
oldProgram, | ||
configFileParsingDiagnostics: this.configFileParsingDiagnostics, | ||
configFileParsingDiagnostics: this.#configFileParsingDiagnostics, | ||
...options, | ||
}); | ||
this._oldProgram = program; | ||
this.#oldProgram = program; | ||
return program; | ||
} | ||
getLanguageService() { | ||
return ts.createLanguageService(this.languageServiceHost, this._sourceFileCache.documentRegistry); | ||
return ts.createLanguageService(this.#languageServiceHost, this.#sourceFileCache.documentRegistry); | ||
} | ||
getSourceFileOrThrow(fileNameOrSearchFunction) { | ||
const sourceFile = this.getSourceFile(fileNameOrSearchFunction); | ||
const sourceFile = this.#getSourceFileInternal(fileNameOrSearchFunction); | ||
if (sourceFile != null) | ||
@@ -335,3 +342,3 @@ return sourceFile; | ||
if (FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0) { | ||
const errorFileNameOrPath = this._fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath); | ||
const errorFileNameOrPath = this.#fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath); | ||
throw new errors.InvalidOperationError(`Could not find source file in project at the provided path: ${errorFileNameOrPath}`); | ||
@@ -348,5 +355,8 @@ } | ||
getSourceFile(fileNameOrSearchFunction) { | ||
const filePathOrSearchFunction = getFilePathOrSearchFunction(this._fileSystemWrapper); | ||
return this.#getSourceFileInternal(fileNameOrSearchFunction); | ||
} | ||
#getSourceFileInternal(fileNameOrSearchFunction) { | ||
const filePathOrSearchFunction = getFilePathOrSearchFunction(this.#fileSystemWrapper); | ||
if (isStandardizedFilePath(filePathOrSearchFunction)) { | ||
return this._sourceFileCache.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction); | ||
return this.#sourceFileCache.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction); | ||
} | ||
@@ -382,7 +392,7 @@ const allSourceFilesIterable = this.getSourceFiles(); | ||
getSourceFiles() { | ||
return Array.from(this._sourceFileCache.getSourceFiles()); | ||
return Array.from(this.#sourceFileCache.getSourceFiles()); | ||
} | ||
formatDiagnosticsWithColorAndContext(diagnostics, opts = {}) { | ||
return ts.formatDiagnosticsWithColorAndContext(diagnostics, { | ||
getCurrentDirectory: () => this._fileSystemWrapper.getCurrentDirectory(), | ||
getCurrentDirectory: () => this.#fileSystemWrapper.getCurrentDirectory(), | ||
getCanonicalFileName: fileName => fileName, | ||
@@ -394,5 +404,5 @@ getNewLine: () => opts.newLineChar || runtime.getEndOfLine(), | ||
return createModuleResolutionHost({ | ||
transactionalFileSystem: this._fileSystemWrapper, | ||
transactionalFileSystem: this.#fileSystemWrapper, | ||
getEncoding: () => this.compilerOptions.getEncoding(), | ||
sourceFileContainer: this._sourceFileCache, | ||
sourceFileContainer: this.#sourceFileCache, | ||
}); | ||
@@ -407,3 +417,12 @@ } | ||
], Project.prototype, "getModuleResolutionHost", null); | ||
async function addSourceFilesForTsConfigResolver(project, tsConfigResolver, compilerOptions) { | ||
const sourceFiles = []; | ||
await Promise.all(tsConfigResolver.getPaths(compilerOptions).filePaths | ||
.map(p => project.addSourceFileAtPath(p).then(s => sourceFiles.push(s)))); | ||
return sourceFiles; | ||
} | ||
function addSourceFilesForTsConfigResolverSync(project, tsConfigResolver, compilerOptions) { | ||
return tsConfigResolver.getPaths(compilerOptions).filePaths.map(p => project.addSourceFileAtPathSync(p)); | ||
} | ||
export { Project, createProject, createProjectSync }; |
@@ -19,3 +19,5 @@ 'use strict'; | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
function __decorate(decorators, target, key, desc) { | ||
@@ -28,21 +30,29 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { | ||
var e = new Error(message); | ||
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; | ||
}; | ||
class SourceFileCache { | ||
#sourceFilesByFilePath = new Map(); | ||
#projectVersion = 0; | ||
#fileSystemWrapper; | ||
#compilerOptions; | ||
documentRegistry; | ||
constructor(fileSystemWrapper, compilerOptions) { | ||
this.fileSystemWrapper = fileSystemWrapper; | ||
this.compilerOptions = compilerOptions; | ||
this.sourceFilesByFilePath = new Map(); | ||
this.projectVersion = 0; | ||
this.documentRegistry = new common.DocumentRegistry(fileSystemWrapper); | ||
this.#fileSystemWrapper = fileSystemWrapper; | ||
this.#compilerOptions = compilerOptions; | ||
} | ||
containsSourceFileAtPath(filePath) { | ||
return this.sourceFilesByFilePath.has(filePath); | ||
return this.#sourceFilesByFilePath.has(filePath); | ||
} | ||
getSourceFilePaths() { | ||
return this.sourceFilesByFilePath.keys(); | ||
return this.#sourceFilesByFilePath.keys(); | ||
} | ||
getSourceFiles() { | ||
return this.sourceFilesByFilePath.values(); | ||
return this.#sourceFilesByFilePath.values(); | ||
} | ||
getProjectVersion() { | ||
return this.projectVersion; | ||
return this.#projectVersion; | ||
} | ||
@@ -53,8 +63,8 @@ getSourceFileVersion(sourceFile) { | ||
getSourceFileFromCacheFromFilePath(filePath) { | ||
return this.sourceFilesByFilePath.get(filePath); | ||
return this.#sourceFilesByFilePath.get(filePath); | ||
} | ||
async addOrGetSourceFileFromFilePath(filePath, options) { | ||
let sourceFile = this.sourceFilesByFilePath.get(filePath); | ||
let sourceFile = this.#sourceFilesByFilePath.get(filePath); | ||
if (sourceFile == null) { | ||
const fileText = await this.fileSystemWrapper.readFileIfExists(filePath, this.compilerOptions.getEncoding()); | ||
const fileText = await this.#fileSystemWrapper.readFileIfExists(filePath, this.#compilerOptions.getEncoding()); | ||
if (fileText != null) { | ||
@@ -67,5 +77,5 @@ sourceFile = this.createSourceFileFromText(filePath, fileText, options); | ||
addOrGetSourceFileFromFilePathSync(filePath, options) { | ||
let sourceFile = this.sourceFilesByFilePath.get(filePath); | ||
let sourceFile = this.#sourceFilesByFilePath.get(filePath); | ||
if (sourceFile == null) { | ||
const fileText = this.fileSystemWrapper.readFileIfExistsSync(filePath, this.compilerOptions.getEncoding()); | ||
const fileText = this.#fileSystemWrapper.readFileIfExistsSync(filePath, this.#compilerOptions.getEncoding()); | ||
if (fileText != null) { | ||
@@ -78,7 +88,7 @@ sourceFile = this.createSourceFileFromText(filePath, fileText, options); | ||
createSourceFileFromText(filePath, text, options) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
filePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
const hasBom = common.StringUtils.hasBom(text); | ||
if (hasBom) | ||
text = common.StringUtils.stripBom(text); | ||
const sourceFile = this.documentRegistry.createOrUpdateSourceFile(filePath, this.compilerOptions.get(), common.ts.ScriptSnapshot.fromString(text), options.scriptKind); | ||
const sourceFile = this.documentRegistry.createOrUpdateSourceFile(filePath, this.#compilerOptions.get(), common.ts.ScriptSnapshot.fromString(text), options.scriptKind); | ||
this.setSourceFile(sourceFile); | ||
@@ -88,19 +98,19 @@ return sourceFile; | ||
setSourceFile(sourceFile) { | ||
const standardizedFilePath = this.fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
const standardizedFilePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
sourceFile.fileName = standardizedFilePath; | ||
this.documentRegistry.updateDocument(standardizedFilePath, this.compilerOptions.get(), common.ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
this.documentRegistry.updateDocument(standardizedFilePath, this.#compilerOptions.get(), common.ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
const dirPath = common.FileUtils.getDirPath(standardizedFilePath); | ||
if (!this.fileSystemWrapper.directoryExistsSync(dirPath)) | ||
this.fileSystemWrapper.queueMkdir(dirPath); | ||
this.sourceFilesByFilePath.set(standardizedFilePath, sourceFile); | ||
this.projectVersion++; | ||
if (!this.#fileSystemWrapper.directoryExistsSync(dirPath)) | ||
this.#fileSystemWrapper.queueMkdir(dirPath); | ||
this.#sourceFilesByFilePath.set(standardizedFilePath, sourceFile); | ||
this.#projectVersion++; | ||
} | ||
removeSourceFile(filePath) { | ||
this.sourceFilesByFilePath.delete(filePath); | ||
this.#sourceFilesByFilePath.delete(filePath); | ||
} | ||
containsDirectoryAtPath(dirPath) { | ||
return this.fileSystemWrapper.directoryExistsSync(dirPath); | ||
return this.#fileSystemWrapper.directoryExistsSync(dirPath); | ||
} | ||
getChildDirectoriesOfDirectory(dirPath) { | ||
return this.fileSystemWrapper.getDirectories(dirPath); | ||
return this.#fileSystemWrapper.getDirectories(dirPath); | ||
} | ||
@@ -112,3 +122,3 @@ } | ||
if (tsConfigResolver != null && options.skipAddingFilesFromTsConfig !== true) { | ||
await project._addSourceFilesForTsConfigResolver(tsConfigResolver, project.compilerOptions.get()); | ||
await addSourceFilesForTsConfigResolver(project, tsConfigResolver, project.compilerOptions.get()); | ||
if (!options.skipFileDependencyResolution) | ||
@@ -122,3 +132,3 @@ project.resolveSourceFileDependencies(); | ||
if (tsConfigResolver != null && options.skipAddingFilesFromTsConfig !== true) { | ||
project._addSourceFilesForTsConfigResolverSync(tsConfigResolver, project.compilerOptions.get()); | ||
addSourceFilesForTsConfigResolverSync(project, tsConfigResolver, project.compilerOptions.get()); | ||
if (!options.skipFileDependencyResolution) | ||
@@ -151,6 +161,5 @@ project.resolveSourceFileDependencies(); | ||
function getFileSystem() { | ||
var _a; | ||
if (options.useInMemoryFileSystem) | ||
return new common.InMemoryFileSystemHost(); | ||
return (_a = options.fileSystem) !== null && _a !== void 0 ? _a : new common.RealFileSystemHost(); | ||
return options.fileSystem ?? new common.RealFileSystemHost(); | ||
} | ||
@@ -165,11 +174,15 @@ function getEncodingFromProvidedOptions() { | ||
class Project { | ||
#sourceFileCache; | ||
#fileSystemWrapper; | ||
#languageServiceHost; | ||
#compilerHost; | ||
#configFileParsingDiagnostics; | ||
constructor(objs, options) { | ||
var _a; | ||
const { tsConfigResolver } = objs; | ||
this.fileSystem = objs.fileSystem; | ||
this._fileSystemWrapper = objs.fileSystemWrapper; | ||
this.#fileSystemWrapper = objs.fileSystemWrapper; | ||
const tsCompilerOptions = getCompilerOptions(); | ||
this.compilerOptions = new common.CompilerOptionsContainer(); | ||
this.compilerOptions.set(tsCompilerOptions); | ||
this._sourceFileCache = new SourceFileCache(this._fileSystemWrapper, this.compilerOptions); | ||
this.#sourceFileCache = new SourceFileCache(this.#fileSystemWrapper, this.compilerOptions); | ||
const resolutionHost = !options.resolutionHost | ||
@@ -180,8 +193,8 @@ ? undefined | ||
const { languageServiceHost, compilerHost } = common.createHosts({ | ||
transactionalFileSystem: this._fileSystemWrapper, | ||
sourceFileContainer: this._sourceFileCache, | ||
transactionalFileSystem: this.#fileSystemWrapper, | ||
sourceFileContainer: this.#sourceFileCache, | ||
compilerOptions: this.compilerOptions, | ||
getNewLine: () => newLineKind, | ||
resolutionHost: resolutionHost || {}, | ||
getProjectVersion: () => this._sourceFileCache.getProjectVersion().toString(), | ||
getProjectVersion: () => this.#sourceFileCache.getProjectVersion().toString(), | ||
isKnownTypesPackageName: options.isKnownTypesPackageName, | ||
@@ -191,5 +204,5 @@ libFolderPath: options.libFolderPath, | ||
}); | ||
this.languageServiceHost = languageServiceHost; | ||
this.compilerHost = compilerHost; | ||
this.configFileParsingDiagnostics = (_a = tsConfigResolver === null || tsConfigResolver === void 0 ? void 0 : tsConfigResolver.getErrors()) !== null && _a !== void 0 ? _a : []; | ||
this.#languageServiceHost = languageServiceHost; | ||
this.#compilerHost = compilerHost; | ||
this.#configFileParsingDiagnostics = tsConfigResolver?.getErrors() ?? []; | ||
function getCompilerOptions() { | ||
@@ -207,6 +220,8 @@ return { | ||
} | ||
compilerOptions; | ||
fileSystem; | ||
async addSourceFileAtPath(filePath, options) { | ||
const sourceFile = await this.addSourceFileAtPathIfExists(filePath, options); | ||
if (sourceFile == null) | ||
throw new common.errors.FileNotFoundError(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
throw new common.errors.FileNotFoundError(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
return sourceFile; | ||
@@ -217,7 +232,7 @@ } | ||
if (sourceFile == null) | ||
throw new common.errors.FileNotFoundError(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
throw new common.errors.FileNotFoundError(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath)); | ||
return sourceFile; | ||
} | ||
addSourceFileAtPathIfExists(filePath, options) { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePath(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
return this.#sourceFileCache.addOrGetSourceFileFromFilePath(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
scriptKind: options && options.scriptKind, | ||
@@ -227,3 +242,3 @@ }); | ||
addSourceFileAtPathIfExistsSync(filePath, options) { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePathSync(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
return this.#sourceFileCache.addOrGetSourceFileFromFilePathSync(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
scriptKind: options && options.scriptKind, | ||
@@ -237,3 +252,3 @@ }); | ||
const sourceFiles = []; | ||
for (const filePath of await this._fileSystemWrapper.glob(fileGlobs)) { | ||
for (const filePath of await this.#fileSystemWrapper.glob(fileGlobs)) { | ||
sourceFilePromises.push(this.addSourceFileAtPathIfExists(filePath).then(sourceFile => { | ||
@@ -251,3 +266,3 @@ if (sourceFile != null) | ||
const sourceFiles = []; | ||
for (const filePath of this._fileSystemWrapper.globSync(fileGlobs)) { | ||
for (const filePath of this.#fileSystemWrapper.globSync(fileGlobs)) { | ||
const sourceFile = this.addSourceFileAtPathIfExistsSync(filePath); | ||
@@ -260,15 +275,15 @@ if (sourceFile != null) | ||
addSourceFilesFromTsConfig(tsConfigFilePath) { | ||
const resolver = this._getTsConfigResolver(tsConfigFilePath); | ||
return this._addSourceFilesForTsConfigResolver(resolver, resolver.getCompilerOptions()); | ||
const resolver = this.#getTsConfigResolver(tsConfigFilePath); | ||
return addSourceFilesForTsConfigResolver(this, resolver, resolver.getCompilerOptions()); | ||
} | ||
addSourceFilesFromTsConfigSync(tsConfigFilePath) { | ||
const resolver = this._getTsConfigResolver(tsConfigFilePath); | ||
return this._addSourceFilesForTsConfigResolverSync(resolver, resolver.getCompilerOptions()); | ||
const resolver = this.#getTsConfigResolver(tsConfigFilePath); | ||
return addSourceFilesForTsConfigResolverSync(this, resolver, resolver.getCompilerOptions()); | ||
} | ||
_getTsConfigResolver(tsConfigFilePath) { | ||
const standardizedFilePath = this._fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
return new common.TsConfigResolver(this._fileSystemWrapper, standardizedFilePath, this.compilerOptions.getEncoding()); | ||
#getTsConfigResolver(tsConfigFilePath) { | ||
const standardizedFilePath = this.#fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
return new common.TsConfigResolver(this.#fileSystemWrapper, standardizedFilePath, this.compilerOptions.getEncoding()); | ||
} | ||
createSourceFile(filePath, sourceFileText, options) { | ||
return this._sourceFileCache.createSourceFileFromText(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
return this.#sourceFileCache.createSourceFileFromText(this.#fileSystemWrapper.getStandardizedAbsolutePath(filePath), sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
} | ||
@@ -280,3 +295,3 @@ updateSourceFile(filePathOrSourceFile, sourceFileText, options) { | ||
ensureScriptSnapshot(filePathOrSourceFile); | ||
return this._sourceFileCache.setSourceFile(filePathOrSourceFile); | ||
return this.#sourceFileCache.setSourceFile(filePathOrSourceFile); | ||
function incrementVersion(sourceFile) { | ||
@@ -297,3 +312,3 @@ let version = sourceFile.version || "-1"; | ||
removeSourceFile(filePathOrSourceFile) { | ||
this._sourceFileCache.removeSourceFile(this._fileSystemWrapper.getStandardizedAbsolutePath(typeof filePathOrSourceFile === "string" ? filePathOrSourceFile : filePathOrSourceFile.fileName)); | ||
this.#sourceFileCache.removeSourceFile(this.#fileSystemWrapper.getStandardizedAbsolutePath(typeof filePathOrSourceFile === "string" ? filePathOrSourceFile : filePathOrSourceFile.fileName)); | ||
} | ||
@@ -303,29 +318,21 @@ resolveSourceFileDependencies() { | ||
} | ||
async _addSourceFilesForTsConfigResolver(tsConfigResolver, compilerOptions) { | ||
const sourceFiles = []; | ||
await Promise.all(tsConfigResolver.getPaths(compilerOptions).filePaths | ||
.map(p => this.addSourceFileAtPath(p).then(s => sourceFiles.push(s)))); | ||
return sourceFiles; | ||
} | ||
_addSourceFilesForTsConfigResolverSync(tsConfigResolver, compilerOptions) { | ||
return tsConfigResolver.getPaths(compilerOptions).filePaths.map(p => this.addSourceFileAtPathSync(p)); | ||
} | ||
#oldProgram; | ||
createProgram(options) { | ||
const oldProgram = this._oldProgram; | ||
const oldProgram = this.#oldProgram; | ||
const program = common.ts.createProgram({ | ||
rootNames: Array.from(this._sourceFileCache.getSourceFilePaths()), | ||
rootNames: Array.from(this.#sourceFileCache.getSourceFilePaths()), | ||
options: this.compilerOptions.get(), | ||
host: this.compilerHost, | ||
host: this.#compilerHost, | ||
oldProgram, | ||
configFileParsingDiagnostics: this.configFileParsingDiagnostics, | ||
configFileParsingDiagnostics: this.#configFileParsingDiagnostics, | ||
...options, | ||
}); | ||
this._oldProgram = program; | ||
this.#oldProgram = program; | ||
return program; | ||
} | ||
getLanguageService() { | ||
return common.ts.createLanguageService(this.languageServiceHost, this._sourceFileCache.documentRegistry); | ||
return common.ts.createLanguageService(this.#languageServiceHost, this.#sourceFileCache.documentRegistry); | ||
} | ||
getSourceFileOrThrow(fileNameOrSearchFunction) { | ||
const sourceFile = this.getSourceFile(fileNameOrSearchFunction); | ||
const sourceFile = this.#getSourceFileInternal(fileNameOrSearchFunction); | ||
if (sourceFile != null) | ||
@@ -336,3 +343,3 @@ return sourceFile; | ||
if (common.FileUtils.pathIsAbsolute(fileNameOrPath) || fileNameOrPath.indexOf("/") >= 0) { | ||
const errorFileNameOrPath = this._fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath); | ||
const errorFileNameOrPath = this.#fileSystemWrapper.getStandardizedAbsolutePath(fileNameOrPath); | ||
throw new common.errors.InvalidOperationError(`Could not find source file in project at the provided path: ${errorFileNameOrPath}`); | ||
@@ -349,5 +356,8 @@ } | ||
getSourceFile(fileNameOrSearchFunction) { | ||
const filePathOrSearchFunction = getFilePathOrSearchFunction(this._fileSystemWrapper); | ||
return this.#getSourceFileInternal(fileNameOrSearchFunction); | ||
} | ||
#getSourceFileInternal(fileNameOrSearchFunction) { | ||
const filePathOrSearchFunction = getFilePathOrSearchFunction(this.#fileSystemWrapper); | ||
if (isStandardizedFilePath(filePathOrSearchFunction)) { | ||
return this._sourceFileCache.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction); | ||
return this.#sourceFileCache.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction); | ||
} | ||
@@ -383,7 +393,7 @@ const allSourceFilesIterable = this.getSourceFiles(); | ||
getSourceFiles() { | ||
return Array.from(this._sourceFileCache.getSourceFiles()); | ||
return Array.from(this.#sourceFileCache.getSourceFiles()); | ||
} | ||
formatDiagnosticsWithColorAndContext(diagnostics, opts = {}) { | ||
return common.ts.formatDiagnosticsWithColorAndContext(diagnostics, { | ||
getCurrentDirectory: () => this._fileSystemWrapper.getCurrentDirectory(), | ||
getCurrentDirectory: () => this.#fileSystemWrapper.getCurrentDirectory(), | ||
getCanonicalFileName: fileName => fileName, | ||
@@ -395,5 +405,5 @@ getNewLine: () => opts.newLineChar || common.runtime.getEndOfLine(), | ||
return common.createModuleResolutionHost({ | ||
transactionalFileSystem: this._fileSystemWrapper, | ||
transactionalFileSystem: this.#fileSystemWrapper, | ||
getEncoding: () => this.compilerOptions.getEncoding(), | ||
sourceFileContainer: this._sourceFileCache, | ||
sourceFileContainer: this.#sourceFileCache, | ||
}); | ||
@@ -408,2 +418,11 @@ } | ||
], Project.prototype, "getModuleResolutionHost", null); | ||
async function addSourceFilesForTsConfigResolver(project, tsConfigResolver, compilerOptions) { | ||
const sourceFiles = []; | ||
await Promise.all(tsConfigResolver.getPaths(compilerOptions).filePaths | ||
.map(p => project.addSourceFileAtPath(p).then(s => sourceFiles.push(s)))); | ||
return sourceFiles; | ||
} | ||
function addSourceFilesForTsConfigResolverSync(project, tsConfigResolver, compilerOptions) { | ||
return tsConfigResolver.getPaths(compilerOptions).filePaths.map(p => project.addSourceFileAtPathSync(p)); | ||
} | ||
@@ -410,0 +429,0 @@ Object.defineProperty(exports, 'CompilerOptionsContainer', { |
@@ -7,3 +7,3 @@ import { RuntimeDirEntry, ts } from "@ts-morph/common"; | ||
export declare class CompilerOptionsContainer extends SettingsContainer<ts.CompilerOptions> { | ||
constructor(); | ||
constructor(defaultSettings?: ts.CompilerOptions); | ||
/** | ||
@@ -81,2 +81,3 @@ * Sets one or all of the compiler options. | ||
export declare class InMemoryFileSystemHost implements FileSystemHost { | ||
#private; | ||
/** | ||
@@ -151,2 +152,3 @@ * Constructor. | ||
export declare abstract class SettingsContainer<T extends object> { | ||
#private; | ||
protected _settings: T; | ||
@@ -229,2 +231,3 @@ /** | ||
export declare class Project { | ||
#private; | ||
private constructor(); | ||
@@ -231,0 +234,0 @@ /** Gets the compiler options for modification. */ |
{ | ||
"name": "@ts-morph/bootstrap", | ||
"version": "0.21.0", | ||
"version": "0.22.0", | ||
"description": "API for getting quickly set up with the TypeScript Compiler API.", | ||
@@ -11,3 +11,3 @@ "keywords": [ | ||
"main": "dist/ts-morph-bootstrap.js", | ||
"typings": "lib/ts-morph-bootstrap.d.ts", | ||
"types": "lib/ts-morph-bootstrap.d.ts", | ||
"author": "David Sherret", | ||
@@ -28,17 +28,17 @@ "license": "MIT", | ||
"dependencies": { | ||
"@ts-morph/common": "~0.21.0" | ||
"@ts-morph/common": "~0.22.0" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^11.0.0", | ||
"@types/chai": "^4.3.4", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^18.15.9", | ||
"chai": "^4.3.7", | ||
"@rollup/plugin-typescript": "^11.1.5", | ||
"@types/chai": "^4.3.11", | ||
"@types/mocha": "^10.0.6", | ||
"@types/node": "^20.10.0", | ||
"chai": "^4.3.10", | ||
"cross-env": "^7.0.3", | ||
"mocha": "^10.2.0", | ||
"rimraf": "^4.4.1", | ||
"rollup": "=3.20.2", | ||
"rimraf": "^5.0.5", | ||
"rollup": "=4.5.2", | ||
"ts-node": "^10.9.1", | ||
"tslib": "^2.5.0", | ||
"typescript": "~5.2.2" | ||
"tslib": "^2.6.2", | ||
"typescript": "~5.3.2" | ||
}, | ||
@@ -45,0 +45,0 @@ "publishConfig": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64042
1193
+ Added@ts-morph/common@0.22.0(transitive)
+ Addedminimatch@9.0.5(transitive)
+ Addedmkdirp@3.0.1(transitive)
- Removed@ts-morph/common@0.21.0(transitive)
- Removedminimatch@7.4.6(transitive)
- Removedmkdirp@2.1.6(transitive)
Updated@ts-morph/common@~0.22.0