How to prepend all user variables with an underscore?

99 views
Skip to first unread message

George

unread,
Jan 31, 2021, 12:39:58 PM1/31/21
to Blockly
In the generated JavaScript, how can I make all user variables prepended with an underscore to avoid name collisions with the generated code?

Also this seems like a far better solution to the "reserved keywords" stuff....

Beka Westberg

unread,
Jan 31, 2021, 3:07:32 PM1/31/21
to blo...@googlegroups.com
Hello,

You should be able to override the default javascript generators so that it appends an underscore the variable name before returning the code-string :D If you're unfamiliar with code generators you can check out the generating code documentation.

> Also this seems like a far better solution to the "reserved keywords" stuff....

That's an interesting opinion I haven't heard before! Why do you feel that way?

I hope that helps! If you have any further questions please reply!
--Beka

On Sun, Jan 31, 2021 at 9:39 AM George <gbake...@gmail.com> wrote:
In the generated JavaScript, how can I make all user variables prepended with an underscore to avoid name collisions with the generated code?

Also this seems like a far better solution to the "reserved keywords" stuff....

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/1bd76ffc-1b4f-4c52-9faa-284575fa042dn%40googlegroups.com.

George

unread,
Jan 31, 2021, 3:13:45 PM1/31/21
to blo...@googlegroups.com
I actually already tried editing https://github.com/google/blockly/blob/edd475f37b02888819b87ddfca1d6eeebe68898a/generators/javascript/variables.js#L18 , it doesn't change the `var one, two;` header at the top of the generated ouptut. I guess `javascript.js` itself would have to be changed. In the end I decided it's a bit too much hassle, I'm just going to dynamically change the reserved words instead.

> Also this seems like a far better solution to the "reserved keywords" stuff....
That's an interesting opinion I haven't heard before! Why do you feel that way?

Then custom generators can write with whatever auxiliary variables they want as long as they aren't prepending underscores, without worrying about collisions and dynamically generating the reserved words set as I have had to

You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/nG23ge5sTBc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAEBzR94aEo%2BA0QZGU65%2BKvBEjPx4YRxjdcKR7ypCvW4EvD7OJg%40mail.gmail.com.

Beka Westberg

unread,
Jan 31, 2021, 3:22:27 PM1/31/21
to blo...@googlegroups.com
> Then custom generators can write with whatever auxiliary variables they want as long as they aren't prepending underscores, without worrying about collisions and dynamically generating the reserved words set as I have had to

Ahhh ok that makes sense :D It does make it nice that you can use whatever auxiliary variables you want.

I'm a bit curious about why you're dynamically generating the reserved words though. My understanding from the docs is that they're supposed to be set statically before generating the code, usually using a string literal. Unless by dynamically you just mean you're going to have to keep changing it as you write more generators (which is admittedly... a bit of a pain hehe)

Anyway, I'm just glad you found a solution you think is going to work for you =)

Best,
--Beka

feni...@google.com

unread,
Feb 2, 2021, 5:31:59 PM2/2/21
to Blockly
Rather than changing the variable names used by the developer, I suggest putting a prefix on the variable names that you're using in your generators, since you have full control of those. 

If that isn't an option, generating a list of reserved words and adding them to the generator is the way to go.

Cheers,
Rachel

Reply all
Reply to author
Forward
0 new messages