escodegen icon indicating copy to clipboard operation
escodegen copied to clipboard

Literal value is changed

Open darkoverlordofdata opened this issue 8 years ago • 1 comments

Is there an option to keep the raw value for literals?

For example, using the online tool: https://estools.github.io/escodegen/demo/index.html I enter in the text box:

var doubleValue = 0.0;

And it is re-generated as

var doubleValue = 0;

Normally that is the same, but I am attempting to generate asm.js (http://asmjs.org/spec/latest/) compatible code, and in that case, both FF and Chrome are now compile 0 with no decimal point as an int value and 0.0 as a double value, so I want to keep the 0.0 value.

darkoverlordofdata avatar Jul 07 '16 04:07 darkoverlordofdata

A little too late, probably, but maybe someone will find this useful.

API documentation discusses the parse option, which enables the use of the raw value.

option.parse

Mozilla Parser API compatible parse function, e.g., the parse function exported by esprima. If it is provided, generator tries to use the 'raw' representation. See esprima raw information. Default is null.

But actually, escodegen uses this condition to determine whether to use the raw value.

if (expr.hasOwnProperty("raw") && parse && extra.raw)

  • The first part checks whether the node contains a raw property.

  • The second part checks whether the parse option has been set.

  • The third option checks whether the raw option was set (the variable extra points to the options object).

To use the raw value, you need to set the raw option to true. The API documentation doesn't mention it at all.

pvvrzv avatar Aug 12 '23 14:08 pvvrzv