Repository: FHIR/sushi (ts)

Issue:
fshToFHIR is not clearing the errors array between invocations

Pull request:
Reset errors and warnings between fshToFhir calls
Fixes #625 by clearing out the `errors` and `warnings` arrays at the start of the `fshToFhir` function, so that the reported errors and warnings only relate to that call.

To test, I've been using the following script:
```
const { sushiClient } = require('fsh-sushi');

const FSH0 = `
Alias: $USCoreLab = http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation-lab
Alias: $USCorePatient = http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient
Alias: $LOINC = http://loinc.org

Profile: MyObservationProfile
Parent: $USCoreLab
* value[x] only Quantity

Instance: MyPatient
InstanceOf: $USCorePatient
* identifier[0].system = "http://foo.org/id"
* identifier[0].value = "1"
* name[0].given[0] = "Homer"
* name.family = "Simpson"
* gender = #male


Instance: MyObservationInstance
InstanceOf: MyObservationProfile
* subject = Reference(MyPatient)
* code = $LOINC#1234-5
* status = #final
* valueQuantity = 55 'mm'
`;

const FSH1 = `
Instance: MyOtherObservationInstance
InstanceOf: MyObservationProfile
* subject = Reference(MyPatient)
* code = $LOINC#9876-5
* status = #final
* valueQuantity = 42 cm'
`

const OPTIONS = {
  canonical: 'http://foo.org/bar',
  version: '0.0.1',
  dependencies: [{ packageId: 'hl7.fhir.us.core', version: '3.1.1' }],
  logLevel: 'debug'
}
void async function() {
  const results = await sushiClient.fshToFhir([FSH0, FSH1], OPTIONS);
  console.log(results.errors)

  const results2 = await sushiClient.fshToFhir([FSH0, FSH1], OPTIONS);
  console.log(results2.errors)


  const results3 = await sushiClient.fshToFhir([FSH0, FSH1], OPTIONS);
  console.log(results3.errors)
}();
```
which has an error on the last line, the `42 cm'` is missing a leading `'`. You can run this script with node, just make sure it can find your locally built version of `fsh-sushi`. If you run this script with a dependency on the built version of `master`, you will see 2 errors, then 4, then 6, as the errors accumulate over the three calls. If you run this script with a dependency on the built version of this branch, you will see 2 errors, then 2, then 2, as the errors no longer accumulate. I also removed the manual resetting that was happening in the tests, as that is no longer necessary.

Patch:
diff --git a/src/run/FshToFhir.ts b/src/run/FshToFhir.ts
index 0d502b0..04cef8f 100644
--- a/src/run/FshToFhir.ts
+++ b/src/run/FshToFhir.ts
@@ -10,6 +10,14 @@ import {
   ErrorsAndWarnings
 } from '../utils';
 
+/**
+ * This function can be used to process input string(s) containing FSH definitions into JSON.
+ * NOTE: This function is not safe for true asynchronous usage. If two calls of this function are running at once, the error and warnings reported
+ * will be inconsistent. Always ensure a given call to this function completes before making a new call.
+ * @param {string|string[]} input - A string or array of strings containing FSH
+ * @param {fshToFhirOptions} options - An object containing options for processing. Options include canonical, version, dependencies, and logLevel
+ * @returns {Promise<{fhir: any[]; errors: ErrorsAndWarnings['errors']; warnings: ErrorsAndWarnings['warnings'];}>} - Object containing generated fhir, and errors/warnings from processing
+ */
 export async function fshToFhir(
   input: string | string[],
   options: fshToFhirOptions = {}
@@ -19,6 +27,7 @@ export async function fshToFhir(
   warnings: ErrorsAndWarnings['warnings'];
 }> {
   // track errors and warnings, and determine log level from options
+  errorsAndWarnings.reset();
   errorsAndWarnings.shouldTrack = true;
   if (options.logLevel == 'silent') {
     logger.transports[0].silent = true;


Test patch:
diff --git a/test/run/FshToFhir.test.ts b/test/run/FshToFhir.test.ts
index a22f5af..c08cfcf 100644
--- a/test/run/FshToFhir.test.ts
+++ b/test/run/FshToFhir.test.ts
@@ -1,7 +1,7 @@
 import fs from 'fs-extra';
 import path from 'path';
 import { loggerSpy } from '../testhelpers';
-import { errorsAndWarnings, logger } from '../../src/utils/';
+import { logger } from '../../src/utils/';
 import { fshToFhir } from '../../src/run';
 import * as utils from '../../src/utils';
 import { Configuration } from '../../src/fshtypes';
@@ -25,7 +25,6 @@ describe('#FshToFhir', () => {
   beforeEach(() => {
     loadSpy.mockClear();
     loggerSpy.reset();
-    errorsAndWarnings.reset();
   });
 
   it('should use the "info" logging level by default', async () => {