You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since .map() does not seem to be an issue, and member expressions of course aren’t, I’m not sure why this fails. The actual error is TypeError: Cannot read properties of null (reading 'a')
Statically evaluating a MemberExpression is not harder than a BinaryExpression, is it?
After looking at the code, it seems to me that in both cases the x statement is statically evaluated to null. In the BinaryExpression code, the binary evaluation returns null + 1 which is 1, and you then allow and execute the function.
However since null["a"] raises an error, the function is not allowed and executed. However x["a"] is knowable statically (it is either the property or undefined if it’s absent − if you call evaluate('x["b"]', {x: {a: 1}}), you get undefined, not an error), so the function should execute in this case as well.
There is a simple workaround which is to return (x || {a: ""})["a"] instead of x["a"], this avoids throwing an error by ensuring there is a fallback value to x.a if x is undefined.
The text was updated successfully, but these errors were encountered:
Why does the following work:
But not the following:
Since
.map()
does not seem to be an issue, and member expressions of course aren’t, I’m not sure why this fails. The actual error isTypeError: Cannot read properties of null (reading 'a')
The inner function ASTs are respectively:
And:
Statically evaluating a MemberExpression is not harder than a BinaryExpression, is it?
After looking at the code, it seems to me that in both cases the
x
statement is statically evaluated tonull
. In theBinaryExpression
code, the binary evaluation returnsnull + 1
which is1
, and you then allow and execute the function.However since
null["a"]
raises an error, the function is not allowed and executed. Howeverx["a"]
is knowable statically (it is either the property orundefined
if it’s absent − if you callevaluate('x["b"]', {x: {a: 1}})
, you getundefined
, not an error), so the function should execute in this case as well.There is a simple workaround which is to return
(x || {a: ""})["a"]
instead ofx["a"]
, this avoids throwing an error by ensuring there is a fallback value tox.a
ifx
is undefined.The text was updated successfully, but these errors were encountered: