All files / linter checkCircularDependency.ts

70.58% Statements 12/17
25% Branches 2/8
100% Functions 2/2
68.75% Lines 11/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46        1x           3x       3x   3x       3x 3x   3x       3x   3x       3x   3x                    
import type { FeatureKey, Required } from "@featurevisor/types";
 
import { Datasource } from "../datasource";
 
export async function checkForCircularDependencyInRequired(
  datasource: Datasource,
  featureKey: FeatureKey,
  required?: Required[],
  chain: FeatureKey[] = [],
) {
  Iif (!required) {
    return;
  }
 
  const requiredKeys = required.map((r) => (typeof r === "string" ? r : r.key));
 
  Iif (requiredKeys.length === 0) {
    return;
  }
 
  for (const requiredKey of requiredKeys) {
    chain.push(requiredKey);
 
    Iif (chain.indexOf(featureKey) > -1) {
      throw new Error(`circular dependency found: ${chain.join(" -> ")}`);
    }
 
    const requiredFeatureExists = await datasource.featureExists(featureKey);
 
    Iif (!requiredFeatureExists) {
      throw new Error(`required feature "${requiredKey}" not found`);
    }
 
    const requiredParsedFeature = await datasource.readFeature(requiredKey);
 
    Iif (requiredParsedFeature.required) {
      await checkForCircularDependencyInRequired(
        datasource,
        featureKey,
        requiredParsedFeature.required,
        chain,
      );
    }
  }
}