Skip to content

Address nameDB_ or add api to get name of a variable #6008

@maribethb

Description

@maribethb

Is your feature request related to a problem? Please describe.

The recommended way to get the name of a variable is the same way we do it in the built-in variable blocks:

const varName = JavaScript.nameDB_.getName(
      block.getFieldValue('VAR'), NameType.VARIABLE);

However, as you can tell by the underscore, nameDB_ is a protected variable on the generator, so it shouldn't be accessed outside the generator class (including by us in the default block).

Also, somewhat related, we are not currently exporting Blockly.Names.NameType in blockly.js so developers can't even do this successfully outside of Blockly.

Describe the solution you'd like

We could add an API to get the name of a variable for a given generator. That way developers don't have to know about the nuances of the nameDB, which seems too low-level to want to surface to them just to do this task.

Describe alternatives you've considered

Alternatively, we could make nameDB_ not protected. I don't think this is ideal because it's asking developers to be too familiar with the implementation. Plus, we just renamed the variable so there's a lot of churn if we renamed it again to remove the underscore, or it would be confusing if we didn't remove it.

Additional context

Discovered this via https://groups.google.com/g/blockly/c/JzVgbKEcyaw

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions