All files / src/shared/lib/variables extractVariableReferences.ts

90.27% Statements 65/72
90.19% Branches 92/102
100% Functions 8/8
90.14% Lines 64/71

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160  18x 18x 18x       18x         18x 18x         18x     21x                     18x 4x 4x   4x 6x     6x 18x 10x         4x     18x 2x       2x 2x           18x 2x       2x 4x               2x   2x       18x       16x 16x 16x       16x 55x 55x       16x 90x 90x   90x 6x 2x 4x 4x   6x     84x       13x 7x 6x 6x           84x 5x 5x       84x 2x 1x 4x         84x 4x     84x 2x     84x 2x         84x 2x       16x    
import type { ScriptEvent } from "shared/lib/resources/types";
import { lexText } from "shared/lib/compiler/lexText";
import { isScriptDataTable } from "shared/lib/scriptDataTable/types";
import {
  expressionToScriptValue,
  extractScriptValueVariables,
} from "shared/lib/scriptValue/helpers";
import {
  isScriptValue,
  isScriptValueArray,
  isScriptValueVariable,
} from "shared/lib/scriptValue/types";
import { normalizeVariableId } from "shared/lib/variables/variableIds";
import {
  isVariableFieldType,
  type ScriptEventDefsFieldTypeLookup,
} from "shared/lib/scripts/scriptDefHelpers";
 
const isUnionVariableValue = (
  value: unknown,
): value is { type: "variable"; value?: string } => {
  return (
    typeof value === "object" &&
    value !== null &&
    "type" in value &&
    value.type === "variable" &&
    (!("value" in value) ||
      value.value === undefined ||
      typeof value.value === "string")
  );
};
 
const extractDialogueVariableIds = (value: unknown): string[] => {
  const textValues = Array.isArray(value) ? value : [value];
  const variableIds: string[] = [];
 
  for (const textValue of textValues) {
    Iif (typeof textValue !== "string") {
      continue;
    }
    for (const token of lexText(textValue)) {
      if ("variableId" in token) {
        variableIds.push(token.variableId);
      }
    }
  }
 
  return variableIds;
};
 
const extractExpressionVariableIds = (value: unknown): string[] => {
  Iif (typeof value !== "string") {
    return [];
  }
 
  try {
    return extractScriptValueVariables(expressionToScriptValue(value));
  } catch {
    return [];
  }
};
 
const extractReferencedVariableIds = (value: unknown): string[] => {
  Iif (!Array.isArray(value)) {
    return [];
  }
 
  return value.flatMap((reference) => {
    if (
      typeof reference === "object" &&
      reference !== null &&
      "type" in reference &&
      reference.type === "variable" &&
      "id" in reference &&
      typeof reference.id === "string"
    ) {
      return [reference.id];
    }
    return [];
  });
};
 
export const extractVariableIdsFromScriptEvent = (
  scriptEvent: Pick<ScriptEvent, "id" | "command" | "args">,
  scriptEventDefs: ScriptEventDefsFieldTypeLookup,
): string[] => {
  const variableIds = new Set<string>();
  const args = scriptEvent.args;
  Iif (!args) {
    return [];
  }
 
  const addVariableId = (variableId: string | undefined) => {
    Eif (variableId) {
      variableIds.add(normalizeVariableId(variableId));
    }
  };
 
  for (const [key, value] of Object.entries(args)) {
    const field = scriptEventDefs[scriptEvent.command]?.fieldsLookup[key];
    const isCustomEventVariableArg = key.startsWith("$variable[");
 
    if (isCustomEventVariableArg) {
      if (typeof value === "string") {
        addVariableId(value);
      } else Eif (isScriptValue(value)) {
        extractScriptValueVariables(value).forEach(addVariableId);
      }
      continue;
    }
 
    if (
      isVariableFieldType(field?.type) ||
      (field && isUnionVariableValue(value))
    ) {
      if (typeof value === "string") {
        addVariableId(value);
      } else if (isScriptValueVariable(value)) {
        extractScriptValueVariables(value).forEach(addVariableId);
      } else Eif (isUnionVariableValue(value)) {
        addVariableId(value.value);
      }
    }
 
    if (field?.type === "value") {
      Eif (isScriptValue(value)) {
        extractScriptValueVariables(value).forEach(addVariableId);
      }
    }
 
    if (field?.type === "arraySet") {
      if (isScriptValueArray(value)) {
        value.forEach((val) =>
          extractScriptValueVariables(val).forEach(addVariableId),
        );
      }
    }
 
    if (field?.type === "text" || field?.type === "textarea") {
      extractDialogueVariableIds(value).forEach(addVariableId);
    }
 
    if (field?.type === "matharea") {
      extractExpressionVariableIds(value).forEach(addVariableId);
    }
 
    if (field?.type === "dataTable" && isScriptDataTable(value)) {
      value.variables
        .flatMap(extractScriptValueVariables)
        .forEach(addVariableId);
    }
 
    if (field?.type === "references") {
      extractReferencedVariableIds(value).forEach(addVariableId);
    }
  }
 
  return Array.from(variableIds);
};