Skip to content

@babel/generator comment deduplication removes too many comments #12769

@mischnic

Description

@mischnic

Bug Report

  • I would like to work on a fix!

Input Code

import { parse } from "@babel/parser";
import generator from "@babel/generator";
import t from "@babel/types";

const code1 = `
// hello!
a();
`;

const code2 = `
// hello
b();
`;

let ast1 = parse(code1).program;
let ast2 = parse(code2).program;

let ast = t.program([...ast1.body, ...ast2.body]);

console.log(generator.default(ast).code);

Expected behavior

// hello!
a();
// hello
b();

Current behavior

// hello!
a();
b();

Environment

    "@babel/generator": "7.12.15",
    "@babel/parser": "7.12.15",

Possible Solution

Additional context

I was surprised that there are cases where comment exists twice (as leading and trailing), e.g.

if (1) {
  a();
}
// /* */
else {
  b();
}

. Looks like babel-generators "workaround" for this isn't quite right.

Distantly related to parcel-bundler/parcel#5787

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions