From 04eb3f6a55c7cf1ed2bb0b8aaa3789381661c623 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 16 Mar 2020 22:52:38 -0400 Subject: [PATCH] Simplify the Trace API further. --- src/core/Form.ts | 4 +- src/core/Line.ts | 22 +++++------ src/core/Trace.test.ts | 2 +- src/core/Trace.ts | 88 +++++++++++++++++++---------------------- src/fed2019/Form8949.ts | 6 +-- 5 files changed, 57 insertions(+), 65 deletions(-) diff --git a/src/core/Form.ts b/src/core/Form.ts index 9b46ab3..b8c16e8 100644 --- a/src/core/Form.ts +++ b/src/core/Form.ts @@ -5,7 +5,7 @@ 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'; @@ -54,7 +54,7 @@ export default abstract class Form }, 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]; } diff --git a/src/core/Line.ts b/src/core/Line.ts index b473130..3a838a0 100644 --- a/src/core/Line.ts +++ b/src/core/Line.ts @@ -4,7 +4,7 @@ // 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 { @@ -39,9 +39,9 @@ export class ComputedLine extends Line { } value(tr: TaxReturn): T { - const trace = new Trace(this); + Trace.begin(this); const value = this._compute(tr); - trace.end(); + Trace.end(); return value; } }; @@ -65,14 +65,14 @@ export class ReferenceLine, } 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; } }; @@ -90,13 +90,13 @@ export class InputLine extends Line } 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(this._input); - trace.end(); + Trace.end(); return value; } }; @@ -114,10 +114,10 @@ export class AccumulatorLine, } 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; } }; diff --git a/src/core/Trace.test.ts b/src/core/Trace.test.ts index 342e168..42689d8 100644 --- a/src/core/Trace.test.ts +++ b/src/core/Trace.test.ts @@ -6,7 +6,7 @@ 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; } diff --git a/src/core/Trace.ts b/src/core/Trace.ts index 5db974a..69c4138 100644 --- a/src/core/Trace.ts +++ b/src/core/Trace.ts @@ -5,66 +5,58 @@ 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) { - 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) { + 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): 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): string { + const description = line.description ? ` (${line.description})` : ''; + if (line.form === undefined) + return `${line.constructor.name}${description}`; + return `${line.form.name}-${line.id}${description}`; } diff --git a/src/fed2019/Form8949.ts b/src/fed2019/Form8949.ts index 53e5c21..d47a00b 100644 --- a/src/fed2019/Form8949.ts +++ b/src/fed2019/Form8949.ts @@ -3,7 +3,7 @@ // 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'; @@ -65,7 +65,7 @@ class Form8949Line extends 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'); @@ -74,7 +74,7 @@ class Form8949Line extends Line { f8949.getInput('adjustments') .filter(a => f1099bs.includes(a.entry)) .reduce((acc, curr) => acc + curr.amount, 0); - trace.end(); + Trace.end(); return { proceeds, costBasis, -- 2.43.5