Build: Add experimental_preserveFormat to @babel/generator (#16804)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-10-31 00:05:28 +08:00
1 parent ad16bc71a0
commit 1abee7352f
2 files changed
+29 -12

No files matched your search

+19 -2
View File
@@ -43,7 +43,12 @@ function transform(original, file) {
let changed = false;
const injected = new Set();
const ast = parse(original, { sourceType: "module" });
const ast = parse(original, {
filename: file,
sourceType: "module",
tokens: true,
createParenthesizedExpressions: true,
});
traverse(ast, (node) => {
for (const transform of transforms) {
if (!transform.test(node)) {
@@ -64,7 +69,19 @@ function transform(original, file) {
return original;
}
let { code } = generate(ast);
let { code } = generate(
ast,
{
sourceFileName: file,
experimental_preserveFormat: true,
retainLines: true,
comments: true,
jsescOption: null,
minified: false,
compact: false,
},
original,
);
if (injected.size > 0) {
code = outdent`
+10 -10
View File
@@ -16,7 +16,7 @@ const transform = (code) =>
test("Object.hasOwn", () => {
expect(transform("Object.hasOwn(foo, bar)")).toMatchInlineSnapshot(
`"Object.prototype.hasOwnProperty.call(foo, bar);"`,
`"Object.prototype.hasOwnProperty.call(foo,bar)"`,
);
});
@@ -24,25 +24,25 @@ test(".at", () => {
expect(transform("foo.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at(/* isOptionalObject */false, foo, -1);"
__at(/* isOptionalObject */false,foo,-1)"
`);
expect(transform("foo?.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at(/* isOptionalObject */true, foo, -1);"
__at(/* isOptionalObject */true,foo,-1)"
`);
expect(transform("foo?.bar.baz.at(-1)")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at(/* isOptionalObject */true, foo?.bar.baz, -1);"
__at(/* isOptionalObject */true,foo?.bar.baz,-1)"
`);
expect(transform("foo.at(-1)?.bar")).toMatchInlineSnapshot(`
"import __at from "<SHIMS>/at.js";
__at(/* isOptionalObject */false, foo, -1)?.bar;"
__at(/* isOptionalObject */false,foo,-1)?.bar"
`);
// Don't support optional call
@@ -53,7 +53,7 @@ test("String#replaceAll", () => {
expect(transform("foo.replaceAll('a', 'b')")).toMatchInlineSnapshot(`
"import __stringReplaceAll from "<SHIMS>/string-replace-all.js";
__stringReplaceAll(/* isOptionalObject */false, foo, 'a', 'b');"
__stringReplaceAll(/* isOptionalObject */false,foo,'a','b')"
`);
});
@@ -61,12 +61,12 @@ test("Array#findLast", () => {
expect(transform("foo.findLast(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLast from "<SHIMS>/array-find-last.js";
__arrayFindLast(/* isOptionalObject */false, foo, callback);"
__arrayFindLast(/* isOptionalObject */false,foo,callback)"
`);
expect(transform("foo?.findLast(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLast from "<SHIMS>/array-find-last.js";
__arrayFindLast(/* isOptionalObject */true, foo, callback);"
__arrayFindLast(/* isOptionalObject */true,foo,callback)"
`);
// Don't support
@@ -79,12 +79,12 @@ test("Array#findLastIndex", () => {
expect(transform("foo.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex(/* isOptionalObject */false, foo, callback);"
__arrayFindLastIndex(/* isOptionalObject */false,foo,callback)"
`);
expect(transform("foo?.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex(/* isOptionalObject */true, foo, callback);"
__arrayFindLastIndex(/* isOptionalObject */true,foo,callback)"
`);
// Don't support