Code completion and error flagging on a global variable

Is there a way to define a global variable and get code completion on it's keys in the function nodes code editors ? Like typescript has string literals and will complain when you misspell one of the constants.
I find myself re-using and re-typing a lot of keys (to extract global variables and message topics). That's not DRY.

Hi. I assume you are using Monaco editor for the rich environment it provides?

If yes, then you can annotate variables with jsdoc type hints to get rich help from the editor. For example...

/** @type {Buffer} */
const buf = global.get('myBuffer')

/** @type {string} */
const str = global.get('myString')


However, regarding global custom annotations, no, there is presently no concept of this I'm afraid. (actually that's isn't 100% accurate so I'll just say not easily not in a low-code user friendly manner)

@Steve-Mcl thanks for the tip
I'm using node-red 3 right out of the box.
I'm a Typescript developer, it doesn't have to be 'low-code user friendly'.

My problem is with the 'myString' in your example. That litteral will be used in different places. As a developer you have to keep track of the litterals manually.
Misspellings or typos like 'my-string' or 'mtString' break the flow. There is no code completion or other help in the editor.
Your tip would be usefull if the type indication could be done at the definition of the variable and inherited to the places where the global variable is used.

An better solution would be to have the ability to define global litteral constants like f.i.

// initialisation
const SIMULATION_SPEED_KEY = 'sim-speed'
global.set( SIMULATION_SPEED_KEY , 1)
const BATTERY_LEVEL_KEY - 'battery-level'
global.set( BATTERY_LEVEL_KEY , 2500)

// in function node(s) somewhere in the flow
const batLevel = global.get( BATTERY_LEVEL_KEY) + chargeAmount - dischargeAmount
global.set( BATTERY_LEVEL_KEY , batLevel)

I don't mind if I have to do that in some OS level file. I currently keeping track of the key names in a notepad file opened to the side of node-red which is obviously bad practice.

Steve, would it be worth starting an FAQ to cover some more esoteric settings? For example, is it possible to configure snippets?

No, but definitely another I want to explore at some point.

The only reason these are not already there are ...

  1. first introduction was MVP
  2. anything I add to RED.editor.x needs to be supported (or fail gracefully) across both monaco and ACE
  3. time :frowning:

When it happens, I will make sure it is documented here (or someone will remind me :wink: )

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.