Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Diff in range data of Program vs other parsers for leading newline(s) #673

Closed
JamesHenry opened this issue Aug 15, 2017 · 4 comments
Closed

Comments

@JamesHenry
Copy link
Member

JamesHenry commented Aug 15, 2017

Eurgh... 😅

I am honestly not sure on what to suggest to address this, but if you take this really simple source (the leading newline is key but not that clear in the codeblock):

Input Code

function foo() {

}

You end up with different range data for the Program node across the different parsers.

Please see: http://astexplorer.net/#/gist/c336be172990d7b467d1ed3dbc1882ee/215640e9e6eece750582ef00b16a884a5c66530a

...and take a look at the different parsers.

I found this when running AST comparison tests between babylon and typescript-eslint-parser (which behaves like espree/esprima) for JS source fixtures.

The diff essentially boils down to the inclusion of the leading newline (or not in the case of babylon):

      "column": 0,
           "line": 1,
         },
       },
       "range": Array [
    -    1,
    +    0,
         28,
       ],
       "sourceType": "script",
       "type": "Program",
     }

I do have the ESTree plugin enabled, so I would theoretically expect to see parity here.

@hzoo
Copy link
Member

hzoo commented Aug 15, 2017

Hey @JamesHenry! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@JamesHenry
Copy link
Member Author

Same thing with leading comments:

/* foo */
if (/* bar */  a) {}

It appears Espree, Esprima, Flow and Recast have one view of the world, Acorn and Babylon have another.

Espree:

{
  "type": "Program",
  "start": 0,
  "end": 31,
  "range": [
    10,
    30
  ],
...

Esprima:

{
  "type": "Program",
  "range": [
    10,
    30
  ]
...

Flow:

{
  "type": "Program",
  "range": [
    10,
    30
  ],
...

Recast:

{
    "type": "Program",
    "range": [
      10,
      30
    ],
...

Acorn:

{
  "type": "Program",
  "start": 0,
  "end": 31,
  "range": [
    0,
    31
  ],
...

Babylon:

{
    "type": "Program",
    "start": 0,
    "end": 31,
    "range": [
      0,
      31
    ],
...

@JamesHenry
Copy link
Member Author

It seems the same groupings of parsers hold true for observed differences in TemplateLiteral.quasis location data too...

`42`;

Espree:

{
   "quasis": [{
      "type": "TemplateElement",
      "start": 1, // This combined with the range seems like a bug?
      "end": 3,  // This combined with the range seems like a bug?
      "range": [
         0,
         4
      ],
...

Esprima:

{
   "quasis": [{
      "type": "TemplateElement",
      "range": [
         0,
         4
      ],
...

Flow:

{
   "quasis": [{
      "type": "TemplateElement",
      "range": [
         0,
         4
      ],
...

Recast:

{
   "quasis": [{
      "type": "TemplateElement",
      "range": [
         0,
         4
      ],
...

Acorn:

{
   "quasis": [{
      "type": "TemplateElement",
      "start": 1,
      "end": 3,
      "range": [
         1,
         3
      ],
...

Babylon:

{
   "quasis": [{
      "type": "TemplateElement",
      "start": 1,
      "end": 3,
      "range": [
         1,
         3
      ],
...

@babel-bot
Copy link

This issue has been moved to babel/babel#6681.

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

No branches or pull requests

3 participants