Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member expression on number literal generates wrong output #104

Closed
Gozala opened this issue Jun 27, 2013 · 10 comments
Closed

Member expression on number literal generates wrong output #104

Gozala opened this issue Jun 27, 2013 · 10 comments

Comments

@Gozala
Copy link
Contributor

Gozala commented Jun 27, 2013

require("escodegen").generate({
            "type": "ExpressionStatement",
            "expression": {
                "type": "MemberExpression",
                "computed": false,
                "object": {
                    "type": "Literal",
                    "value": 4,
                    "raw": "4"
                },
                "property": {
                    "type": "Identifier",
                    "name": "foo"
                }
            }
        })
// => '4..foo;'

While it should output:

(4).foo;
@Gozala
Copy link
Contributor Author

Gozala commented Jun 27, 2013

P.S.: I'm using version: '0.0.23'

@Constellation
Copy link
Member

Thx for filing issue. But this is completely valid ECMAScript.
First dot is floating point and second dot is property lookup.
So you can also write as follows,

4.                .  foo;

You can check it in your JS engine OR http://esprima.org/demo/parse.html?code=4..foo

@Constellation
Copy link
Member

Of cource, 4.foo; is syntax invalid :)

@Gozala
Copy link
Contributor Author

Gozala commented Jun 27, 2013

Oh wow I have not realized that :) Thanks

@dannycochran
Copy link

@Constellation but why does it coerce it to 4..foo?

If you translate 4..foo back to an AST, the "raw" value of the Literal type is "4.", but the AST provided by @Gozala doesn't have that dot following the 4. It seems like the AST should actually be considered invalid, no?

@michaelficarra
Copy link
Member

@dannycochran If the Literal AST node has a raw value (and it is lexically valid), it will be used. So the round-trip property still holds.

@dannycochran
Copy link

@michaelficarra but the AST changes roundtrip, no?

@michaelficarra
Copy link
Member

Ah, I think I see what you mean. Thanks, @dannycochran. The fix here would be to compile to

4 .foo;

@dannycochran
Copy link

dannycochran commented Jul 20, 2017

@michaelficarra -- I think that seems right, though I could also see the case for escodegen to throw an error and say an AST like the one provided by @Gozala is invalid.

it seems like ESTree should have a separate way of represeting 4 .foo and (4).foo, as it does for 4..foo

@michaelficarra
Copy link
Member

@dannycochran I disagree. Those should be identical in an AST. The fact that they're not due to raw on Literal is the problem.

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

No branches or pull requests

4 participants