@samitouri / QOS-React-1 / commits / 2b4a1d86ff

[pipeline] Recognize "use no forget" directive

--- Add "use no forget", not gated behind a feature flag for simplicity ("use no forget" should always tell Forget to not compile a component, right?)

Mofei Zhang committed Aug 17, 2023 at 11:03 UTC 2b4a1d86ffa16863ba6e4ccea33151a3abe589c3
1 file changed +16
compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+16
@@ -45,6 +45,15 @@ function hasAnyUseForgetDirectives(directives: t.Directive[]): boolean {
45 return false;
46 }
47
48 +function hasAnyUseNoForgetDirectives(directives: t.Directive[]): boolean {
49 + for (const directive of directives) {
50 + if (directive.value.value === "use no forget") {
51 + return true;
52 + }
53 + }
54 + return false;
55 +}
56 +
57 /**
58 * Runs the Compiler pipeline and mutates the source AST to include the newly compiled function.
59 * Returns a boolean denoting if the AST was mutated or not.
@@ -305,6 +314,13 @@ function shouldVisitNode(
314 fn: NodePath<t.FunctionDeclaration | t.ArrowFunctionExpression>,
315 pass: CompilerPass
316 ): boolean {
317 + if (fn.node.body.type === "BlockStatement") {
318 + const shouldSkip = hasAnyUseNoForgetDirectives(fn.node.body.directives);
319 + if (shouldSkip) {
320 + return false;
321 + }
322 + }
323 +
324 if (pass.opts.enableOnlyOnReactScript && fn.isFunctionDeclaration()) {
325 return isComponentDeclaration(fn.node);
326 }