babel-plugin-react-compiler 0.0.0-experimental-afa1d5a-20250319 → 0.0.0-experimental-3452ff1-20250321

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,65 @@ import * as t from '@babel/types';
4
4
  import { z } from 'zod';
5
5
  import { Scope, NodePath } from '@babel/traverse';
6
6
 
7
+ interface Result<T, E> {
8
+ map<U>(fn: (val: T) => U): Result<U, E>;
9
+ mapErr<F>(fn: (val: E) => F): Result<T, F>;
10
+ mapOr<U>(fallback: U, fn: (val: T) => U): U;
11
+ mapOrElse<U>(fallback: () => U, fn: (val: T) => U): U;
12
+ andThen<U>(fn: (val: T) => Result<U, E>): Result<U, E>;
13
+ and<U>(res: Result<U, E>): Result<U, E>;
14
+ or(res: Result<T, E>): Result<T, E>;
15
+ orElse<F>(fn: (val: E) => Result<T, F>): Result<T, F>;
16
+ isOk(): this is OkImpl<T>;
17
+ isErr(): this is ErrImpl<E>;
18
+ expect(msg: string): T;
19
+ expectErr(msg: string): E;
20
+ unwrap(): T;
21
+ unwrapOr(fallback: T): T;
22
+ unwrapOrElse(fallback: (val: E) => T): T;
23
+ unwrapErr(): E;
24
+ }
25
+ declare class OkImpl<T> implements Result<T, never> {
26
+ private val;
27
+ constructor(val: T);
28
+ map<U>(fn: (val: T) => U): Result<U, never>;
29
+ mapErr<F>(_fn: (val: never) => F): Result<T, F>;
30
+ mapOr<U>(_fallback: U, fn: (val: T) => U): U;
31
+ mapOrElse<U>(_fallback: () => U, fn: (val: T) => U): U;
32
+ andThen<U>(fn: (val: T) => Result<U, never>): Result<U, never>;
33
+ and<U>(res: Result<U, never>): Result<U, never>;
34
+ or(_res: Result<T, never>): Result<T, never>;
35
+ orElse<F>(_fn: (val: never) => Result<T, F>): Result<T, F>;
36
+ isOk(): this is OkImpl<T>;
37
+ isErr(): this is ErrImpl<never>;
38
+ expect(_msg: string): T;
39
+ expectErr(msg: string): never;
40
+ unwrap(): T;
41
+ unwrapOr(_fallback: T): T;
42
+ unwrapOrElse(_fallback: (val: never) => T): T;
43
+ unwrapErr(): never;
44
+ }
45
+ declare class ErrImpl<E> implements Result<never, E> {
46
+ private val;
47
+ constructor(val: E);
48
+ map<U>(_fn: (val: never) => U): Result<U, E>;
49
+ mapErr<F>(fn: (val: E) => F): Result<never, F>;
50
+ mapOr<U>(fallback: U, _fn: (val: never) => U): U;
51
+ mapOrElse<U>(fallback: () => U, _fn: (val: never) => U): U;
52
+ andThen<U>(_fn: (val: never) => Result<U, E>): Result<U, E>;
53
+ and<U>(_res: Result<U, E>): Result<U, E>;
54
+ or(res: Result<never, E>): Result<never, E>;
55
+ orElse<F>(fn: (val: E) => ErrImpl<F>): Result<never, F>;
56
+ isOk(): this is OkImpl<never>;
57
+ isErr(): this is ErrImpl<E>;
58
+ expect(msg: string): never;
59
+ expectErr(_msg: string): E;
60
+ unwrap(): never;
61
+ unwrapOr<T>(fallback: T): T;
62
+ unwrapOrElse<T>(fallback: (val: E) => T): T;
63
+ unwrapErr(): E;
64
+ }
65
+
7
66
  type BuiltInType = PrimitiveType | FunctionType | ObjectType;
8
67
  type Type = BuiltInType | PhiType | TypeVar | PolyType | PropType | ObjectMethod$1;
