jquery / jquery/esprima

Comments inside the function definition are added as leading comments to next succeeding node

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

Nobody has claimed this yet.

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

Description

I have a comment inside the function definition. So adding that as a trailing comment for that node seems to be correct. But the same comments are added as a leading comment to the next node seems to be meaningless as the comments are in the function scope of the previous node.

Version :
esprima : 4.0.1

The example input:

esprima.parse(`
function resolveAfter2Seconds(x) {
    var a = 5;    /*resolve arguments*/
}
const add = function (x) {
};`,{attachComment: true,range :true})

Expected output :

"body": [ {
   "type": "FunctionDeclaration",
   "body": {
    "type": "BlockStatement",
    "body": [
     {
      "type": "VariableDeclaration",
      "declarations": [ {  
        }, }
      ],
      "kind": "var",
      "trailingComments": [
       {
        "type": "Block",
        "value": "resolve arguments",
        "range": [
         52,
         73
        ]}]} ],},
   },{
   "type": "VariableDeclaration",
   "declarations": [ 
      ],
   }]
}

Actual output :

{
 "type": "Program",
 "body": [
  {
   "type": "FunctionDeclaration",
   "body": {
    "type": "BlockStatement",
    "body": [
     {
      "type": "VariableDeclaration",
      "declarations": [],
      "kind": "var",
      "trailingComments": [
       {
        "type": "Block",
        "value": "resolve arguments",
        "range": [
         52,
         73 ]}]}],
   },
  },
  {
   "type": "VariableDeclaration",
   "declarations": [
   ],
   "kind": "const",
   "leadingComments": [
    {
     "type": "Block",
     "value": "resolve arguments",
     "range": [
      52,
      73
     ]}]}]
}

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

Start with the esprima.parse call using attachComment: true and range: true, then trace how the inline comment after the variable declaration is attached to surrounding nodes. Reproduce the example and compare the AST with the expected output; done means the comment remains trailing on the declaration without also appearing as a leading comment on the following node.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.