UXP removes (optimizes out) a variable that is being used / Bug

,

FYI

I seems I hit a but where apparently UXP is optimizing out a variable that is being used.
( Note: fileToTranscriptLUT has been defined as a Map() and initialized prior to the call)

This fails (“transcript” is unassigned, and “unassigned” is passed to the function)

    if (start.indexOf("+") > 0) {
      const transcript = fileToTranscriptLUT.get(sourceFile)
      [start, end] = getStartAndEnd(text, start.slice(0,-1), end, transcript)
    }

… but this works fine (value associated with sourceFile is returned as expected and passed to the function):

   if (start.indexOf("+") > 0) {
      const transcript = fileToTranscriptLUT.get(sourceFile)
      typeof(transcript); // looks useless but needed to work around bug
      [start, end] = getStartAndEnd(text, start.slice(0,-1), end, transcript)
    }

It seems like the engine is trying to optimize the variable transcript out (despite being passed to function just below).
The extra line using the variable as well (typeof(transcript)) to prevent the engine from optimizing it out (it worked as well when I was logging the variable logWarning(transcript) instead of that line).

Modern JS syntax additions have their downsides.

A semicolon at the end of the const … statement should do. Otherwise the statement is continued with the stray array index [start,end] . The chained assignment sets yourGetResult[end] before the result of that assignment - the assigned value - is stored in the const.

3 Likes

Great, thank you @Dirk !

Too much Python coding over the years pushed me into bad habits indeed.
I’ll start put those semi-colon back in my JavaScript to avoid that type of issues in the future :slight_smile:

( Note to self: treat JS syntax more like C than Python )