From 32b97d380dc3f0d62aa5f8076260ac2528ba044a Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sun, 15 Dec 2024 20:32:26 -0500 Subject: [PATCH 1/2] [compiler] Add option for firing effect functions Config flag for `fire` -- --- .../babel-plugin-react-compiler/src/HIR/Environment.ts | 2 ++ compiler/packages/snap/src/compiler.ts | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts index fa581d8ed8d81..4371ea99569d2 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts @@ -241,6 +241,8 @@ const EnvironmentConfigSchema = z.object({ */ enableOptionalDependencies: z.boolean().default(true), + enableFire: z.boolean().default(false), + /** * Enables inference and auto-insertion of effect dependencies. Takes in an array of * configurable module and import pairs to allow for user-land experimentation. For example, diff --git a/compiler/packages/snap/src/compiler.ts b/compiler/packages/snap/src/compiler.ts index 1cb8fe48b9451..dc6708982a085 100644 --- a/compiler/packages/snap/src/compiler.ts +++ b/compiler/packages/snap/src/compiler.ts @@ -56,6 +56,7 @@ function makePluginOptions( let validatePreserveExistingMemoizationGuarantees = false; let customMacros: null | Array = null; let validateBlocklistedImports = null; + let enableFire = false; let target: CompilerReactTarget = '19'; if (firstLine.indexOf('@compilationMode(annotation)') !== -1) { @@ -127,6 +128,10 @@ function makePluginOptions( validatePreserveExistingMemoizationGuarantees = true; } + if (firstLine.includes('@enableFire')) { + enableFire = true; + } + const hookPatternMatch = /@hookPattern:"([^"]+)"/.exec(firstLine); if ( hookPatternMatch && @@ -205,6 +210,7 @@ function makePluginOptions( hookPattern, validatePreserveExistingMemoizationGuarantees, validateBlocklistedImports, + enableFire, }, compilationMode, logger, From e8faeb423cda4af6c0123bd1fee142ef1daa32fa Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Sun, 15 Dec 2024 20:32:29 -0500 Subject: [PATCH 2/2] [compiler] Add fire to known React APIs Makes `fire` a known export for type-based analysis -- --- .../src/HIR/Globals.ts | 16 ++++++++++++++++ .../src/HIR/ObjectShape.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts index 2525b87bd86bc..2249e33491fa8 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts @@ -9,6 +9,7 @@ import {Effect, ValueKind, ValueReason} from './HIR'; import { BUILTIN_SHAPES, BuiltInArrayId, + BuiltInFireId, BuiltInMixedReadonlyId, BuiltInUseActionStateId, BuiltInUseContextHookId, @@ -468,6 +469,21 @@ const REACT_APIS: Array<[string, BuiltInType]> = [ BuiltInUseOperatorId, ), ], + [ + 'fire', + addFunction( + DEFAULT_SHAPES, + [], + { + positionalParams: [], + restParam: null, + returnType: {kind: 'Primitive'}, + calleeEffect: Effect.Read, + returnValueKind: ValueKind.Frozen, + }, + BuiltInFireId, + ), + ], ]; TYPED_GLOBALS.push( diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts index 14f809f2c4082..4482d17890196 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts @@ -213,6 +213,7 @@ export const BuiltInDispatchId = 'BuiltInDispatch'; export const BuiltInUseContextHookId = 'BuiltInUseContextHook'; export const BuiltInUseTransitionId = 'BuiltInUseTransition'; export const BuiltInStartTransitionId = 'BuiltInStartTransition'; +export const BuiltInFireId = 'BuiltInFire'; // ShapeRegistry with default definitions for built-ins. export const BUILTIN_SHAPES: ShapeRegistry = new Map();