UNPKG

types-testing

Version:

Test TypeScript types at test runner runtime - Works seamlessly with Jest, Vitest, and Bun.

96 lines (95 loc) 3.35 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; import ts from "typescript"; const throwIfConfigError = (diagnostics) => { if (diagnostics && diagnostics.length > 0) { const errors = diagnostics.map( (error) => new Error(error.messageText) ); throw new AggregateError(errors); } }; const createProgram = (options) => { const { basePath, tsConfig, compilerOptions, files, projectReferences } = options; if (basePath !== void 0 && tsConfig !== void 0) { const tsConfigPath = ts.sys.resolvePath(`${basePath}/${tsConfig}`); const rawConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile); if (rawConfig.error) { throw new Error( `failed to read tsconfig file. ${rawConfig.error.messageText}` ); } const config = ts.parseJsonConfigFileContent( rawConfig.config, ts.sys, basePath, compilerOptions ); throwIfConfigError(config.errors); if (files && files.length > 0) { config.fileNames = config.fileNames.concat( files.filter((file) => !config.fileNames.includes(file)) ); } if (compilerOptions) { config.options = __spreadValues(__spreadValues({}, config.options), compilerOptions); } if (projectReferences && projectReferences.length > 0) { if (config.projectReferences && config.projectReferences.length > 0) { const existingPath = config.projectReferences.map((ref) => ref.path); config.projectReferences = [ ...config.projectReferences, ...projectReferences.filter((ref) => !existingPath.includes(ref.path)) ]; } else { config.projectReferences = projectReferences; } } const program = ts.createProgram({ rootNames: config.fileNames, options: config.options, projectReferences: config.projectReferences }); program.__internal = { files: program.getRootFileNames(), options: program.getCompilerOptions(), projectReferences: program.getProjectReferences() }; return program; } if (compilerOptions !== void 0 && files !== void 0) { const program = ts.createProgram({ rootNames: files, options: compilerOptions, projectReferences }); throwIfConfigError(program.getOptionsDiagnostics()); program.__internal = { files: program.getRootFileNames(), options: program.getCompilerOptions(), projectReferences: program.getProjectReferences() }; return program; } throw new Error( "define basePath and tsConfig to implement configuration from tsconfig file\nor define compilerOptions and files to implement configuration without tsconfig file." ); }; export { createProgram };