Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leadingComments drops last line comment before expression #9957

Open
ian-craig opened this issue May 7, 2019 · 3 comments

Comments

Projects
None yet
4 participants
@ian-craig
Copy link
Contributor

commented May 7, 2019

Bug Report

Current Behavior
leadingComments is mising last line comment before expression when expression contains parens.

Input Code
https://astexplorer.net/#/gist/ff2da32a9fe6bf725c83b66adadc148d/6aa7b8c2421146443f11eaaa5937c5ff990a61fa

let fun = () => {
    // one
    // two
    // three
    return (1);
}

Current AST (@babel/parser 7.3.3+)

{
  "type": "ReturnStatement",
  ...
  "leadingComments": [
    {
      "type": "CommentLine",
      "value": " one",
      ...
    },
    {
      "type": "CommentLine",
      "value": " two",
      ...
    }
  ]
}

Expected behavior/code
Expect that the leadingComments should have three comments, as there are three in the source code. @babel/parser 7.3.2 is correct and outputs the following

{
  "type": "ReturnStatement",
  ...
  "leadingComments": [
    {
      "type": "CommentLine",
      "value": " one",
      ...
    },
    {
      "type": "CommentLine",
      "value": " two",
      ...
    },
    {
      "type": "CommentLine",
      "value": " three",
      ...
    }
  ]
}

Babel Configuration (.babelrc, package.json, cli command)

Repros on AST Explorer which may be a more standard environment than I'm running.

{
  "dependencies": {
    "@babel/generator": "7.4.4",
    "@babel/parser": "7.4.4",
    "@babel/template": "7.4.4",
    "@babel/traverse": "7.4.4",
    "@babel/types": "7.4.4",
  }
}
import { parse } from '@babel/parser';

parse(sourceText, {
    sourceType: 'module',
    strictMode: false,
    plugins: ['classProperties', 'typescript', 'decorators-legacy'],
});

Environment

  • Babel version(s): @babel/parser 7.3.3+ (Regression since @babel/parser 7.3.2)
  • Node/npm version: 10.15.1
  • OS: Windows 10
  • Monorepo: no
  • How you are using Babel: plugin, or AST explorer repros the issue too (link above)

Additional context/Screenshots

This only seems to repro when the statement with leading comments has parens.

Example 1

The below works just fine

let fun = () => {
   // one
   // two
   // three
   return 1;
}

The below drops comment // three

let fun = () => {
   // one
   // two
   // three
   return (1);
}

Example 2

The below works just fine

let fun = () => {
   // one
   // two
   // three
   if (!condition) {
      ...
   }
}

The below drops comment // three

let fun = () => {
   // one
   // two
   // three
   if (!(condition1)) {
      ...
   }
}
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented May 7, 2019

Hey @ian-craig! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@matthewrobertson

This comment has been minimized.

Copy link
Contributor

commented May 10, 2019

this was caused by this commit #9463

@danez

This comment has been minimized.

Copy link
Member

commented May 19, 2019

Thanks @matthewrobertson for pointing out which commit introduced the issue. Made it easy for me to find the issue.

Did you use git bisect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.