@samitouri / QOS-React-2 / commits / 3605199646

[Babel] Refactor gating test

Move to separate function

Sathya Gunasekaran committed Mar 20, 2023 at 17:42 UTC 3605199646718806a8daa9d537827696238e2a18
1 file changed +38 -30
compiler/forget/src/Babel/BabelPlugin.ts
+38 -30
@@ -55,7 +55,7 @@ export default function ReactForgetBabelPlugin(
55 }
56
57 hasForgetCompiledCode = true;
58 - const ast = compile(fn, pass.opts.environment);
58 + const compiled = compile(fn, pass.opts.environment);
59
60 if (pass.opts.gatingModule) {
61 // Rename existing function
@@ -64,38 +64,15 @@ export default function ReactForgetBabelPlugin(
64 fn.node.id = addSuffix(fn.node.id, "_uncompiled");
65
66 // Rename and append compiled function
67 - invariant(ast.id, "FunctionDeclaration must produce a name");
68 - ast.id = addSuffix(ast.id, "_forget");
69 - const compiledFn = fn.insertAfter(ast)[0];
67 + invariant(compiled.id, "FunctionDeclaration must produce a name");
68 + compiled.id = addSuffix(compiled.id, "_forget");
69 + const compiledFn = fn.insertAfter(compiled)[0];
70 compiledFn.skip();
71
72 - // Build gating test
73 - const test = buildTest({
74 - compiled: ast.id,
75 - uncompiled: fn.node.id,
76 - original,
77 - });
78 -
79 - // Re-export new declaration
80 - const parent = fn.parentPath;
81 - if (t.isExportDefaultDeclaration(parent)) {
82 - // Re-add uncompiled function
83 - parent.replaceWith(fn)[0].skip();
84 -
85 - // Add test and synthesize new export
86 - compiledFn.insertAfter([test, t.exportDefaultDeclaration(original)]);
87 - } else if (t.isExportNamedDeclaration(parent)) {
88 - // Re-add uncompiled function
89 - parent.replaceWith(fn)[0].skip();
90 -
91 - // Add and export test
92 - compiledFn.insertAfter(t.exportNamedDeclaration(test));
93 - } else {
94 - // Just add the test, no need for re-export
95 - compiledFn.insertAfter(test);
96 - }
72 + // Build and append gating test
73 + compiledFn.insertAfter(buildGatingTest(fn, compiled.id, original));
74 } else {
98 - fn.replaceWith(ast);
75 + fn.replaceWith(compiled);
76 }
77
78 // We are generating a new FunctionDeclaration node, so we must skip over it or this
@@ -136,6 +113,37 @@ export default function ReactForgetBabelPlugin(
113 };
114 }
115
116 +function buildGatingTest(
117 + uncompiled: BabelCore.NodePath<t.FunctionDeclaration>,
118 + compiled: t.Identifier,
119 + original: t.Identifier
120 +): t.Node | t.Node[] {
121 + const test = buildTest({
122 + uncompiled: uncompiled.node.id!,
123 + compiled,
124 + original,
125 + });
126 +
127 + // Re-export new declaration
128 + const parent = uncompiled.parentPath;
129 + if (t.isExportDefaultDeclaration(parent)) {
130 + // Re-add uncompiled function
131 + parent.replaceWith(uncompiled)[0].skip();
132 +
133 + // Add test and synthesize new export
134 + return [test, t.exportDefaultDeclaration(original)];
135 + } else if (t.isExportNamedDeclaration(parent)) {
136 + // Re-add uncompiled function
137 + parent.replaceWith(uncompiled)[0].skip();
138 +
139 + // Add and export test
140 + return t.exportNamedDeclaration(test);
141 + }
142 +
143 + // Just add the test, no need for re-export
144 + return test;
145 +}
146 +
147 function addSuffix(id: t.Identifier, suffix: string): t.Identifier {
148 return t.identifier(`${id.name}${suffix}`);
149 }