react-jsx-parser icon indicating copy to clipboard operation
react-jsx-parser copied to clipboard

Fix: Pass scope through in binary expressions

Open andrewkshim opened this issue 3 years ago • 0 comments

Hi there, I noticed that list operations like Array.filter aren't working as expected. Here's a Codesandbox example.

The issue is that list.filter(item => item % 2 === 0) is returning an empty result when it should be [2, 4], and the fix is simply to pass along the scope into the parseExpressions of the BinaryExpression case (otherwise variables like item aren't properly resolved).

Open to any feedback. Thanks again for this library! For context, I have another PR open that's independent of this one.

Edit: ~~also added || and && support to BinaryExpression parsing.~~ Oops, never mind, I see this is taken care of in LogicalExpression; will revert. I am encountering an issue but this was a red herring, will come back with another solution.

Edit II: I ended up passing scope into all the other parseExpressions that didn't already have them. The test I added illustrates a case I was running into where things weren't evaluating as expected.

andrewkshim avatar Jun 15 '22 18:06 andrewkshim