Add flag for treating function deps as conditional
Sathya Gunasekaran committed
Jan 15, 2024 at 12:44 UTC
121e72a342839383abfa750a8e9e29ce722a9a59
1 file changed
+22
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+22
@@ -322,6 +322,28 @@ const EnvironmentConfigSchema = z.object({
322
* simulate unexpected exceptions e.g. errors from babel functions.
323
*/
324
throwUnknownException__testonly: z.boolean().default(false),
325
+
326
+ /**
327
+ * Enables deps of a function epxression to be treated as conditional. This
328
+ * makes sure we don't load a dep when it's a property (to check if it has
329
+ * changed) and instead check the receiver.
330
+ *
331
+ * This makes sure we don't end up throwing when the reciver is null. Consider
332
+ * this code:
333
+ *
334
+ * ```
335
+ * function getLength() {
336
+ * return props.bar.length;
337
+ * }
338
+ * ```
339
+ *
340
+ * It's only safe to memoize `getLength` against props, not props.bar, as
341
+ * props.bar could be null when this `getLength` function is created.
342
+ *
343
+ * This does cause the memoization to now be coarse grained, which is
344
+ * non-ideal.
345
+ */
346
+ enableTreatFunctionDepsAsConditional: z.boolean().default(false),
347
});
348
349
export type EnvironmentConfig = z.infer<typeof EnvironmentConfigSchema>;