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

Comment Attachment is confused by comment within condintion #155

Closed
lo1tuma opened this issue Jun 24, 2015 · 1 comment
Closed

Comment Attachment is confused by comment within condintion #155

lo1tuma opened this issue Jun 24, 2015 · 1 comment

Comments

@lo1tuma
Copy link
Member

lo1tuma commented Jun 24, 2015

Given the following code:

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

produces the following partial AST when attachComment is enabled:

{
  "type": "IfStatement",
  "test": {
    "type": "Identifier",
    "name": "a",
    "leadingComments": [
      {
        "type": "Block",
        "value": " foo "
      },
      {
        "type": "Block",
        "value": " bar "
      }
    ]
  },
  "consequent": {
    "type": "BlockStatement",
    "body": []
  },
  "alternate": null
}

As you can see both comments are attached to the Identifier node of the IfStatement test.

If the comment in the test condition is not present, the first comment foo is directly attached to the IfStatement node.

/* foo */
if (a) {}
{
  "type": "IfStatement",
  "test": {
    "type": "Identifier",
    "name": "a"
  },
  "consequent": {
    "type": "BlockStatement",
    "body": []
  },
  "alternate": null,
  "leadingComments": [
    {
      "type": "Block",
      "value": " foo "
    }
  ]
}

I would expect a more reliable behavior of the comment attachment algorithm, so that the first comment foo is always attached to the same node (preferable the IfStatement node).

Same problem exists for WhileStatement and I would guess ForStatement, ForInStatement, and ForOfStatement.

@nzakas
Copy link
Member

nzakas commented Jun 24, 2015

Agreed, seems like a bug. Comment attachment is hard.

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

No branches or pull requests

2 participants