| 33 | ] |
| 34 | |
| 35 | export class FileContext { |
| 36 | constants: string[] = []; |
| 37 | code: ArrayJoinInput<string>[] = []; |
| 38 | functions: CodeGenFunction[] = []; |
| 39 | |
| 40 | constructor(public opts: GeneratorOptions) {} |
| 41 | |
| 42 | get checkOverflows() { |
| 43 | return this.opts.oversizedInputs && !this.opts.unsafe; |
| 44 | } |
| 45 | |
| 46 | addFunctions(fns: CodeGenFunction[], sectionTitle?: string) { |
| 47 | if (!fns.length) return; |
| 48 | this.functions.push(...fns); |
| 49 | const code: ArrayJoinInput<string>[] = []; |
| 50 | for (const fn of fns) { |
| 51 | code.push('', ...generateFunctionCode(fn)) |
| 52 | } |
| 53 | if (sectionTitle) { |
| 54 | this.addSection(sectionTitle, code); |
| 55 | } else { |
| 56 | this.code.push(...code) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | clearCode() { |
| 61 | this.code = []; |
| 62 | if (!this.opts.constantsFile) { |
| 63 | this.constants = []; |
| 64 | } |
| 65 | this.functions = []; |
| 66 | } |
| 67 | |
| 68 | addSection(title: string, code: ArrayJoinInput<string>[]) { |
| 69 | this.code.push('', ...toCommentSeparator(title)) |
| 70 | this.code.push(...code) |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add a constant and get the reference to it |
| 75 | * @param name Name of constant to use |
| 76 | * @param value Value for the constant |
| 77 | * @returns Reference to the constant - `name` if `inline` |
| 78 | * option is false, otherwise `value`. |
| 79 | */ |
| 80 | addConstant(name: string, value: string): string { |
| 81 | if (this.opts.inline) { |
| 82 | return value; |
| 83 | } |
| 84 | const _declaration = getDeclareConstant(name, value); |
| 85 | if (!this.constants.includes(_declaration)) { |
| 86 | this.constants.push(_declaration); |
| 87 | } |
| 88 | return name; |
| 89 | } |
| 90 | } |
nothing calls this directly
no outgoing calls
no test coverage detected