esprima icon indicating copy to clipboard operation
esprima copied to clipboard

Comments are attached to wrong nodes as inner position

Open ciplogic opened this issue 6 years ago • 0 comments

Steps to reproduce:

  1. Use Esprima demo page: https://esprima.org/demo/parse.html
  2. Copy paste this code inside the editor
function a() {
   var b = 5;
   /* Comment1 */
}
/* Comment2 */
{
   /* Comment 3 */
}
  1. Check option (checkbox) "Attach comments"

Expected output: All comments are trailing or leading with exception of the inner comment for "Comment 3"

Actual output:

{
    "type": "Program",
    "body": [
        {
            "type": "FunctionDeclaration",
            "id": {
                "type": "Identifier",
                "name": "a"
            },
            "params": [],
            "body": {
                "type": "BlockStatement",
                "body": [
                    {
                        "type": "VariableDeclaration",
                        "declarations": [
                            {
                                "type": "VariableDeclarator",
                                "id": {
                                    "type": "Identifier",
                                    "name": "b"
                                },
                                "init": {
                                    "type": "Literal",
                                    "value": 5,
                                    "raw": "5"
                                }
                            }
                        ],
                        "kind": "var",
                        "trailingComments": [
                            {
                                "type": "Block",
                                "value": " Comment1 ",
                                "range": [
                                    32,
                                    46
                                ]
                            }
                        ]
                    }
                ]
            },
            "generator": false,
            "expression": false,
            "async": false,
            "trailingComments": [
                {
                    "type": "Block",
                    "value": " Comment2 ",
                    "range": [
                        49,
                        63
                    ]
                }
            ]
        },
        {
            "type": "BlockStatement",
            "body": [],
            "innerComments": [
                {
                    "type": "Block",
                    "value": " Comment1 ",
                    "range": [
                        32,
                        46
                    ]
                },
                {
                    "type": "Block",
                    "value": " Comment2 ",
                    "range": [
                        49,
                        63
                    ]
                },
                {
                    "type": "Block",
                    "value": " Comment 3 ",
                    "range": [
                        69,
                        84
                    ]
                }
            ]
        }
    ],
    "sourceType": "script"
}

ciplogic avatar Oct 24 '19 14:10 ciplogic