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 enum FormatType {
17 interface LineOptions {
18 formatType?: FormatType;
21 export abstract class Line<T> {
22 private _description?: string;
23 private _options?: LineOptions;
25 _id: string; // _id is set by Form.init().
26 form: Form; // Set by Form.init();
28 constructor(description?: string, options?: LineOptions) {
29 this._description = description;
30 this._options = options;
37 get description(): string {
38 return this._description;
41 get options(): LineOptions {
42 return this._options || {};
45 abstract value(tr: TaxReturn): T;
48 type ComputeFunc<T> = (tr: TaxReturn) => T;
50 export class ComputedLine<T> extends Line<T> {
51 private _compute: ComputeFunc<T>;
53 constructor(compute: ComputeFunc<T>, description?: string, options?: LineOptions) {
54 super(description, options);
55 this._compute = compute;
58 value(tr: TaxReturn): T {
60 const value = this._compute(tr);
66 export class ReferenceLine<F extends Form,
67 L extends keyof F['lines'],
68 T extends ReturnType<F['lines'][L]['value']>>
70 private _form: FormClass<F>;
72 private _fallback?: T;
74 // If creating a ReferenceLine and F is the same class as the
75 // the one the Line is in, erase |form|'s type with |as any| to
76 // keep TypeScript happy.
77 constructor(form: FormClass<F>, line: L, description?: string, fallback?: T, options?: LineOptions) {
78 super(description || `Reference ${form.name}@${line}`, options);
81 this._fallback = fallback;
84 value(tr: TaxReturn): T {
86 const form: F = tr.findForm(this._form);
87 if (this._fallback !== undefined && !form) {
89 return this._fallback;
91 const value: T = form.getValue(tr, this._line);
97 // SymbolicLine cannot be used for lines defined on F itself. For those cases, use:
98 // new ComputedLine((tr) => this.K(tr));
99 export class SymbolicLine<F extends Form & { [key in K]: ComputeFunc<ReturnType<F[K]>> },
101 extends Line<ReturnType<F[K]>> {
102 private _form: FormClass<F>;
105 constructor(form: FormClass<F>, key: K, description?: string, options?: LineOptions) {
106 super(description || `Reference ${form.name}/${key}`, options);
111 value(tr: TaxReturn): ReturnType<F[K]> {
113 const form: F = tr.findForm(this._form);
114 const value = form[this._key](tr);
120 export class InputLine<U = unknown, T extends keyof U = any> extends Line<U[T]> {
122 private _fallback: U[T];
126 constructor(input: T, description?: string, fallback?: U[T], options?: LineOptions) {
127 super(description || `Input from ${input}`, options);
129 this._fallback = fallback;
132 value(tr: TaxReturn): U[T] {
134 if (!this.form.hasInput(this._input) && this._fallback !== undefined) {
136 return this._fallback;
138 const value = this.form.getInput<T>(this._input);
144 export class AccumulatorLine<F extends Form,
145 L extends keyof F['lines']>
146 extends Line<number> {
147 private _form: FormClass<F>;
150 constructor(form: FormClass<F>, line: L, description?: string, options?: LineOptions) {
151 super(description || `Accumulator ${form.name}@${line}`, options);
158 const forms: F[] = tr.findForms(this._form);
159 const value = sumLineOfForms(tr, forms, this._line);
165 export class UnsupportedLine extends Line<number> {
166 constructor(description?: string) {
167 super(description || 'Unsupported');
171 // Unsupported lines are deliberately omitted from Trace.
176 export function sumLineOfForms<F extends Form, L extends keyof F['lines']>(
177 tr: TaxReturn, forms: F[], line: L): number {
178 const reducer = (acc: number, curr: F) => acc + curr.getValue(tr, line);
179 return forms.reduce(reducer, 0);
182 export function sumFormLines<F extends Form, L extends keyof F['lines']>(
183 tr: TaxReturn, form: F, lines: L[]): number {
185 for (const line of lines)
186 value += form.getValue(tr, line);