From 637c2e68f1d6580dea84dd1b30284e6797bd6db2 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Mon, 6 Mar 2023 19:53:46 +0000 Subject: [PATCH] [be][babel] Move directive checking to separate function --- compiler/forget/src/Babel/BabelPlugin.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/forget/src/Babel/BabelPlugin.ts b/compiler/forget/src/Babel/BabelPlugin.ts index 4d3bd5a15b..89d51fc9e7 100644 --- a/compiler/forget/src/Babel/BabelPlugin.ts +++ b/compiler/forget/src/Babel/BabelPlugin.ts @@ -19,6 +19,15 @@ type BabelPluginPass = { opts: PluginOptions; }; +function hasUseForgetDirective(directives: t.Directive[]): boolean { + for (const directive of directives) { + if (directive.value.value === "use forget") { + return true; + } + } + return false; +} + /** * The React Forget Babel Plugin * @param {*} _babel @@ -33,17 +42,11 @@ export default function ReactForgetBabelPlugin( pass: BabelPluginPass ) { if (pass.opts.enableOnlyOnUseForgetDirective) { - let hasUseForgetDirective = false; - for (const directive of fn.node.body.directives) { - if (directive.value.value === "use forget") { - hasUseForgetDirective = true; - break; - } - } - if (!hasUseForgetDirective) { + if (!hasUseForgetDirective(fn.node.body.directives)) { return; } } + if (fn.scope.getProgramParent() !== fn.scope.parent) { return; }