Background
I have developed a plugin to import and export Local Variables.
So I tried to run the export on a Figma sheet that already had Local Variables set up, and got an unexpected error. See the example code below.
Problems
The format of the value obtained by variable.valuesByMode
is { [modeId: string]: VariableValue }
. For example, a value like { '522:0': {r: 1, g: 1, b: 1, a: 1}, '522:1': {r: 0, g: 0, b: 0, a: 1} }
.
I wrote a process that changes this key to the mode name. For example, a value like { 'Light': {r: 1, g: 1, b: 1, a: 1}, 'Dark': {r: 0, g: 0, b: 0, a: 1} }
. Light and Dark values were taken from collection.modes
.
However, one variable returned an unknown third modeId. The program crashed because of a modeId that does not exist in collection.modes
.
In other words, I have reached the inside an if statement in the following code.
Example Code
const collections = await figma.variables.getLocalVariableCollectionsAsync();
const collection = collections[0]; // Assume a collection exists.
const variableId = collection.variableIds[0]; // Assume a variable exists.
const variable = await figma.variables.getVariableByIdAsync(variableId) as Variable;
const modeLengthOfVariable = Object.keys(variable.valuesByMode).length;
const modeLengthOfCollection = Object.keys(collection.modes).length;
if (modeLengthOfVariable !== modeLengthOfCollection) {
// Is it possible to reach here?
}
Is this a known defect? Any information on how to fix this problem?