@notjustcoders/ioc-arise
Version:
Arise type-safe IoC containers from your code. Zero overhead, zero coupling.
83 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeneratedCodeValidator = void 0;
const ts_morph_1 = require("ts-morph");
const logger_1 = require("./logger");
class GeneratedCodeValidator {
/**
* Validates generated TypeScript files for errors
* @param filePaths - Array of absolute paths to generated files
* @param sourceDir - Source directory of the project for context
*/
static validate(filePaths, sourceDir) {
if (filePaths.length === 0)
return;
logger_1.Logger.custom("🔍", "Validating generated code...", logger_1.Logger.getColors().cyan + logger_1.Logger.getColors().bright);
const project = new ts_morph_1.Project({
compilerOptions: {
noEmit: true,
skipLibCheck: true,
allowJs: true,
esModuleInterop: true,
experimentalDecorators: true,
emitDecoratorMetadata: true,
moduleResolution: 2, // NodeNext
},
});
// Add source files from the project for context (to resolve imports)
logger_1.Logger.debug(`Adding context from: ${sourceDir}/**/*.ts`);
project.addSourceFilesAtPaths([
`${sourceDir}/**/*.ts`,
`!${sourceDir}/**/*.gen.ts`, // Exclude already generated files
`!**/node_modules/**`
]);
// Add exactly the generated files we want to check
for (const filePath of filePaths) {
if (filePath.endsWith('.d.ts')) {
logger_1.Logger.debug(`Skipping .d.ts file: ${filePath}`);
continue;
}
logger_1.Logger.debug(`Validating: ${filePath}`);
project.addSourceFileAtPath(filePath);
}
// Get all diagnostics
const diagnostics = project.getPreEmitDiagnostics();
if (diagnostics.length > 0) {
this.reportErrors(diagnostics, filePaths);
}
else {
logger_1.Logger.success("No TypeScript errors found in generated files!");
}
}
static reportErrors(diagnostics, generatedFiles) {
const generatedFilesSet = new Set(generatedFiles);
// Filter diagnostics to only show those in our generated files
const relevantDiagnostics = diagnostics.filter(d => {
const sourceFile = d.getSourceFile();
return sourceFile && generatedFilesSet.has(sourceFile.getFilePath());
});
if (relevantDiagnostics.length === 0) {
logger_1.Logger.success("No TypeScript errors found in generated files!");
return;
}
logger_1.Logger.error(`Found ${relevantDiagnostics.length} TypeScript error(s) in generated files:`);
relevantDiagnostics.forEach((diagnostic) => {
const message = diagnostic.getMessageText();
const sourceFile = diagnostic.getSourceFile();
const line = diagnostic.getLineNumber();
const fileName = sourceFile ? sourceFile.getBaseName() : "unknown";
const filePath = sourceFile ? sourceFile.getFilePath() : "unknown";
const formattedMessage = typeof message === "string"
? message
: message.getMessageText();
logger_1.Logger.log(`${logger_1.Logger.getColors().red}❌ ${fileName}:${line}${logger_1.Logger.getColors().reset} - ${formattedMessage}`);
logger_1.Logger.log(logger_1.Logger.colorizeText(` at ${filePath}:${line}`, logger_1.Logger.getColors().gray));
});
// We don't necessarily want to exit here if we want the user to see the files,
// but typically if the generated code is invalid, it's a failure.
// For now, let's just log it. If the user wants it to fail, we can throw.
throw new Error("Generated code has TypeScript errors.");
}
}
exports.GeneratedCodeValidator = GeneratedCodeValidator;
//# sourceMappingURL=generatedCodeValidator.js.map