From 7caa18c4b937c0c24a3199ee08ffc61a520bb247 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 23 Jun 2023 14:36:55 +0530 Subject: [PATCH] [playground] Parse pragma in playground --- .../playground/components/Editor/index.tsx | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/compiler/forget/apps/playground/components/Editor/index.tsx b/compiler/forget/apps/playground/components/Editor/index.tsx index b1e3bfc2b0..18943b10a2 100644 --- a/compiler/forget/apps/playground/components/Editor/index.tsx +++ b/compiler/forget/apps/playground/components/Editor/index.tsx @@ -119,14 +119,55 @@ function compile(source: string): CompilerOutput { } }; try { + // Extract the first line to quickly check for custom test directives + const firstLine = source.substring(0, source.indexOf("\n")); + + let memoizeJsxElements = true; + let enableAssumeHooksFollowRulesOfReact = true; + let enableTreatHooksAsFunctions = true; + let disableAllMemoization = false; + let validateRefAccessDuringRender = true; + let enableEmitFreeze = null; + let enableOptimizeFunctionExpressions = false; + if (firstLine.includes("@memoizeJsxElements false")) { + memoizeJsxElements = false; + } + if (firstLine.includes("@enableAssumeHooksFollowRulesOfReact true")) { + enableAssumeHooksFollowRulesOfReact = true; + } + if (firstLine.includes("@enableTreatHooksAsFunctions false")) { + enableTreatHooksAsFunctions = false; + } + if (firstLine.includes("@disableAllMemoization true")) { + disableAllMemoization = true; + } + if (firstLine.includes("@validateRefAccessDuringRender false")) { + validateRefAccessDuringRender = false; + } + if (firstLine.includes("@enableOptimizeFunctionExpressions")) { + enableOptimizeFunctionExpressions = true; + } + if (firstLine.includes("@enableEmitFreeze")) { + enableEmitFreeze = { + source: "react-forget-runtime", + importSpecifierName: "makeReadOnly", + }; + } + for (const fn of parseFunctions(source)) { for (const result of run(fn, { customHooks: new Map([...COMMON_HOOKS]), - enableAssumeHooksFollowRulesOfReact: true, + enableAssumeHooksFollowRulesOfReact, enableFunctionCallSignatureOptimizations: true, + disableAllMemoization, + enableTreatHooksAsFunctions, inlineUseMemo: true, - memoizeJsxElements: true, + memoizeJsxElements, validateHooksUsage: true, + validateRefAccessDuringRender, + validateFrozenLambdas: true, + enableEmitFreeze, + enableOptimizeFunctionExpressions, assertValidMutableRanges: true, })) { const fnName = fn.node.id?.name ?? null;