import Person from './Person';
import TaxReturn from './TaxReturn';
-import Trace from './Trace';
+import * as Trace from './Trace';
import { Line } from './Line';
import { InconsistencyError, NotFoundError } from './Errors';
if (!(name in this._input)) {
throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
}
- Trace.add(`${this.name} input: ${name}`);
+ Trace.mark(`${this.name} input: ${name}`);
return this._input[name];
}
// SPDX-License-Identifier: GPL-3.0-only
import TaxReturn from './TaxReturn';
-import Trace from './Trace';
+import * as Trace from './Trace';
import Form, { FormClass } from './Form';
export abstract class Line<T> {
}
value(tr: TaxReturn): T {
- const trace = new Trace(this);
+ Trace.begin(this);
const value = this._compute(tr);
- trace.end();
+ Trace.end();
return value;
}
};
}
value(tr: TaxReturn): T {
- const trace = new Trace(this);
+ Trace.begin(this);
const form: F = tr.findForm(this._form);
if (this._fallback !== undefined && !form) {
- trace.end();
+ Trace.end();
return this._fallback;
}
const value: T = form.getValue(tr, this._line);
- trace.end();
+ Trace.end();
return value;
}
};
}
value(tr: TaxReturn): U[T] {
- const trace = new Trace(this);
+ Trace.begin(this);
if (!this.form.hasInput(this._input) && this._fallback !== undefined) {
- trace.end();
+ Trace.end();
return this._fallback;
}
const value = this.form.getInput<T>(this._input);
- trace.end();
+ Trace.end();
return value;
}
};
}
value(tr): number {
- const trace = new Trace(this);
+ Trace.begin(this);
const forms: F[] = tr.findForms(this._form);
const value = sumLineOfForms(tr, forms, this._line);
- trace.end();
+ Trace.end();
return value;
}
};
import Form from './Form';
import TaxReturn from './TaxReturn';
import { ComputedLine, InputLine, ReferenceLine } from './Line';
-import Trace, { Edge, getLastTraceList } from './Trace';
+import { Edge, getLastTraceList } from './Trace';
class TestTaxReturn extends TaxReturn {
get year() { return 2019; }
import { Line } from './Line';
-var current: Trace = null;
-
-var traces: Trace[] = [];
-
export type Edge = [string, string];
-export default class Trace {
- private _edges: { [key: string]: Edge } = {};
- private _stack: string[] = [];
- private _name: string;
+type Trace = { [key: string]: Edge };
- constructor(line: Line<any>) {
- this._name = this._formatLine(line);
+var stack: string[] = [];
- if (current === null)
- current = this;
-
- if (current._stack.length != 0) {
- current._addEdge([ current._previousEdge(), this._name ]);
- }
-
- current._stack.push(this._name);
- }
+var current: Trace = null;
+var previous: Trace = null;
- static add(id: string) {
- if (current === null)
- return;
- current._addEdge([ current._previousEdge(), id ]);
- }
+export function begin(line: Line<any>) {
+ const name = formatLine(line);
- end() {
- current._stack.pop();
- if (current === this) {
- current = null;
- traces.push(this);
- }
- }
+ if (current === null)
+ current = {} as Trace;
- get traceList(): readonly Edge[] {
- return Object.values(this._edges);
- }
+ if (stack.length != 0)
+ addEdge([ previousEdge(), name ]);
- private _addEdge(e: Edge) {
- this._edges[`${e[0]}|${e[1]}`] = e;
- }
+ stack.push(name);
+}
- private _previousEdge(): string {
- return this._stack[this._stack.length - 1];
- }
+export function mark(id: string) {
+ if (current === null)
+ return;
+ addEdge([ previousEdge(), id ]);
+}
- private _formatLine(line: Line<any>): string {
- const description = line.description ? ` (${line.description})` : '';
- if (line.form === undefined)
- return `${line.constructor.name}${description}`;
- return `${line.form.name}-${line.id}${description}`;
+export function end() {
+ stack.pop();
+ if (stack.length == 0) {
+ previous = current;
+ current = null;
}
-};
+}
export function getLastTraceList(): readonly Edge[] {
- if (traces.length == 0)
+ if (previous === null)
return null;
- return traces[traces.length - 1].traceList;
+ return Object.values(previous);
+}
+
+function addEdge(e: Edge) {
+ current[`${e[0]}|${e[1]}`] = e;
+}
+
+function previousEdge(): string {
+ return stack[stack.length - 1];
+}
+
+function formatLine(line: Line<any>): string {
+ const description = line.description ? ` (${line.description})` : '';
+ if (line.form === undefined)
+ return `${line.constructor.name}${description}`;
+ return `${line.form.name}-${line.id}${description}`;
}
// version 3.0. The full text of the license can be found in LICENSE.txt.
// SPDX-License-Identifier: GPL-3.0-only
-import Trace from '../core/Trace';
+import * as Trace from '../core/Trace';
import { Form, Person, TaxReturn } from '../core';
import { Line, InputLine, ComputedLine, sumLineOfForms } from '../core/Line';
}
value(tr: TaxReturn): Form8949Total {
- const trace = new Trace(this);
+ Trace.begin(this);
const f1099bs = matching1099Bs(tr, this._box);
const proceeds = sumLineOfForms(tr, f1099bs, '1d');
const costBasis = sumLineOfForms(tr, f1099bs, '1e');
f8949.getInput('adjustments')
.filter(a => f1099bs.includes(a.entry))
.reduce((acc, curr) => acc + curr.amount, 0);
- trace.end();
+ Trace.end();
return {
proceeds,
costBasis,