Simplify the Trace API further.
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 17 Mar 2020 02:52:38 +0000 (22:52 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 17 Mar 2020 03:18:32 +0000 (23:18 -0400)
src/core/Form.ts
src/core/Line.ts
src/core/Trace.test.ts
src/core/Trace.ts
src/fed2019/Form8949.ts

index 9b46ab3254bae562604f2cea8c115a97e11c52da..b8c16e8401d0433c389c7461f039205c23e62d7e 100644 (file)
@@ -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<L extends { [key: string]: Line<any> },
     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];
   }
 
index b4731305bbe439657df5aba03dc3522ec3d40b4a..3a838a033551c7d14511f845567db2e35c9b8713 100644 (file)
@@ -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<T> {
@@ -39,9 +39,9 @@ export class ComputedLine<T> extends 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;
   }
 };
@@ -65,14 +65,14 @@ export class ReferenceLine<F extends Form<any>,
   }
 
   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<U = unknown, T extends keyof U = any> extends Line<U[T]>
   }
 
   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;
   }
 };
@@ -114,10 +114,10 @@ export class AccumulatorLine<F extends Form<any>,
   }
 
   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;
   }
 };
index 342e168f302d34ea01f5a9fcc6daf07bc4b97d4c..42689d8fae6dd5d24025f98047f169482a73518f 100644 (file)
@@ -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; }
index 5db974ab73262cbdfa744720506d9cba28cdc989..69c41384c7c9bbb7d2065f3617609c32ed85bb70 100644 (file)
@@ -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<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}`;
 }
index 53e5c21ce8c0f2ea5ed94179737cecbbc6abcc86..d47a00bc4a7bda5efab658d479987f54cb743b6d 100644 (file)
@@ -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<Form8949Total> {
   }
 
   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<Form8949Total> {
         f8949.getInput('adjustments')
           .filter(a => f1099bs.includes(a.entry))
           .reduce((acc, curr) => acc + curr.amount, 0);
-    trace.end();
+    Trace.end();
     return {
       proceeds,
       costBasis,