mo4islona / node-blockly

Blockly for Node.js and Browser via CommonJS module
133 stars 81 forks source link

Blockly.JavaScript.addReservedWords() dosen't add a reserved word. #41

Closed FirstPotatoMan closed 4 years ago

FirstPotatoMan commented 4 years ago

In my Blockly Program, I have callbacks that reference certain variable names, such as msg, channel, and member. However, when I try to add those words as reserved words, they appear in the list of RESERVEDWORDS, but when I name a variable msg, it still says var msg in the generated code, not var msg2. Any way around this?

My code (using react-blockly-drawer):

  {
    name: "initMessageListener",
    category: "Listeners",
    block: {
      init: function() {
        this.jsonInit({
          message0: "On Message %1",
          args0: [
            {
              type: "input_statement",
              name: "CALLBACK"
            }
          ],
          previousStatement: null,
          nextStatement: null,
          colour: "#ff6d6b",
          tooltip:
            "Every time message is sent on the server, the code inside this block runs",
          helpUrl:
            "https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-message"
        });
      }
    },
    generator: block => {
      if (!Blockly.JavaScript.RESERVED_WORDS_.includes("msg")) {
        Blockly.JavaScript.addReservedWords("msg");
      }
  }
}
FirstPotatoMan commented 4 years ago

Turns out instead of putting it inside the block generator, I needed to put .addReservedWords() in componentDidMount().