Skip to content

[Bug]: ESTree plugin can stop comments from being attached to directives #14919

@hegemonic

Description

@hegemonic

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

"use strict";

/** @external foo */

Configuration file name

No response

Configuration

{
  "plugins": ["estree"]
}

Current and expected behavior

Current behavior

If a source file contains 1) a directive, followed by 2) a comment, and nothing else, and the ESTree plugin is enabled, then @babel/parser does not attach the comment to any AST node.

Current AST
{
  "type": "File",
  "start": 0,
  "end": 36,
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 4,
      "column": 0
    }
  },
  "range": [
    0,
    36
  ],
  "errors": [],
  "program": {
    "type": "Program",
    "start": 0,
    "end": 36,
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 4,
        "column": 0
      }
    },
    "range": [
      0,
      36
    ],
    "sourceType": "script",
    "interpreter": null,
    "body": [
      {
        "type": "ExpressionStatement",
        "start": 0,
        "end": 13,
        "loc": {
          "start": {
            "line": 1,
            "column": 0
          },
          "end": {
            "line": 1,
            "column": 13
          }
        },
        "range": [
          0,
          13
        ],
        "expression": {
          "type": "Literal",
          "start": 0,
          "end": 12,
          "loc": {
            "start": {
              "line": 1,
              "column": 0
            },
            "end": {
              "line": 1,
              "column": 12
            }
          },
          "range": [
            0,
            12
          ],
          "value": "use strict",
          "raw": "'use strict'"
        },
        "directive": "use strict"
      }
    ]
  },
  "comments": [
    {
      "type": "CommentBlock",
      "value": "* @external foo ",
      "start": 15,
      "end": 35,
      "loc": {
        "start": {
          "line": 3,
          "column": 0,
          "index": 15
        },
        "end": {
          "line": 3,
          "column": 20,
          "index": 35
        }
      }
    }
  ]
}

Expected behavior

@babel/parser attaches the comment to the ExpressionStatement node for the directive as a trailing comment.

Expected AST
{
  "type": "File",
  "start": 0,
  "end": 36,
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 4,
      "column": 0
    }
  },
  "range": [
    0,
    36
  ],
  "errors": [],
  "program": {
    "type": "Program",
    "start": 0,
    "end": 36,
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 4,
        "column": 0
      }
    },
    "range": [
      0,
      36
    ],
    "sourceType": "script",
    "interpreter": null,
    "body": [
      {
        "type": "ExpressionStatement",
        "start": 0,
        "end": 13,
        "loc": {
          "start": {
            "line": 1,
            "column": 0
          },
          "end": {
            "line": 1,
            "column": 13
          }
        },
        "range": [
          0,
          13
        ],
        "trailingComments": [
          {
            "type": "CommentBlock",
            "value": "* @external foo ",
            "start": 15,
            "end": 35,
            "loc": {
              "start": {
                "line": 3,
                "column": 0,
                "index": 15
              },
              "end": {
                "line": 3,
                "column": 20,
                "index": 35
              }
            }
          }
        ],
        "expression": {
          "type": "Literal",
          "start": 0,
          "end": 12,
          "loc": {
            "start": {
              "line": 1,
              "column": 0
            },
            "end": {
              "line": 1,
              "column": 12
            }
          },
          "range": [
            0,
            12
          ],
          "value": "use strict",
          "raw": "'use strict'"
        },
        "directive": "use strict"
      }
    ]
  },
  "comments": [
    {
      "type": "CommentBlock",
      "value": "* @external foo ",
      "start": 15,
      "end": 35,
      "loc": {
        "start": {
          "line": 3,
          "column": 0,
          "index": 15
        },
        "end": {
          "line": 3,
          "column": 20,
          "index": 35
        }
      }
    }
  ]
}

Environment

  • Babel version: 7.19.0
  • Node version: 18.8.0
  • npm version: 8.18.0
  • OS: macOS 12.0.1
  • Monorepo: yes

Possible solution

Pull request coming shortly.

Additional context

This issue is a regression introduced by #13521, which rewrote the comment-attachment code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: estreeoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions