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: Correctly indenting when retainLines is enabled #16166

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -1042,6 +1042,7 @@ class Printer {
HAS_NEWLINE.test(comment.value)
) {
this.token("(");
this.indent();
_parenPushNewlineState.printed = true;
}
val = `/*${comment.value}*/`;
Expand Down
137 changes: 87 additions & 50 deletions packages/babel-generator/test/index.js
Expand Up @@ -1465,79 +1465,116 @@ describe("programmatic generation", function () {
line*/"
`);
});
});

it("comment in arrow function with return type", () => {
const arrow = t.arrowFunctionExpression(
[t.identifier("x"), t.identifier("y")],
t.identifier("z"),
);
arrow.returnType = t.tsTypeAnnotation(t.tsAnyKeyword());
arrow.returnType.trailingComments = [
{ type: "CommentBlock", value: "foo" },
// This comment is dropped. There is no way to safely print it
// as a trailingComment of the return type.
{ type: "CommentBlock", value: "new\nline" },
];
expect(generate(arrow).code).toMatchInlineSnapshot(
`"(x, y): any /*foo*/ => z"`,
);
});
it("comment in arrow function with return type", () => {
const arrow = t.arrowFunctionExpression(
[t.identifier("x"), t.identifier("y")],
t.identifier("z"),
);
arrow.returnType = t.tsTypeAnnotation(t.tsAnyKeyword());
arrow.returnType.trailingComments = [
{ type: "CommentBlock", value: "foo" },
// This comment is dropped. There is no way to safely print it
// as a trailingComment of the return type.
{ type: "CommentBlock", value: "new\nline" },
];
expect(generate(arrow).code).toMatchInlineSnapshot(
`"(x, y): any /*foo*/ => z"`,
);
});

it("multi-line leading comment after return", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(generate(t.returnStatement(val)).code).toMatch(`return (
it("multi-line leading comment after return", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(generate(t.returnStatement(val)).code).toMatch(`return (
/*new
line*/
val
);`);
});
});

it("multi-line leading comment after return 2", () => {
const ast = parse(
`return (
it("multi-line leading comment after return 2", () => {
const ast = parse(
`return (
/*new
line*/ val);`,
{
allowReturnOutsideFunction: true,
},
);
// Remove `parenthesized`
ast.program.body[0].argument.extra = null;
expect(generate(ast).code).toMatchInlineSnapshot(`
{
allowReturnOutsideFunction: true,
},
);
// Remove `parenthesized`
ast.program.body[0].argument.extra = null;
expect(generate(ast).code).toMatchInlineSnapshot(`
"return (
/*new
line*/
val
);"
`);
});
});

it("multi-line leading comment after return compact", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
compact: true,
}).code,
).toMatchInlineSnapshot(`
it("multi-line leading comment after return compact", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
compact: true,
}).code,
).toMatchInlineSnapshot(`
"return(/*new
line*/val);"
`);
});
});

it("multi-line leading comment after return concise", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
concise: true,
}).code,
).toMatchInlineSnapshot(`
it("multi-line leading comment after return concise", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
concise: true,
}).code,
).toMatchInlineSnapshot(`
"return (/*new
line*/ val );"
`);
});
});

it("correctly indenting when `retainLines`", () => {
const ast = parse(
`
export const App = () => {
return (
/**
* First
*/
2
);
};

/**
* Second
*/`,
{
sourceType: "module",
},
);

expect(generate(ast, { retainLines: true }).code).toMatchInlineSnapshot(`
"
export const App = () => {
return (
/**
* First
*/
2);

};

/**
* Second
*/"
`);
});
});

Expand Down