Skip to content

Incorrect leading comments. #256

@mysticatea

Description

@mysticatea

From eslint/eslint#5512

In the following case:

var foo = {
  // foo
};
var bar;

// foo comment has attached to the leading of var bar.


"use strict";

const espree = require("espree");
const ast = espree.parse(
    `var foo = {
  // foo
};
var bar;`,
    {
        range: true,
        attachComment: true
    }
);

console.log(JSON.stringify(ast, null, 4));
{
    "type": "Program",
    "start": 0,
    "end": 32,
    "range": [
        0,
        32
    ],
    "body": [
        {
            "type": "VariableDeclaration",
            "start": 0,
            "end": 23,
            "range": [
                0,
                23
            ],
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "start": 4,
                    "end": 22,
                    "range": [
                        4,
                        22
                    ],
                    "id": {
                        "type": "Identifier",
                        "start": 4,
                        "end": 7,
                        "range": [
                            4,
                            7
                        ],
                        "name": "foo"
                    },
                    "init": {
                        "type": "ObjectExpression",
                        "start": 10,
                        "end": 22,
                        "range": [
                            10,
                            22
                        ],
                        "properties": [],
                        "trailingComments": [
                            {
                                "type": "Line",
                                "value": " foo",
                                "start": 14,
                                "end": 20,
                                "range": [
                                    14,
                                    20
                                ]
                            }
                        ]
                    }
                }
            ],
            "kind": "var"
        },
        {
            "type": "VariableDeclaration",
            "start": 24,
            "end": 32,
            "range": [
                24,
                32
            ],
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "start": 28,
                    "end": 31,
                    "range": [
                        28,
                        31
                    ],
                    "id": {
                        "type": "Identifier",
                        "start": 28,
                        "end": 31,
                        "range": [
                            28,
                            31
                        ],
                        "name": "bar"
                    },
                    "init": null
                }
            ],
            "kind": "var",
            "leadingComments": [
                {
                    "type": "Line",
                    "value": " foo",
                    "start": 14,
                    "end": 20,
                    "range": [
                        14,
                        20
                    ]
                }
            ]
        }
    ],
    "sourceType": "script",
    "comments": [
        {
            "type": "Line",
            "value": " foo",
            "start": 14,
            "end": 20,
            "range": [
                14,
                20
            ]
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions