Always clone DEFAULT_HOOKS
Reusing DEFAULT_HOOKS instance is a bit scary in case we mutate it by mistake and this gets reflected across all compiles. This isn't a concern now as we can't change the config during compilation. But this PR keeps us safe in case we change this behavior in the future. The tradeoff is a bit of perf which I think is the right tradeoff here.
Sathya Gunasekaran committed
Nov 8, 2023 at 16:09 UTC
521f0568750dbb3abb851c5bef11458b606863e9
1 file changed
+23
-27
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+23
-27
@@ -312,35 +312,31 @@ export class Environment {
312
config: EnvironmentConfig,
313
contextIdentifiers: Set<t.Identifier>
314
) {
315
- this.#shapes = new Map(DEFAULT_SHAPES);
315
this.config = config;
316
+ this.#shapes = new Map(DEFAULT_SHAPES);
317
+ this.#globals = new Map(DEFAULT_GLOBALS);
318
318
- if (this.config.customHooks.size > 0) {
319
- this.#globals = new Map(DEFAULT_GLOBALS);
320
- for (const [hookName, hook] of this.config.customHooks) {
321
- CompilerError.invariant(!this.#globals.has(hookName), {
322
- reason: `[Globals] Found existing definition in global registry for custom hook ${hookName}`,
323
- description: null,
324
- loc: null,
325
- suggestions: null,
326
- });
327
- this.#globals.set(
328
- hookName,
329
- addHook(this.#shapes, [], {
330
- positionalParams: [],
331
- restParam: hook.effectKind,
332
- returnType: hook.transitiveMixedData
333
- ? { kind: "Object", shapeId: BuiltInMixedReadonlyId }
334
- : { kind: "Poly" },
335
- returnValueKind: hook.valueKind,
336
- calleeEffect: Effect.Read,
337
- hookKind: "Custom",
338
- noAlias: hook.noAlias ?? false,
339
- })
340
- );
341
- }
342
- } else {
343
- this.#globals = DEFAULT_GLOBALS;
319
+ for (const [hookName, hook] of this.config.customHooks) {
320
+ CompilerError.invariant(!this.#globals.has(hookName), {
321
+ reason: `[Globals] Found existing definition in global registry for custom hook ${hookName}`,
322
+ description: null,
323
+ loc: null,
324
+ suggestions: null,
325
+ });
326
+ this.#globals.set(
327
+ hookName,
328
+ addHook(this.#shapes, [], {
329
+ positionalParams: [],
330
+ restParam: hook.effectKind,
331
+ returnType: hook.transitiveMixedData
332
+ ? { kind: "Object", shapeId: BuiltInMixedReadonlyId }
333
+ : { kind: "Poly" },
334
+ returnValueKind: hook.valueKind,
335
+ calleeEffect: Effect.Read,
336
+ hookKind: "Custom",
337
+ noAlias: hook.noAlias ?? false,
338
+ })
339
+ );
340
}
341
342
this.#contextIdentifiers = contextIdentifiers;