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

89.7% Statements 61/68
89.36% Branches 84/94
100% Functions 7/7
89.55% Lines 60/67

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  15x 15x 15x       15x       15x 15x         15x     19x                     15x 4x 4x   4x 6x     6x 18x 10x         4x     15x 2x       2x 2x           15x 2x       2x 4x               2x   2x       15x       14x 14x 14x       14x 50x 50x       14x 88x 88x   88x 6x 2x 4x 4x   6x     82x       13x 7x 6x 6x           82x 5x 5x       82x 4x     82x 2x     82x 2x         82x 2x       14x    
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,
  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 === "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);
};