Are these warnings/errors displayed by the line numbers part of Node-RED's codebase, or is this functionality from another library?
The warnings/errors shown are wrong sometimes, e.g. defining a variable with var inside an if statement and then using it right after says it's out of scope (that would be true for let or const, not var).
It is the JavaScript parser that the ACE editor comes with.
I know it trips over some of the newer ES6 syntax.
Can you provide some specific examples?
This is within a function definition:
// create request data
if ([3, 4, 5, 6].includes(fc)) {
var n = 2;
var reqd = Buffer.from([(val >> 8) & 0xff, val & 0xff]);
} else if (fc === 16) {
const n_val = val.length;
if (n_val > 127)
throw 'Number of data values exceeds limit (127)';
var n = 2*n_val + 3;
var reqd = Buffer.concat([Buffer.from([0, n_val, 2*n_val]), ...val.map(x => Buffer.from([(x >> 8) & 0xff, x & 0xff]))]);
} else
throw 'Function code not implemented';
// fill in message length
n += 4;
reqh[4] = (n >> 8) & 0xff;
reqh[5] = n & 0xff;
return Buffer.concat([reqh, reqd]);
It complains that n is already defined with at the 2nd "var n =" statement inside the else if (not true), complains @ fill in message length that n, reqh, and reqd are out of scope (not true, var is function scoped)
I've also used "== null" to test if something is null or undefined, and it gripes about how I need to use === to test against null. IMO it should say "be careful of the difference between == and ===" or something like that.
var
is function scoped and you have used var n
twice in this function.... so the error message is correct.
1 Like
The var n
's are in different branches of an if statement, only one will be executed.
Doesn't matter. They are in the same function so will both get hoisted to the start of the function.
Touché, it appears you are correct. Oh goody, yet another JS quirk...I assumed that it worked like C/C++.
Never assume haha.
I mostly use let
or const
in block scope to avoid the side affects.