mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

`calculateNecessaryParentheses` blows up cryptically when `getPrecedence` returns null

Open mattvague opened this issue 3 years ago • 1 comments

I accidentally created a node with the fn name mult instead of multiply i.e. new OperatorNode('*', 'mult', [...]), and when I went to tex-ify it I got the error Cannot read property 'OperatorNode:mult' of undefined which I found quite cryptic and took quite a bit of digging to figure out.

Is there a reason why getPrecedence returns null instead of throwing an error when precedence can't be determined? If so what would you recommend as a fix, if not could I change that?

mattvague avatar May 23 '22 20:05 mattvague

The function getPrecedence(node, parenthesis) can be executed with any node type. Precedence does not have a meaning for all types of nodes: it is relevant for operators and assignment, but has no meaning for a constant or symbol for example. So I think it makes sense to return null in those cases: it's not an exception but "normal" behavior that some nodes do not have a precedence.

It would be good to see why toTex throws an exception when having an unknown type of OperatorNode, I think it shouldn't. Or if there is a good reason, we should improve the error message. I tried the following but cannot yet reproduce your issue:

console.log(new math.OperatorNode('*', 'mult', [
  new math.ConstantNode(2), 
  new math.ConstantNode(3)
]).toTex()) 
// '2*2'

josdejong avatar May 24 '22 07:05 josdejong