9
68
  type PrimitiveType = {
@@ -177,6 +236,7 @@ declare const EnvironmentConfigSchema: z.ZodObject<{
177
236
  validateNoSetStateInRender: z.ZodDefault<z.ZodBoolean>;
178
237
  validateNoSetStateInPassiveEffects: z.ZodDefault<z.ZodBoolean>;
179
238
  validateNoJSXInTryStatements: z.ZodDefault<z.ZodBoolean>;
239
+ validateStaticComponents: z.ZodDefault<z.ZodBoolean>;
180
240
  validateMemoizedEffectDependencies: z.ZodDefault<z.ZodBoolean>;
181
241
  validateNoCapitalizedCalls: z.ZodDefault<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
182
242
  validateBlocklistedImports: z.ZodDefault<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
@@ -336,6 +396,7 @@ declare const EnvironmentConfigSchema: z.ZodObject<{
336
396
  validateNoSetStateInRender: boolean;
337
397
  validateNoSetStateInPassiveEffects: boolean;
338
398
  validateNoJSXInTryStatements: boolean;
399
+ validateStaticComponents: boolean;
339
400
  validateMemoizedEffectDependencies: boolean;
340
401
  validateNoCapitalizedCalls: string[] | null;
341
402
  validateBlocklistedImports: string[] | null;
@@ -419,6 +480,7 @@ declare const EnvironmentConfigSchema: z.ZodObject<{
419
480
  validateNoSetStateInRender?: boolean | undefined;
420
481
  validateNoSetStateInPassiveEffects?: boolean | undefined;
421
482
  validateNoJSXInTryStatements?: boolean | undefined;
483
+ validateStaticComponents?: boolean | undefined;
422
484
  validateMemoizedEffectDependencies?: boolean | undefined;
423
485
  validateNoCapitalizedCalls?: string[] | null | undefined;
424
486
  validateBlocklistedImports?: string[] | null | undefined;
@@ -487,6 +549,7 @@ declare class Environment {
487
549
  get nextIdentifierId(): IdentifierId;
488
550
  get nextBlockId(): BlockId;
489
551
  get nextScopeId(): ScopeId;
552
+ logErrors(errors: Result<void, CompilerError>): void;
490
553
  isContextIdentifier(node: t.Identifier): boolean;
491
554
  isHoistedIdentifier(node: t.Identifier): boolean;
492
555
  generateGloballyUniqueIdentifierName(name: string | null): ValidatedIdentifier;
@@ -1423,6 +1486,7 @@ declare class CompilerError extends Error {
1423
1486
  push(options: CompilerErrorDetailOptions): CompilerErrorDetail;
1424
1487
  pushErrorDetail(detail: CompilerErrorDetail): CompilerErrorDetail;
1425
1488
  hasErrors(): boolean;
1489
+ asResult(): Result<void, CompilerError>;
1426
1490
  isCritical(): boolean;
1427
1491
  }
1428
1492
 
package/dist/index.js CHANGED
@@ -110448,6 +110448,126 @@ var import_invariant4 = __toESM(require_invariant());
110448
110448
  // src/Entrypoint/Gating.ts
110449
110449
  var t = __toESM(require("@babel/types"));
110450
110450
 
110451
+ // src/Utils/Result.ts
110452
+ function Ok(val) {
110453
+ return new OkImpl(val);
110454
+ }
110455
+ var OkImpl = class _OkImpl {
110456
+ constructor(val) {
110457
+ this.val = val;
110458
+ }
110459
+ map(fn) {
110460
+ return new _OkImpl(fn(this.val));
110461
+ }
110462
+ mapErr(_fn) {
110463
+ return this;
110464
+ }
110465
+ mapOr(_fallback, fn) {
110466
+ return fn(this.val);
110467
+ }
110468
+ mapOrElse(_fallback, fn) {
110469
+ return fn(this.val);
110470
+ }
110471
+ andThen(fn) {
110472
+ return fn(this.val);
110473
+ }
110474
+ and(res) {
110475
+ return res;
110476
+ }
110477
+ or(_res) {
110478
+ return this;
110479
+ }
110480
+ orElse(_fn) {
110481
+ return this;
110482
+ }
110483
+ isOk() {
110484
+ return true;
110485
+ }
110486
+ isErr() {
110487
+ return false;
110488
+ }
110489
+ expect(_msg) {
110490
+ return this.val;
110491
+ }
110492
+ expectErr(msg) {
110493
+ throw new Error(`${msg}: ${this.val}`);
110494
+ }
110495
+ unwrap() {
110496
+ return this.val;
110497
+ }
110498
+ unwrapOr(_fallback) {
110499
+ return this.val;
110500
+ }
110501
+ unwrapOrElse(_fallback) {
110502
+ return this.val;
110503
+ }
110504
+ unwrapErr() {
110505
+ if (this.val instanceof Error) {
110506
+ throw this.val;
110507
+ }
110508
+ throw new Error(`Can't unwrap \`Ok\` to \`Err\`: ${this.val}`);
110509
+ }
110510
+ };
110511
+ function Err(val) {
110512
+ return new ErrImpl(val);
110513
+ }
110514
+ var ErrImpl = class _ErrImpl {
110515
+ constructor(val) {
110516
+ this.val = val;
110517
+ }
110518
+ map(_fn) {
110519
+ return this;
110520
+ }
110521
+ mapErr(fn) {
110522
+ return new _ErrImpl(fn(this.val));
110523
+ }
110524
+ mapOr(fallback, _fn) {
110525
+ return fallback;
110526
+ }
110527
+ mapOrElse(fallback, _fn) {
110528
+ return fallback();
110529
+ }
110530
+ andThen(_fn) {
110531
+ return this;
110532
+ }
110533
+ and(_res) {
110534
+ return this;
110535
+ }
110536
+ or(res) {
110537
+ return res;
110538
+ }
110539
+ orElse(fn) {
110540
+ return fn(this.val);
110541
+ }
110542
+ isOk() {
110543
+ return false;
110544
+ }
110545
+ isErr() {
110546
+ return true;
110547
+ }
110548
+ expect(msg) {
110549
+ throw new Error(`${msg}: ${this.val}`);
110550
+ }
110551
+ expectErr(_msg) {
110552
+ return this.val;
110553
+ }
110554
+ unwrap() {
110555
+ if (this.val instanceof Error) {
110556
+ throw this.val;
110557
+ }
110558
+ throw new Error(`Can't unwrap \`Err\` to \`Ok\`: ${this.val}`);
110559
+ }
110560
+ unwrapOr(fallback) {
110561
+ return fallback;
110562
+ }
110563
+ unwrapOrElse(fallback) {
110564
+ return fallback(this.val);
110565
+ }
110566
+ unwrapErr() {
110567
+ return this.val;
110568
+ }
110569
+ };
110570
+
110451
110571
  // src/Utils/utils.ts
110452
110572
  function assertExhaustive(_, errorMsg) {
110453
110573
  throw new Error(errorMsg);
@@ -110682,6 +110802,9 @@ var CompilerError = class _CompilerError extends Error {
110682
110802
  hasErrors() {
110683
110803
  return this.details.length > 0;
110684
110804
  }
110805
+ asResult() {
110806
+ return this.hasErrors() ? Err(this) : Ok(void 0);
110807
+ }
110685
110808
  /*
110686
110809
  * An error is critical if it means the compiler has entered into a broken state and cannot
110687
110810
  * continue safely. Other expected errors such as Todos mean that we can skip over that component
@@ -117244,126 +117367,6 @@ function validateMutableRange(mutableRange) {
117244
117367
  // src/HIR/BuildHIR.ts
117245
117368
  var import_invariant2 = __toESM(require_invariant());
117246
117369
 
117247
- // src/Utils/Result.ts
117248
- function Ok(val) {
117249
- return new OkImpl(val);
117250
- }
117251
- var OkImpl = class _OkImpl {
117252
- constructor(val) {
117253
- this.val = val;
117254
- }
117255
- map(fn) {
117256
- return new _OkImpl(fn(this.val));
117257
- }
117258
- mapErr(_fn) {
117259
- return this;
117260
- }
117261
- mapOr(_fallback, fn) {
117262
- return fn(this.val);
117263
- }
117264
- mapOrElse(_fallback, fn) {
117265
- return fn(this.val);
117266
- }
117267
- andThen(fn) {
117268
- return fn(this.val);
117269
- }
117270
- and(res) {
117271
- return res;
117272
- }
117273
- or(_res) {
117274
- return this;
117275
- }
117276
- orElse(_fn) {
117277
- return this;
117278
- }
117279
- isOk() {
117280
- return true;
117281
- }
117282
- isErr() {
117283
- return false;
117284
- }
117285
- expect(_msg) {
117286
- return this.val;
117287
- }
117288
- expectErr(msg) {
117289
- throw new Error(`${msg}: ${this.val}`);
117290
- }
117291
- unwrap() {
117292
- return this.val;
117293
- }
117294
- unwrapOr(_fallback) {
117295
- return this.val;
117296
- }
117297
- unwrapOrElse(_fallback) {
117298
- return this.val;
117299
- }
117300
- unwrapErr() {
117301
- if (this.val instanceof Error) {
117302
- throw this.val;
117303
- }
117304
- throw new Error(`Can't unwrap \`Ok\` to \`Err\`: ${this.val}`);
117305
- }
117306
- };
117307
- function Err(val) {
117308
- return new ErrImpl(val);
117309
- }
117310
- var ErrImpl = class _ErrImpl {
117311
- constructor(val) {
117312
- this.val = val;
117313
- }
117314
- map(_fn) {
117315
- return this;
117316
- }
117317
- mapErr(fn) {
117318
- return new _ErrImpl(fn(this.val));
117319
- }
117320
- mapOr(fallback, _fn) {
117321
- return fallback;
117322
- }
117323
- mapOrElse(fallback, _fn) {
117324
- return fallback();
117325
- }
117326
- andThen(_fn) {
117327
- return this;
117328
- }
117329
- and(_res) {
117330
- return this;
117331
- }
117332
- or(res) {
117333
- return res;
117334
- }
117335
- orElse(fn) {
117336
- return fn(this.val);
117337
- }
117338
- isOk() {
117339
- return false;
117340
- }
117341
- isErr() {
117342
- return true;
117343
- }
117344
- expect(msg) {
117345
- throw new Error(`${msg}: ${this.val}`);
117346
- }
117347
- expectErr(_msg) {
117348
- return this.val;
117349
- }
117350
- unwrap() {
117351
- if (this.val instanceof Error) {
117352
- throw this.val;
117353
- }
117354
- throw new Error(`Can't unwrap \`Err\` to \`Ok\`: ${this.val}`);
117355
- }
117356
- unwrapOr(fallback) {
117357
- return fallback;
117358
- }
117359
- unwrapOrElse(fallback) {
117360
- return fallback(this.val);
117361
- }
117362
- unwrapErr() {
117363
- return this.val;
117364
- }
117365
- };
117366
-
117367
117370
  // src/HIR/HIRBuilder.ts
117368
117371
  function newBlock(id, kind) {
117369
117372
  return { id, kind, instructions: [] };
@@ -123583,6 +123586,10 @@ var EnvironmentConfigSchema = z.object({
123583
123586
  * instead.
123584
123587
  */
123585
123588
  validateNoJSXInTryStatements: z.boolean().default(false),
123589
+ /**
123590
+ * Validates against dynamically creating components during render.
123591
+ */
123592
+ validateStaticComponents: z.boolean().default(false),
123586
123593
  /**
123587
123594
  * Validates that the dependencies of all effect hooks are memoized. This helps ensure
123588
123595
  * that Forget does not introduce infinite renders caused by a dependency changing,
@@ -124073,6 +124080,18 @@ var Environment = class {
124073
124080
  get nextScopeId() {
124074
124081
  return makeScopeId(__privateWrapper(this, _nextScope)._++);
124075
124082
  }
124083
+ logErrors(errors) {
124084
+ if (errors.isOk() || this.logger == null) {
124085
+ return;
124086
+ }
124087
+ for (const error of errors.unwrapErr().details) {
124088
+ this.logger.logEvent(this.filename, {
124089
+ kind: "CompileError",
124090
+ detail: error,
124091
+ fnLoc: null
124092
+ });
124093
+ }
124094
+ }
124076
124095
  isContextIdentifier(node) {
124077
124096
  return __privateGet(this, _contextIdentifiers).has(node);
124078
124097
  }
@@ -129176,7 +129195,7 @@ function codegenFunction(fn, {
129176
129195
  }
129177
129196
  const compiled = compileResult.unwrap();
129178
129197
  const hookGuard = fn.env.config.enableEmitHookGuards;
129179
- if (hookGuard != null) {
129198
+ if (hookGuard != null && fn.env.isInferredMemoEnabled) {
129180
129199
  compiled.body = t4.blockStatement([
129181
129200
  createHookGuard(
129182
129201
  hookGuard,
@@ -129265,7 +129284,7 @@ function codegenFunction(fn, {
129265
129284
  compiled.body.body.unshift(...preface);
129266
129285
  }
129267
129286
  const emitInstrumentForget = fn.env.config.enableEmitInstrumentForget;
129268
- if (emitInstrumentForget != null && fn.id != null) {
129287
+ if (emitInstrumentForget != null && fn.id != null && fn.env.isInferredMemoEnabled) {
129269
129288
  let gating;
129270
129289
  if (emitInstrumentForget.gating != null && emitInstrumentForget.globalGating != null) {
129271
129290
  gating = t4.logicalExpression(
@@ -129500,7 +129519,7 @@ function codegenBlockNoReset(cx, block) {
129500
129519
  return t4.blockStatement(statements);
129501
129520
  }
129502
129521
  function wrapCacheDep(cx, value) {
129503
- if (cx.env.config.enableEmitFreeze != null) {
129522
+ if (cx.env.config.enableEmitFreeze != null && cx.env.isInferredMemoEnabled) {
129504
129523
  return t4.conditionalExpression(
129505
129524
  t4.identifier("__DEV__"),
129506
129525
  t4.callExpression(
@@ -130367,13 +130386,13 @@ function createHookGuard(guard, stmts, before, after) {
130367
130386
  t4.blockStatement([createHookGuardImpl(after)])
130368
130387
  );
130369
130388
  }
130370
- function createCallExpression(config, callee, args, loc, isHook2) {
130389
+ function createCallExpression(env, callee, args, loc, isHook2) {
130371
130390
  const callExpr = t4.callExpression(callee, args);
130372
130391
  if (loc != null && loc != GeneratedSource) {
130373
130392
  callExpr.loc = loc;
130374
130393
  }
130375
- const hookGuard = config.enableEmitHookGuards;
130376
- if (hookGuard != null && isHook2) {
130394
+ const hookGuard = env.config.enableEmitHookGuards;
130395
+ if (hookGuard != null && isHook2 && env.isInferredMemoEnabled) {
130377
130396
  const iife = t4.functionExpression(
130378
130397
  null,
130379
130398
  [],
@@ -130491,7 +130510,7 @@ function codegenInstructionValue(cx, instrValue) {
130491
130510
  const callee = codegenPlaceToExpression(cx, instrValue.callee);
130492
130511
  const args = instrValue.args.map((arg) => codegenArgument(cx, arg));
130493
130512
  value = createCallExpression(
130494
- cx.env.config,
130513
+ cx.env,
130495
130514
  callee,
130496
130515
  args,
130497
130516
  instrValue.loc,
@@ -130574,7 +130593,7 @@ function codegenInstructionValue(cx, instrValue) {
130574
130593
  );
130575
130594
  const args = instrValue.args.map((arg) => codegenArgument(cx, arg));
130576
130595
  value = createCallExpression(
130577
- cx.env.config,
130596
+ cx.env,
130578
130597
  memberExpr,
130579
130598
  args,
130580
130599
  instrValue.loc,
@@ -138033,9 +138052,7 @@ function validateHooksUsage(fn) {
138033
138052
  for (const [, error] of errorsByPlace) {
138034
138053
  errors.push(error);
138035
138054
  }
138036
- if (errors.hasErrors()) {
138037
- throw errors;
138038
- }
138055
+ return errors.asResult();
138039
138056
  }
138040
138057
  function visitFunctionExpression(errors, fn) {
138041
138058
  for (const [, block] of fn.body.blocks) {
@@ -138072,9 +138089,7 @@ function visitFunctionExpression(errors, fn) {
138072
138089
  function validateMemoizedEffectDependencies(fn) {
138073
138090
  const errors = new CompilerError();
138074
138091
  visitReactiveFunction(fn, new Visitor10(), errors);
138075
- if (errors.hasErrors()) {
138076
- throw errors;
138077
- }
138092
+ return errors.asResult();
138078
138093
  }
138079
138094
  var Visitor10 = class extends ReactiveFunctionVisitor {
138080
138095
  constructor() {
@@ -138136,6 +138151,7 @@ function validateNoCapitalizedCalls(fn) {
138136
138151
  const isAllowed = (name) => {
138137
138152
  return ALLOW_LIST.has(name) || hookPattern != null && hookPattern.test(name);
138138
138153
  };
138154
+ const errors = new CompilerError();
138139
138155
  const capitalLoadGlobals = /* @__PURE__ */ new Map();
138140
138156
  const capitalizedProperties = /* @__PURE__ */ new Map();
138141
138157
  const reason = "Capitalized functions are reserved for components, which must be invoked with JSX. If this is a component, render it with JSX. Otherwise, ensure that it has no hook calls and rename it to begin with a lowercase letter. Alternatively, if you know for a fact that this function is not a component, you can allowlist it via the compiler config";
@@ -138172,7 +138188,8 @@ function validateNoCapitalizedCalls(fn) {
138172
138188
  const propertyIdentifier = value.property.identifier.id;
138173
138189
  const propertyName = capitalizedProperties.get(propertyIdentifier);
138174
138190
  if (propertyName != null) {
138175
- CompilerError.throwInvalidReact({
138191
+ errors.push({
138192
+ severity: "InvalidReact" /* InvalidReact */,
138176
138193
  reason,
138177
138194
  description: `${propertyName} may be a component.`,
138178
138195
  loc: value.loc,
@@ -138184,6 +138201,7 @@ function validateNoCapitalizedCalls(fn) {
138184
138201
  }
138185
138202
  }
138186
138203
  }
138204
+ return errors.asResult();
138187
138205
  }
138188
138206
 
138189
138207
  // src/Validation/ValidateNoRefAccesInRender.ts
@@ -138225,7 +138243,7 @@ var Env = class extends Map {
138225
138243
  _changed = new WeakMap();
138226
138244
  function validateNoRefAccessInRender(fn) {
138227
138245
  const env = new Env();
138228
- validateNoRefAccessInRenderImpl(fn, env).unwrap();
138246
+ return validateNoRefAccessInRenderImpl(fn, env).map((_) => void 0);
138229
138247
  }
138230
138248
  function refTypeOfType(place) {
138231
138249
  if (isRefValueType(place.identifier)) {
@@ -138687,7 +138705,7 @@ function validateNoDirectRefValueAccess(errors, operand, env) {
138687
138705
  // src/Validation/ValidateNoSetStateInRender.ts
138688
138706
  function validateNoSetStateInRender(fn) {
138689
138707
  const unconditionalSetStateFunctions = /* @__PURE__ */ new Set();
138690
- validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions).unwrap();
138708
+ return validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions);
138691
138709
  }
138692
138710
  function validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions) {
138693
138711
  const unconditionalBlocks = computeUnconditionalBlocks(fn);
@@ -138772,11 +138790,7 @@ function validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions) {
138772
138790
  }
138773
138791
  }
138774
138792
  }
138775
- if (errors.hasErrors()) {
138776
- return Err(errors);
138777
- } else {
138778
- return Ok(void 0);
138779
- }
138793
+ return errors.asResult();
138780
138794
  }
138781
138795
 
138782
138796
  // src/Validation/ValidatePreservedManualMemoization.ts
@@ -138786,9 +138800,7 @@ function validatePreservedManualMemoization(fn) {
138786
138800
  manualMemoState: null
138787
138801
  };
138788
138802
  visitReactiveFunction(fn, new Visitor11(), state);
138789
- if (state.errors.hasErrors()) {
138790
- throw state.errors;
138791
- }
138803
+ return state.errors.asResult();
138792
138804
  }
138793
138805
  var DEBUG4 = false;
138794
138806
  function prettyPrintScopeDependency(val) {
@@ -139108,6 +139120,7 @@ function isUnmemoized2(operand, scopes) {
139108
139120
 
139109
139121
  // src/Validation/ValidateUseMemo.ts
139110
139122
  function validateUseMemo(fn) {
139123
+ const errors = new CompilerError();
139111
139124
  const useMemos = /* @__PURE__ */ new Set();
139112
139125
  const react = /* @__PURE__ */ new Set();
139113
139126
  const functions = /* @__PURE__ */ new Map();
@@ -139150,7 +139163,8 @@ function validateUseMemo(fn) {
139150
139163
  continue;
139151
139164
  }
139152
139165
  if (body.loweredFunc.func.params.length > 0) {
139153
- CompilerError.throwInvalidReact({
139166
+ errors.push({
139167
+ severity: "InvalidReact" /* InvalidReact */,
139154
139168
  reason: "useMemo callbacks may not accept any arguments",
139155
139169
  description: null,
139156
139170
  loc: body.loc,
@@ -139158,7 +139172,8 @@ function validateUseMemo(fn) {
139158
139172
  });
139159
139173
  }
139160
139174
  if (body.loweredFunc.func.async || body.loweredFunc.func.generator) {
139161
- CompilerError.throwInvalidReact({
139175
+ errors.push({
139176
+ severity: "InvalidReact" /* InvalidReact */,
139162
139177
  reason: "useMemo callbacks may not be async or generator functions",
139163
139178
  description: null,
139164
139179
  loc: body.loc,
@@ -139170,6 +139185,7 @@ function validateUseMemo(fn) {
139170
139185
  }
139171
139186
  }
139172
139187
  }
139188
+ return errors.asResult();
139173
139189
  }
139174
139190
 
139175
139191
  // src/Validation/ValidateLocalsNotReassignedAfterRender.ts
@@ -139676,9 +139692,7 @@ function validateNoSetStateInPassiveEffects(fn) {
139676
139692
  }
139677
139693
  }
139678
139694
  }
139679
- if (errors.hasErrors()) {
139680
- throw errors;
139681
- }
139695
+ return errors.asResult();
139682
139696
  }
139683
139697
  function getSetStateCall(fn, setStateFunctions) {
139684
139698
  for (const [, block] of fn.body.blocks) {
@@ -139744,9 +139758,7 @@ function validateNoJSXInTryStatement(fn) {
139744
139758
  activeTryBlocks.push(block.terminal.handler);
139745
139759
  }
139746
139760
  }
139747
- if (errors.hasErrors()) {
139748
- throw errors;
139749
- }
139761
+ return errors.asResult();
139750
139762
  }
139751
139763
 
139752
139764
  // src/HIR/CollectHoistablePropertyLoads.ts
@@ -141951,7 +141963,7 @@ function rewriteInstructions(rewriteInstrs, instructions) {
141951
141963
  return instructions;
141952
141964
  }
141953
141965
 
141954
- // src/Validation/ValiateNoImpureFunctionsInRender.ts
141966
+ // src/Validation/ValidateNoImpureFunctionsInRender.ts
141955
141967
  function validateNoImpureFunctionsInRender(fn) {
141956
141968
  const errors = new CompilerError();
141957
141969
  for (const [, block] of fn.body.blocks) {
@@ -141975,9 +141987,75 @@ function validateNoImpureFunctionsInRender(fn) {
141975
141987
  }
141976
141988
  }
141977
141989
  }
141978
- if (errors.hasErrors()) {
141979
- throw errors;
141990
+ return errors.asResult();
141991
+ }
141992
+
141993
+ // src/Validation/ValidateStaticComponents.ts
141994
+ function validateStaticComponents(fn) {
141995
+ const error = new CompilerError();
141996
+ const knownDynamicComponents = /* @__PURE__ */ new Map();
141997
+ for (const block of fn.body.blocks.values()) {
141998
+ phis: for (const phi of block.phis) {
141999
+ for (const operand of phi.operands.values()) {
142000
+ const loc = knownDynamicComponents.get(operand.identifier.id);
142001
+ if (loc != null) {
142002
+ knownDynamicComponents.set(phi.place.identifier.id, loc);
142003
+ continue phis;
142004
+ }
142005
+ }
142006
+ }
142007
+ for (const instr of block.instructions) {
142008
+ const { lvalue, value } = instr;
142009
+ switch (value.kind) {
142010
+ case "FunctionExpression":
142011
+ case "NewExpression":
142012
+ case "MethodCall":
142013
+ case "CallExpression": {
142014
+ knownDynamicComponents.set(lvalue.identifier.id, value.loc);
142015
+ break;
142016
+ }
142017
+ case "LoadLocal": {
142018
+ const loc = knownDynamicComponents.get(value.place.identifier.id);
142019
+ if (loc != null) {
142020
+ knownDynamicComponents.set(lvalue.identifier.id, loc);
142021
+ }
142022
+ break;
142023
+ }
142024
+ case "StoreLocal": {
142025
+ const loc = knownDynamicComponents.get(value.value.identifier.id);
142026
+ if (loc != null) {
142027
+ knownDynamicComponents.set(lvalue.identifier.id, loc);
142028
+ knownDynamicComponents.set(value.lvalue.place.identifier.id, loc);
142029
+ }
142030
+ break;
142031
+ }
142032
+ case "JsxExpression": {
142033
+ if (value.tag.kind === "Identifier") {
142034
+ const location = knownDynamicComponents.get(
142035
+ value.tag.identifier.id
142036
+ );
142037
+ if (location != null) {
142038
+ error.push({
142039
+ reason: `Components created during render will reset their state each time they are created. Declare components outside of render. `,
142040
+ severity: "InvalidReact" /* InvalidReact */,
142041
+ loc: value.tag.loc,
142042
+ description: null,
142043
+ suggestions: null
142044
+ });
142045
+ error.push({
142046
+ reason: `The component may be created during render`,
142047
+ severity: "InvalidReact" /* InvalidReact */,
142048
+ loc: location,
142049
+ description: null,
142050
+ suggestions: null
142051
+ });
142052
+ }
142053
+ }
142054
+ }
142055
+ }
142056
+ }
141980
142057
  }
142058
+ return error.asResult();
141981
142059
  }
141982
142060
 
141983
142061
  // src/Entrypoint/Pipeline.ts
@@ -142012,7 +142090,7 @@ function runWithEnvironment(func, env) {
142012
142090
  pruneMaybeThrows(hir);
142013
142091
  log2({ kind: "hir", name: "PruneMaybeThrows", value: hir });
142014
142092
  validateContextVariableLValues(hir);
142015
- validateUseMemo(hir);
142093
+ validateUseMemo(hir).unwrap();
142016
142094
  if (env.isInferredMemoEnabled && !env.config.enablePreserveExistingManualUseMemo && !env.config.disableMemoizationForDebugging && !env.config.enableChangeDetectionForDebugging) {
142017
142095
  dropManualMemoization(hir);
142018
142096
  log2({ kind: "hir", name: "DropManualMemoization", value: hir });
@@ -142038,10 +142116,10 @@ function runWithEnvironment(func, env) {
142038
142116
  log2({ kind: "hir", name: "InferTypes", value: hir });
142039
142117
  if (env.isInferredMemoEnabled) {
142040
142118
  if (env.config.validateHooksUsage) {
142041
- validateHooksUsage(hir);
142119
+ validateHooksUsage(hir).unwrap();
142042
142120
  }
142043
142121
  if (env.config.validateNoCapitalizedCalls) {
142044
- validateNoCapitalizedCalls(hir);
142122
+ validateNoCapitalizedCalls(hir).unwrap();
142045
142123
  }
142046
142124
  }
142047
142125
  if (env.config.enableFire) {
@@ -142078,19 +142156,19 @@ function runWithEnvironment(func, env) {
142078
142156
  assertValidMutableRanges(hir);
142079
142157
  }
142080
142158
  if (env.config.validateRefAccessDuringRender) {
142081
- validateNoRefAccessInRender(hir);
142159
+ validateNoRefAccessInRender(hir).unwrap();
142082
142160
  }
142083
142161
  if (env.config.validateNoSetStateInRender) {
142084
- validateNoSetStateInRender(hir);
142162
+ validateNoSetStateInRender(hir).unwrap();
142085
142163
  }
142086
142164
  if (env.config.validateNoSetStateInPassiveEffects) {
142087
- validateNoSetStateInPassiveEffects(hir);
142165
+ env.logErrors(validateNoSetStateInPassiveEffects(hir));
142088
142166
  }
142089
142167
  if (env.config.validateNoJSXInTryStatements) {
142090
- validateNoJSXInTryStatement(hir);
142168
+ env.logErrors(validateNoJSXInTryStatement(hir));
142091
142169
  }
142092
142170
  if (env.config.validateNoImpureFunctionsInRender) {
142093
- validateNoImpureFunctionsInRender(hir);
142171
+ validateNoImpureFunctionsInRender(hir).unwrap();
142094
142172
  }
142095
142173
  }
142096
142174
  inferReactivePlaces(hir);
@@ -142108,6 +142186,9 @@ function runWithEnvironment(func, env) {
142108
142186
  value: hir
142109
142187
  });
142110
142188
  if (env.isInferredMemoEnabled) {
142189
+ if (env.config.validateStaticComponents) {
142190
+ env.logErrors(validateStaticComponents(hir));
142191
+ }
142111
142192
  inferReactiveScopeVariables(hir);
142112
142193
  log2({ kind: "hir", name: "InferReactiveScopeVariables", value: hir });
142113
142194
  }
@@ -142288,10 +142369,10 @@ function runWithEnvironment(func, env) {
142288
142369
  value: reactiveFunction
142289
142370
  });
142290
142371
  if (env.config.validateMemoizedEffectDependencies) {
142291
- validateMemoizedEffectDependencies(reactiveFunction);
142372
+ validateMemoizedEffectDependencies(reactiveFunction).unwrap();
142292
142373
  }
142293
142374
  if (env.config.enablePreserveExistingMemoizationGuarantees || env.config.validatePreserveExistingMemoizationGuarantees) {
142294
- validatePreservedManualMemoization(reactiveFunction);
142375
+ validatePreservedManualMemoization(reactiveFunction).unwrap();
142295
142376
  }
142296
142377
  const ast = codegenFunction(reactiveFunction, {
142297
142378
  uniqueIdentifiers,
@@ -142732,6 +142813,9 @@ function compileProgram(program, pass) {
142732
142813
  pass.code
142733
142814
  )
142734
142815
  };
142816
+ if (!compileResult.compiledFn.hasFireRewrite && !compileResult.compiledFn.hasLoweredContextAccess) {
142817
+ return null;
142818
+ }
142735
142819
  } catch (err) {
142736
142820
  if (err instanceof CompilerError) {
142737
142821
  retryErrors.push({ fn, error: err });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-react-compiler",
3
- "version": "0.0.0-experimental-afa1d5a-20250319",
3
+ "version": "0.0.0-experimental-3452ff1-20250321",
4
4
  "description": "Babel plugin for React Compiler.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",