Something went wrong. Try again.
A native-speed rewrite of the roblox-ts compiler in golang
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127const path = require("node:path");const { createPluginNotFoundDiagnostic } = require("./diagnostics");
function getPluginConfigs(ts, tsConfigPath) { const configFile = ts.readConfigFile(tsConfigPath, ts.sys.readFile); if (configFile.error) { throw new Error(ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n")); }
const pluginConfigs = []; const config = configFile.config ?? {}; const plugins = config.compilerOptions?.plugins; if (Array.isArray(plugins)) { for (const pluginConfig of plugins) { if (pluginConfig && typeof pluginConfig.transform === "string") { pluginConfigs.push(pluginConfig); } } }
if (typeof config.extends === "string") { const extendedPath = require.resolve(config.extends, { paths: [path.dirname(tsConfigPath)], }); pluginConfigs.push(...getPluginConfigs(ts, extendedPath)); }
return pluginConfigs;}
function getTransformerFromFactory(ts, factory, config, program) { const { after, afterDeclarations, type, ...manualConfig } = config; let transformer;
switch (type) { case undefined: case "program": transformer = factory(program, manualConfig, { ts }); break; case "checker": transformer = factory(program.getTypeChecker(), manualConfig); break; case "compilerOptions": transformer = factory(program.getCompilerOptions(), manualConfig); break; case "config": transformer = factory(manualConfig); break; case "raw": transformer = (context) => factory(context, program, manualConfig); break; default: return undefined; }
if (typeof transformer === "function") { if (after) { return { after: transformer }; } if (afterDeclarations) { return { afterDeclarations: transformer }; } return { before: transformer }; }
return transformer;}
function createTransformerList(ts, program, configs, baseDir) { const transforms = { before: [], after: [], afterDeclarations: [], }; const diagnostics = [];
for (const config of configs) { if (!config.transform) { continue; }
try { const modulePath = require.resolve(config.transform, { paths: [baseDir] }); const requiredModule = require(modulePath); const factoryModule = typeof requiredModule === "function" ? { default: requiredModule } : requiredModule; const factory = factoryModule[config.import ?? "default"];
if (typeof factory !== "function") { throw new Error("factory not a function"); }
const transformer = getTransformerFromFactory(ts, factory, config, program); if (!transformer) { continue; }
if (transformer.afterDeclarations) { transforms.afterDeclarations.push(transformer.afterDeclarations); } if (transformer.after) { transforms.after.push(transformer.after); } if (transformer.before) { transforms.before.push(transformer.before); } } catch (error) { diagnostics.push(createPluginNotFoundDiagnostic(config.transform, error)); } }
return { transforms, diagnostics };}
function flattenIntoTransformers(transforms) { return [ ...transforms.after, ...transforms.before, ...transforms.afterDeclarations, ];}
module.exports = { createTransformerList, flattenIntoTransformers, getPluginConfigs,};