dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5.03k stars 196 forks source link

make TsConfigResolver._parseJsonConfigFileContent() function public #1503

Open niieani opened 9 months ago

niieani commented 9 months ago

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

Hey @dsherret! Awesome project, thank you :bow:! 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):

https://github.com/dsherret/ts-morph/blob/930779a0d3699be35ab17bbe9dd26ec00350fe53/packages/common/src/tsconfig/TsConfigResolver.ts#L21-L27

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.