dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.

Home Page:https://ts-morph.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

make TsConfigResolver._parseJsonConfigFileContent() function public

niieani opened this issue · comments

Is your feature request related to a problem? Please describe.

Hey @dsherret! Awesome project, thank you 🙇!
I'm working on a small package that would help rename specifiers on-the-fly when using multiple TS compilation targets.
To create the TS Program I need to support project references, but currently the TsConfigResolver class does not expose those.

Hence, to recreate the tsProgram correctly I have to write this code that uses the private method tsConfigResolver._parseJsonConfigFileContent():

const tfsHost = new TransactionalFileSystem({
  fileSystem: fsHost,
  skipLoadingLibFiles: true,
  libFolderPath: undefined,
});

const standardizedTsConfigPath =
  tfsHost.getStandardizedAbsolutePath(tsConfigFilePath);

const encoding = "utf-8";
const tsConfigResolver = new TsConfigResolver(
  tfsHost,
  standardizedTsConfigPath,
  encoding,
) as unknown as PrivateTsConfigResolver;
//                           ^^ need to cast this to include the private method

const tsConfigContent = tsConfigResolver._parseJsonConfigFileContent();

const tsProgram = ts.createProgram({
  options: tsConfigContent.options,
  configFileParsingDiagnostics: tsConfigContent.errors,
  projectReferences: tsConfigContent.projectReferences,
  //                                                            ^^ here
  rootNames: [],
});

Describe the solution you'd like

Ideally, the TsConfigResolver would expose the full config contents. Currently it only exposes these two (getCompilerOptions, getErrors):

getCompilerOptions() {
return this._parseJsonConfigFileContent().options;
}
getErrors() {
return this._parseJsonConfigFileContent().errors || [];
}

Either adding a public method like getParsedCommandLine() or exposing parseJsonConfigFileContent() as public would solve my problem.

Describe alternatives you've considered

Alternative I've considered is to copy and paste a bunch of code from ts-morph and including that in my project. Not ideal, since I'd need to sync that every time the upstream changes.