google / blockly-games

Games for tomorrow's programmers.
https://blockly.games/
Apache License 2.0
1.28k stars 603 forks source link

Blockly Pond renames variables x and y, only on compressed #197

Closed moniika closed 3 years ago

moniika commented 4 years ago

Describe the bug

Generated code with variables x and y, renames them to x2 and y2, unlike running on uncompressed which does not rename these variables.

To Reproduce

Steps to reproduce the behavior:

  1. Go to https://blockly.games/admin
  2. Set debug to uncompressed
  3. Go to https://blockly.games/pond-duck?lang=en
  4. Add a function definition block with parameters x, y, and z, any order
  5. Observe generated code does not rename variables
  6. Repeat previous steps, but set debug compressed and observe generated code renames x and y to x2 and y2

Expected behavior

Compressed should not rename variables x and y.

Desktop (please complete the following information):

Additional context

Can be repro'd on blockly.games or locally using code on master. Does not repro on Blockly developer playground.

NeilFraser commented 3 years ago

The Closure Compiler renames all sorts of things to make the code shorter. In this case, Blockly.Block.prototype.getFieldValue is being renamed to x (and y is some other function I don't immediately recognize). The result is that x and y are global variables when compiled.

Blockly's JS generator scans the current environment for globals, and renames any conflicts. That deals with stuff like Math, or encodeURIComponent that would cause a conflict in any environment. In this case renaming x and y is not needed since those two globals only exist in the browser's environment, not the JS-Interpreter's sandbox.

To summarize, the renaming is not required, but it is harmless, and it is the expected behaviour of Blockly's code generators.

In any event, if this were to be addressed, it would be on Blockly's side, not Blockly Games.