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 mapping of tokens with generated nodes in between #16948

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add test for current wrong behavior
  • Loading branch information
nicolo-ribaudo committed Oct 30, 2024
commit eb56e816e35fd31c2228ece1b856bedbe0c95a28
42 changes: 42 additions & 0 deletions packages/babel-generator/test/preserve-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,47 @@ describe("experimental_preserveFormat", () => {

expect(out.code.trimEnd()).toBe(expected.trimEnd());
});

it("node injection", () => {
const input = `
const foo
= 3;
const bar =
3;
bax
`;
const expected = `
const foo
= 3;hello;
const bar=
3
bax
`;

const out = babel.transformSync(input, {
configFile: false,
plugins: [
({ types: t }) => ({
visitor: {
Program(path) {
path
.get("body.0")
.insertAfter(t.expressionStatement(t.identifier("hello")));
},
},
}),
],
parserOpts: {
createParenthesizedExpressions: true,
tokens: true,
},
generatorOpts: {
retainLines: true,
experimental_preserveFormat: true,
},
});

expect(out.code.trimEnd()).toBe(expected.trimEnd());
});
});
});