PiotrDabkowski / pyjsparser

Fast JavaScript parser for Python.
MIT License
246 stars 37 forks source link

[PyJsParser.scanNumericLiteral()] more flexible JS Number parsing #30

Closed pawelmhm closed 4 years ago

pawelmhm commented 4 years ago

This will be allow for closer translation to Python, otherwise in many cases where you expected int you'll get float.

pawelmhm commented 4 years ago

cc @PiotrDabkowski fixes: https://github.com/PiotrDabkowski/pyjsparser/issues/3

pawelmhm commented 4 years ago

Python 2.6 travis build is failing because download of Python 2.6 fails, it looks like it is no longer available in Travis so I'd suggest removing it from travis,yml https://docs.travis-ci.com/user/languages/python/#python-versions

PiotrDabkowski commented 4 years ago

Hi, thanks, but in JS there is no int type, everything is a float. Converting to float was done on purpose to be compatible with JS and esprima.

pawelmhm commented 4 years ago

@PiotrDabkowski Javascript has Number type that represents both integer and floating point numbers. It is up to you to decide how to translate it to Python. If you convert some things to int some to floats it is still compatible with JS, it is just Python representation of JavaScript. It is like some french word can be translated one way or other to english and it still works. Personally for my use case converting thing that looks like int to float (e.g. 4888) is not working for me. But of course up to you to decide.

PiotrDabkowski commented 4 years ago

Right, but this is float under the hood, you can check that by running:

>>> (43954440935438493084343940398430943843).toPrecision(100)
43954440935438496900120504762330251264.00000000000000000000000000000000000000000000000000000000000000

There is a raw value entry in the dictionary that can be used to convert to int if needed. Converting to int by default (when the number can be represented by int) will break projects that depend on current float behavior (such as Js2Py).

pawelmhm commented 4 years ago

ah ok right, I'll use raw value