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

looks like leading comments wrong when var decls not ends with semi #9102

Closed
HairyRabbit opened this issue Nov 30, 2018 · 4 comments · Fixed by #10445
Closed

looks like leading comments wrong when var decls not ends with semi #9102

HairyRabbit opened this issue Nov 30, 2018 · 4 comments · Fixed by #10445
Labels
area: comments Has PR outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@HairyRabbit
Copy link

Bug Report

Bugs maybe

Current Behavior
See the below astexplorer.net screenshots, the second var decl haven't leading comments:

image
image

If the first decl ends by semi, it works fine

Input Code

  • REPL or Repo link if applicable:

I write a plugin to transform my code, it works with the leading comments

var s = `
/**
 * @test
 */
const a = {
  b: 42
}

/**
 * @test
 */
const c = 42
`
require('@babel/core').transform(s, { plugins: [
  function plugin(path) {
    return {
      visitor: {
        VariableDeclaration(path) { console.log(path.node.leadingComments, path.toString()) }
      }
    }
  }
]})

output:

image

Environment

image

@babel-bot
Copy link
Collaborator

Hey @HairyRabbit! 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.

@coreyfarrell
Copy link
Contributor

I've encountered this as well as it effects nyc ignore hints. From what I can tell the issue occurs to any comment following an object-literal assignment with no trailing semi-colon. I can only reproduce the error if the object-literal has at least one key.

obj = {a: 1}
/* istanbul ignore else */
if (true) {}

Using const, var or let to declare instead of assign obj makes no difference. The comment is recorded under tailingComments for the ObjectProperty representing the a property as well as the ExpressionStatement or VariableDeclaration, it is not recorded under leadingComments for the IfStatement.

Adding the semi-colon after {a: 1} fixes this. The comment is no longer recorded under tailingComments for the ObjectProperty, it is still recorded for the ExpressionStatement or VariableDeclaration and it is recorded under leadingComments of the IfStatement.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Sep 16, 2019

Might be fixed by #10445

@coreyfarrell
Copy link
Contributor

@nicolo-ribaudo I can confirm this fixes the issue both for my example code and for the code posted by @HairyRabbit.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 23, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: comments Has PR outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants