UNPKG

@compas/code-gen

Version:

Generate various boring parts of your server

189 lines (188 loc) 6.71 kB
/** * Create a generic file, most options have defaults that work for JavaScript style * languages. Most options can be overwritten by custom file implementations. * * @param {import("../generate").GenerateContext} generateContext * @param {string} relativePath * @param {Partial<GenerateFile>} options * @returns {GenerateFile} */ export function fileContextCreateGeneric( generateContext: import("../generate").GenerateContext, relativePath: string, options: Partial<GenerateFile>, ): GenerateFile; /** * Get a file by relative path from the context * * @param {import("../generate").GenerateContext} generateContext * @param {string} relativePath * @returns {GenerateFile} */ export function fileContextGet( generateContext: import("../generate").GenerateContext, relativePath: string, ): GenerateFile; /** * Get a file by relative path from the context, returns undefined if no file is created * yet. * * @param {import("../generate").GenerateContext} generateContext * @param {string} relativePath * @returns {GenerateFile|undefined} */ export function fileContextGetOptional( generateContext: import("../generate").GenerateContext, relativePath: string, ): GenerateFile | undefined; /** * Add or remove indentation for the next lines. A positive delta adds new levels of * indentation. A negative delta removes indentation. Note that removing indentation may * conflict with other utilities that use the {@link GenerateFile.calculatedLinePrefix}. * * Resetting the line prefix and thus the indentation can be done by passing * {@link FILE_INDENT_RESET} as the {@link delta}. * * @param {GenerateFile} file * @param {number} delta */ export function fileContextSetIndent(file: GenerateFile, delta: number): void; /** * Add to the total line prefix that is printed on each line. * * @param {GenerateFile} file * @param {string} prefix */ export function fileContextAddLinePrefix( file: GenerateFile, prefix: string, ): void; /** * Remove the {@link prefixLength} number of characters from the end of the line prefix. * * @param {GenerateFile} file * @param {number} prefixLength */ export function fileContextRemoveLinePrefix( file: GenerateFile, prefixLength: number, ): void; /** * Convert files from the context to output files. * * @param {import("../generate").GenerateContext} generateContext * @returns {import("../generate").OutputFile[]} */ export function fileContextConvertToOutputFiles( generateContext: import("../generate").GenerateContext, ): import("../generate").OutputFile[]; /** * Add {@link GenerateFile.addGeneratedByComment} if necessary, and return the file with * newline at the end. * * @param {GenerateFile} generateFile * @returns {string} */ export function fileContextFinalizeGenerateFile( generateFile: GenerateFile, ): string; /** * Represent a work in progress generated file. * We try to keep the options as flat and explicit as possible. All options are required * and have defaults that align more with JavaScript like languages. * * @typedef {object} GenerateFile * @property {string} relativePath The relative file path. * @property {string} contents The file contents the final file always has a trailing * newline, and optionally an initial comment added via * {@link GenerateFile.addGeneratedByComment}. * @property {boolean} addGeneratedByComment Determine if the file should contain an * initial 'Generated by \@compas/code-gen' comment. Defaults to 'true'. * @property {string} [additionToGeneratedByComment] Optional string to append to the * initial generated by comment. * @property {string} indentationValue The indentation value used. Defaults to 2 spaces. * @property {string} inlineCommentPrefix Supported inline comment styles. Defaults * to '// '. If your file format does not support inline comments, you should * make sure that no comment creation function is called. * @property {{ toString(): string }} [importCollector] Each language could provide their * own imports implementation. If this property is set, {@link * fileContextCreateGeneric} will reserve a spot for the imports and call `toString()` * when the file is finalized. * @property {string} calculatedLinePrefix The accumulated prefix to write on new lines. * Can be mutated via {@link fileContextSetIndent}, {@link fileContextAddLinePrefix} * and {@link fileContextRemoveLinePrefix} * @property {{ * hasWrittenLinePrefix: boolean, * }} lineState Object to keep track of what the writer already did. */ /** * @typedef {Map<string, GenerateFile>} GenerateFileMap */ /** * Use this constant to reset {@link GenerateFile.calculatedLinePrefix} via * {@link fileContextSetIndent} * * @type {number} */ export const FILE_INDENT_RESET: number; /** * Represent a work in progress generated file. * We try to keep the options as flat and explicit as possible. All options are required * and have defaults that align more with JavaScript like languages. */ export type GenerateFile = { /** * The relative file path. */ relativePath: string; /** * The file contents the final file always has a trailing * newline, and optionally an initial comment added via * {@link GenerateFile.addGeneratedByComment }. */ contents: string; /** * Determine if the file should contain an * initial 'Generated by \@compas/code-gen' comment. Defaults to 'true'. */ addGeneratedByComment: boolean; /** * Optional string to append to the * initial generated by comment. */ additionToGeneratedByComment?: string | undefined; /** * The indentation value used. Defaults to 2 spaces. */ indentationValue: string; /** * Supported inline comment styles. Defaults * to '// '. If your file format does not support inline comments, you should * make sure that no comment creation function is called. */ inlineCommentPrefix: string; /** * Each language could provide their * own imports implementation. If this property is set, {@link * fileContextCreateGeneric} will reserve a spot for the imports and call `toString()` * when the file is finalized. */ importCollector?: | { toString(): string; } | undefined; /** * The accumulated prefix to write on new lines. * Can be mutated via {@link fileContextSetIndent }, {@link fileContextAddLinePrefix } * and {@link fileContextRemoveLinePrefix } */ calculatedLinePrefix: string; /** * Object to keep track of what the writer already did. */ lineState: { hasWrittenLinePrefix: boolean; }; }; export type GenerateFileMap = Map<string, GenerateFile>; //# sourceMappingURL=context.d.ts.map