Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Added support for Infinity and -Infinity #166

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

richthegeek
Copy link

This allows Genghis to display and save Infinity values. This only works for the Ruby version at the moment, and there's probably some style issues with it... but it works!

if node.value.type is 'Identifier' and node.value.name is 'NaN'
node.update("\"#{node.key.name}\": {\"$genghisType\": \"NaN\"}")
# -Infinity is seen as a UnaryExp, so handle it with a special case
if node.value.type is 'UnaryExpression' and node.value.argument.name is 'Infinity'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know how esprima parse trees look for other unary expressions, but it seems like this line should be:

if node.value.type is 'UnaryExpression' and node.value.argument.type is 'Identifier' and node.value.argument.name is 'Infinity'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I guess the extra check on the argument type would be good practice.

@bobthecow
Copy link
Owner

Thanks for the fix :)

Do you mind adding a test case for this as well?

@richthegeek
Copy link
Author

Oh hey, absolutely - I whipped this together because me and @9point6 where having a problem on our server.

I guess for the test cases to pass i'll need to do the PHP version also?

@richthegeek
Copy link
Author

Ok, looks like those tests should pass now - just some silly typos really :s

if node.value.type is 'Identifier' and node.value.name is 'NaN'
node.update("\"#{node.key.name}\": {\"$genghisType\": \"NaN\"}")
# -Infinity is seen as a UnaryExp, so handle it with a special case
if node.value.type is 'UnaryExpression' and node.value.argument.type is 'Identifier' and node.value.argument.name is 'Infinity'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other unary expressions (e.g. ~) so we should be checking the operator as well, and maybe whether it's a prefix operator. Otherwise things like this:

~Infinity

would get encoded as -Infinity. This is getting long and heinous now, so I'd extract the conditions into a helper function :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richthegeek poke :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure in what case someone would do that exactly? Should be a simple change either way, but I'm at home now so I'll sort it tomorrow.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is when someone unintentionally does that. It should be a parse error, not an implicit thunk into -Infinity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants