jquery / jquery/esprima

Comments are attached to wrong nodes as inner position

Open
#2,004 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
772
PR merge metrics
No merged PRs in 30d

Description

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"
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in the Esprima demo parse page using the supplied JavaScript and the “Attach comments” option. Trace comment attachment from that entry point and verify that comments are not duplicated or attached to the wrong nodes, with Comment 3 retained as the inner comment for its block.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.