1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import TaxReturn from './TaxReturn';
7 import * as Trace from './Trace';
8 import Form, { FormClass } from './Form';
10 export abstract class Line<T> {
11 private _description?: string;
13 _id: string; // _id is set by Form.init().
14 form: Form; // Set by Form.init();
16 constructor(description?: string) {
17 this._description = description;
24 get description(): string {
25 return this._description;
28 abstract value(tr: TaxReturn): T;
31 type ComputeFunc<T> = (tr: TaxReturn) => T;
33 export class ComputedLine<T> extends Line<T> {
34 private _compute: ComputeFunc<T>;
36 constructor(compute: ComputeFunc<T>, description?: string) {
38 this._compute = compute;
41 value(tr: TaxReturn): T {
43 const value = this._compute(tr);
49 export class ReferenceLine<F extends Form,
50 L extends keyof F['lines'],
51 T extends ReturnType<F['lines'][L]['value']>>
53 private _form: FormClass<F>;
55 private _fallback?: T;
57 // If creating a ReferenceLine and F is the same class as the
58 // the one the Line is in, erase |form|'s type with |as any| to
59 // keep TypeScript happy.
60 constructor(form: FormClass<F>, line: L, description?: string, fallback?: T) {
61 super(description || `Reference ${form.name}@${line}`);
64 this._fallback = fallback;
67 value(tr: TaxReturn): T {
69 const form: F = tr.findForm(this._form);
70 if (this._fallback !== undefined && !form) {
72 return this._fallback;
74 const value: T = form.getValue(tr, this._line);
80 // SymbolicLine cannot be used for lines defined on F itself. For those cases, use:
81 // new ComputedLine((tr) => this.K(tr));
82 export class SymbolicLine<F extends Form & { [key in K]: ComputeFunc<ReturnType<F[K]>> },
84 extends Line<ReturnType<F[K]>> {
85 private _form: FormClass<F>;
88 constructor(form: FormClass<F>, key: K, description?: string) {
89 super(description || `Reference ${form.name}/${key}`);
94 value(tr: TaxReturn): ReturnType<F[K]> {
96 const form: F = tr.findForm(this._form);
97 const value = form[this._key](tr);
103 export class InputLine<U = unknown, T extends keyof U = any> extends Line<U[T]> {
105 private _fallback: U[T];
109 constructor(input: T, description?: string, fallback?: U[T]) {
110 super(description || `Input from ${input}`);
112 this._fallback = fallback;
115 value(tr: TaxReturn): U[T] {
117 if (!this.form.hasInput(this._input) && this._fallback !== undefined) {
119 return this._fallback;
121 const value = this.form.getInput<T>(this._input);
127 export class AccumulatorLine<F extends Form,
128 L extends keyof F['lines']>
129 extends Line<number> {
130 private _form: FormClass<F>;
133 constructor(form: FormClass<F>, line: L, description?: string) {
134 super(description || `Accumulator ${form.name}@${line}`);
141 const forms: F[] = tr.findForms(this._form);
142 const value = sumLineOfForms(tr, forms, this._line);
148 export class UnsupportedLine extends Line<number> {
149 constructor(description?: string) {
150 super(description || 'Unsupported');
154 // Unsupported lines are deliberately omitted from Trace.
159 export function sumLineOfForms<F extends Form, L extends keyof F['lines']>(
160 tr: TaxReturn, forms: F[], line: L): number {
161 const reducer = (acc: number, curr: F) => acc + curr.getValue(tr, line);
162 return forms.reduce(reducer, 0);
165 export function sumFormLines<F extends Form, L extends keyof F['lines']>(
166 tr: TaxReturn, form: F, lines: L[]): number {
168 for (const line of lines)
169 value += form.getValue(tr, line);