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

fix: don't deduplicate comments with same start index #13169

Merged
merged 1 commit into from Apr 21, 2021

Conversation

gzzhanghao
Copy link
Contributor

@gzzhanghao gzzhanghao commented Apr 18, 2021

Q                       A
Fixed Issues? Fixes #12769
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link No
Any Dependency Changes? No
License MIT

Input code

const template = require('@babel/template');
const generator = require('@babel/generator');
const t = require("@babel/types");

const createStatement = template.statement("/*#__PURE__*/ foo()", {
  preserveComments: true,
});

const result = t.program([createStatement(), createStatement()]);

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

Expected behavior

/*#__PURE__*/
foo();
/*#__PURE__*/
foo();

Current behavior

/*#__PURE__*/
foo();
foo();

This PR includes following changes:

@babel-bot
Copy link
Collaborator

babel-bot commented Apr 18, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/45409/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 18, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 70e0c2b:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@gzzhanghao gzzhanghao changed the title fix: generate comments with same start index (#12769) fix: generate comments with same source Apr 18, 2021
packages/babel-generator/test/index.js Outdated Show resolved Hide resolved
@@ -348,6 +348,20 @@ describe("generation", function () {
const output = generate(type).code;
expect(output).toBe("(infer T)[]");
});

it("comments with same start index", () => {
const code1 = "/* leading a */ a();";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example of generating multiple statements for the program body in the PR author comment seems like a more likely scenario than what we're testing here. Can we change this to match that?

packages/babel-types/src/clone/cloneNode.ts Outdated Show resolved Hide resolved
packages/babel-generator/src/printer.ts Show resolved Hide resolved
@kaicataldo
Copy link
Member

Since modifying anything with comments ends up affecting a rather large surface area, I wonder if it would be prudent to split this up into two PRs (the cloning fix in one and the deduplication fix in the other) in case we find regressions and need to revert something.

@kaicataldo kaicataldo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator pkg: types labels Apr 19, 2021
@gzzhanghao gzzhanghao closed this Apr 20, 2021
@gzzhanghao gzzhanghao deleted the fx/duplicate-comment branch April 20, 2021 05:15
@gzzhanghao gzzhanghao restored the fx/duplicate-comment branch April 20, 2021 05:16
@gzzhanghao gzzhanghao reopened this Apr 20, 2021
@gzzhanghao
Copy link
Contributor Author

@kaicataldo Good point, the cloning fix was moved to #13178.

@gzzhanghao gzzhanghao changed the title fix: generate comments with same source fix: don't deduplicate comments with same start index Apr 20, 2021
@mischnic
Copy link
Contributor

I think the check that is removed here was added so that the comment in this example (which exists twice in the AST, both as leading and trailing)

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

isn't printed twice.

Have you checked that this case still behaves correctly?

@gzzhanghao
Copy link
Contributor Author

@mischnic We already have a similar test case: babel-generator/test/fixtures/comments/try-block-line-comment/input.js

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I think that the original use case for this object is now better handled by the _printedComments WeakSet.

@nicolo-ribaudo nicolo-ribaudo merged commit 66181db into babel:main Apr 21, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@babel/generator comment deduplication removes too many comments
6 participants