diff --git a/.eslintrc.js b/.eslintrc.js index 952149f30ca7..2cebe8698c7c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -507,7 +507,6 @@ module.exports = { __IS_FIREFOX__: 'readonly', __IS_EDGE__: 'readonly', __IS_NATIVE__: 'readonly', - __IS_INTERNAL_MCP_BUILD__: 'readonly', __IS_INTERNAL_VERSION__: 'readonly', chrome: 'readonly', }, diff --git a/.github/workflows/runtime_eslint_plugin_e2e.yml b/.github/workflows/runtime_eslint_plugin_e2e.yml index 92921646c1bc..bd6f0782066e 100644 --- a/.github/workflows/runtime_eslint_plugin_e2e.yml +++ b/.github/workflows/runtime_eslint_plugin_e2e.yml @@ -29,6 +29,7 @@ jobs: - "7" - "8" - "9" + - "10" steps: - uses: actions/checkout@v4 with: diff --git a/compiler/CLAUDE.md b/compiler/CLAUDE.md index 94a7d4b5d6d9..0fade42b2bf6 100644 --- a/compiler/CLAUDE.md +++ b/compiler/CLAUDE.md @@ -35,6 +35,20 @@ yarn snap -p -d yarn snap -u ``` +## Linting + +```bash +# Run lint on the compiler source +yarn workspace babel-plugin-react-compiler lint +``` + +## Formatting + +```bash +# Run prettier on all files (from the react root directory, not compiler/) +yarn prettier-all +``` + ## Compiling Arbitrary Files Use `yarn snap compile` to compile any file (not just fixtures) with the React Compiler: @@ -215,12 +229,12 @@ const UseEffectEventHook = addHook( Feature flags are configured in `src/HIR/Environment.ts`, for example `enableJsxOutlining`. Test fixtures can override the active feature flags used for that fixture via a comment pragma on the first line of the fixture input, for example: ```javascript -// enableJsxOutlining @enableChangeVariableCodegen:false +// enableJsxOutlining @enableNameAnonymousFunctions:false ...code... ``` -Would enable the `enableJsxOutlining` feature and disable the `enableChangeVariableCodegen` feature. +Would enable the `enableJsxOutlining` feature and disable the `enableNameAnonymousFunctions` feature. ## Debugging Tips @@ -229,20 +243,19 @@ Would enable the `enableJsxOutlining` feature and disable the `enableChangeVaria 3. Look for `Impure`, `Render`, `Capture` effects on instructions 4. Check the pass ordering in Pipeline.ts to understand when effects are populated vs validated -## Error Handling for Unsupported Features +## Error Handling and Fault Tolerance -When the compiler encounters an unsupported but known pattern, use `CompilerError.throwTodo()` instead of `CompilerError.invariant()`. Todo errors cause graceful bailouts in production; Invariant errors are hard failures indicating unexpected/invalid states. +The compiler is fault-tolerant: it runs all passes and accumulates errors on the `Environment` rather than throwing on the first error. This lets users see all compilation errors at once. -```typescript -// Unsupported but expected pattern - graceful bailout -CompilerError.throwTodo({ - reason: `Support [description of unsupported feature]`, - loc: terminal.loc, -}); - -// Invariant is for truly unexpected/invalid states - hard failure -CompilerError.invariant(false, { - reason: `Unexpected [thing]`, - loc: terminal.loc, -}); -``` +**Recording errors** — Passes record errors via `env.recordError(diagnostic)`. Errors are accumulated on `Environment.#errors` and checked at the end of the pipeline via `env.hasErrors()` / `env.aggregateErrors()`. + +**`tryRecord()` wrapper** — In Pipeline.ts, validation passes are wrapped in `env.tryRecord(() => pass(hir))` which catches thrown `CompilerError`s (non-invariant) and records them. Infrastructure/transformation passes are NOT wrapped in `tryRecord()` because later passes depend on their output being structurally valid. + +**Error categories:** +- `CompilerError.throwTodo()` — Unsupported but known pattern. Graceful bailout. Can be caught by `tryRecord()`. +- `CompilerError.invariant()` — Truly unexpected/invalid state. Always throws immediately, never caught by `tryRecord()`. +- Non-`CompilerError` exceptions — Always re-thrown. + +**Key files:** `Environment.ts` (`recordError`, `tryRecord`, `hasErrors`, `aggregateErrors`), `Pipeline.ts` (pass orchestration), `Program.ts` (`tryCompileFunction` handles the `Result`). + +**Test fixtures:** `__tests__/fixtures/compiler/fault-tolerance/` contains multi-error fixtures verifying all errors are reported. diff --git a/compiler/apps/playground/__tests__/e2e/__snapshots__/page.spec.ts/disableMemoizationForDebugging-output.txt b/compiler/apps/playground/__tests__/e2e/__snapshots__/page.spec.ts/disableMemoizationForDebugging-output.txt deleted file mode 100644 index c0a2769bb3fa..000000000000 --- a/compiler/apps/playground/__tests__/e2e/__snapshots__/page.spec.ts/disableMemoizationForDebugging-output.txt +++ /dev/null @@ -1,14 +0,0 @@ -import { c as _c } from "react/compiler-runtime"; -export default function TestComponent(t0) { - const $ = _c(2); - const { x } = t0; - let t1; - if ($[0] !== x || true) { - t1 = ; - $[0] = x; - $[1] = t1; - } else { - t1 = $[1]; - } - return t1; -} diff --git a/compiler/apps/playground/__tests__/e2e/page.spec.ts b/compiler/apps/playground/__tests__/e2e/page.spec.ts index 8ad9f122aab3..20596e5d93bf 100644 --- a/compiler/apps/playground/__tests__/e2e/page.spec.ts +++ b/compiler/apps/playground/__tests__/e2e/page.spec.ts @@ -283,37 +283,6 @@ test('error is displayed when config has validation error', async ({page}) => { expect(output.replace(/\s+/g, ' ')).toContain('Unexpected compilationMode'); }); -test('disableMemoizationForDebugging flag works as expected', async ({ - page, -}) => { - const store: Store = { - source: TEST_SOURCE, - config: `import type { PluginOptions } from 'babel-plugin-react-compiler/dist'; - -({ - environment: { - disableMemoizationForDebugging: true - } -} satisfies PluginOptions);`, - showInternals: false, - }; - const hash = encodeStore(store); - await page.goto(`/#${hash}`, {waitUntil: 'networkidle'}); - await page.waitForFunction(isMonacoLoaded); - await expandConfigs(page); - await page.screenshot({ - fullPage: true, - path: 'test-results/07-config-disableMemoizationForDebugging-flag.png', - }); - - const text = - (await page.locator('.monaco-editor-output').allInnerTexts()) ?? []; - const output = await formatPrint(text); - - expect(output).not.toEqual(''); - expect(output).toMatchSnapshot('disableMemoizationForDebugging-output.txt'); -}); - test('error is displayed when source has syntax error', async ({page}) => { const syntaxErrorSource = `function TestComponent(props) { const oops = props. diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/06-inferTypes.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/06-inferTypes.md index eca37e884312..10878fa4a525 100644 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/06-inferTypes.md +++ b/compiler/packages/babel-plugin-react-compiler/docs/passes/06-inferTypes.md @@ -70,9 +70,6 @@ The `occursCheck` method prevents infinite types by detecting when a type variab - `DeclareContext` and `LoadContext` generate no type equations (intentionally untyped) - `StoreContext` with `Const` kind does propagate the rvalue type to enable ref inference through context variables -### Event Handler Inference -When `enableInferEventHandlers` is enabled, JSX props starting with "on" (e.g., `onClick`) on built-in DOM elements (excluding web components with hyphens) are inferred as `Function`. - ## TODOs 1. **Hook vs Function type ambiguity**: > "TODO: callee could be a hook or a function, so this type equation isn't correct. We should change Hook to a subtype of Function or change unifier logic." diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/31-codegenReactiveFunction.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/31-codegenReactiveFunction.md index a2132990ff98..922b2e9dec47 100644 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/31-codegenReactiveFunction.md +++ b/compiler/packages/babel-plugin-react-compiler/docs/passes/31-codegenReactiveFunction.md @@ -205,8 +205,6 @@ if ($[0] !== "source_hash_abc123") { } ``` -### Change Detection for Debugging -When `enableChangeDetectionForDebugging` is configured, additional code is generated to detect when cached values unexpectedly change. ### Labeled Breaks Control flow with labeled breaks (for early returns or loop exits) uses `codegenLabel` to generate consistent label names: @@ -231,7 +229,6 @@ type CodegenFunction = { prunedMemoBlocks: number; // Scopes that were pruned prunedMemoValues: number; // Values in pruned scopes hasInferredEffect: boolean; - hasFireRewrite: boolean; }; ``` diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/32-transformFire.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/32-transformFire.md deleted file mode 100644 index 26ee425ceea4..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/32-transformFire.md +++ /dev/null @@ -1,203 +0,0 @@ -# transformFire - -## File -`src/Transform/TransformFire.ts` - -## Purpose -This pass transforms `fire(fn())` calls inside `useEffect` lambdas into calls to a `useFire` hook that provides stable function references. The `fire()` function is a React API that allows effect callbacks to call functions with their current values while maintaining stable effect dependencies. - -Without this transform, if an effect depends on a function that changes every render, the effect would re-run on every render. The `useFire` hook provides a stable wrapper that always calls the latest version of the function. - -## Input Invariants -- The `enableFire` feature flag must be enabled -- `fire()` calls must only appear inside `useEffect` lambdas -- Each `fire()` call must have exactly one argument (a function call expression) -- The function being fired must be consistent across all `fire()` calls in the same effect - -## Output Guarantees -- All `fire(fn(...args))` calls are replaced with direct calls `fired_fn(...args)` -- A `useFire(fn)` hook call is inserted before the `useEffect` -- The fired function is stored in a temporary and captured by the effect -- The original function `fn` is removed from the effect's captured context - -## Algorithm - -### Phase 1: Find Fire Calls -```typescript -function replaceFireFunctions(fn: HIRFunction, context: Context): void { - // For each useEffect call instruction: - // 1. Find all fire() calls in the effect lambda - // 2. Validate they have proper arguments - // 3. Track which functions are being fired - - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - if (isUseEffectCall(instr)) { - const lambda = getEffectLambda(instr); - findAndReplaceFireCalls(lambda, fireFunctions); - } - } - } -} -``` - -### Phase 2: Insert useFire Hooks -For each function being fired, insert a `useFire` call: -```typescript -// Before: -useEffect(() => { - fire(foo(props)); -}, [foo, props]); - -// After: -const t0 = useFire(foo); -useEffect(() => { - t0(props); -}, [t0, props]); -``` - -### Phase 3: Replace Fire Calls -Transform `fire(fn(...args))` to `firedFn(...args)`: -```typescript -// The fire() wrapper is removed -// The inner function call uses the useFire'd version -fire(foo(x, y)) → t0(x, y) // where t0 = useFire(foo) -``` - -### Phase 4: Validate No Remaining Fire Uses -```typescript -function ensureNoMoreFireUses(fn: HIRFunction, context: Context): void { - // Ensure all fire() uses have been transformed - // Report errors for any remaining fire() calls -} -``` - -## Edge Cases - -### Fire Outside Effect -`fire()` calls outside `useEffect` lambdas cause a validation error: -```javascript -// ERROR: fire() can only be used inside useEffect -function Component() { - fire(callback()); -} -``` - -### Mixed Fire and Non-Fire Calls -All calls to the same function must either all use `fire()` or none: -```javascript -// ERROR: Cannot mix fire() and non-fire calls -useEffect(() => { - fire(foo(x)); - foo(y); // Error: foo is used with and without fire() -}); -``` - -### Multiple Arguments to Fire -`fire()` accepts exactly one argument (the function call): -```javascript -// ERROR: fire() takes exactly one argument -fire(foo, bar) // Invalid -fire() // Invalid -``` - -### Nested Effects -Fire calls in nested effects are validated separately: -```javascript -useEffect(() => { - useEffect(() => { // Error: nested effects not allowed - fire(foo()); - }); -}); -``` - -### Deep Scope Handling -The pass handles fire calls within deeply nested scopes inside effects: -```javascript -useEffect(() => { - if (cond) { - while (x) { - fire(foo(x)); // Still transformed correctly - } - } -}); -``` - -## TODOs -None in the source file. - -## Example - -### Fixture: `transform-fire/basic.js` - -**Input:** -```javascript -// @enableFire -function Component(props) { - const foo = (props_0) => { - console.log(props_0); - }; - useEffect(() => { - fire(foo(props)); - }); - return null; -} -``` - -**After TransformFire:** -``` -bb0 (block): - [1] $25 = Function @context[] ... // foo definition - [2] StoreLocal Const foo$32 = $25 - [3] $45 = LoadGlobal import { useFire } from 'react/compiler-runtime' - [4] $46 = LoadLocal foo$32 - [5] $47 = Call $45($46) // useFire(foo) - [6] StoreLocal Const #t44$44 = $47 - [7] $34 = LoadGlobal(global) useEffect - [8] $35 = Function @context[#t44$44, props$24] ... - <>(): - [1] $37 = LoadLocal #t44$44 // Load the fired function - [2] $38 = LoadLocal props$24 - [3] $39 = Call $37($38) // Call it directly (no fire wrapper) - [4] Return Void - [9] Call $34($35) // useEffect(lambda) - [10] Return null -``` - -**Generated Code:** -```javascript -import { useFire as _useFire } from "react/compiler-runtime"; -function Component(props) { - const $ = _c(4); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = (props_0) => { - console.log(props_0); - }; - $[0] = t0; - } else { - t0 = $[0]; - } - const foo = t0; - const t1 = _useFire(foo); - let t2; - if ($[1] !== props || $[2] !== t1) { - t2 = () => { - t1(props); - }; - $[1] = props; - $[2] = t1; - $[3] = t2; - } else { - t2 = $[3]; - } - useEffect(t2); - return null; -} -``` - -Key observations: -- `useFire` is imported from `react/compiler-runtime` -- `fire(foo(props))` becomes `t1(props)` where `t1 = _useFire(foo)` -- The effect now depends on `t1` (stable) and `props` (reactive) -- The original `foo` function is memoized and passed to `useFire` diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/33-lowerContextAccess.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/33-lowerContextAccess.md deleted file mode 100644 index e91bd3ec8e83..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/33-lowerContextAccess.md +++ /dev/null @@ -1,174 +0,0 @@ -# lowerContextAccess - -## File -`src/Optimization/LowerContextAccess.ts` - -## Purpose -This pass optimizes `useContext` calls by generating selector functions that extract only the needed properties from the context. Instead of subscribing to the entire context object, components can subscribe to specific slices, enabling more granular re-rendering. - -When a component destructures specific properties from a context, this pass transforms the `useContext` call to use a selector-based API that only triggers re-renders when the selected properties change. - -## Input Invariants -- The `lowerContextAccess` configuration must be set with: - - `source`: The module to import the lowered context hook from - - `importSpecifierName`: The name of the hook function -- The function must use `useContext` with destructuring patterns -- Only object destructuring patterns with identifier values are supported - -## Output Guarantees -- `useContext(Ctx)` calls with destructuring are replaced with selector calls -- A selector function is generated that extracts the needed properties -- The return type is changed from object to array for positional access -- Unused original `useContext` calls are removed by dead code elimination - -## Algorithm - -### Phase 1: Collect Context Access Patterns -```typescript -function lowerContextAccess(fn: HIRFunction, config: ExternalFunction): void { - const contextAccess: Map = new Map(); - const contextKeys: Map> = new Map(); - - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - // Find useContext calls - if (isUseContextCall(instr)) { - contextAccess.set(instr.lvalue.identifier.id, instr.value); - } - - // Find destructuring patterns that access context results - if (isDestructure(instr) && contextAccess.has(instr.value.value.id)) { - const keys = extractPropertyKeys(instr.value.pattern); - contextKeys.set(instr.value.value.id, keys); - } - } - } -} -``` - -### Phase 2: Generate Selector Functions -For each context access with known keys: -```typescript -// Original: -const {foo, bar} = useContext(MyContext); - -// Selector function generated: -(ctx) => [ctx.foo, ctx.bar] -``` - -### Phase 3: Transform Context Calls -```typescript -// Before: -$0 = useContext(MyContext) -{foo, bar} = $0 - -// After: -$0 = useContext_withSelector(MyContext, (ctx) => [ctx.foo, ctx.bar]) -[foo, bar] = $0 -``` - -### Phase 4: Update Destructuring -Change object destructuring to array destructuring to match selector return: -```typescript -// Before: { foo: foo$15, bar: bar$16 } = $14 -// After: [ foo$15, bar$16 ] = $14 -``` - -## Edge Cases - -### Dynamic Property Access -If context properties are accessed dynamically (not through destructuring), the optimization is skipped: -```javascript -const ctx = useContext(MyContext); -const x = ctx[dynamicKey]; // Cannot optimize -``` - -### Spread in Destructuring -Spread patterns prevent optimization: -```javascript -const {foo, ...rest} = useContext(MyContext); // Cannot optimize -``` - -### Non-Identifier Values -Only simple identifier destructuring is supported: -```javascript -const {foo: bar} = useContext(MyContext); // Supported (rename) -const {foo = defaultVal} = useContext(MyContext); // Not supported -``` - -### Multiple Context Accesses -Each `useContext` call is transformed independently: -```javascript -const {a} = useContext(CtxA); // Transformed -const {b} = useContext(CtxB); // Transformed separately -``` - -### Hook Guards -When `enableEmitHookGuards` is enabled, the selector function includes proper hook guard annotations. - -## TODOs -None in the source file. - -## Example - -### Fixture: `lower-context-selector-simple.js` - -**Input:** -```javascript -// @lowerContextAccess -function App() { - const {foo, bar} = useContext(MyContext); - return ; -} -``` - -**After OptimizePropsMethodCalls (where lowering happens):** -``` -bb0 (block): - [1] $12 = LoadGlobal(global) useContext // Original (now unused) - [2] $13 = LoadGlobal(global) MyContext - [3] $22 = LoadGlobal import { useContext_withSelector } from 'react-compiler-runtime' - [4] $36 = Function @context[] - <>(#t23$30): - [1] $31 = LoadLocal #t23$30 - [2] $32 = PropertyLoad $31.foo - [3] $33 = LoadLocal #t23$30 - [4] $34 = PropertyLoad $33.bar - [5] $35 = Array [$32, $34] // Return [foo, bar] - [6] Return $35 - [5] $14 = Call $22($13, $36) // useContext_withSelector(MyContext, selector) - [6] $17 = Destructure Const { foo: foo$15, bar: bar$16 } = $14 - ... -``` - -**Generated Code:** -```javascript -import { c as _c } from "react/compiler-runtime"; -import { useContext_withSelector } from "react-compiler-runtime"; -function App() { - const $ = _c(2); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = (ctx) => [ctx.foo, ctx.bar]; - $[0] = t0; - } else { - t0 = $[0]; - } - const { foo, bar } = useContext_withSelector(MyContext, t0); - let t1; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - $[1] = t1; - } else { - t1 = $[1]; - } - return t1; -} -``` - -Key observations: -- `useContext` is replaced with `useContext_withSelector` -- A selector function `(ctx) => [ctx.foo, ctx.bar]` is generated -- The selector function is memoized (first cache slot) -- Only `foo` and `bar` properties are extracted, enabling granular subscriptions -- The selector return type changes from object to array diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/42-validateNoCapitalizedCalls.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/42-validateNoCapitalizedCalls.md index 70038be14e21..cf4c2bbe9545 100644 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/42-validateNoCapitalizedCalls.md +++ b/compiler/packages/babel-plugin-react-compiler/docs/passes/42-validateNoCapitalizedCalls.md @@ -49,13 +49,8 @@ const ALLOW_LIST = new Set([ ...(envConfig.validateNoCapitalizedCalls ?? []), // User-configured allowlist ]); -const hookPattern = envConfig.hookPattern != null - ? new RegExp(envConfig.hookPattern) - : null; - const isAllowed = (name: string): boolean => { - return ALLOW_LIST.has(name) || - (hookPattern != null && hookPattern.test(name)); + return ALLOW_LIST.has(name); }; ``` @@ -137,13 +132,6 @@ Users can allowlist specific functions via configuration: validateNoCapitalizedCalls: ['MyUtility', 'SomeFactory'] ``` -### Hook Patterns -Functions matching the configured hook pattern are allowed even if capitalized: -```typescript -// With hookPattern: 'React\\$use.*' -const x = React$useState(); // Allowed if it matches the hook pattern -``` - ### Method Calls vs Function Calls Both direct function calls and method calls on objects are checked: ```javascript diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/52-validateMemoizedEffectDependencies.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/52-validateMemoizedEffectDependencies.md deleted file mode 100644 index 81d78a39111d..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/52-validateMemoizedEffectDependencies.md +++ /dev/null @@ -1,93 +0,0 @@ -# validateMemoizedEffectDependencies - -## File -`src/Validation/ValidateMemoizedEffectDependencies.ts` - -## Purpose -Validates that all known effect dependencies (for `useEffect`, `useLayoutEffect`, and `useInsertionEffect`) are properly memoized. This prevents a common bug where unmemoized effect dependencies can cause infinite re-render loops or other unexpected behavior. - -## Input Invariants -- Operates on ReactiveFunction (post-reactive scope inference) -- Reactive scopes have been assigned to values that need memoization -- Must run after scope inference but before codegen - -## Validation Rules -This pass checks two conditions: - -1. **Unmemoized dependencies with assigned scopes**: Disallows effect dependencies that should be memoized (have a reactive scope assigned) but where that reactive scope does not exist in the output. This catches cases where a reactive scope was pruned, such as when it spans a hook call. - -2. **Mutable dependencies at effect call site**: Disallows effect dependencies whose mutable range encompasses the effect call. This catches values that the compiler knows may be mutated after the effect is set up. - -When either condition is violated, the pass produces: -``` -Compilation Skipped: React Compiler has skipped optimizing this component because -the effect dependencies could not be memoized. Unmemoized effect dependencies can -trigger an infinite loop or other unexpected behavior -``` - -## Algorithm -1. Traverse the reactive function using a visitor pattern -2. Track all scopes that exist in the AST by adding them to a `Set` during `visitScope` -3. Only record a scope if its dependencies are also memoized (transitive memoization check) -4. When visiting an instruction that is an effect hook call (`useEffect`, `useLayoutEffect`, `useInsertionEffect`) with at least 2 arguments (function + deps array): - - Check if the dependency array is mutable at the call site using `isMutable()` - - Check if the dependency array's scope exists using `isUnmemoized()` - - If either check fails, push an error - -### Key Helper Functions - -**isEffectHook(identifier)**: Returns true if the identifier is `useEffect`, `useLayoutEffect`, or `useInsertionEffect`. - -**isUnmemoized(operand, scopes)**: Returns true if the operand has a scope assigned (`operand.scope != null`) but that scope doesn't exist in the set of valid scopes. - -## Edge Cases -- Only validates effects with 2+ arguments (ignores effects without dependency arrays) -- Transitive memoization: A scope is only considered valid if all its dependencies are also memoized -- Merged scopes are tracked together with their primary scope - -## TODOs -From the source code: -```typescript -// TODO: isMutable is not safe to call here as it relies on identifier mutableRange -// which is no longer valid at this point in the pipeline -``` - -## Example - -### Fixture: `error.invalid-useEffect-dep-not-memoized.js` - -**Input:** -```javascript -// @validateMemoizedEffectDependencies -import {useEffect} from 'react'; - -function Component(props) { - const data = {}; - useEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} -``` - -**Error:** -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because -the effect dependencies could not be memoized. Unmemoized effect dependencies can -trigger an infinite loop or other unexpected behavior - -error.invalid-useEffect-dep-not-memoized.ts:6:2 - 4 | function Component(props) { - 5 | const data = {}; -> 6 | useEffect(() => { - | ^^^^^^^^^^^^^^^^^ -> 7 | console.log(props.value); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 8 | }, [data]); - | ^^^^^^^^^^^^^ -``` - -**Why it fails:** The `data` object is mutated after the `useEffect` call, which extends its mutable range past the effect. This means `data` cannot be safely memoized as an effect dependency because it might change after the effect is set up. diff --git a/compiler/packages/babel-plugin-react-compiler/docs/passes/README.md b/compiler/packages/babel-plugin-react-compiler/docs/passes/README.md index bc9e17ac5238..9e99265392f0 100644 --- a/compiler/packages/babel-plugin-react-compiler/docs/passes/README.md +++ b/compiler/packages/babel-plugin-react-compiler/docs/passes/README.md @@ -25,7 +25,7 @@ This directory contains detailed documentation for each pass in the React Compil ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ PHASE 2: OPTIMIZATION │ │ │ -│ constantPropagation ──▶ deadCodeElimination ──▶ instructionReordering │ +│ constantPropagation ──▶ deadCodeElimination │ │ │ └─────────────────────────────────────────────────────────────────────────────────────┘ │ @@ -195,8 +195,6 @@ This directory contains detailed documentation for each pass in the React Compil | # | Pass | File | Description | |---|------|------|-------------| -| 32 | [transformFire](32-transformFire.md) | `Transform/TransformFire.ts` | Transform `fire()` calls in effects | -| 33 | [lowerContextAccess](33-lowerContextAccess.md) | `Optimization/LowerContextAccess.ts` | Optimize context access with selectors | | 34 | [optimizePropsMethodCalls](34-optimizePropsMethodCalls.md) | `Optimization/OptimizePropsMethodCalls.ts` | Normalize props method calls | | 35 | [optimizeForSSR](35-optimizeForSSR.md) | `Optimization/OptimizeForSSR.ts` | SSR-specific optimizations | | 36 | [outlineJSX](36-outlineJSX.md) | `Optimization/OutlineJsx.ts` | Outline JSX to components | @@ -220,7 +218,6 @@ This directory contains detailed documentation for each pass in the React Compil | 49 | [validateNoRefAccessInRender](49-validateNoRefAccessInRender.md) | `Validation/ValidateNoRefAccessInRender.ts` | Ref access constraints | | 50 | [validateNoFreezingKnownMutableFunctions](50-validateNoFreezingKnownMutableFunctions.md) | `Validation/ValidateNoFreezingKnownMutableFunctions.ts` | Mutable function isolation | | 51 | [validateExhaustiveDependencies](51-validateExhaustiveDependencies.md) | `Validation/ValidateExhaustiveDependencies.ts` | Dependency array completeness | -| 52 | [validateMemoizedEffectDependencies](52-validateMemoizedEffectDependencies.md) | `Validation/ValidateMemoizedEffectDependencies.ts` | Effect scope memoization | | 53 | [validatePreservedManualMemoization](53-validatePreservedManualMemoization.md) | `Validation/ValidatePreservedManualMemoization.ts` | Manual memo preservation | | 54 | [validateStaticComponents](54-validateStaticComponents.md) | `Validation/ValidateStaticComponents.ts` | Component identity stability | | 55 | [validateSourceLocations](55-validateSourceLocations.md) | `Validation/ValidateSourceLocations.ts` | Source location preservation | @@ -275,8 +272,6 @@ Many passes are controlled by feature flags in `Environment.ts`: | Flag | Enables Pass | |------|--------------| -| `enableFire` | transformFire | -| `lowerContextAccess` | lowerContextAccess | | `enableJsxOutlining` | outlineJSX | | `enableFunctionOutlining` | outlineFunctions | | `validateNoSetStateInRender` | validateNoSetStateInRender | @@ -307,6 +302,15 @@ yarn snap minimize yarn snap -u ``` +## Fault Tolerance + +The pipeline is fault-tolerant: all passes run to completion, accumulating errors on `Environment` rather than aborting on the first error. + +- **Validation passes** are wrapped in `env.tryRecord()` in Pipeline.ts, which catches non-invariant `CompilerError`s and records them. If a validation pass throws, compilation continues. +- **Infrastructure/transformation passes** (enterSSA, eliminateRedundantPhi, inferMutationAliasingEffects, codegen, etc.) are NOT wrapped in `tryRecord()` because subsequent passes depend on their output being structurally valid. If they fail, compilation aborts. +- **`lower()` (BuildHIR)** always produces an `HIRFunction`, recording errors on `env` instead of returning `Err`. Unsupported constructs (e.g., `var`) are lowered best-effort. +- At the end of the pipeline, `env.hasErrors()` determines whether to return `Ok(codegen)` or `Err(aggregatedErrors)`. + ## Further Reading - [MUTABILITY_ALIASING_MODEL.md](../../src/Inference/MUTABILITY_ALIASING_MODEL.md): Detailed aliasing model docs diff --git a/compiler/packages/babel-plugin-react-compiler/src/Babel/BabelPlugin.ts b/compiler/packages/babel-plugin-react-compiler/src/Babel/BabelPlugin.ts index ed74f4664953..6764e12f025c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Babel/BabelPlugin.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Babel/BabelPlugin.ts @@ -11,7 +11,6 @@ import { injectReanimatedFlag, pipelineUsesReanimatedPlugin, } from '../Entrypoint/Reanimated'; -import validateNoUntransformedReferences from '../Entrypoint/ValidateNoUntransformedReferences'; import {CompilerError} from '..'; const ENABLE_REACT_COMPILER_TIMINGS = @@ -64,19 +63,12 @@ export default function BabelPluginReactCompiler( }, }; } - const result = compileProgram(prog, { + compileProgram(prog, { opts, filename: pass.filename ?? null, comments: pass.file.ast.comments ?? [], code: pass.file.code, }); - validateNoUntransformedReferences( - prog, - pass.filename ?? null, - opts.logger, - opts.environment, - result, - ); if (ENABLE_REACT_COMPILER_TIMINGS === true) { performance.mark(`${filename}:end`, { detail: 'BabelPlugin:Program:end', diff --git a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts index 1c9281eb0720..42822384d207 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts @@ -565,15 +565,12 @@ function printCodeFrame( function printErrorSummary(category: ErrorCategory, message: string): string { let heading: string; switch (category) { - case ErrorCategory.AutomaticEffectDependencies: case ErrorCategory.CapitalizedCalls: case ErrorCategory.Config: case ErrorCategory.EffectDerivationsOfState: case ErrorCategory.EffectSetState: case ErrorCategory.ErrorBoundaries: - case ErrorCategory.Factories: case ErrorCategory.FBT: - case ErrorCategory.Fire: case ErrorCategory.Gating: case ErrorCategory.Globals: case ErrorCategory.Hooks: @@ -637,10 +634,6 @@ export enum ErrorCategory { * Checking that useMemos always return a value */ VoidUseMemo = 'VoidUseMemo', - /** - * Checking for higher order functions acting as factories for components/hooks - */ - Factories = 'Factories', /** * Checks that manual memoization is preserved */ @@ -718,14 +711,6 @@ export enum ErrorCategory { * Suppressions */ Suppression = 'Suppression', - /** - * Issues with auto deps - */ - AutomaticEffectDependencies = 'AutomaticEffectDependencies', - /** - * Issues with `fire` - */ - Fire = 'Fire', /** * fbt-specific issues */ @@ -790,16 +775,6 @@ export function getRuleForCategory(category: ErrorCategory): LintRule { function getRuleForCategoryImpl(category: ErrorCategory): LintRule { switch (category) { - case ErrorCategory.AutomaticEffectDependencies: { - return { - category, - severity: ErrorSeverity.Error, - name: 'automatic-effect-dependencies', - description: - 'Verifies that automatic effect dependencies are compiled if opted-in', - preset: LintRulePreset.Off, - }; - } case ErrorCategory.CapitalizedCalls: { return { category, @@ -870,17 +845,6 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule { preset: LintRulePreset.Recommended, }; } - case ErrorCategory.Factories: { - return { - category, - severity: ErrorSeverity.Error, - name: 'component-hook-factories', - description: - 'Validates against higher order functions defining nested components or hooks. ' + - 'Components and hooks should be defined at the module level', - preset: LintRulePreset.Recommended, - }; - } case ErrorCategory.FBT: { return { category, @@ -890,15 +854,6 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule { preset: LintRulePreset.Off, }; } - case ErrorCategory.Fire: { - return { - category, - severity: ErrorSeverity.Error, - name: 'fire', - description: 'Validates usage of `fire`', - preset: LintRulePreset.Off, - }; - } case ErrorCategory.Gating: { return { category, diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts index df78607fc196..15796d77a70e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts @@ -19,7 +19,7 @@ import {getOrInsertWith} from '../Utils/utils'; import {ExternalFunction, isHookName} from '../HIR/Environment'; import {Err, Ok, Result} from '../Utils/Result'; import {LoggerEvent, ParsedPluginOptions} from './Options'; -import {BabelFn, getReactCompilerRuntimeModule} from './Program'; +import {getReactCompilerRuntimeModule} from './Program'; import {SuppressionRange} from './Suppression'; export function validateRestrictedImports( @@ -84,12 +84,6 @@ export class ProgramContext { // generated imports imports: Map> = new Map(); - /** - * Metadata from compilation - */ - retryErrors: Array<{fn: BabelFn; error: CompilerError}> = []; - inferredEffectLocations: Set = new Set(); - constructor({ program, suppressions, @@ -108,14 +102,7 @@ export class ProgramContext { } isHookName(name: string): boolean { - if (this.opts.environment.hookPattern == null) { - return isHookName(name); - } else { - const match = new RegExp(this.opts.environment.hookPattern).exec(name); - return ( - match != null && typeof match[1] === 'string' && isHookName(match[1]) - ); - } + return isHookName(name); } hasReference(name: string): boolean { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts index b758d7b024d0..c0576c7521f1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts @@ -228,8 +228,6 @@ const CompilerOutputModeSchema = z.enum([ 'ssr', // Build optimized for the client, with auto memoization 'client', - // Build optimized for the client without auto memo - 'client-no-memo', // Lint mode, the output is unused but validations should run 'lint', ]); @@ -254,10 +252,9 @@ export type LoggerEvent = | CompileErrorEvent | CompileDiagnosticEvent | CompileSkipEvent + | CompileUnexpectedThrowEvent | PipelineErrorEvent - | TimingEvent - | AutoDepsDecorationsEvent - | AutoDepsEligibleEvent; + | TimingEvent; export type CompileErrorEvent = { kind: 'CompileError'; @@ -290,21 +287,15 @@ export type PipelineErrorEvent = { fnLoc: t.SourceLocation | null; data: string; }; +export type CompileUnexpectedThrowEvent = { + kind: 'CompileUnexpectedThrow'; + fnLoc: t.SourceLocation | null; + data: string; +}; export type TimingEvent = { kind: 'Timing'; measurement: PerformanceMeasure; }; -export type AutoDepsDecorationsEvent = { - kind: 'AutoDepsDecorations'; - fnLoc: t.SourceLocation; - decorations: Array; -}; -export type AutoDepsEligibleEvent = { - kind: 'AutoDepsEligible'; - fnLoc: t.SourceLocation; - depArrayLoc: t.SourceLocation; -}; - export type Logger = { logEvent: (filename: string | null, event: LoggerEvent) => void; debugLogIRs?: (value: CompilerPipelineValue) => void; diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts index 30d665227159..a0cd02817828 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts @@ -9,6 +9,8 @@ import {NodePath} from '@babel/traverse'; import * as t from '@babel/types'; import prettyFormat from 'pretty-format'; import {CompilerOutputMode, Logger, ProgramContext} from '.'; +import {CompilerError} from '../CompilerError'; +import {Err, Ok, Result} from '../Utils/Result'; import { HIRFunction, ReactiveFunction, @@ -34,15 +36,12 @@ import { dropManualMemoization, inferReactivePlaces, inlineImmediatelyInvokedFunctionExpressions, - inferEffectDependencies, } from '../Inference'; import { constantPropagation, deadCodeElimination, pruneMaybeThrows, - inlineJsxTransform, } from '../Optimization'; -import {instructionReordering} from '../Optimization/InstructionReordering'; import { CodegenFunction, alignObjectMethodScopes, @@ -69,7 +68,6 @@ import {alignReactiveScopesToBlockScopesHIR} from '../ReactiveScopes/AlignReacti import {flattenReactiveLoopsHIR} from '../ReactiveScopes/FlattenReactiveLoopsHIR'; import {flattenScopesWithHooksOrUseHIR} from '../ReactiveScopes/FlattenScopesWithHooksOrUseHIR'; import {pruneAlwaysInvalidatingScopes} from '../ReactiveScopes/PruneAlwaysInvalidatingScopes'; -import pruneInitializationDependencies from '../ReactiveScopes/PruneInitializationDependencies'; import {stabilizeBlockIds} from '../ReactiveScopes/StabilizeBlockIds'; import { eliminateRedundantPhi, @@ -80,7 +78,6 @@ import {inferTypes} from '../TypeInference'; import { validateContextVariableLValues, validateHooksUsage, - validateMemoizedEffectDependencies, validateNoCapitalizedCalls, validateNoRefAccessInRender, validateNoSetStateInRender, @@ -89,14 +86,11 @@ import { } from '../Validation'; import {validateLocalsNotReassignedAfterRender} from '../Validation/ValidateLocalsNotReassignedAfterRender'; import {outlineFunctions} from '../Optimization/OutlineFunctions'; -import {lowerContextAccess} from '../Optimization/LowerContextAccess'; import {validateNoSetStateInEffects} from '../Validation/ValidateNoSetStateInEffects'; import {validateNoJSXInTryStatement} from '../Validation/ValidateNoJSXInTryStatement'; import {propagateScopeDependenciesHIR} from '../HIR/PropagateScopeDependenciesHIR'; import {outlineJSX} from '../Optimization/OutlineJsx'; import {optimizePropsMethodCalls} from '../Optimization/OptimizePropsMethodCalls'; -import {transformFire} from '../Transform'; -import {validateNoImpureFunctionsInRender} from '../Validation/ValidateNoImpureFunctionsInRender'; import {validateStaticComponents} from '../Validation/ValidateStaticComponents'; import {validateNoFreezingKnownMutableFunctions} from '../Validation/ValidateNoFreezingKnownMutableFunctions'; import {inferMutationAliasingEffects} from '../Inference/InferMutationAliasingEffects'; @@ -125,7 +119,7 @@ function run( logger: Logger | null, filename: string | null, code: string | null, -): CodegenFunction { +): Result { const contextIdentifiers = findContextIdentifiers(func); const env = new Environment( func.scope, @@ -156,26 +150,21 @@ function runWithEnvironment( t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression >, env: Environment, -): CodegenFunction { +): Result { const log = (value: CompilerPipelineValue): void => { env.logger?.debugLogIRs?.(value); }; - const hir = lower(func, env).unwrap(); + const hir = lower(func, env); log({kind: 'hir', name: 'HIR', value: hir}); pruneMaybeThrows(hir); log({kind: 'hir', name: 'PruneMaybeThrows', value: hir}); validateContextVariableLValues(hir); - validateUseMemo(hir).unwrap(); + validateUseMemo(hir); - if ( - env.enableDropManualMemoization && - !env.config.enablePreserveExistingManualUseMemo && - !env.config.disableMemoizationForDebugging && - !env.config.enableChangeDetectionForDebugging - ) { - dropManualMemoization(hir).unwrap(); + if (env.enableDropManualMemoization) { + dropManualMemoization(hir); log({kind: 'hir', name: 'DropManualMemoization', value: hir}); } @@ -208,35 +197,21 @@ function runWithEnvironment( if (env.enableValidations) { if (env.config.validateHooksUsage) { - validateHooksUsage(hir).unwrap(); + validateHooksUsage(hir); } if (env.config.validateNoCapitalizedCalls) { - validateNoCapitalizedCalls(hir).unwrap(); + validateNoCapitalizedCalls(hir); } } - if (env.config.enableFire) { - transformFire(hir); - log({kind: 'hir', name: 'TransformFire', value: hir}); - } - - if (env.config.lowerContextAccess) { - lowerContextAccess(hir, env.config.lowerContextAccess); - } - optimizePropsMethodCalls(hir); log({kind: 'hir', name: 'OptimizePropsMethodCalls', value: hir}); analyseFunctions(hir); log({kind: 'hir', name: 'AnalyseFunctions', value: hir}); - const mutabilityAliasingErrors = inferMutationAliasingEffects(hir); + inferMutationAliasingEffects(hir); log({kind: 'hir', name: 'InferMutationAliasingEffects', value: hir}); - if (env.enableValidations) { - if (mutabilityAliasingErrors.isErr()) { - throw mutabilityAliasingErrors.unwrapErr(); - } - } if (env.outputMode === 'ssr') { optimizeForSSR(hir); @@ -246,37 +221,26 @@ function runWithEnvironment( // Note: Has to come after infer reference effects because "dead" code may still affect inference deadCodeElimination(hir); log({kind: 'hir', name: 'DeadCodeElimination', value: hir}); - - if (env.config.enableInstructionReordering) { - instructionReordering(hir); - log({kind: 'hir', name: 'InstructionReordering', value: hir}); - } - pruneMaybeThrows(hir); log({kind: 'hir', name: 'PruneMaybeThrows', value: hir}); - const mutabilityAliasingRangeErrors = inferMutationAliasingRanges(hir, { + inferMutationAliasingRanges(hir, { isFunctionExpression: false, }); log({kind: 'hir', name: 'InferMutationAliasingRanges', value: hir}); if (env.enableValidations) { - if (mutabilityAliasingRangeErrors.isErr()) { - throw mutabilityAliasingRangeErrors.unwrapErr(); - } validateLocalsNotReassignedAfterRender(hir); - } - if (env.enableValidations) { if (env.config.assertValidMutableRanges) { assertValidMutableRanges(hir); } if (env.config.validateRefAccessDuringRender) { - validateNoRefAccessInRender(hir).unwrap(); + validateNoRefAccessInRender(hir); } if (env.config.validateNoSetStateInRender) { - validateNoSetStateInRender(hir).unwrap(); + validateNoSetStateInRender(hir); } if ( @@ -296,11 +260,7 @@ function runWithEnvironment( env.logErrors(validateNoJSXInTryStatement(hir)); } - if (env.config.validateNoImpureFunctionsInRender) { - validateNoImpureFunctionsInRender(hir).unwrap(); - } - - validateNoFreezingKnownMutableFunctions(hir).unwrap(); + validateNoFreezingKnownMutableFunctions(hir); } inferReactivePlaces(hir); @@ -312,7 +272,7 @@ function runWithEnvironment( env.config.validateExhaustiveEffectDependencies ) { // NOTE: this relies on reactivity inference running first - validateExhaustiveDependencies(hir).unwrap(); + validateExhaustiveDependencies(hir); } } @@ -426,6 +386,7 @@ function runWithEnvironment( }); assertTerminalSuccessorsExist(hir); assertTerminalPredsExist(hir); + propagateScopeDependenciesHIR(hir); log({ kind: 'hir', @@ -433,24 +394,6 @@ function runWithEnvironment( value: hir, }); - if (env.config.inferEffectDependencies) { - inferEffectDependencies(hir); - log({ - kind: 'hir', - name: 'InferEffectDependencies', - value: hir, - }); - } - - if (env.config.inlineJsxTransform) { - inlineJsxTransform(hir, env.config.inlineJsxTransform); - log({ - kind: 'hir', - name: 'inlineJsxTransform', - value: hir, - }); - } - const reactiveFunction = buildReactiveFunction(hir); log({ kind: 'reactive', @@ -503,15 +446,6 @@ function runWithEnvironment( value: reactiveFunction, }); - if (env.config.enableChangeDetectionForDebugging != null) { - pruneInitializationDependencies(reactiveFunction); - log({ - kind: 'reactive', - name: 'PruneInitializationDependencies', - value: reactiveFunction, - }); - } - propagateEarlyReturns(reactiveFunction); log({ kind: 'reactive', @@ -561,28 +495,24 @@ function runWithEnvironment( value: reactiveFunction, }); - if (env.config.validateMemoizedEffectDependencies) { - validateMemoizedEffectDependencies(reactiveFunction).unwrap(); - } - if ( env.config.enablePreserveExistingMemoizationGuarantees || env.config.validatePreserveExistingMemoizationGuarantees ) { - validatePreservedManualMemoization(reactiveFunction).unwrap(); + validatePreservedManualMemoization(reactiveFunction); } const ast = codegenFunction(reactiveFunction, { uniqueIdentifiers, fbtOperands, - }).unwrap(); + }); log({kind: 'ast', name: 'Codegen', value: ast}); for (const outlined of ast.outlined) { log({kind: 'ast', name: 'Codegen (outlined)', value: outlined.fn}); } if (env.config.validateSourceLocations) { - validateSourceLocations(func, ast).unwrap(); + validateSourceLocations(func, ast, env); } /** @@ -594,7 +524,10 @@ function runWithEnvironment( throw new Error('unexpected error'); } - return ast; + if (env.hasErrors()) { + return Err(env.aggregateErrors()); + } + return Ok(ast); } export function compileFn( @@ -608,7 +541,7 @@ export function compileFn( logger: Logger | null, filename: string | null, code: string | null, -): CodegenFunction { +): Result { return run( func, config, diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts index 80ce909f35aa..c8b9d38084e9 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts @@ -350,10 +350,6 @@ function isFilePartOfSources( return false; } -export type CompileProgramMetadata = { - retryErrors: Array<{fn: BabelFn; error: CompilerError}>; - inferredEffectLocations: Set; -}; /** * Main entrypoint for React Compiler. * @@ -364,7 +360,7 @@ export type CompileProgramMetadata = { export function compileProgram( program: NodePath, pass: CompilerPass, -): CompileProgramMetadata | null { +): void { /** * This is directly invoked by the react-compiler babel plugin, so exceptions * thrown by this function will fail the babel build. @@ -377,7 +373,7 @@ export function compileProgram( * the outlined functions. */ if (shouldSkipCompilation(program, pass)) { - return null; + return; } const restrictedImportsErr = validateRestrictedImports( program, @@ -385,7 +381,7 @@ export function compileProgram( ); if (restrictedImportsErr) { handleError(restrictedImportsErr, pass, null); - return null; + return; } /* * Record lint errors and critical errors as depending on Forget's config, @@ -479,16 +475,11 @@ export function compileProgram( ); handleError(error, programContext, null); } - return null; + return; } // Insert React Compiler generated functions into the Babel AST applyCompiledFunctions(program, compiledFns, pass, programContext); - - return { - retryErrors: programContext.retryErrors, - inferredEffectLocations: programContext.inferredEffectLocations, - }; } type CompileSource = { @@ -518,10 +509,6 @@ function findFunctionsToCompile( const fnType = getReactFunctionType(fn, pass); - if (pass.opts.environment.validateNoDynamicallyCreatedComponentsOrHooks) { - validateNoDynamicallyCreatedComponentsOrHooks(fn, pass, programContext); - } - if (fnType === null || programContext.alreadyCompiled.has(fn.node)) { return; } @@ -633,15 +620,7 @@ function processFn( } else { handleError(compileResult.error, programContext, fn.node.loc ?? null); } - if (outputMode === 'client') { - const retryResult = retryCompileFunction(fn, fnType, programContext); - if (retryResult == null) { - return null; - } - compiledFn = retryResult; - } else { - return null; - } + return null; } else { compiledFn = compileResult.compiledFn; } @@ -678,16 +657,6 @@ function processFn( if (programContext.hasModuleScopeOptOut) { return null; } else if (programContext.opts.outputMode === 'lint') { - /** - * inferEffectDependencies + noEmit is currently only used for linting. In - * this mode, add source locations for where the compiler *can* infer effect - * dependencies. - */ - for (const loc of compiledFn.inferredEffectLocations) { - if (loc !== GeneratedSource) { - programContext.inferredEffectLocations.add(loc); - } - } return null; } else if ( programContext.opts.compilationMode === 'annotation' && @@ -728,67 +697,37 @@ function tryCompileFunction( } try { - return { - kind: 'compile', - compiledFn: compileFn( - fn, - programContext.opts.environment, - fnType, - outputMode, - programContext, - programContext.opts.logger, - programContext.filename, - programContext.code, - ), - }; - } catch (err) { - return {kind: 'error', error: err}; - } -} - -/** - * If non-memo feature flags are enabled, retry compilation with a more minimal - * feature set. - * - * @returns a CodegenFunction if retry was successful - */ -function retryCompileFunction( - fn: BabelFn, - fnType: ReactFunctionType, - programContext: ProgramContext, -): CodegenFunction | null { - const environment = programContext.opts.environment; - if ( - !(environment.enableFire || environment.inferEffectDependencies != null) - ) { - return null; - } - /** - * Note that function suppressions are not checked in the retry pipeline, as - * they only affect auto-memoization features. - */ - try { - const retryResult = compileFn( + const result = compileFn( fn, - environment, + programContext.opts.environment, fnType, - 'client-no-memo', + outputMode, programContext, programContext.opts.logger, programContext.filename, programContext.code, ); - - if (!retryResult.hasFireRewrite && !retryResult.hasInferredEffect) { - return null; + if (result.isOk()) { + return {kind: 'compile', compiledFn: result.unwrap()}; + } else { + return {kind: 'error', error: result.unwrapErr()}; } - return retryResult; } catch (err) { - // TODO: we might want to log error here, but this will also result in duplicate logging - if (err instanceof CompilerError) { - programContext.retryErrors.push({fn, error: err}); + /** + * A pass incorrectly threw instead of recording the error. + * Log for detection in development. + */ + if ( + err instanceof CompilerError && + err.details.every(detail => detail.category !== ErrorCategory.Invariant) + ) { + programContext.logEvent({ + kind: 'CompileUnexpectedThrow', + fnLoc: fn.node.loc ?? null, + data: err.toString(), + }); } - return null; + return {kind: 'error', error: err}; } } @@ -876,84 +815,17 @@ function shouldSkipCompilation( return false; } -/** - * Validates that Components/Hooks are always defined at module level. This prevents scope reference - * errors that occur when the compiler attempts to optimize the nested component/hook while its - * parent function remains uncompiled. - */ -function validateNoDynamicallyCreatedComponentsOrHooks( - fn: BabelFn, - pass: CompilerPass, - programContext: ProgramContext, -): void { - const parentNameExpr = getFunctionName(fn); - const parentName = - parentNameExpr !== null && parentNameExpr.isIdentifier() - ? parentNameExpr.node.name - : ''; - - const validateNestedFunction = ( - nestedFn: NodePath< - t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression - >, - ): void => { - if ( - nestedFn.node === fn.node || - programContext.alreadyCompiled.has(nestedFn.node) - ) { - return; - } - - if (nestedFn.scope.getProgramParent() !== nestedFn.scope.parent) { - const nestedFnType = getReactFunctionType(nestedFn as BabelFn, pass); - const nestedFnNameExpr = getFunctionName(nestedFn as BabelFn); - const nestedName = - nestedFnNameExpr !== null && nestedFnNameExpr.isIdentifier() - ? nestedFnNameExpr.node.name - : ''; - if (nestedFnType === 'Component' || nestedFnType === 'Hook') { - CompilerError.throwDiagnostic({ - category: ErrorCategory.Factories, - reason: `Components and hooks cannot be created dynamically`, - description: `The function \`${nestedName}\` appears to be a React ${nestedFnType.toLowerCase()}, but it's defined inside \`${parentName}\`. Components and Hooks should always be declared at module scope`, - details: [ - { - kind: 'error', - message: 'this function dynamically created a component/hook', - loc: parentNameExpr?.node.loc ?? fn.node.loc ?? null, - }, - { - kind: 'error', - message: 'the component is created here', - loc: nestedFnNameExpr?.node.loc ?? nestedFn.node.loc ?? null, - }, - ], - }); - } - } - - nestedFn.skip(); - }; - - fn.traverse({ - FunctionDeclaration: validateNestedFunction, - FunctionExpression: validateNestedFunction, - ArrowFunctionExpression: validateNestedFunction, - }); -} - function getReactFunctionType( fn: BabelFn, pass: CompilerPass, ): ReactFunctionType | null { - const hookPattern = pass.opts.environment.hookPattern; if (fn.node.body.type === 'BlockStatement') { const optInDirectives = tryFindDirectiveEnablingMemoization( fn.node.body.directives, pass.opts, ); if (optInDirectives.unwrapOr(null) != null) { - return getComponentOrHookLike(fn, hookPattern) ?? 'Other'; + return getComponentOrHookLike(fn) ?? 'Other'; } } @@ -974,13 +846,13 @@ function getReactFunctionType( } case 'infer': { // Check if this is a component or hook-like function - return componentSyntaxType ?? getComponentOrHookLike(fn, hookPattern); + return componentSyntaxType ?? getComponentOrHookLike(fn); } case 'syntax': { return componentSyntaxType; } case 'all': { - return getComponentOrHookLike(fn, hookPattern) ?? 'Other'; + return getComponentOrHookLike(fn) ?? 'Other'; } default: { assertExhaustive( @@ -1022,10 +894,7 @@ function hasMemoCacheFunctionImport( return hasUseMemoCache; } -function isHookName(s: string, hookPattern: string | null): boolean { - if (hookPattern !== null) { - return new RegExp(hookPattern).test(s); - } +function isHookName(s: string): boolean { return /^use[A-Z0-9]/.test(s); } @@ -1034,16 +903,13 @@ function isHookName(s: string, hookPattern: string | null): boolean { * containing a hook name. */ -function isHook( - path: NodePath, - hookPattern: string | null, -): boolean { +function isHook(path: NodePath): boolean { if (path.isIdentifier()) { - return isHookName(path.node.name, hookPattern); + return isHookName(path.node.name); } else if ( path.isMemberExpression() && !path.node.computed && - isHook(path.get('property'), hookPattern) + isHook(path.get('property')) ) { const obj = path.get('object').node; const isPascalCaseNameSpace = /^[A-Z].*/; @@ -1184,19 +1050,18 @@ function getComponentOrHookLike( node: NodePath< t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression >, - hookPattern: string | null, ): ReactFunctionType | null { const functionName = getFunctionName(node); // Check if the name is component or hook like: if (functionName !== null && isComponentName(functionName)) { let isComponent = - callsHooksOrCreatesJsx(node, hookPattern) && + callsHooksOrCreatesJsx(node) && isValidComponentParams(node.get('params')) && !returnsNonNode(node); return isComponent ? 'Component' : null; - } else if (functionName !== null && isHook(functionName, hookPattern)) { + } else if (functionName !== null && isHook(functionName)) { // Hooks have hook invocations or JSX, but can take any # of arguments - return callsHooksOrCreatesJsx(node, hookPattern) ? 'Hook' : null; + return callsHooksOrCreatesJsx(node) ? 'Hook' : null; } /* @@ -1206,7 +1071,7 @@ function getComponentOrHookLike( if (node.isFunctionExpression() || node.isArrowFunctionExpression()) { if (isForwardRefCallback(node) || isMemoCallback(node)) { // As an added check we also look for hook invocations or JSX - return callsHooksOrCreatesJsx(node, hookPattern) ? 'Component' : null; + return callsHooksOrCreatesJsx(node) ? 'Component' : null; } } return null; @@ -1232,7 +1097,6 @@ function callsHooksOrCreatesJsx( node: NodePath< t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression >, - hookPattern: string | null, ): boolean { let invokesHooks = false; let createsJsx = false; @@ -1243,7 +1107,7 @@ function callsHooksOrCreatesJsx( }, CallExpression(call) { const callee = call.get('callee'); - if (callee.isExpression() && isHook(callee, hookPattern)) { + if (callee.isExpression() && isHook(callee)) { invokesHooks = true; } }, @@ -1324,6 +1188,13 @@ function getFunctionName( if (parent.isVariableDeclarator() && parent.get('init').node === path.node) { // const useHook = () => {}; id = parent.get('id'); + } else if ( + parent.isCallExpression() && + parent.parentPath.isVariableDeclarator() && + parent.parentPath.get('init').node === parent.node + ) { + // const MyComponent = wrap(() => {}); + id = parent.parentPath.get('id'); } else if ( parent.isAssignmentExpression() && parent.get('right').node === path.node && diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/ValidateNoUntransformedReferences.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/ValidateNoUntransformedReferences.ts deleted file mode 100644 index fab0865b54d3..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/ValidateNoUntransformedReferences.ts +++ /dev/null @@ -1,327 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {NodePath} from '@babel/core'; -import * as t from '@babel/types'; - -import {CompilerError, EnvironmentConfig, Logger} from '..'; -import {getOrInsertWith} from '../Utils/utils'; -import {Environment, GeneratedSource} from '../HIR'; -import {DEFAULT_EXPORT} from '../HIR/Environment'; -import {CompileProgramMetadata} from './Program'; -import { - CompilerDiagnostic, - CompilerDiagnosticOptions, - ErrorCategory, -} from '../CompilerError'; - -function throwInvalidReact( - options: CompilerDiagnosticOptions, - {logger, filename}: TraversalState, -): never { - logger?.logEvent(filename, { - kind: 'CompileError', - fnLoc: null, - detail: new CompilerDiagnostic(options), - }); - CompilerError.throwDiagnostic(options); -} - -function isAutodepsSigil( - arg: NodePath, -): boolean { - // Check for AUTODEPS identifier imported from React - if (arg.isIdentifier() && arg.node.name === 'AUTODEPS') { - const binding = arg.scope.getBinding(arg.node.name); - if (binding && binding.path.isImportSpecifier()) { - const importSpecifier = binding.path.node as t.ImportSpecifier; - if (importSpecifier.imported.type === 'Identifier') { - return (importSpecifier.imported as t.Identifier).name === 'AUTODEPS'; - } - } - return false; - } - - // Check for React.AUTODEPS member expression - if (arg.isMemberExpression() && !arg.node.computed) { - const object = arg.get('object'); - const property = arg.get('property'); - - if ( - object.isIdentifier() && - object.node.name === 'React' && - property.isIdentifier() && - property.node.name === 'AUTODEPS' - ) { - return true; - } - } - - return false; -} -function assertValidEffectImportReference( - autodepsIndex: number, - paths: Array>, - context: TraversalState, -): void { - for (const path of paths) { - const parent = path.parentPath; - if (parent != null && parent.isCallExpression()) { - const args = parent.get('arguments'); - const maybeCalleeLoc = path.node.loc; - const hasInferredEffect = - maybeCalleeLoc != null && - context.inferredEffectLocations.has(maybeCalleeLoc); - /** - * Error on effect calls that still have AUTODEPS in their args - */ - const hasAutodepsArg = args.some(isAutodepsSigil); - if (hasAutodepsArg && !hasInferredEffect) { - const maybeErrorDiagnostic = matchCompilerDiagnostic( - path, - context.transformErrors, - ); - /** - * Note that we cannot easily check the type of the first argument here, - * as it may have already been transformed by the compiler (and not - * memoized). - */ - throwInvalidReact( - { - category: ErrorCategory.AutomaticEffectDependencies, - reason: - 'Cannot infer dependencies of this effect. This will break your build!', - description: - 'To resolve, either pass a dependency array or fix reported compiler bailout diagnostics' + - (maybeErrorDiagnostic ? ` ${maybeErrorDiagnostic}` : ''), - details: [ - { - kind: 'error', - message: 'Cannot infer dependencies', - loc: parent.node.loc ?? GeneratedSource, - }, - ], - }, - context, - ); - } - } - } -} - -function assertValidFireImportReference( - paths: Array>, - context: TraversalState, -): void { - if (paths.length > 0) { - const maybeErrorDiagnostic = matchCompilerDiagnostic( - paths[0], - context.transformErrors, - ); - throwInvalidReact( - { - category: ErrorCategory.Fire, - reason: '[Fire] Untransformed reference to compiler-required feature.', - description: - 'Either remove this `fire` call or ensure it is successfully transformed by the compiler' + - (maybeErrorDiagnostic != null ? ` ${maybeErrorDiagnostic}` : ''), - details: [ - { - kind: 'error', - message: 'Untransformed `fire` call', - loc: paths[0].node.loc ?? GeneratedSource, - }, - ], - }, - context, - ); - } -} -export default function validateNoUntransformedReferences( - path: NodePath, - filename: string | null, - logger: Logger | null, - env: EnvironmentConfig, - compileResult: CompileProgramMetadata | null, -): void { - const moduleLoadChecks = new Map< - string, - Map - >(); - if (env.enableFire) { - /** - * Error on any untransformed references to `fire` (e.g. including non-call - * expressions) - */ - for (const module of Environment.knownReactModules) { - const react = getOrInsertWith(moduleLoadChecks, module, () => new Map()); - react.set('fire', assertValidFireImportReference); - } - } - if (env.inferEffectDependencies) { - for (const { - function: {source, importSpecifierName}, - autodepsIndex, - } of env.inferEffectDependencies) { - const module = getOrInsertWith(moduleLoadChecks, source, () => new Map()); - module.set( - importSpecifierName, - assertValidEffectImportReference.bind(null, autodepsIndex), - ); - } - } - if (moduleLoadChecks.size > 0) { - transformProgram(path, moduleLoadChecks, filename, logger, compileResult); - } -} - -type TraversalState = { - shouldInvalidateScopes: boolean; - program: NodePath; - logger: Logger | null; - filename: string | null; - transformErrors: Array<{fn: NodePath; error: CompilerError}>; - inferredEffectLocations: Set; -}; -type CheckInvalidReferenceFn = ( - paths: Array>, - context: TraversalState, -) => void; - -function validateImportSpecifier( - specifier: NodePath, - importSpecifierChecks: Map, - state: TraversalState, -): void { - const imported = specifier.get('imported'); - const specifierName: string = - imported.node.type === 'Identifier' - ? imported.node.name - : imported.node.value; - const checkFn = importSpecifierChecks.get(specifierName); - if (checkFn == null) { - return; - } - if (state.shouldInvalidateScopes) { - state.shouldInvalidateScopes = false; - state.program.scope.crawl(); - } - - const local = specifier.get('local'); - const binding = local.scope.getBinding(local.node.name); - CompilerError.invariant(binding != null, { - reason: 'Expected binding to be found for import specifier', - loc: local.node.loc ?? GeneratedSource, - }); - checkFn(binding.referencePaths, state); -} - -function validateNamespacedImport( - specifier: NodePath, - importSpecifierChecks: Map, - state: TraversalState, -): void { - if (state.shouldInvalidateScopes) { - state.shouldInvalidateScopes = false; - state.program.scope.crawl(); - } - const local = specifier.get('local'); - const binding = local.scope.getBinding(local.node.name); - const defaultCheckFn = importSpecifierChecks.get(DEFAULT_EXPORT); - - CompilerError.invariant(binding != null, { - reason: 'Expected binding to be found for import specifier', - loc: local.node.loc ?? GeneratedSource, - }); - const filteredReferences = new Map< - CheckInvalidReferenceFn, - Array> - >(); - for (const reference of binding.referencePaths) { - if (defaultCheckFn != null) { - getOrInsertWith(filteredReferences, defaultCheckFn, () => []).push( - reference, - ); - } - const parent = reference.parentPath; - if ( - parent != null && - parent.isMemberExpression() && - parent.get('object') === reference - ) { - if (parent.node.computed || parent.node.property.type !== 'Identifier') { - continue; - } - const checkFn = importSpecifierChecks.get(parent.node.property.name); - if (checkFn != null) { - getOrInsertWith(filteredReferences, checkFn, () => []).push(parent); - } - } - } - - for (const [checkFn, references] of filteredReferences) { - checkFn(references, state); - } -} -function transformProgram( - path: NodePath, - - moduleLoadChecks: Map>, - filename: string | null, - logger: Logger | null, - compileResult: CompileProgramMetadata | null, -): void { - const traversalState: TraversalState = { - shouldInvalidateScopes: true, - program: path, - filename, - logger, - transformErrors: compileResult?.retryErrors ?? [], - inferredEffectLocations: - compileResult?.inferredEffectLocations ?? new Set(), - }; - path.traverse({ - ImportDeclaration(path: NodePath) { - const importSpecifierChecks = moduleLoadChecks.get( - path.node.source.value, - ); - if (importSpecifierChecks == null) { - return; - } - const specifiers = path.get('specifiers'); - for (const specifier of specifiers) { - if (specifier.isImportSpecifier()) { - validateImportSpecifier( - specifier, - importSpecifierChecks, - traversalState, - ); - } else { - validateNamespacedImport( - specifier as NodePath< - t.ImportNamespaceSpecifier | t.ImportDefaultSpecifier - >, - importSpecifierChecks, - traversalState, - ); - } - } - }, - }); -} - -function matchCompilerDiagnostic( - badReference: NodePath, - transformErrors: Array<{fn: NodePath; error: CompilerError}>, -): string | null { - for (const {fn, error} of transformErrors) { - if (fn.isAncestor(badReference)) { - return error.toString(); - } - } - return null; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts index f43b3dd70157..8f44594c0031 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -11,10 +11,10 @@ import invariant from 'invariant'; import { CompilerDiagnostic, CompilerError, + CompilerErrorDetail, CompilerSuggestionOperation, ErrorCategory, } from '../CompilerError'; -import {Err, Ok, Result} from '../Utils/Result'; import {assertExhaustive, hasNode} from '../Utils/utils'; import {Environment} from './Environment'; import { @@ -75,7 +75,7 @@ export function lower( // Bindings captured from the outer function, in case lower() is called recursively (for lambdas) bindings: Bindings | null = null, capturedRefs: Map = new Map(), -): Result { +): HIRFunction { const builder = new HIRBuilder(env, { bindings, context: capturedRefs, @@ -106,7 +106,7 @@ export function lower( if (param.isIdentifier()) { const binding = builder.resolveIdentifier(param); if (binding.kind !== 'Identifier') { - builder.errors.pushDiagnostic( + builder.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Invariant, reason: 'Could not find binding', @@ -170,7 +170,7 @@ export function lower( 'Assignment', ); } else { - builder.errors.pushDiagnostic( + builder.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Todo, reason: `Handle ${param.node.type} parameters`, @@ -201,7 +201,7 @@ export function lower( lowerStatement(builder, body); directives = body.get('directives').map(d => d.node.value.value); } else { - builder.errors.pushDiagnostic( + builder.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Syntax, reason: `Unexpected function body kind`, @@ -218,16 +218,14 @@ export function lower( if (id != null) { const idResult = validateIdentifierName(id); if (idResult.isErr()) { - builder.errors.merge(idResult.unwrapErr()); + for (const detail of idResult.unwrapErr().details) { + builder.recordError(detail); + } } else { validatedId = idResult.unwrap().value; } } - if (builder.errors.hasAnyErrors()) { - return Err(builder.errors); - } - builder.terminate( { kind: 'return', @@ -244,23 +242,24 @@ export function lower( null, ); - return Ok({ + const hirBody = builder.build(); + + return { id: validatedId, nameHint: null, params, fnType: bindings == null ? env.fnType : 'Other', returnTypeAnnotation: null, // TODO: extract the actual return type node if present returns: createTemporaryPlace(env, func.node.loc ?? GeneratedSource), - body: builder.build(), + body: hirBody, context, generator: func.node.generator === true, async: func.node.async === true, loc: func.node.loc ?? GeneratedSource, env, - effects: null, aliasingEffects: null, directives, - }); + }; } // Helper to lower a statement @@ -281,13 +280,15 @@ function lowerStatement( * for control-flow and is generally considered an anti-pattern. we can likely * just not support this pattern, unless it really becomes necessary for some reason. */ - builder.errors.push({ - reason: - '(BuildHIR::lowerStatement) Support ThrowStatement inside of try/catch', - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + '(BuildHIR::lowerStatement) Support ThrowStatement inside of try/catch', + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); } const terminal: ThrowTerminal = { kind: 'throw', @@ -469,22 +470,26 @@ function lowerStatement( } else if (binding.path.isFunctionDeclaration()) { kind = InstructionKind.HoistedFunction; } else if (!binding.path.isVariableDeclarator()) { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: 'Unsupported declaration type for hoisting', - description: `variable "${binding.identifier.name}" declared with ${binding.path.type}`, - suggestions: null, - loc: id.parentPath.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: 'Unsupported declaration type for hoisting', + description: `variable "${binding.identifier.name}" declared with ${binding.path.type}`, + suggestions: null, + loc: id.parentPath.node.loc ?? GeneratedSource, + }), + ); continue; } else { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: 'Handle non-const declarations for hoisting', - description: `variable "${binding.identifier.name}" declared with ${binding.kind}`, - suggestions: null, - loc: id.parentPath.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: 'Handle non-const declarations for hoisting', + description: `variable "${binding.identifier.name}" declared with ${binding.kind}`, + suggestions: null, + loc: id.parentPath.node.loc ?? GeneratedSource, + }), + ); continue; } @@ -555,16 +560,42 @@ function lowerStatement( const initBlock = builder.enter('loop', _blockId => { const init = stmt.get('init'); - if (!init.isVariableDeclaration()) { - builder.errors.push({ - reason: - '(BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement', - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, + if (init.node == null) { + /* + * No init expression (e.g., `for (; ...)`), add a placeholder to avoid + * invariant about empty blocks + */ + lowerValueToTemporary(builder, { + kind: 'Primitive', + value: undefined, + loc: stmt.node.loc ?? GeneratedSource, }); return { - kind: 'unsupported', + kind: 'goto', + block: testBlock.id, + variant: GotoVariant.Break, + id: makeInstructionId(0), + loc: stmt.node.loc ?? GeneratedSource, + }; + } + if (!init.isVariableDeclaration()) { + builder.recordError( + new CompilerErrorDetail({ + reason: + '(BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement', + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); + // Lower the init expression as best-effort and continue + if (init.isExpression()) { + lowerExpressionToTemporary(builder, init as NodePath); + } + return { + kind: 'goto', + block: testBlock.id, + variant: GotoVariant.Break, id: makeInstructionId(0), loc: init.node?.loc ?? GeneratedSource, }; @@ -629,12 +660,31 @@ function lowerStatement( const test = stmt.get('test'); if (test.node == null) { - builder.errors.push({ - reason: `(BuildHIR::lowerStatement) Handle empty test in ForStatement`, - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerStatement) Handle empty test in ForStatement`, + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); + // Treat `for(;;)` as `while(true)` to keep the builder state consistent + builder.terminateWithContinuation( + { + kind: 'branch', + test: lowerValueToTemporary(builder, { + kind: 'Primitive', + value: true, + loc: stmt.node.loc ?? GeneratedSource, + }), + consequent: bodyBlock, + alternate: continuationBlock.id, + fallthrough: continuationBlock.id, + id: makeInstructionId(0), + loc: stmt.node.loc ?? GeneratedSource, + }, + continuationBlock, + ); } else { builder.terminateWithContinuation( { @@ -780,12 +830,14 @@ function lowerStatement( const testExpr = case_.get('test'); if (testExpr.node == null) { if (hasDefault) { - builder.errors.push({ - reason: `Expected at most one \`default\` branch in a switch statement, this code should have failed to parse`, - category: ErrorCategory.Syntax, - loc: case_.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected at most one \`default\` branch in a switch statement, this code should have failed to parse`, + category: ErrorCategory.Syntax, + loc: case_.node.loc ?? null, + suggestions: null, + }), + ); break; } hasDefault = true; @@ -852,16 +904,20 @@ function lowerStatement( const stmt = stmtPath as NodePath; const nodeKind: t.VariableDeclaration['kind'] = stmt.node.kind; if (nodeKind === 'var') { - builder.errors.push({ - reason: `(BuildHIR::lowerStatement) Handle ${nodeKind} kinds in VariableDeclaration`, - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); - return; + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerStatement) Handle ${nodeKind} kinds in VariableDeclaration`, + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); + // Treat `var` as `let` so references to the variable don't break } const kind = - nodeKind === 'let' ? InstructionKind.Let : InstructionKind.Const; + nodeKind === 'let' || nodeKind === 'var' + ? InstructionKind.Let + : InstructionKind.Const; for (const declaration of stmt.get('declarations')) { const id = declaration.get('id'); const init = declaration.get('init'); @@ -880,12 +936,14 @@ function lowerStatement( } else if (id.isIdentifier()) { const binding = builder.resolveIdentifier(id); if (binding.kind !== 'Identifier') { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`, - category: ErrorCategory.Invariant, - loc: id.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`, + category: ErrorCategory.Invariant, + loc: id.node.loc ?? null, + suggestions: null, + }), + ); } else { const place: Place = { effect: Effect.Unknown, @@ -897,19 +955,21 @@ function lowerStatement( if (builder.isContextIdentifier(id)) { if (kind === InstructionKind.Const) { const declRangeStart = declaration.parentPath.node.start!; - builder.errors.push({ - reason: `Expect \`const\` declaration not to be reassigned`, - category: ErrorCategory.Syntax, - loc: id.node.loc ?? null, - suggestions: [ - { - description: 'Change to a `let` declaration', - op: CompilerSuggestionOperation.Replace, - range: [declRangeStart, declRangeStart + 5], // "const".length - text: 'let', - }, - ], - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expect \`const\` declaration not to be reassigned`, + category: ErrorCategory.Syntax, + loc: id.node.loc ?? null, + suggestions: [ + { + description: 'Change to a `let` declaration', + op: CompilerSuggestionOperation.Replace, + range: [declRangeStart, declRangeStart + 5], // "const".length + text: 'let', + }, + ], + }), + ); } lowerValueToTemporary(builder, { kind: 'DeclareContext', @@ -943,13 +1003,15 @@ function lowerStatement( } } } else { - builder.errors.push({ - reason: `Expected variable declaration to be an identifier if no initializer was provided`, - description: `Got a \`${id.type}\``, - category: ErrorCategory.Syntax, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected variable declaration to be an identifier if no initializer was provided`, + description: `Got a \`${id.type}\``, + category: ErrorCategory.Syntax, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); } } return; @@ -1050,12 +1112,14 @@ function lowerStatement( const testBlock = builder.reserve('loop'); if (stmt.node.await) { - builder.errors.push({ - reason: `(BuildHIR::lowerStatement) Handle for-await loops`, - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerStatement) Handle for-await loops`, + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); return; } @@ -1278,21 +1342,25 @@ function lowerStatement( const handlerPath = stmt.get('handler'); if (!hasNode(handlerPath)) { - builder.errors.push({ - reason: `(BuildHIR::lowerStatement) Handle TryStatement without a catch clause`, - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerStatement) Handle TryStatement without a catch clause`, + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); return; } if (hasNode(stmt.get('finalizer'))) { - builder.errors.push({ - reason: `(BuildHIR::lowerStatement) Handle TryStatement with a finalizer ('finally') clause`, - category: ErrorCategory.Todo, - loc: stmt.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerStatement) Handle TryStatement with a finalizer ('finally') clause`, + category: ErrorCategory.Todo, + loc: stmt.node.loc ?? null, + suggestions: null, + }), + ); } const handlerBindingPath = handlerPath.get('param'); @@ -1379,13 +1447,15 @@ function lowerStatement( return; } case 'WithStatement': { - builder.errors.push({ - reason: `JavaScript 'with' syntax is not supported`, - description: `'with' syntax is considered deprecated and removed from JavaScript standards, consider alternatives`, - category: ErrorCategory.UnsupportedSyntax, - loc: stmtPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `JavaScript 'with' syntax is not supported`, + description: `'with' syntax is considered deprecated and removed from JavaScript standards, consider alternatives`, + category: ErrorCategory.UnsupportedSyntax, + loc: stmtPath.node.loc ?? null, + suggestions: null, + }), + ); lowerValueToTemporary(builder, { kind: 'UnsupportedNode', loc: stmtPath.node.loc ?? GeneratedSource, @@ -1399,13 +1469,15 @@ function lowerStatement( * and complex enough to support that we don't anticipate supporting anytime soon. Developers * are encouraged to lift classes out of component/hook declarations. */ - builder.errors.push({ - reason: 'Inline `class` declarations are not supported', - description: `Move class declarations outside of components/hooks`, - category: ErrorCategory.UnsupportedSyntax, - loc: stmtPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: 'Inline `class` declarations are not supported', + description: `Move class declarations outside of components/hooks`, + category: ErrorCategory.UnsupportedSyntax, + loc: stmtPath.node.loc ?? null, + suggestions: null, + }), + ); lowerValueToTemporary(builder, { kind: 'UnsupportedNode', loc: stmtPath.node.loc ?? GeneratedSource, @@ -1428,13 +1500,15 @@ function lowerStatement( case 'ImportDeclaration': case 'TSExportAssignment': case 'TSImportEqualsDeclaration': { - builder.errors.push({ - reason: - 'JavaScript `import` and `export` statements may only appear at the top level of a module', - category: ErrorCategory.Syntax, - loc: stmtPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + 'JavaScript `import` and `export` statements may only appear at the top level of a module', + category: ErrorCategory.Syntax, + loc: stmtPath.node.loc ?? null, + suggestions: null, + }), + ); lowerValueToTemporary(builder, { kind: 'UnsupportedNode', loc: stmtPath.node.loc ?? GeneratedSource, @@ -1443,13 +1517,15 @@ function lowerStatement( return; } case 'TSNamespaceExportDeclaration': { - builder.errors.push({ - reason: - 'TypeScript `namespace` statements may only appear at the top level of a module', - category: ErrorCategory.Syntax, - loc: stmtPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + 'TypeScript `namespace` statements may only appear at the top level of a module', + category: ErrorCategory.Syntax, + loc: stmtPath.node.loc ?? null, + suggestions: null, + }), + ); lowerValueToTemporary(builder, { kind: 'UnsupportedNode', loc: stmtPath.node.loc ?? GeneratedSource, @@ -1494,9 +1570,6 @@ function lowerObjectMethod( ): InstructionValue { const loc = property.node.loc ?? GeneratedSource; const loweredFunc = lowerFunction(builder, property); - if (!loweredFunc) { - return {kind: 'UnsupportedNode', node: property.node, loc: loc}; - } return { kind: 'ObjectMethod', @@ -1533,12 +1606,14 @@ function lowerObjectPropertyKey( }; } - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Expected Identifier, got ${key.type} key in ObjectExpression`, - category: ErrorCategory.Todo, - loc: key.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Expected Identifier, got ${key.type} key in ObjectExpression`, + category: ErrorCategory.Todo, + loc: key.node.loc ?? null, + suggestions: null, + }), + ); return null; } @@ -1590,12 +1665,14 @@ function lowerExpression( } const valuePath = propertyPath.get('value'); if (!valuePath.isExpression()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${valuePath.type} values in ObjectExpression`, - category: ErrorCategory.Todo, - loc: valuePath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${valuePath.type} values in ObjectExpression`, + category: ErrorCategory.Todo, + loc: valuePath.node.loc ?? null, + suggestions: null, + }), + ); continue; } const value = lowerExpressionToTemporary(builder, valuePath); @@ -1616,12 +1693,14 @@ function lowerExpression( }); } else if (propertyPath.isObjectMethod()) { if (propertyPath.node.kind !== 'method') { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${propertyPath.node.kind} functions in ObjectExpression`, - category: ErrorCategory.Todo, - loc: propertyPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${propertyPath.node.kind} functions in ObjectExpression`, + category: ErrorCategory.Todo, + loc: propertyPath.node.loc ?? null, + suggestions: null, + }), + ); continue; } const method = lowerObjectMethod(builder, propertyPath); @@ -1637,12 +1716,14 @@ function lowerExpression( key: loweredKey, }); } else { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${propertyPath.type} properties in ObjectExpression`, - category: ErrorCategory.Todo, - loc: propertyPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${propertyPath.type} properties in ObjectExpression`, + category: ErrorCategory.Todo, + loc: propertyPath.node.loc ?? null, + suggestions: null, + }), + ); continue; } } @@ -1670,12 +1751,14 @@ function lowerExpression( ); elements.push({kind: 'Spread', place}); } else { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${element.type} elements in ArrayExpression`, - category: ErrorCategory.Todo, - loc: element.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${element.type} elements in ArrayExpression`, + category: ErrorCategory.Todo, + loc: element.node.loc ?? null, + suggestions: null, + }), + ); continue; } } @@ -1689,13 +1772,15 @@ function lowerExpression( const expr = exprPath as NodePath; const calleePath = expr.get('callee'); if (!calleePath.isExpression()) { - builder.errors.push({ - reason: `Expected an expression as the \`new\` expression receiver (v8 intrinsics are not supported)`, - description: `Got a \`${calleePath.node.type}\``, - category: ErrorCategory.Syntax, - loc: calleePath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected an expression as the \`new\` expression receiver (v8 intrinsics are not supported)`, + description: `Got a \`${calleePath.node.type}\``, + category: ErrorCategory.Syntax, + loc: calleePath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } const callee = lowerExpressionToTemporary(builder, calleePath); @@ -1716,12 +1801,14 @@ function lowerExpression( const expr = exprPath as NodePath; const calleePath = expr.get('callee'); if (!calleePath.isExpression()) { - builder.errors.push({ - reason: `Expected Expression, got ${calleePath.type} in CallExpression (v8 intrinsics not supported). This error is likely caused by a bug in React Compiler. Please file an issue`, - category: ErrorCategory.Todo, - loc: calleePath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected Expression, got ${calleePath.type} in CallExpression (v8 intrinsics not supported). This error is likely caused by a bug in React Compiler. Please file an issue`, + category: ErrorCategory.Todo, + loc: calleePath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } if (calleePath.isMemberExpression()) { @@ -1750,24 +1837,28 @@ function lowerExpression( const expr = exprPath as NodePath; const leftPath = expr.get('left'); if (!leftPath.isExpression()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Expected Expression, got ${leftPath.type} lval in BinaryExpression`, - category: ErrorCategory.Todo, - loc: leftPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Expected Expression, got ${leftPath.type} lval in BinaryExpression`, + category: ErrorCategory.Todo, + loc: leftPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } const left = lowerExpressionToTemporary(builder, leftPath); const right = lowerExpressionToTemporary(builder, expr.get('right')); const operator = expr.node.operator; if (operator === '|>') { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Pipe operator not supported`, - category: ErrorCategory.Todo, - loc: leftPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Pipe operator not supported`, + category: ErrorCategory.Todo, + loc: leftPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } return { @@ -1791,12 +1882,14 @@ function lowerExpression( last = lowerExpressionToTemporary(builder, item); } if (last === null) { - builder.errors.push({ - reason: `Expected sequence expression to have at least one expression`, - category: ErrorCategory.Syntax, - loc: expr.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected sequence expression to have at least one expression`, + category: ErrorCategory.Syntax, + loc: expr.node.loc ?? null, + suggestions: null, + }), + ); } else { lowerValueToTemporary(builder, { kind: 'StoreLocal', @@ -2002,13 +2095,15 @@ function lowerExpression( * OptionalMemberExpressions as the left side of an AssignmentExpression are Stage 1 and * not supported by React Compiler yet. */ - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Unsupported syntax on the left side of an AssignmentExpression`, - description: `Expected an LVal, got: ${left.type}`, - category: ErrorCategory.Todo, - loc: left.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Unsupported syntax on the left side of an AssignmentExpression`, + description: `Expected an LVal, got: ${left.type}`, + category: ErrorCategory.Todo, + loc: left.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } } @@ -2031,12 +2126,14 @@ function lowerExpression( }; const binaryOperator = operators[operator]; if (binaryOperator == null) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${operator} operators in AssignmentExpression`, - category: ErrorCategory.Todo, - loc: expr.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${operator} operators in AssignmentExpression`, + category: ErrorCategory.Todo, + loc: expr.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } const left = expr.get('left'); @@ -2130,12 +2227,14 @@ function lowerExpression( } } default: { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Expected Identifier or MemberExpression, got ${expr.type} lval in AssignmentExpression`, - category: ErrorCategory.Todo, - loc: expr.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Expected Identifier or MemberExpression, got ${expr.type} lval in AssignmentExpression`, + category: ErrorCategory.Todo, + loc: expr.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } } @@ -2169,12 +2268,14 @@ function lowerExpression( continue; } if (!attribute.isJSXAttribute()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${attribute.type} attributes in JSXElement`, - category: ErrorCategory.Todo, - loc: attribute.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${attribute.type} attributes in JSXElement`, + category: ErrorCategory.Todo, + loc: attribute.node.loc ?? null, + suggestions: null, + }), + ); continue; } const namePath = attribute.get('name'); @@ -2182,13 +2283,15 @@ function lowerExpression( if (namePath.isJSXIdentifier()) { propName = namePath.node.name; if (propName.indexOf(':') !== -1) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Unexpected colon in attribute name \`${propName}\``, - category: ErrorCategory.Todo, - loc: namePath.node.loc ?? null, - suggestions: null, - }); - } + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Unexpected colon in attribute name \`${propName}\``, + category: ErrorCategory.Todo, + loc: namePath.node.loc ?? null, + suggestions: null, + }), + ); + } } else { CompilerError.invariant(namePath.isJSXNamespacedName(), { reason: 'Refinement', @@ -2210,22 +2313,26 @@ function lowerExpression( }); } else { if (!valueExpr.isJSXExpressionContainer()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${valueExpr.type} attribute values in JSXElement`, - category: ErrorCategory.Todo, - loc: valueExpr.node?.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${valueExpr.type} attribute values in JSXElement`, + category: ErrorCategory.Todo, + loc: valueExpr.node?.loc ?? null, + suggestions: null, + }), + ); continue; } const expression = valueExpr.get('expression'); if (!expression.isExpression()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${expression.type} expressions in JSXExpressionContainer within JSXElement`, - category: ErrorCategory.Todo, - loc: valueExpr.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${expression.type} expressions in JSXExpressionContainer within JSXElement`, + category: ErrorCategory.Todo, + loc: valueExpr.node.loc ?? null, + suggestions: null, + }), + ); continue; } value = lowerExpressionToTemporary(builder, expression); @@ -2276,18 +2383,20 @@ function lowerExpression( }); for (const [name, locations] of Object.entries(fbtLocations)) { if (locations.length > 1) { - CompilerError.throwDiagnostic({ - category: ErrorCategory.Todo, - reason: 'Support duplicate fbt tags', - description: `Support \`<${tagName}>\` tags with multiple \`<${tagName}:${name}>\` values`, - details: locations.map(loc => { - return { - kind: 'error', - message: `Multiple \`<${tagName}:${name}>\` tags found`, - loc, - }; + builder.recordError( + new CompilerDiagnostic({ + category: ErrorCategory.Todo, + reason: 'Support duplicate fbt tags', + description: `Support \`<${tagName}>\` tags with multiple \`<${tagName}:${name}>\` values`, + details: locations.map(loc => { + return { + kind: 'error' as const, + message: `Multiple \`<${tagName}:${name}>\` tags found`, + loc, + }; + }), }), - }); + ); } } } @@ -2335,13 +2444,15 @@ function lowerExpression( case 'TaggedTemplateExpression': { const expr = exprPath as NodePath; if (expr.get('quasi').get('expressions').length !== 0) { - builder.errors.push({ - reason: - '(BuildHIR::lowerExpression) Handle tagged template with interpolations', - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + '(BuildHIR::lowerExpression) Handle tagged template with interpolations', + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } CompilerError.invariant(expr.get('quasi').get('quasis').length == 1, { @@ -2351,13 +2462,15 @@ function lowerExpression( }); const value = expr.get('quasi').get('quasis').at(0)!.node.value; if (value.raw !== value.cooked) { - builder.errors.push({ - reason: - '(BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value', - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + '(BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value', + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } @@ -2374,22 +2487,26 @@ function lowerExpression( const quasis = expr.get('quasis'); if (subexprs.length !== quasis.length - 1) { - builder.errors.push({ - reason: `Unexpected quasi and subexpression lengths in template literal`, - category: ErrorCategory.Syntax, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Unexpected quasi and subexpression lengths in template literal`, + category: ErrorCategory.Syntax, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } if (subexprs.some(e => !e.isExpression())) { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle TSType in TemplateLiteral.`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle TSType in TemplateLiteral.`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } @@ -2426,8 +2543,26 @@ function lowerExpression( }; } } else { - builder.errors.push({ - reason: `Only object properties can be deleted`, + builder.recordError( + new CompilerErrorDetail({ + reason: `Only object properties can be deleted`, + category: ErrorCategory.Syntax, + loc: expr.node.loc ?? null, + suggestions: [ + { + description: 'Remove this line', + range: [expr.node.start!, expr.node.end!], + op: CompilerSuggestionOperation.Remove, + }, + ], + }), + ); + return {kind: 'UnsupportedNode', node: expr.node, loc: exprLoc}; + } + } else if (expr.node.operator === 'throw') { + builder.recordError( + new CompilerErrorDetail({ + reason: `Throw expressions are not supported`, category: ErrorCategory.Syntax, loc: expr.node.loc ?? null, suggestions: [ @@ -2437,22 +2572,8 @@ function lowerExpression( op: CompilerSuggestionOperation.Remove, }, ], - }); - return {kind: 'UnsupportedNode', node: expr.node, loc: exprLoc}; - } - } else if (expr.node.operator === 'throw') { - builder.errors.push({ - reason: `Throw expressions are not supported`, - category: ErrorCategory.Syntax, - loc: expr.node.loc ?? null, - suggestions: [ - { - description: 'Remove this line', - range: [expr.node.start!, expr.node.end!], - op: CompilerSuggestionOperation.Remove, - }, - ], - }); + }), + ); return {kind: 'UnsupportedNode', node: expr.node, loc: exprLoc}; } else { return { @@ -2562,20 +2683,24 @@ function lowerExpression( }; } if (!argument.isIdentifier()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle UpdateExpression with ${argument.type} argument`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle UpdateExpression with ${argument.type} argument`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } else if (builder.isContextIdentifier(argument)) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle UpdateExpression to variables captured within lambdas.`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle UpdateExpression to variables captured within lambdas.`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } const lvalue = lowerIdentifierForAssignment( @@ -2589,22 +2714,26 @@ function lowerExpression( * lowerIdentifierForAssignment should have already reported an error if it returned null, * we check here just in case */ - if (!builder.errors.hasAnyErrors()) { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Found an invalid UpdateExpression without a previously reported error`, - category: ErrorCategory.Invariant, - loc: exprLoc, - suggestions: null, - }); + if (!builder.environment.hasErrors()) { + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Found an invalid UpdateExpression without a previously reported error`, + category: ErrorCategory.Invariant, + loc: exprLoc, + suggestions: null, + }), + ); } return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } else if (lvalue.kind === 'Global') { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Support UpdateExpression where argument is a global`, - category: ErrorCategory.Todo, - loc: exprLoc, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Support UpdateExpression where argument is a global`, + category: ErrorCategory.Todo, + loc: exprLoc, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } const value = lowerIdentifier(builder, argument); @@ -2654,21 +2783,25 @@ function lowerExpression( }; } - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle MetaProperty expressions other than import.meta`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle MetaProperty expressions other than import.meta`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } default: { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${exprPath.type} expressions`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${exprPath.type} expressions`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; } } @@ -2958,12 +3091,14 @@ function lowerReorderableExpression( expr: NodePath, ): Place { if (!isReorderableExpression(builder, expr, true)) { - builder.errors.push({ - reason: `(BuildHIR::node.lowerReorderableExpression) Expression type \`${expr.type}\` cannot be safely reordered`, - category: ErrorCategory.Todo, - loc: expr.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::node.lowerReorderableExpression) Expression type \`${expr.type}\` cannot be safely reordered`, + category: ErrorCategory.Todo, + loc: expr.node.loc ?? null, + suggestions: null, + }), + ); } return lowerExpressionToTemporary(builder, expr); } @@ -3160,12 +3295,14 @@ function lowerArguments( } else if (argPath.isExpression()) { args.push(lowerExpressionToTemporary(builder, argPath)); } else { - builder.errors.push({ - reason: `(BuildHIR::lowerExpression) Handle ${argPath.type} arguments in CallExpression`, - category: ErrorCategory.Todo, - loc: argPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerExpression) Handle ${argPath.type} arguments in CallExpression`, + category: ErrorCategory.Todo, + loc: argPath.node.loc ?? null, + suggestions: null, + }), + ); } } return args; @@ -3195,12 +3332,14 @@ function lowerMemberExpression( } else if (propertyNode.isNumericLiteral()) { property = makePropertyLiteral(propertyNode.node.value); } else { - builder.errors.push({ - reason: `(BuildHIR::lowerMemberExpression) Handle ${propertyNode.type} property`, - category: ErrorCategory.Todo, - loc: propertyNode.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerMemberExpression) Handle ${propertyNode.type} property`, + category: ErrorCategory.Todo, + loc: propertyNode.node.loc ?? null, + suggestions: null, + }), + ); return { object, property: propertyNode.toString(), @@ -3216,12 +3355,14 @@ function lowerMemberExpression( return {object, property, value}; } else { if (!propertyNode.isExpression()) { - builder.errors.push({ - reason: `(BuildHIR::lowerMemberExpression) Expected Expression, got ${propertyNode.type} property`, - category: ErrorCategory.Todo, - loc: propertyNode.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerMemberExpression) Expected Expression, got ${propertyNode.type} property`, + category: ErrorCategory.Todo, + loc: propertyNode.node.loc ?? null, + suggestions: null, + }), + ); return { object, property: propertyNode.toString(), @@ -3274,13 +3415,15 @@ function lowerJsxElementName( const name = exprPath.node.name.name; const tag = `${namespace}:${name}`; if (namespace.indexOf(':') !== -1 || name.indexOf(':') !== -1) { - builder.errors.push({ - reason: `Expected JSXNamespacedName to have no colons in the namespace or name`, - description: `Got \`${namespace}\` : \`${name}\``, - category: ErrorCategory.Syntax, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected JSXNamespacedName to have no colons in the namespace or name`, + description: `Got \`${namespace}\` : \`${name}\``, + category: ErrorCategory.Syntax, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); } const place = lowerValueToTemporary(builder, { kind: 'Primitive', @@ -3289,12 +3432,14 @@ function lowerJsxElementName( }); return place; } else { - builder.errors.push({ - reason: `(BuildHIR::lowerJsxElementName) Handle ${exprPath.type} tags`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerJsxElementName) Handle ${exprPath.type} tags`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); return lowerValueToTemporary(builder, { kind: 'UnsupportedNode', node: exprNode, @@ -3383,12 +3528,14 @@ function lowerJsxElement( }); return place; } else { - builder.errors.push({ - reason: `(BuildHIR::lowerJsxElement) Unhandled JsxElement, got: ${exprPath.type}`, - category: ErrorCategory.Todo, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerJsxElement) Unhandled JsxElement, got: ${exprPath.type}`, + category: ErrorCategory.Todo, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); const place = lowerValueToTemporary(builder, { kind: 'UnsupportedNode', node: exprNode, @@ -3468,9 +3615,6 @@ function lowerFunctionToValue( const exprNode = expr.node; const exprLoc = exprNode.loc ?? GeneratedSource; const loweredFunc = lowerFunction(builder, expr); - if (!loweredFunc) { - return {kind: 'UnsupportedNode', node: exprNode, loc: exprLoc}; - } return { kind: 'FunctionExpression', name: loweredFunc.func.id, @@ -3489,7 +3633,7 @@ function lowerFunction( | t.FunctionDeclaration | t.ObjectMethod >, -): LoweredFunction | null { +): LoweredFunction { const componentScope: Scope = builder.environment.parentFunction.scope; const capturedContext = gatherCapturedContext(expr, componentScope); @@ -3501,19 +3645,12 @@ function lowerFunction( * This isn't a problem in practice because use Babel's scope analysis to * identify the correct references. */ - const lowering = lower( + const loweredFunc = lower( expr, builder.environment, builder.bindings, new Map([...builder.context, ...capturedContext]), ); - let loweredFunc: HIRFunction; - if (lowering.isErr()) { - const functionErrors = lowering.unwrapErr(); - builder.errors.merge(functionErrors); - return null; - } - loweredFunc = lowering.unwrap(); return { func: loweredFunc, }; @@ -3565,14 +3702,16 @@ function lowerIdentifier( } default: { if (binding.kind === 'Global' && binding.name === 'eval') { - builder.errors.push({ - reason: `The 'eval' function is not supported`, - description: - 'Eval is an anti-pattern in JavaScript, and the code executed cannot be evaluated by React Compiler', - category: ErrorCategory.UnsupportedSyntax, - loc: exprPath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `The 'eval' function is not supported`, + description: + 'Eval is an anti-pattern in JavaScript, and the code executed cannot be evaluated by React Compiler', + category: ErrorCategory.UnsupportedSyntax, + loc: exprPath.node.loc ?? null, + suggestions: null, + }), + ); } return lowerValueToTemporary(builder, { kind: 'LoadGlobal', @@ -3623,27 +3762,31 @@ function lowerIdentifierForAssignment( return {kind: 'Global', name: path.node.name}; } else { // Else its an internal error bc we couldn't find the binding - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`, - category: ErrorCategory.Invariant, - loc: path.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Could not find binding for declaration.`, + category: ErrorCategory.Invariant, + loc: path.node.loc ?? null, + suggestions: null, + }), + ); return null; } } else if ( binding.bindingKind === 'const' && kind === InstructionKind.Reassign ) { - builder.errors.push({ - reason: `Cannot reassign a \`const\` variable`, - category: ErrorCategory.Syntax, - loc: path.node.loc ?? null, - description: - binding.identifier.name != null - ? `\`${binding.identifier.name.value}\` is declared as const` - : null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Cannot reassign a \`const\` variable`, + category: ErrorCategory.Syntax, + loc: path.node.loc ?? null, + description: + binding.identifier.name != null + ? `\`${binding.identifier.name.value}\` is declared as const` + : null, + }), + ); return null; } @@ -3692,12 +3835,14 @@ function lowerAssignment( let temporary; if (builder.isContextIdentifier(lvalue)) { if (kind === InstructionKind.Const && !isHoistedIdentifier) { - builder.errors.push({ - reason: `Expected \`const\` declaration not to be reassigned`, - category: ErrorCategory.Syntax, - loc: lvalue.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Expected \`const\` declaration not to be reassigned`, + category: ErrorCategory.Syntax, + loc: lvalue.node.loc ?? null, + suggestions: null, + }), + ); } if ( @@ -3706,12 +3851,14 @@ function lowerAssignment( kind !== InstructionKind.Let && kind !== InstructionKind.Function ) { - builder.errors.push({ - reason: `Unexpected context variable kind`, - category: ErrorCategory.Syntax, - loc: lvalue.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `Unexpected context variable kind`, + category: ErrorCategory.Syntax, + loc: lvalue.node.loc ?? null, + suggestions: null, + }), + ); temporary = lowerValueToTemporary(builder, { kind: 'UnsupportedNode', node: lvalueNode, @@ -3775,24 +3922,28 @@ function lowerAssignment( loc, }); } else { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle ${property.type} properties in MemberExpression`, - category: ErrorCategory.Todo, - loc: property.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle ${property.type} properties in MemberExpression`, + category: ErrorCategory.Todo, + loc: property.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: lvalueNode, loc}; } return {kind: 'LoadLocal', place: temporary, loc: temporary.loc}; } else { if (!property.isExpression()) { - builder.errors.push({ - reason: - '(BuildHIR::lowerAssignment) Expected private name to appear as a non-computed property', - category: ErrorCategory.Todo, - loc: property.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: + '(BuildHIR::lowerAssignment) Expected private name to appear as a non-computed property', + category: ErrorCategory.Todo, + loc: property.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: lvalueNode, loc}; } const propertyPlace = lowerExpressionToTemporary(builder, property); @@ -3853,12 +4004,14 @@ function lowerAssignment( if (identifier === null) { continue; } else if (identifier.kind === 'Global') { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: - 'Expected reassignment of globals to enable forceTemporaries', - loc: element.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: + 'Expected reassignment of globals to enable forceTemporaries', + loc: element.node.loc ?? GeneratedSource, + }), + ); continue; } items.push({ @@ -3892,12 +4045,14 @@ function lowerAssignment( if (identifier === null) { continue; } else if (identifier.kind === 'Global') { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: - 'Expected reassignment of globals to enable forceTemporaries', - loc: element.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: + 'Expected reassignment of globals to enable forceTemporaries', + loc: element.node.loc ?? GeneratedSource, + }), + ); continue; } items.push(identifier); @@ -3965,12 +4120,14 @@ function lowerAssignment( if (property.isRestElement()) { const argument = property.get('argument'); if (!argument.isIdentifier()) { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle ${argument.node.type} rest element in ObjectPattern`, - category: ErrorCategory.Todo, - loc: argument.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle ${argument.node.type} rest element in ObjectPattern`, + category: ErrorCategory.Todo, + loc: argument.node.loc ?? null, + suggestions: null, + }), + ); continue; } if ( @@ -3997,12 +4154,14 @@ function lowerAssignment( if (identifier === null) { continue; } else if (identifier.kind === 'Global') { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: - 'Expected reassignment of globals to enable forceTemporaries', - loc: property.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: + 'Expected reassignment of globals to enable forceTemporaries', + loc: property.node.loc ?? GeneratedSource, + }), + ); continue; } properties.push({ @@ -4013,21 +4172,25 @@ function lowerAssignment( } else { // TODO: this should always be true given the if/else if (!property.isObjectProperty()) { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle ${property.type} properties in ObjectPattern`, - category: ErrorCategory.Todo, - loc: property.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle ${property.type} properties in ObjectPattern`, + category: ErrorCategory.Todo, + loc: property.node.loc ?? null, + suggestions: null, + }), + ); continue; } if (property.node.computed) { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern`, - category: ErrorCategory.Todo, - loc: property.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern`, + category: ErrorCategory.Todo, + loc: property.node.loc ?? null, + suggestions: null, + }), + ); continue; } const loweredKey = lowerObjectPropertyKey(builder, property); @@ -4036,12 +4199,14 @@ function lowerAssignment( } const element = property.get('value'); if (!element.isLVal()) { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Expected object property value to be an LVal, got: ${element.type}`, - category: ErrorCategory.Todo, - loc: element.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Expected object property value to be an LVal, got: ${element.type}`, + category: ErrorCategory.Todo, + loc: element.node.loc ?? null, + suggestions: null, + }), + ); continue; } if ( @@ -4059,12 +4224,14 @@ function lowerAssignment( if (identifier === null) { continue; } else if (identifier.kind === 'Global') { - builder.errors.push({ - category: ErrorCategory.Todo, - reason: - 'Expected reassignment of globals to enable forceTemporaries', - loc: element.node.loc ?? GeneratedSource, - }); + builder.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: + 'Expected reassignment of globals to enable forceTemporaries', + loc: element.node.loc ?? GeneratedSource, + }), + ); continue; } properties.push({ @@ -4208,12 +4375,14 @@ function lowerAssignment( ); } default: { - builder.errors.push({ - reason: `(BuildHIR::lowerAssignment) Handle ${lvaluePath.type} assignments`, - category: ErrorCategory.Todo, - loc: lvaluePath.node.loc ?? null, - suggestions: null, - }); + builder.recordError( + new CompilerErrorDetail({ + reason: `(BuildHIR::lowerAssignment) Handle ${lvaluePath.type} assignments`, + category: ErrorCategory.Todo, + loc: lvaluePath.node.loc ?? null, + suggestions: null, + }), + ); return {kind: 'UnsupportedNode', node: lvalueNode, loc}; } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts index 77494d0a04ea..c47a41145157 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts @@ -31,6 +31,7 @@ import { PropertyLiteral, ReactiveScopeDependency, ScopeId, + SourceLocation, TInstruction, } from './HIR'; @@ -123,9 +124,7 @@ export function collectHoistablePropertyLoads( hoistableFromOptionals, registry, nestedFnImmutableContext: null, - assumedInvokedFns: fn.env.config.enableTreatFunctionDepsAsConditional - ? new Set() - : getAssumedInvokedFunctions(fn), + assumedInvokedFns: getAssumedInvokedFunctions(fn), }); } @@ -141,9 +140,7 @@ export function collectHoistablePropertyLoadsInInnerFn( hoistableFromOptionals, registry: new PropertyPathRegistry(), nestedFnImmutableContext: null, - assumedInvokedFns: fn.env.config.enableTreatFunctionDepsAsConditional - ? new Set() - : getAssumedInvokedFunctions(fn), + assumedInvokedFns: getAssumedInvokedFunctions(fn), }; const nestedFnImmutableContext = new Set( fn.context @@ -244,6 +241,7 @@ class PropertyPathRegistry { getOrCreateIdentifier( identifier: Identifier, reactive: boolean, + loc: SourceLocation, ): PropertyPathNode { /** * Reads from a statically scoped variable are always safe in JS, @@ -260,6 +258,7 @@ class PropertyPathRegistry { identifier, reactive, path: [], + loc, }, hasOptional: false, parent: null, @@ -290,6 +289,7 @@ class PropertyPathRegistry { identifier: parent.fullPath.identifier, reactive: parent.fullPath.reactive, path: parent.fullPath.path.concat(entry), + loc: entry.loc, }, hasOptional: parent.hasOptional || entry.optional, }; @@ -304,7 +304,7 @@ class PropertyPathRegistry { * so all subpaths of a PropertyLoad should already exist * (e.g. a.b is added before a.b.c), */ - let currNode = this.getOrCreateIdentifier(n.identifier, n.reactive); + let currNode = this.getOrCreateIdentifier(n.identifier, n.reactive, n.loc); if (n.path.length === 0) { return currNode; } @@ -323,20 +323,21 @@ class PropertyPathRegistry { } function getMaybeNonNullInInstruction( - instr: InstructionValue, + value: InstructionValue, context: CollectHoistablePropertyLoadsContext, ): PropertyPathNode | null { let path: ReactiveScopeDependency | null = null; - if (instr.kind === 'PropertyLoad') { - path = context.temporaries.get(instr.object.identifier.id) ?? { - identifier: instr.object.identifier, - reactive: instr.object.reactive, + if (value.kind === 'PropertyLoad') { + path = context.temporaries.get(value.object.identifier.id) ?? { + identifier: value.object.identifier, + reactive: value.object.reactive, path: [], + loc: value.loc, }; - } else if (instr.kind === 'Destructure') { - path = context.temporaries.get(instr.value.identifier.id) ?? null; - } else if (instr.kind === 'ComputedLoad') { - path = context.temporaries.get(instr.object.identifier.id) ?? null; + } else if (value.kind === 'Destructure') { + path = context.temporaries.get(value.value.identifier.id) ?? null; + } else if (value.kind === 'ComputedLoad') { + path = context.temporaries.get(value.object.identifier.id) ?? null; } return path != null ? context.registry.getOrCreateProperty(path) : null; } @@ -393,7 +394,11 @@ function collectNonNullsInBlocks( ) { const identifier = fn.params[0].identifier; knownNonNullIdentifiers.add( - context.registry.getOrCreateIdentifier(identifier, true), + context.registry.getOrCreateIdentifier( + identifier, + true, + fn.params[0].loc, + ), ); } const nodes = new Map< @@ -468,6 +473,7 @@ function collectNonNullsInBlocks( identifier: dep.root.value.identifier, path: dep.path.slice(0, i), reactive: dep.root.value.reactive, + loc: dep.loc, }); assumedNonNullObjects.add(depNode); } @@ -654,17 +660,23 @@ function reduceMaybeOptionalChains( changed = false; for (const original of optionalChainNodes) { - let {identifier, path: origPath, reactive} = original.fullPath; + let { + identifier, + path: origPath, + reactive, + loc: origLoc, + } = original.fullPath; let currNode: PropertyPathNode = registry.getOrCreateIdentifier( identifier, reactive, + origLoc, ); for (let i = 0; i < origPath.length; i++) { const entry = origPath[i]; // If the base is known to be non-null, replace with a non-optional load const nextEntry: DependencyPathEntry = entry.optional && nodes.has(currNode) - ? {property: entry.property, optional: false} + ? {property: entry.property, optional: false, loc: entry.loc} : entry; currNode = PropertyPathRegistry.getOrCreatePropertyEntry( currNode, diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts index 75dad4c1bfe6..ece62bf56a27 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {CompilerError} from '..'; +import {CompilerError, SourceLocation} from '..'; import {assertNonNull} from './CollectHoistablePropertyLoads'; import { BlockId, @@ -169,6 +169,7 @@ function matchOptionalTestBlock( propertyId: IdentifierId; storeLocalInstr: Instruction; consequentGoto: BlockId; + propertyLoadLoc: SourceLocation; } | null { const consequentBlock = assertNonNull(blocks.get(terminal.consequent)); if ( @@ -221,6 +222,7 @@ function matchOptionalTestBlock( propertyId: propertyLoad.lvalue.identifier.id, storeLocalInstr, consequentGoto: consequentBlock.terminal.block, + propertyLoadLoc: propertyLoad.loc, }; } return null; @@ -275,7 +277,11 @@ function traverseOptionalBlock( instrVal.kind === 'PropertyLoad' && instrVal.object.identifier.id === prevInstr.lvalue.identifier.id ) { - path.push({property: instrVal.property, optional: false}); + path.push({ + property: instrVal.property, + optional: false, + loc: instrVal.loc, + }); } else { return null; } @@ -292,6 +298,7 @@ function traverseOptionalBlock( identifier: maybeTest.instructions[0].value.place.identifier, reactive: maybeTest.instructions[0].value.place.reactive, path, + loc: maybeTest.instructions[0].value.place.loc, }; test = maybeTest.terminal; } else if (maybeTest.terminal.kind === 'optional') { @@ -303,16 +310,13 @@ function traverseOptionalBlock( * - a optional base block with a separate nested optional-chain (e.g. a(c?.d)?.d) */ const testBlock = context.blocks.get(maybeTest.terminal.fallthrough)!; - if (testBlock!.terminal.kind !== 'branch') { - /** - * Fallthrough of the inner optional should be a block with no - * instructions, terminating with Test($) - */ - CompilerError.throwTodo({ - reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`, - loc: maybeTest.terminal.loc, - }); + /** + * Fallthrough of the inner optional should be a block with no + * instructions, terminating with Test($) + */ + if (testBlock.terminal.kind !== 'branch') { + return null; } /** * Recurse into inner optional blocks to collect inner optional-chain @@ -390,7 +394,7 @@ function traverseOptionalBlock( loc: optional.terminal.loc, }, ); - const load = { + const load: ReactiveScopeDependency = { identifier: baseObject.identifier, reactive: baseObject.reactive, path: [ @@ -398,8 +402,10 @@ function traverseOptionalBlock( { property: matchConsequentResult.property, optional: optional.terminal.optional, + loc: matchConsequentResult.propertyLoadLoc, }, ], + loc: matchConsequentResult.propertyLoadLoc, }; context.processedInstrsInOptional.add(matchConsequentResult.storeLocalInstr); context.processedInstrsInOptional.add(test); diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts index 7819ab39b2c6..2850e73ca5a0 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts @@ -12,6 +12,7 @@ import { Identifier, PropertyLiteral, ReactiveScopeDependency, + SourceLocation, } from '../HIR'; import {printIdentifier} from '../HIR/PrintHIR'; @@ -36,12 +37,13 @@ export class ReactiveScopeDependencyTreeHIR { * duplicates when traversing the CFG. */ constructor(hoistableObjects: Iterable) { - for (const {path, identifier, reactive} of hoistableObjects) { + for (const {path, identifier, reactive, loc} of hoistableObjects) { let currNode = ReactiveScopeDependencyTreeHIR.#getOrCreateRoot( identifier, reactive, this.#hoistableObjects, path.length > 0 && path[0].optional ? 'Optional' : 'NonNull', + loc, ); for (let i = 0; i < path.length; i++) { @@ -62,6 +64,7 @@ export class ReactiveScopeDependencyTreeHIR { nextNode = { properties: new Map(), accessType, + loc: path[i].loc, }; currNode.properties.set(path[i].property, nextNode); } @@ -75,6 +78,7 @@ export class ReactiveScopeDependencyTreeHIR { reactive: boolean, roots: Map & {reactive: boolean}>, defaultAccessType: T, + loc: SourceLocation, ): TreeNode { // roots can always be accessed unconditionally in JS let rootNode = roots.get(identifier); @@ -84,6 +88,7 @@ export class ReactiveScopeDependencyTreeHIR { properties: new Map(), reactive, accessType: defaultAccessType, + loc, }; roots.set(identifier, rootNode); } else { @@ -102,12 +107,13 @@ export class ReactiveScopeDependencyTreeHIR { * safe-to-evaluate subpath */ addDependency(dep: ReactiveScopeDependency): void { - const {identifier, reactive, path} = dep; + const {identifier, reactive, path, loc} = dep; let depCursor = ReactiveScopeDependencyTreeHIR.#getOrCreateRoot( identifier, reactive, this.#deps, PropertyAccessType.UnconditionalAccess, + loc, ); /** * hoistableCursor is null if depCursor is not an object we can hoist @@ -153,6 +159,7 @@ export class ReactiveScopeDependencyTreeHIR { depCursor, entry.property, accessType, + entry.loc, ); } else if ( hoistableCursor != null && @@ -163,6 +170,7 @@ export class ReactiveScopeDependencyTreeHIR { depCursor, entry.property, PropertyAccessType.UnconditionalAccess, + entry.loc, ); } else { /** @@ -306,6 +314,7 @@ function merge( type TreeNode = { properties: Map>; accessType: T; + loc: SourceLocation; }; type HoistableNode = TreeNode<'Optional' | 'NonNull'>; type DependencyNode = TreeNode; @@ -323,7 +332,7 @@ function collectMinimalDependenciesInSubtree( results: Set, ): void { if (isDependency(node.accessType)) { - results.add({identifier: rootIdentifier, reactive, path}); + results.add({identifier: rootIdentifier, reactive, path, loc: node.loc}); } else { for (const [childName, childNode] of node.properties) { collectMinimalDependenciesInSubtree( @@ -335,6 +344,7 @@ function collectMinimalDependenciesInSubtree( { property: childName, optional: isOptional(childNode.accessType), + loc: childNode.loc, }, ], results, @@ -362,12 +372,14 @@ function makeOrMergeProperty( node: DependencyNode, property: PropertyLiteral, accessType: PropertyAccessType, + loc: SourceLocation, ): DependencyNode { let child = node.properties.get(property); if (child == null) { child = { properties: new Map(), accessType, + loc, }; node.properties.set(property, child); } else { 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 7ceb5bf005ce..98cf1ed57d9f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts @@ -8,7 +8,12 @@ import * as t from '@babel/types'; import {ZodError, z} from 'zod/v4'; import {fromZodError} from 'zod-validation-error/v4'; -import {CompilerError} from '../CompilerError'; +import { + CompilerDiagnostic, + CompilerError, + CompilerErrorDetail, + ErrorCategory, +} from '../CompilerError'; import {CompilerOutputMode, Logger, ProgramContext} from '../Entrypoint'; import {Err, Ok, Result} from '../Utils/Result'; import { @@ -54,14 +59,6 @@ import {FlowTypeEnv} from '../Flood/Types'; import {defaultModuleTypeProvider} from './DefaultModuleTypeProvider'; import {assertExhaustive} from '../Utils/utils'; -export const ReactElementSymbolSchema = z.object({ - elementSymbol: z.union([ - z.literal('react.element'), - z.literal('react.transitional.element'), - ]), - globalDevVar: z.string(), -}); - export const ExternalFunctionSchema = z.object({ // Source for the imported module that exports the `importSpecifierName` functions source: z.string(), @@ -82,8 +79,6 @@ export const InstrumentationSchema = z ); export type ExternalFunction = z.infer; -export const USE_FIRE_FUNCTION_NAME = 'useFire'; -export const EMIT_FREEZE_GLOBAL_GATING = '__DEV__'; export const MacroSchema = z.string(); @@ -236,24 +231,9 @@ export const EnvironmentConfigSchema = z.object({ .enum(['off', 'all', 'missing-only', 'extra-only']) .default('off'), - /** - * When this is true, rather than pruning existing manual memoization but ensuring or validating - * that the memoized values remain memoized, the compiler will simply not prune existing calls to - * useMemo/useCallback. - */ - enablePreserveExistingManualUseMemo: z.boolean().default(false), - // 🌲 enableForest: z.boolean().default(false), - /** - * Enable use of type annotations in the source to drive type inference. By default - * Forget attemps to infer types using only information that is guaranteed correct - * given the source, and does not trust user-supplied type annotations. This mode - * enables trusting user type annotations. - */ - enableUseTypeAnnotations: z.boolean().default(false), - /** * Allows specifying a function that can populate HIR with type information from * Flow @@ -268,53 +248,8 @@ export const EnvironmentConfigSchema = z.object({ */ enableOptionalDependencies: z.boolean().default(true), - enableFire: z.boolean().default(false), - enableNameAnonymousFunctions: 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, - * [ - * { - * module: 'react', - * imported: 'useEffect', - * autodepsIndex: 1, - * },{ - * module: 'MyExperimentalEffectHooks', - * imported: 'useExperimentalEffect', - * autodepsIndex: 2, - * }, - * ] - * would insert dependencies for calls of `useEffect` imported from `react` and calls of - * useExperimentalEffect` from `MyExperimentalEffectHooks`. - * - * `autodepsIndex` tells the compiler which index we expect the AUTODEPS to appear in. - * With the configuration above, we'd insert dependencies for `useEffect` if it has two - * arguments, and the second is AUTODEPS. - * - * Still experimental. - */ - inferEffectDependencies: z - .nullable( - z.array( - z.object({ - function: ExternalFunctionSchema, - autodepsIndex: z.number().min(1, 'autodepsIndex must be > 0'), - }), - ), - ) - .default(null), - - /** - * Enables inlining ReactElement object literals in place of JSX - * An alternative to the standard JSX transform which replaces JSX with React's jsxProd() runtime - * Currently a prod-only optimization, requiring Fast JSX dependencies - * - * The symbol configuration is set for backwards compatability with pre-React 19 transforms - */ - inlineJsxTransform: ReactElementSymbolSchema.nullable().default(null), - /* * Enable validation of hooks to partially check that the component honors the rules of hooks. * When disabled, the component is assumed to follow the rules (though the Babel plugin looks @@ -366,16 +301,6 @@ export const EnvironmentConfigSchema = z.object({ */ validateStaticComponents: z.boolean().default(false), - /** - * Validates that the dependencies of all effect hooks are memoized. This helps ensure - * that Forget does not introduce infinite renders caused by a dependency changing, - * triggering an effect, which triggers re-rendering, which causes a dependency to change, - * triggering the effect, etc. - * - * Covers useEffect, useLayoutEffect, useInsertionEffect. - */ - validateMemoizedEffectDependencies: z.boolean().default(false), - /** * Validates that there are no capitalized calls other than those allowed by the allowlist. * Calls to capitalized functions are often functions that used to be components and may @@ -422,38 +347,8 @@ export const EnvironmentConfigSchema = z.object({ * then this flag will assume that `x` is not subusequently modified. */ enableTransitivelyFreezeFunctionExpressions: z.boolean().default(true), - - /* - * Enables codegen mutability debugging. This emits a dev-mode only to log mutations - * to values that Forget assumes are immutable (for Forget compiled code). - * For example: - * emitFreeze: { - * source: 'ReactForgetRuntime', - * importSpecifierName: 'makeReadOnly', - * } - * - * produces: - * import {makeReadOnly} from 'ReactForgetRuntime'; - * - * function Component(props) { - * if (c_0) { - * // ... - * $[0] = __DEV__ ? makeReadOnly(x) : x; - * } else { - * x = $[0]; - * } - * } - */ - enableEmitFreeze: ExternalFunctionSchema.nullable().default(null), - enableEmitHookGuards: ExternalFunctionSchema.nullable().default(null), - /** - * Enable instruction reordering. See InstructionReordering.ts for the details - * of the approach. - */ - enableInstructionReordering: z.boolean().default(false), - /** * Enables function outlinining, where anonymous functions that do not close over * local variables can be extracted into top-level helper functions. @@ -535,80 +430,12 @@ export const EnvironmentConfigSchema = z.object({ // Enable validation of mutable ranges assertValidMutableRanges: z.boolean().default(false), - /* - * Enable emitting "change variables" which store the result of whether a particular - * reactive scope dependency has changed since the scope was last executed. - * - * Ex: - * ``` - * const c_0 = $[0] !== input; // change variable - * let output; - * if (c_0) ... - * ``` - * - * Defaults to false, where the comparison is inlined: - * - * ``` - * let output; - * if ($[0] !== input) ... - * ``` - */ - enableChangeVariableCodegen: z.boolean().default(false), - - /** - * Enable emitting comments that explain Forget's output, and which - * values are being checked and which values produced by each memo block. - * - * Intended for use in demo purposes (incl playground) - */ - enableMemoizationComments: z.boolean().default(false), - /** * [TESTING ONLY] Throw an unknown exception during compilation to * simulate unexpected exceptions e.g. errors from babel functions. */ throwUnknownException__testonly: z.boolean().default(false), - /** - * Enables deps of a function epxression to be treated as conditional. This - * makes sure we don't load a dep when it's a property (to check if it has - * changed) and instead check the receiver. - * - * This makes sure we don't end up throwing when the reciver is null. Consider - * this code: - * - * ``` - * function getLength() { - * return props.bar.length; - * } - * ``` - * - * It's only safe to memoize `getLength` against props, not props.bar, as - * props.bar could be null when this `getLength` function is created. - * - * This does cause the memoization to now be coarse grained, which is - * non-ideal. - */ - enableTreatFunctionDepsAsConditional: z.boolean().default(false), - - /** - * When true, always act as though the dependencies of a memoized value - * have changed. This makes the compiler not actually perform any optimizations, - * but is useful for debugging. Implicitly also sets - * @enablePreserveExistingManualUseMemo, because otherwise memoization in the - * original source will be disabled as well. - */ - disableMemoizationForDebugging: z.boolean().default(false), - - /** - * When true, rather using memoized values, the compiler will always re-compute - * values, and then use a heuristic to compare the memoized value to the newly - * computed one. This detects cases where rules of react violations may cause the - * compiled code to behave differently than the original. - */ - enableChangeDetectionForDebugging: - ExternalFunctionSchema.nullable().default(null), - /** * The react native re-animated library uses custom Babel transforms that * requires the calls to library API remain unmodified. @@ -619,19 +446,6 @@ export const EnvironmentConfigSchema = z.object({ */ enableCustomTypeDefinitionForReanimated: z.boolean().default(false), - /** - * If specified, this value is used as a pattern for determing which global values should be - * treated as hooks. The pattern should have a single capture group, which will be used as - * the hook name for the purposes of resolving hook definitions (for builtin hooks)_. - * - * For example, by default `React$useState` would not be treated as a hook. By specifying - * `hookPattern: 'React$(\w+)'`, the compiler will treat this value equivalently to `useState()`. - * - * This setting is intended for cases where Forget is compiling code that has been prebundled - * and identifiers have been changed. - */ - hookPattern: z.string().nullable().default(null), - /** * If enabled, this will treat objects named as `ref` or if their names end with the substring `Ref`, * and contain a property named `current`, as React refs. @@ -656,28 +470,6 @@ export const EnvironmentConfigSchema = z.object({ */ enableTreatSetIdentifiersAsStateSetters: z.boolean().default(false), - /* - * If specified a value, the compiler lowers any calls to `useContext` to use - * this value as the callee. - * - * A selector function is compiled and passed as an argument along with the - * context to this function call. - * - * The compiler automatically figures out the keys by looking for the immediate - * destructuring of the return value from the useContext call. In the future, - * this can be extended to different kinds of context access like property - * loads and accesses over multiple statements as well. - * - * ``` - * // input - * const {foo, bar} = useContext(MyContext); - * - * // output - * const {foo, bar} = useCompiledContext(MyContext, (c) => [c.foo, c.bar]); - * ``` - */ - lowerContextAccess: ExternalFunctionSchema.nullable().default(null), - /** * If enabled, will validate useMemos that don't return any values: * @@ -689,13 +481,6 @@ export const EnvironmentConfigSchema = z.object({ */ validateNoVoidUseMemo: z.boolean().default(true), - /** - * Validates that Components/Hooks are always defined at module level. This prevents scope - * reference errors that occur when the compiler attempts to optimize the nested component/hook - * while its parent function remains uncompiled. - */ - validateNoDynamicallyCreatedComponentsOrHooks: z.boolean().default(false), - /** * When enabled, allows setState calls in effects based on valid patterns involving refs: * - Allow setState where the value being set is derived from a ref. This is useful where @@ -717,15 +502,6 @@ export const EnvironmentConfigSchema = z.object({ * 3. Force update / external sync - should use useSyncExternalStore */ enableVerboseNoSetStateInEffect: z.boolean().default(false), - - /** - * Enables inference of event handler types for JSX props on built-in DOM elements. - * When enabled, functions passed to event handler props (props starting with "on") - * on primitive JSX tags are inferred to have the BuiltinEventHandlerId type, which - * allows ref access within those functions since DOM event handlers are guaranteed - * by React to only execute in response to events, not during render. - */ - enableInferEventHandlers: z.boolean().default(false), }); export type EnvironmentConfig = z.infer; @@ -767,9 +543,6 @@ export class Environment { fnType: ReactFunctionType; outputMode: CompilerOutputMode; programContext: ProgramContext; - hasFireRewrite: boolean; - hasInferredEffect: boolean; - inferredEffectLocations: Set = new Set(); #contextIdentifiers: Set; #hoistedIdentifiers: Set; @@ -777,6 +550,12 @@ export class Environment { #flowTypeEnvironment: FlowTypeEnv | null; + /** + * Accumulated compilation errors. Passes record errors here instead of + * throwing, so the pipeline can continue and report all errors at once. + */ + #errors: CompilerError = new CompilerError(); + constructor( scope: BabelScope, fnType: ReactFunctionType, @@ -799,20 +578,6 @@ export class Environment { this.programContext = programContext; this.#shapes = new Map(DEFAULT_SHAPES); this.#globals = new Map(DEFAULT_GLOBALS); - this.hasFireRewrite = false; - this.hasInferredEffect = false; - - if ( - config.disableMemoizationForDebugging && - config.enableChangeDetectionForDebugging != null - ) { - CompilerError.throwInvalidConfig({ - reason: `Invalid environment config: the 'disableMemoizationForDebugging' and 'enableChangeDetectionForDebugging' options cannot be used together`, - description: null, - loc: null, - suggestions: null, - }); - } for (const [hookName, hook] of this.config.customHooks) { CompilerError.invariant(!this.#globals.has(hookName), { @@ -875,9 +640,6 @@ export class Environment { case 'ssr': { return true; } - case 'client-no-memo': { - return false; - } default: { assertExhaustive( this.outputMode, @@ -894,8 +656,7 @@ export class Environment { // linting also enables memoization so that we can check if manual memoization is preserved return true; } - case 'ssr': - case 'client-no-memo': { + case 'ssr': { return false; } default: { @@ -914,9 +675,6 @@ export class Environment { case 'ssr': { return true; } - case 'client-no-memo': { - return false; - } default: { assertExhaustive( this.outputMode, @@ -955,6 +713,52 @@ export class Environment { } } + /** + * Record a single diagnostic or error detail on this environment. + * If the error is an Invariant, it is immediately thrown since invariants + * represent internal bugs that cannot be recovered from. + * Otherwise, the error is accumulated and optionally logged. + */ + recordError(error: CompilerDiagnostic | CompilerErrorDetail): void { + if (error.category === ErrorCategory.Invariant) { + const compilerError = new CompilerError(); + if (error instanceof CompilerDiagnostic) { + compilerError.pushDiagnostic(error); + } else { + compilerError.pushErrorDetail(error); + } + throw compilerError; + } + if (error instanceof CompilerDiagnostic) { + this.#errors.pushDiagnostic(error); + } else { + this.#errors.pushErrorDetail(error); + } + } + + /** + * Record all diagnostics from a CompilerError onto this environment. + */ + recordErrors(error: CompilerError): void { + for (const detail of error.details) { + this.recordError(detail); + } + } + + /** + * Returns true if any errors have been recorded during compilation. + */ + hasErrors(): boolean { + return this.#errors.hasAnyErrors(); + } + + /** + * Returns the accumulated CompilerError containing all recorded diagnostics. + */ + aggregateErrors(): CompilerError { + return this.#errors; + } + isContextIdentifier(node: t.Identifier): boolean { return this.#contextIdentifiers.has(node); } @@ -1029,18 +833,6 @@ export class Environment { binding: NonLocalBinding, loc: SourceLocation, ): Global | null { - if (this.config.hookPattern != null) { - const match = new RegExp(this.config.hookPattern).exec(binding.name); - if ( - match != null && - typeof match[1] === 'string' && - isHookName(match[1]) - ) { - const resolvedName = match[1]; - return this.#globals.get(resolvedName) ?? this.#getCustomHookType(); - } - } - switch (binding.kind) { case 'ModuleLocal': { // don't resolve module locals 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 441b5d5452a7..faf7c9f2b72b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts @@ -9,9 +9,6 @@ import {Effect, ValueKind, ValueReason} from './HIR'; import { BUILTIN_SHAPES, BuiltInArrayId, - BuiltInAutodepsId, - BuiltInFireFunctionId, - BuiltInFireId, BuiltInMapId, BuiltInMixedReadonlyId, BuiltInObjectId, @@ -846,26 +843,6 @@ const REACT_APIS: Array<[string, BuiltInType]> = [ BuiltInUseOperatorId, ), ], - [ - 'fire', - addFunction( - DEFAULT_SHAPES, - [], - { - positionalParams: [], - restParam: null, - returnType: { - kind: 'Function', - return: {kind: 'Poly'}, - shapeId: BuiltInFireFunctionId, - isConstructor: false, - }, - calleeEffect: Effect.Read, - returnValueKind: ValueKind.Frozen, - }, - BuiltInFireId, - ), - ], [ 'useEffectEvent', addHook( @@ -887,7 +864,6 @@ const REACT_APIS: Array<[string, BuiltInType]> = [ BuiltInUseEffectEventId, ), ], - ['AUTODEPS', addObject(DEFAULT_SHAPES, BuiltInAutodepsId, [])], ]; TYPED_GLOBALS.push( diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts index dca41eac92fe..ec274b45add2 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts @@ -826,6 +826,7 @@ export type StartMemoize = { * emitting diagnostics with a suggested replacement */ depsLoc: SourceLocation | null; + hasInvalidDeps?: true; loc: SourceLocation; }; export type FinishMemoize = { @@ -1639,6 +1640,7 @@ export function makePropertyLiteral(value: string | number): PropertyLiteral { export type DependencyPathEntry = { property: PropertyLiteral; optional: boolean; + loc: SourceLocation; }; export type DependencyPath = Array; export type ReactiveScopeDependency = { @@ -1656,6 +1658,7 @@ export type ReactiveScopeDependency = { */ reactive: boolean; path: DependencyPath; + loc: SourceLocation; }; export function areEqualPaths(a: DependencyPath, b: DependencyPath): boolean { @@ -1886,12 +1889,6 @@ export function isDispatcherType(id: Identifier): boolean { return id.type.kind === 'Function' && id.type.shapeId === 'BuiltInDispatch'; } -export function isFireFunctionType(id: Identifier): boolean { - return ( - id.type.kind === 'Function' && id.type.shapeId === 'BuiltInFireFunction' - ); -} - export function isEffectEventFunctionType(id: Identifier): boolean { return ( id.type.kind === 'Function' && diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts index ab92904243c5..71874b0afb1d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts @@ -7,7 +7,12 @@ import {Binding, NodePath} from '@babel/traverse'; import * as t from '@babel/types'; -import {CompilerError, ErrorCategory} from '../CompilerError'; +import { + CompilerError, + CompilerDiagnostic, + CompilerErrorDetail, + ErrorCategory, +} from '../CompilerError'; import {Environment} from './Environment'; import { BasicBlock, @@ -110,7 +115,6 @@ export default class HIRBuilder { #bindings: Bindings; #env: Environment; #exceptionHandlerStack: Array = []; - errors: CompilerError = new CompilerError(); /** * Traversal context: counts the number of `fbt` tag parents * of the current babel node. @@ -148,6 +152,10 @@ export default class HIRBuilder { this.#current = newBlock(this.#entry, options?.entryBlockKind ?? 'block'); } + recordError(error: CompilerDiagnostic | CompilerErrorDetail): void { + this.#env.recordError(error); + } + currentBlockKind(): BlockKind { return this.#current.kind; } @@ -308,34 +316,28 @@ export default class HIRBuilder { resolveBinding(node: t.Identifier): Identifier { if (node.name === 'fbt') { - CompilerError.throwDiagnostic({ - category: ErrorCategory.Todo, - reason: 'Support local variables named `fbt`', - description: - 'Local variables named `fbt` may conflict with the fbt plugin and are not yet supported', - details: [ - { - kind: 'error', - message: 'Rename to avoid conflict with fbt plugin', - loc: node.loc ?? GeneratedSource, - }, - ], - }); + this.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.Todo, + reason: 'Support local variables named `fbt`', + description: + 'Local variables named `fbt` may conflict with the fbt plugin and are not yet supported', + loc: node.loc ?? GeneratedSource, + suggestions: null, + }), + ); } if (node.name === 'this') { - CompilerError.throwDiagnostic({ - category: ErrorCategory.UnsupportedSyntax, - reason: '`this` is not supported syntax', - description: - 'React Compiler does not support compiling functions that use `this`', - details: [ - { - kind: 'error', - message: '`this` was used here', - loc: node.loc ?? GeneratedSource, - }, - ], - }); + this.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.UnsupportedSyntax, + reason: '`this` is not supported syntax', + description: + 'React Compiler does not support compiling functions that use `this`', + loc: node.loc ?? GeneratedSource, + suggestions: null, + }), + ); } const originalName = node.name; let name = originalName; @@ -381,12 +383,15 @@ export default class HIRBuilder { instr => instr.value.kind === 'FunctionExpression', ) ) { - CompilerError.throwTodo({ - reason: `Support functions with unreachable code that may contain hoisted declarations`, - loc: block.instructions[0]?.loc ?? block.terminal.loc, - description: null, - suggestions: null, - }); + this.recordError( + new CompilerErrorDetail({ + reason: `Support functions with unreachable code that may contain hoisted declarations`, + loc: block.instructions[0]?.loc ?? block.terminal.loc, + description: null, + suggestions: null, + category: ErrorCategory.Todo, + }), + ); } } ir.blocks = rpoBlocks; 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 d104d799d726..849d73cf3642 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts @@ -383,12 +383,8 @@ export const BuiltInUseTransitionId = 'BuiltInUseTransition'; export const BuiltInUseOptimisticId = 'BuiltInUseOptimistic'; export const BuiltInSetOptimisticId = 'BuiltInSetOptimistic'; export const BuiltInStartTransitionId = 'BuiltInStartTransition'; -export const BuiltInFireId = 'BuiltInFire'; -export const BuiltInFireFunctionId = 'BuiltInFireFunction'; export const BuiltInUseEffectEventId = 'BuiltInUseEffectEvent'; export const BuiltInEffectEventId = 'BuiltInEffectEventFunction'; -export const BuiltInAutodepsId = 'BuiltInAutoDepsId'; -export const BuiltInEventHandlerId = 'BuiltInEventHandlerId'; // See getReanimatedModuleType() in Globals.ts — this is part of supporting Reanimated's ref-like types export const ReanimatedSharedValueId = 'ReanimatedSharedValueId'; @@ -1249,19 +1245,6 @@ addFunction( BuiltInEffectEventId, ); -addFunction( - BUILTIN_SHAPES, - [], - { - positionalParams: [], - restParam: Effect.ConditionallyMutate, - returnType: {kind: 'Poly'}, - calleeEffect: Effect.ConditionallyMutate, - returnValueKind: ValueKind.Mutable, - }, - BuiltInEventHandlerId, -); - /** * MixedReadOnly = * | primitive diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts index 91b7712b881f..19b4fae30fda 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts @@ -31,6 +31,7 @@ import { ObjectMethod, PropertyLiteral, convertHoistedLValueKind, + SourceLocation, } from './HIR'; import { collectHoistablePropertyLoads, @@ -298,6 +299,7 @@ function collectTemporariesSidemapImpl( value.object, value.property, false, + value.loc, temporaries, ); temporaries.set(lvalue.identifier.id, property); @@ -318,6 +320,7 @@ function collectTemporariesSidemapImpl( identifier: value.place.identifier, reactive: value.place.reactive, path: [], + loc: value.loc, }); } } else if ( @@ -339,6 +342,7 @@ function getProperty( object: Place, propertyName: PropertyLiteral, optional: boolean, + loc: SourceLocation, temporaries: ReadonlyMap, ): ReactiveScopeDependency { /* @@ -371,13 +375,18 @@ function getProperty( property = { identifier: object.identifier, reactive: object.reactive, - path: [{property: propertyName, optional}], + path: [{property: propertyName, optional, loc}], + loc, }; } else { property = { identifier: resolvedDependency.identifier, reactive: resolvedDependency.reactive, - path: [...resolvedDependency.path, {property: propertyName, optional}], + path: [ + ...resolvedDependency.path, + {property: propertyName, optional, loc}, + ], + loc, }; } return property; @@ -537,6 +546,7 @@ export class DependencyCollectionContext { identifier: place.identifier, reactive: place.reactive, path: [], + loc: place.loc, }, ); } @@ -545,11 +555,13 @@ export class DependencyCollectionContext { object: Place, property: PropertyLiteral, optional: boolean, + loc: SourceLocation, ): void { const nextDependency = getProperty( object, property, optional, + loc, this.#temporaries, ); this.visitDependency(nextDependency); @@ -602,6 +614,7 @@ export class DependencyCollectionContext { identifier: maybeDependency.identifier, reactive: maybeDependency.reactive, path: [], + loc: maybeDependency.loc, }; } if (this.#checkValidDependency(maybeDependency)) { @@ -626,6 +639,7 @@ export class DependencyCollectionContext { identifier: place.identifier, reactive: place.reactive, path: [], + loc: place.loc, }) ) { currentScope.reassignments.add(place.identifier); @@ -679,7 +693,7 @@ export function handleInstruction( return; } if (value.kind === 'PropertyLoad') { - context.visitProperty(value.object, value.property, false); + context.visitProperty(value.object, value.property, false, value.loc); } else if (value.kind === 'StoreLocal') { context.visitOperand(value.value); if (value.lvalue.kind === InstructionKind.Reassign) { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/AliasingEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/AliasingEffects.ts index 7f30e25a5c06..7d163c84734d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/AliasingEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/AliasingEffects.ts @@ -66,7 +66,11 @@ export type AliasingEffect = /** * Mutates any of the value, its direct aliases, and its transitive captures that are mutable. */ - | {kind: 'MutateTransitiveConditionally'; value: Place} + | { + kind: 'MutateTransitiveConditionally'; + value: Place; + noBackwardRangeExtension?: boolean; + } /** * Records information flow from `from` to `into` in cases where local mutation of the destination * will *not* mutate the source: diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts index 77a2bdcde596..09637dc3af18 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts @@ -54,7 +54,7 @@ function lowerWithMutationAliasing(fn: HIRFunction): void { deadCodeElimination(fn); const functionEffects = inferMutationAliasingRanges(fn, { isFunctionExpression: true, - }).unwrap(); + }); rewriteInstructionKindsBasedOnReassignment(fn); inferReactiveScopeVariables(fn); fn.aliasingEffects = functionEffects; diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts index 3f589bba9545..90acd83ea59f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts @@ -31,7 +31,6 @@ import { makeInstructionId, } from '../HIR'; import {createTemporaryPlace, markInstructionIds} from '../HIR/HIRBuilder'; -import {Result} from '../Utils/Result'; type ManualMemoCallee = { kind: 'useMemo' | 'useCallback'; @@ -74,7 +73,10 @@ export function collectMaybeMemoDependencies( return { root: object.root, // TODO: determine if the access is optional - path: [...object.path, {property: value.property, optional}], + path: [ + ...object.path, + {property: value.property, optional, loc: value.loc}, + ], loc: value.loc, }; } @@ -291,7 +293,7 @@ function extractManualMemoizationArgs( instr: TInstruction | TInstruction, kind: 'useCallback' | 'useMemo', sidemap: IdentifierSidemap, - errors: CompilerError, + env: Environment, ): { fnPlace: Place; depsList: Array | null; @@ -301,7 +303,7 @@ function extractManualMemoizationArgs( Place | SpreadPattern | undefined >; if (fnPlace == null || fnPlace.kind !== 'Identifier') { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: `Expected a callback function to be passed to ${kind}`, @@ -333,7 +335,7 @@ function extractManualMemoizationArgs( ? sidemap.maybeDepsLists.get(depsListPlace.identifier.id) : null; if (maybeDepsList == null) { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: `Expected the dependency list for ${kind} to be an array literal`, @@ -352,7 +354,7 @@ function extractManualMemoizationArgs( for (const dep of maybeDepsList.deps) { const maybeDep = sidemap.maybeDeps.get(dep.identifier.id); if (maybeDep == null) { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: `Expected the dependency list to be an array of simple expressions (e.g. \`x\`, \`x.y.z\`, \`x?.y?.z\`)`, @@ -386,10 +388,7 @@ function extractManualMemoizationArgs( * This pass also validates that useMemo callbacks return a value (not void), ensuring that useMemo * is only used for memoizing values and not for running arbitrary side effects. */ -export function dropManualMemoization( - func: HIRFunction, -): Result { - const errors = new CompilerError(); +export function dropManualMemoization(func: HIRFunction): void { const isValidationEnabled = func.env.config.validatePreserveExistingMemoizationGuarantees || func.env.config.validateNoSetStateInRender || @@ -436,7 +435,7 @@ export function dropManualMemoization( instr as TInstruction | TInstruction, manualMemo.kind, sidemap, - errors, + func.env, ); if (memoDetails == null) { @@ -464,7 +463,7 @@ export function dropManualMemoization( * is rare and likely sketchy. */ if (!sidemap.functions.has(fnPlace.identifier.id)) { - errors.pushDiagnostic( + func.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: `Expected the first argument to be an inline function expression`, @@ -549,8 +548,6 @@ export function dropManualMemoization( markInstructionIds(func.body); } } - - return errors.asResult(); } function findOptionalPlaces(fn: HIRFunction): Set { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts deleted file mode 100644 index 2997a449dead..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferEffectDependencies.ts +++ /dev/null @@ -1,675 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import * as t from '@babel/types'; -import {CompilerError, SourceLocation} from '..'; -import { - ArrayExpression, - Effect, - FunctionExpression, - GeneratedSource, - HIRFunction, - IdentifierId, - Instruction, - makeInstructionId, - TInstruction, - InstructionId, - ScopeId, - ReactiveScopeDependency, - Place, - ReactiveScope, - ReactiveScopeDependencies, - Terminal, - isUseRefType, - isSetStateType, - isFireFunctionType, - makeScopeId, - HIR, - BasicBlock, - BlockId, - isEffectEventFunctionType, -} from '../HIR'; -import {collectHoistablePropertyLoadsInInnerFn} from '../HIR/CollectHoistablePropertyLoads'; -import {collectOptionalChainSidemap} from '../HIR/CollectOptionalChainDependencies'; -import {ReactiveScopeDependencyTreeHIR} from '../HIR/DeriveMinimalDependenciesHIR'; -import {DEFAULT_EXPORT} from '../HIR/Environment'; -import { - createTemporaryPlace, - fixScopeAndIdentifierRanges, - markInstructionIds, - markPredecessors, - reversePostorderBlocks, -} from '../HIR/HIRBuilder'; -import { - collectTemporariesSidemap, - DependencyCollectionContext, - handleInstruction, -} from '../HIR/PropagateScopeDependenciesHIR'; -import {buildDependencyInstructions} from '../HIR/ScopeDependencyUtils'; -import { - eachInstructionOperand, - eachTerminalOperand, - terminalFallthrough, -} from '../HIR/visitors'; -import {empty} from '../Utils/Stack'; -import {getOrInsertWith} from '../Utils/utils'; -import {deadCodeElimination} from '../Optimization'; -import {BuiltInAutodepsId} from '../HIR/ObjectShape'; - -/** - * Infers reactive dependencies captured by useEffect lambdas and adds them as - * a second argument to the useEffect call if no dependency array is provided. - */ -export function inferEffectDependencies(fn: HIRFunction): void { - const fnExpressions = new Map< - IdentifierId, - TInstruction - >(); - - const autodepFnConfigs = new Map>(); - for (const effectTarget of fn.env.config.inferEffectDependencies!) { - const moduleTargets = getOrInsertWith( - autodepFnConfigs, - effectTarget.function.source, - () => new Map(), - ); - moduleTargets.set( - effectTarget.function.importSpecifierName, - effectTarget.autodepsIndex, - ); - } - const autodepFnLoads = new Map(); - const autodepModuleLoads = new Map>(); - - const scopeInfos = new Map(); - - const loadGlobals = new Set(); - - /** - * When inserting LoadLocals, we need to retain the reactivity of the base - * identifier, as later passes e.g. PruneNonReactiveDeps take the reactivity of - * a base identifier as the "maximal" reactivity of all its references. - * Concretely, - * reactive(Identifier i) = Union_{reference of i}(reactive(reference)) - */ - const reactiveIds = inferReactiveIdentifiers(fn); - const rewriteBlocks: Array = []; - - for (const [, block] of fn.body.blocks) { - if (block.terminal.kind === 'scope') { - const scopeBlock = fn.body.blocks.get(block.terminal.block)!; - if ( - scopeBlock.instructions.length === 1 && - scopeBlock.terminal.kind === 'goto' && - scopeBlock.terminal.block === block.terminal.fallthrough - ) { - scopeInfos.set( - block.terminal.scope.id, - block.terminal.scope.dependencies, - ); - } - } - const rewriteInstrs: Array = []; - for (const instr of block.instructions) { - const {value, lvalue} = instr; - if (value.kind === 'FunctionExpression') { - fnExpressions.set( - lvalue.identifier.id, - instr as TInstruction, - ); - } else if (value.kind === 'PropertyLoad') { - if ( - typeof value.property === 'string' && - autodepModuleLoads.has(value.object.identifier.id) - ) { - const moduleTargets = autodepModuleLoads.get( - value.object.identifier.id, - )!; - const propertyName = value.property; - const numRequiredArgs = moduleTargets.get(propertyName); - if (numRequiredArgs != null) { - autodepFnLoads.set(lvalue.identifier.id, numRequiredArgs); - } - } - } else if (value.kind === 'LoadGlobal') { - loadGlobals.add(lvalue.identifier.id); - /* - * TODO: Handle properties on default exports, like - * import React from 'react'; - * React.useEffect(...); - */ - if (value.binding.kind === 'ImportNamespace') { - const moduleTargets = autodepFnConfigs.get(value.binding.module); - if (moduleTargets != null) { - autodepModuleLoads.set(lvalue.identifier.id, moduleTargets); - } - } - if ( - value.binding.kind === 'ImportSpecifier' || - value.binding.kind === 'ImportDefault' - ) { - const moduleTargets = autodepFnConfigs.get(value.binding.module); - if (moduleTargets != null) { - const importSpecifierName = - value.binding.kind === 'ImportSpecifier' - ? value.binding.imported - : DEFAULT_EXPORT; - const numRequiredArgs = moduleTargets.get(importSpecifierName); - if (numRequiredArgs != null) { - autodepFnLoads.set(lvalue.identifier.id, numRequiredArgs); - } - } - } - } else if ( - value.kind === 'CallExpression' || - value.kind === 'MethodCall' - ) { - const callee = - value.kind === 'CallExpression' ? value.callee : value.property; - - const autodepsArgIndex = value.args.findIndex( - arg => - arg.kind === 'Identifier' && - arg.identifier.type.kind === 'Object' && - arg.identifier.type.shapeId === BuiltInAutodepsId, - ); - const autodepsArgExpectedIndex = autodepFnLoads.get( - callee.identifier.id, - ); - - if ( - value.args.length > 0 && - autodepsArgExpectedIndex != null && - autodepsArgIndex === autodepsArgExpectedIndex && - autodepFnLoads.has(callee.identifier.id) && - value.args[0].kind === 'Identifier' - ) { - // We have a useEffect call with no deps array, so we need to infer the deps - const effectDeps: Array = []; - const deps: ArrayExpression = { - kind: 'ArrayExpression', - elements: effectDeps, - loc: GeneratedSource, - }; - const depsPlace = createTemporaryPlace(fn.env, GeneratedSource); - depsPlace.effect = Effect.Read; - - const fnExpr = fnExpressions.get(value.args[0].identifier.id); - if (fnExpr != null) { - // We have a function expression, so we can infer its dependencies - const scopeInfo = - fnExpr.lvalue.identifier.scope != null - ? scopeInfos.get(fnExpr.lvalue.identifier.scope.id) - : null; - let minimalDeps: Set; - if (scopeInfo != null) { - minimalDeps = new Set(scopeInfo); - } else { - minimalDeps = inferMinimalDependencies(fnExpr); - } - /** - * Step 1: push dependencies to the effect deps array - * - * Note that it's invalid to prune all non-reactive deps in this pass, see - * the `infer-effect-deps/pruned-nonreactive-obj` fixture for an - * explanation. - */ - - const usedDeps = []; - for (const maybeDep of minimalDeps) { - if ( - ((isUseRefType(maybeDep.identifier) || - isSetStateType(maybeDep.identifier)) && - !reactiveIds.has(maybeDep.identifier.id)) || - isFireFunctionType(maybeDep.identifier) || - isEffectEventFunctionType(maybeDep.identifier) - ) { - // exclude non-reactive hook results, which will never be in a memo block - continue; - } - - const dep = truncateDepAtCurrent(maybeDep); - const {place, value, exitBlockId} = buildDependencyInstructions( - dep, - fn.env, - ); - rewriteInstrs.push({ - kind: 'block', - location: instr.id, - value, - exitBlockId: exitBlockId, - }); - effectDeps.push(place); - usedDeps.push(dep); - } - - // For LSP autodeps feature. - const decorations: Array = []; - for (const loc of collectDepUsages(usedDeps, fnExpr.value)) { - if (typeof loc === 'symbol') { - continue; - } - decorations.push(loc); - } - if (typeof value.loc !== 'symbol') { - fn.env.logger?.logEvent(fn.env.filename, { - kind: 'AutoDepsDecorations', - fnLoc: value.loc, - decorations, - }); - } - - // Step 2: push the inferred deps array as an argument of the useEffect - rewriteInstrs.push({ - kind: 'instr', - location: instr.id, - value: { - id: makeInstructionId(0), - loc: GeneratedSource, - lvalue: {...depsPlace, effect: Effect.Mutate}, - value: deps, - effects: null, - }, - }); - value.args[autodepsArgIndex] = { - ...depsPlace, - effect: Effect.Freeze, - }; - fn.env.inferredEffectLocations.add(callee.loc); - } else if (loadGlobals.has(value.args[0].identifier.id)) { - // Global functions have no reactive dependencies, so we can insert an empty array - rewriteInstrs.push({ - kind: 'instr', - location: instr.id, - value: { - id: makeInstructionId(0), - loc: GeneratedSource, - lvalue: {...depsPlace, effect: Effect.Mutate}, - value: deps, - effects: null, - }, - }); - value.args[autodepsArgIndex] = { - ...depsPlace, - effect: Effect.Freeze, - }; - fn.env.inferredEffectLocations.add(callee.loc); - } - } else if ( - value.args.length >= 2 && - value.args.length - 1 === autodepFnLoads.get(callee.identifier.id) && - value.args[0] != null && - value.args[0].kind === 'Identifier' - ) { - const penultimateArg = value.args[value.args.length - 2]; - const depArrayArg = value.args[value.args.length - 1]; - if ( - depArrayArg.kind !== 'Spread' && - penultimateArg.kind !== 'Spread' && - typeof depArrayArg.loc !== 'symbol' && - typeof penultimateArg.loc !== 'symbol' && - typeof value.loc !== 'symbol' - ) { - fn.env.logger?.logEvent(fn.env.filename, { - kind: 'AutoDepsEligible', - fnLoc: value.loc, - depArrayLoc: { - ...depArrayArg.loc, - start: penultimateArg.loc.end, - end: depArrayArg.loc.end, - }, - }); - } - } - } - } - rewriteSplices(block, rewriteInstrs, rewriteBlocks); - } - - if (rewriteBlocks.length > 0) { - for (const block of rewriteBlocks) { - fn.body.blocks.set(block.id, block); - } - - /** - * Fixup the HIR to restore RPO, ensure correct predecessors, and renumber - * instructions. - */ - reversePostorderBlocks(fn.body); - markPredecessors(fn.body); - // Renumber instructions and fix scope ranges - markInstructionIds(fn.body); - fixScopeAndIdentifierRanges(fn.body); - deadCodeElimination(fn); - - fn.env.hasInferredEffect = true; - } -} - -function truncateDepAtCurrent( - dep: ReactiveScopeDependency, -): ReactiveScopeDependency { - const idx = dep.path.findIndex(path => path.property === 'current'); - if (idx === -1) { - return dep; - } else { - return {...dep, path: dep.path.slice(0, idx)}; - } -} - -type SpliceInfo = - | {kind: 'instr'; location: InstructionId; value: Instruction} - | { - kind: 'block'; - location: InstructionId; - value: HIR; - exitBlockId: BlockId; - }; - -function rewriteSplices( - originalBlock: BasicBlock, - splices: Array, - rewriteBlocks: Array, -): void { - if (splices.length === 0) { - return; - } - /** - * Splice instructions or value blocks into the original block. - * --- original block --- - * bb_original - * instr1 - * ... - * instr2 <-- splice location - * instr3 - * ... - * - * - * If there is more than one block in the splice, this means that we're - * splicing in a set of value-blocks of the following structure: - * --- blocks we're splicing in --- - * bb_entry: - * instrEntry - * ... - * fallthrough=bb_exit - * - * bb1(value): - * ... - * - * bb_exit: - * instrExit - * ... - * - * - * - * --- rewritten blocks --- - * bb_original - * instr1 - * ... (original instructions) - * instr2 - * instrEntry - * ... (spliced instructions) - * fallthrough=bb_exit - * - * bb1(value): - * ... - * - * bb_exit: - * instrExit - * ... (spliced instructions) - * instr3 - * ... (original instructions) - * - */ - const originalInstrs = originalBlock.instructions; - let currBlock: BasicBlock = {...originalBlock, instructions: []}; - rewriteBlocks.push(currBlock); - - let cursor = 0; - - for (const rewrite of splices) { - while (originalInstrs[cursor].id < rewrite.location) { - CompilerError.invariant( - originalInstrs[cursor].id < originalInstrs[cursor + 1].id, - { - reason: - '[InferEffectDependencies] Internal invariant broken: expected block instructions to be sorted', - loc: originalInstrs[cursor].loc, - }, - ); - currBlock.instructions.push(originalInstrs[cursor]); - cursor++; - } - CompilerError.invariant(originalInstrs[cursor].id === rewrite.location, { - reason: - '[InferEffectDependencies] Internal invariant broken: splice location not found', - loc: originalInstrs[cursor].loc, - }); - - if (rewrite.kind === 'instr') { - currBlock.instructions.push(rewrite.value); - } else if (rewrite.kind === 'block') { - const {entry, blocks} = rewrite.value; - const entryBlock = blocks.get(entry)!; - // splice in all instructions from the entry block - currBlock.instructions.push(...entryBlock.instructions); - if (blocks.size > 1) { - /** - * We're splicing in a set of value-blocks, which means we need - * to push new blocks and update terminals. - */ - CompilerError.invariant( - terminalFallthrough(entryBlock.terminal) === rewrite.exitBlockId, - { - reason: - '[InferEffectDependencies] Internal invariant broken: expected entry block to have a fallthrough', - loc: entryBlock.terminal.loc, - }, - ); - const originalTerminal = currBlock.terminal; - currBlock.terminal = entryBlock.terminal; - - for (const [id, block] of blocks) { - if (id === entry) { - continue; - } - if (id === rewrite.exitBlockId) { - block.terminal = originalTerminal; - currBlock = block; - } - rewriteBlocks.push(block); - } - } - } - } - currBlock.instructions.push(...originalInstrs.slice(cursor)); -} - -function inferReactiveIdentifiers(fn: HIRFunction): Set { - const reactiveIds: Set = new Set(); - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - /** - * No need to traverse into nested functions as - * 1. their effects are recorded in `LoweredFunction.dependencies` - * 2. we don't mark `reactive` in these anyways - */ - for (const place of eachInstructionOperand(instr)) { - if (place.reactive) { - reactiveIds.add(place.identifier.id); - } - } - } - - for (const place of eachTerminalOperand(block.terminal)) { - if (place.reactive) { - reactiveIds.add(place.identifier.id); - } - } - } - return reactiveIds; -} - -function collectDepUsages( - deps: Array, - fnExpr: FunctionExpression, -): Array { - const identifiers: Map = new Map(); - const loadedDeps: Set = new Set(); - const sourceLocations = []; - for (const dep of deps) { - identifiers.set(dep.identifier.id, dep); - } - - for (const [, block] of fnExpr.loweredFunc.func.body.blocks) { - for (const instr of block.instructions) { - if ( - instr.value.kind === 'LoadLocal' && - identifiers.has(instr.value.place.identifier.id) - ) { - loadedDeps.add(instr.lvalue.identifier.id); - } - for (const place of eachInstructionOperand(instr)) { - if (loadedDeps.has(place.identifier.id)) { - // TODO(@jbrown215): handle member exprs!! - sourceLocations.push(place.identifier.loc); - } - } - } - } - - return sourceLocations; -} - -function inferMinimalDependencies( - fnInstr: TInstruction, -): Set { - const fn = fnInstr.value.loweredFunc.func; - - const temporaries = collectTemporariesSidemap(fn, new Set()); - const { - hoistableObjects, - processedInstrsInOptional, - temporariesReadInOptional, - } = collectOptionalChainSidemap(fn); - - const hoistablePropertyLoads = collectHoistablePropertyLoadsInInnerFn( - fnInstr, - temporaries, - hoistableObjects, - ); - const hoistableToFnEntry = hoistablePropertyLoads.get(fn.body.entry); - CompilerError.invariant(hoistableToFnEntry != null, { - reason: - '[InferEffectDependencies] Internal invariant broken: missing entry block', - loc: fnInstr.loc, - }); - - const dependencies = inferDependencies( - fnInstr, - new Map([...temporaries, ...temporariesReadInOptional]), - processedInstrsInOptional, - ); - - const tree = new ReactiveScopeDependencyTreeHIR( - [...hoistableToFnEntry.assumedNonNullObjects].map(o => o.fullPath), - ); - for (const dep of dependencies) { - tree.addDependency({...dep}); - } - - return tree.deriveMinimalDependencies(); -} - -function inferDependencies( - fnInstr: TInstruction, - temporaries: ReadonlyMap, - processedInstrsInOptional: ReadonlySet, -): Set { - const fn = fnInstr.value.loweredFunc.func; - const context = new DependencyCollectionContext( - new Set(), - temporaries, - processedInstrsInOptional, - ); - for (const dep of fn.context) { - context.declare(dep.identifier, { - id: makeInstructionId(0), - scope: empty(), - }); - } - const placeholderScope: ReactiveScope = { - id: makeScopeId(0), - range: { - start: fnInstr.id, - end: makeInstructionId(fnInstr.id + 1), - }, - dependencies: new Set(), - reassignments: new Set(), - declarations: new Map(), - earlyReturnValue: null, - merged: new Set(), - loc: GeneratedSource, - }; - context.enterScope(placeholderScope); - inferDependenciesInFn(fn, context, temporaries); - context.exitScope(placeholderScope, false); - const resultUnfiltered = context.deps.get(placeholderScope); - CompilerError.invariant(resultUnfiltered != null, { - reason: - '[InferEffectDependencies] Internal invariant broken: missing scope dependencies', - loc: fn.loc, - }); - - const fnContext = new Set(fn.context.map(dep => dep.identifier.id)); - const result = new Set(); - for (const dep of resultUnfiltered) { - if (fnContext.has(dep.identifier.id)) { - result.add(dep); - } - } - - return result; -} - -function inferDependenciesInFn( - fn: HIRFunction, - context: DependencyCollectionContext, - temporaries: ReadonlyMap, -): void { - for (const [, block] of fn.body.blocks) { - // Record referenced optional chains in phis - for (const phi of block.phis) { - for (const operand of phi.operands) { - const maybeOptionalChain = temporaries.get(operand[1].identifier.id); - if (maybeOptionalChain) { - context.visitDependency(maybeOptionalChain); - } - } - } - for (const instr of block.instructions) { - if ( - instr.value.kind === 'FunctionExpression' || - instr.value.kind === 'ObjectMethod' - ) { - context.declare(instr.lvalue.identifier, { - id: instr.id, - scope: context.currentScope, - }); - /** - * Recursively visit the inner function to extract dependencies - */ - const innerFn = instr.value.loweredFunc.func; - context.enterInnerFn(instr as TInstruction, () => { - inferDependenciesInFn(innerFn, context, temporaries); - }); - } else { - handleInstruction(instr, context); - } - } - } -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts index 0fb2cf9823c7..bf3d1e815d14 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts @@ -45,7 +45,7 @@ import { eachTerminalOperand, eachTerminalSuccessor, } from '../HIR/visitors'; -import {Ok, Result} from '../Utils/Result'; + import { assertExhaustive, getOrInsertDefault, @@ -100,7 +100,7 @@ export function inferMutationAliasingEffects( {isFunctionExpression}: {isFunctionExpression: boolean} = { isFunctionExpression: false, }, -): Result { +): void { const initialState = InferenceState.empty(fn.env, isFunctionExpression); // Map of blocks to the last (merged) incoming state that was processed @@ -220,7 +220,7 @@ export function inferMutationAliasingEffects( } } } - return Ok(undefined); + return; } function findHoistedContextDeclarations( @@ -1126,12 +1126,29 @@ function applyEffect( } const operand = arg.kind === 'Identifier' ? arg : arg.place; if (operand !== effect.function || effect.mutatesFunction) { + /* + * For the receiver of a MethodCall (where receiver !== function), + * use noBackwardRangeExtension so that the receiver's mutable + * range is not extended backward through aliases. Without this, + * values like `expensiveProcessing(data)` get pulled into the + * same reactive scope as the method call result, preventing + * independent memoization. The receiver itself and any values + * it was captured into are still marked as mutated (forward + * propagation is preserved), maintaining correctness for + * patterns like `s.add(arr); arr.push(x)`. Known mutating + * methods (e.g. Array.push) have explicit aliasing signatures + * that bypass this default path entirely. + */ + const isMethodReceiver = + operand === effect.receiver && + effect.receiver !== effect.function; applyEffect( context, state, { kind: 'MutateTransitiveConditionally', value: operand, + noBackwardRangeExtension: isMethodReceiver, }, initialized, effects, diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts index b8c5eeaa8e23..4016b6480eb2 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingRanges.ts @@ -20,13 +20,14 @@ import { Place, isPrimitiveType, } from '../HIR/HIR'; +import {Environment} from '../HIR/Environment'; import { eachInstructionLValue, eachInstructionValueOperand, eachTerminalOperand, } from '../HIR/visitors'; import {assertExhaustive, getOrInsertWith} from '../Utils/utils'; -import {Err, Ok, Result} from '../Utils/Result'; + import {AliasingEffect, MutationReason} from './AliasingEffects'; /** @@ -74,7 +75,7 @@ import {AliasingEffect, MutationReason} from './AliasingEffects'; export function inferMutationAliasingRanges( fn: HIRFunction, {isFunctionExpression}: {isFunctionExpression: boolean}, -): Result, CompilerError> { +): Array { // The set of externally-visible effects const functionEffects: Array = []; @@ -102,12 +103,13 @@ export function inferMutationAliasingRanges( kind: MutationKind; place: Place; reason: MutationReason | null; + noBackwardRangeExtension: boolean; }> = []; const renders: Array<{index: number; place: Place}> = []; let index = 0; - const errors = new CompilerError(); + const shouldRecordErrors = !isFunctionExpression && fn.env.enableValidations; for (const param of [...fn.params, ...fn.context, fn.returns]) { const place = param.kind === 'Identifier' ? param : param.place; @@ -179,6 +181,9 @@ export function inferMutationAliasingRanges( : MutationKind.Conditional, reason: null, place: effect.value, + noBackwardRangeExtension: + effect.kind === 'MutateTransitiveConditionally' && + (effect.noBackwardRangeExtension ?? false), }); } else if ( effect.kind === 'Mutate' || @@ -194,13 +199,16 @@ export function inferMutationAliasingRanges( : MutationKind.Conditional, reason: effect.kind === 'Mutate' ? (effect.reason ?? null) : null, place: effect.value, + noBackwardRangeExtension: false, }); } else if ( effect.kind === 'MutateFrozen' || effect.kind === 'MutateGlobal' || effect.kind === 'Impure' ) { - errors.pushDiagnostic(effect.error); + if (shouldRecordErrors) { + fn.env.recordError(effect.error); + } functionEffects.push(effect); } else if (effect.kind === 'Render') { renders.push({index: index++, place: effect.place}); @@ -226,6 +234,9 @@ export function inferMutationAliasingRanges( for (const effect of block.terminal.effects) { if (effect.kind === 'Alias') { state.assign(index++, effect.from, effect.into); + if (block.terminal.kind === 'maybe-throw') { + state.catchParams.add(effect.into.identifier); + } } else { CompilerError.invariant(effect.kind === 'Freeze', { reason: `Unexpected '${effect.kind}' effect for MaybeThrow terminal`, @@ -245,11 +256,16 @@ export function inferMutationAliasingRanges( mutation.kind, mutation.place.loc, mutation.reason, - errors, + shouldRecordErrors ? fn.env : null, + mutation.noBackwardRangeExtension, ); } for (const render of renders) { - state.render(render.index, render.place.identifier, errors); + state.render( + render.index, + render.place.identifier, + shouldRecordErrors ? fn.env : null, + ); } for (const param of [...fn.context, ...fn.params]) { const place = param.kind === 'Identifier' ? param : param.place; @@ -498,7 +514,6 @@ export function inferMutationAliasingRanges( * would be transitively mutated needs a capture relationship. */ const tracked: Array = []; - const ignoredErrors = new CompilerError(); for (const param of [...fn.params, ...fn.context, fn.returns]) { const place = param.kind === 'Identifier' ? param : param.place; tracked.push(place); @@ -513,7 +528,7 @@ export function inferMutationAliasingRanges( MutationKind.Conditional, into.loc, null, - ignoredErrors, + null, ); for (const from of tracked) { if ( @@ -547,19 +562,17 @@ export function inferMutationAliasingRanges( } } - if (errors.hasAnyErrors() && !isFunctionExpression) { - return Err(errors); - } - return Ok(functionEffects); + return functionEffects; } -function appendFunctionErrors(errors: CompilerError, fn: HIRFunction): void { +function appendFunctionErrors(env: Environment | null, fn: HIRFunction): void { + if (env == null) return; for (const effect of fn.aliasingEffects ?? []) { switch (effect.kind) { case 'Impure': case 'MutateFrozen': case 'MutateGlobal': { - errors.pushDiagnostic(effect.error); + env.recordError(effect.error); break; } } @@ -594,6 +607,13 @@ type Node = { }; class AliasingState { nodes: Map = new Map(); + /* + * Identifiers that are catch handler bindings. These are tracked + * so the backward-alias heuristic in mutate() can stop walking + * through catch parameter aliases, which would otherwise incorrectly + * match the "function call result" pattern and skip range extension. + */ + catchParams: Set = new Set(); create(place: Place, value: Node['value']): void { this.nodes.set(place.identifier, { @@ -660,7 +680,7 @@ class AliasingState { } } - render(index: number, start: Identifier, errors: CompilerError): void { + render(index: number, start: Identifier, env: Environment | null): void { const seen = new Set(); const queue: Array = [start]; while (queue.length !== 0) { @@ -674,7 +694,7 @@ class AliasingState { continue; } if (node.value.kind === 'Function') { - appendFunctionErrors(errors, node.value.function); + appendFunctionErrors(env, node.value.function); } for (const [alias, when] of node.createdFrom) { if (when >= index) { @@ -706,17 +726,100 @@ class AliasingState { startKind: MutationKind, loc: SourceLocation, reason: MutationReason | null, - errors: CompilerError, + env: Environment | null, + noBackwardRangeExtension: boolean = false, ): void { + /* + * Only skip backward-alias range extension when ALL of these hold: + * 1. The effect requested noBackwardRangeExtension (MethodCall receiver) + * 2. The start node is not a PropertyLoad result (no createdFrom edges), + * since PropertyLoad receivers need backward range extension for + * correctness (e.g. `x.y.push(val)` must extend x and y). + * 3. At least one of the start's backward alias targets was produced by + * a function call (indicated by having maybeAliases from the default + * Apply handler). This distinguishes `expensiveProcessing(data).map(fn)` + * from `localArray.push(val)` — only the former should skip, since the + * call result represents a separately-memoizable value. + */ + const startNode = this.nodes.get(start); + let effectiveSkipAliases = false; + if ( + noBackwardRangeExtension && + startNode != null && + startNode.createdFrom.size === 0 + ) { + /* + * Walk backward through aliases from the start node to check if + * any value in the chain was produced by a function call (Apply). + * Apply results are identifiable by having maybeAliases, since the + * default Apply handler records MaybeAlias from each argument to + * the result. Only walk through alias edges (not maybeAliases or + * captures). Additionally, stop walking when a catch parameter is + * reached, since catch parameters are aliased to call results via + * terminal Alias effects and would otherwise incorrectly trigger + * this heuristic. + */ + const visited = new Set(); + const aliasQueue: Array = [start]; + while (aliasQueue.length > 0) { + const id = aliasQueue.pop()!; + if (visited.has(id)) { + continue; + } + visited.add(id); + const node = this.nodes.get(id); + if (node == null) { + continue; + } + /* + * Stop walking through catch parameter aliases. Catch bindings + * are aliased to call results in the try block, and those call + * results have maybeAliases from the Apply handler. Without + * this check, the heuristic would incorrectly treat catch + * parameters as independently-memoizable function call results, + * preventing backward range extension and causing scope boundary + * issues (e.g. "Expected a break target" in try-catch blocks). + */ + if (this.catchParams.has(id)) { + continue; + } + if (node.maybeAliases.size > 0) { + effectiveSkipAliases = true; + break; + } + for (const [nextAliasId] of node.aliases) { + aliasQueue.push(nextAliasId); + } + } + } const seen = new Map(); const queue: Array<{ place: Identifier; transitive: boolean; direction: 'backwards' | 'forwards'; kind: MutationKind; - }> = [{place: start, transitive, direction: 'backwards', kind: startKind}]; + /* + * When true, skip extending the mutable range for this node. This + * is set for nodes reached via backward alias/maybeAlias edges when + * effectiveSkipAliases is enabled, so that the receiver's backward + * aliases are not pulled into the same reactive scope as the method + * call. Nodes reached via capture edges reset this flag, since + * captures represent structural containment (e.g. function closures) + * where range extension is required for correctness. + */ + skipRangeExtension: boolean; + }> = [ + { + place: start, + transitive, + direction: 'backwards', + kind: startKind, + skipRangeExtension: false, + }, + ]; while (queue.length !== 0) { - const {place: current, transitive, direction, kind} = queue.pop()!; + const {place: current, transitive, direction, kind, skipRangeExtension} = + queue.pop()!; const previousKind = seen.get(current); if (previousKind != null && previousKind >= kind) { continue; @@ -728,7 +831,7 @@ class AliasingState { } node.mutationReason ??= reason; node.lastMutated = Math.max(node.lastMutated, index); - if (end != null) { + if (end != null && !skipRangeExtension) { node.id.mutableRange.end = makeInstructionId( Math.max(node.id.mutableRange.end, end), ); @@ -738,7 +841,7 @@ class AliasingState { node.transitive == null && node.local == null ) { - appendFunctionErrors(errors, node.value.function); + appendFunctionErrors(env, node.value.function); } if (transitive) { if (node.transitive == null || node.transitive.kind < kind) { @@ -764,6 +867,7 @@ class AliasingState { direction: 'forwards', // Traversing a maybeAlias edge always downgrades to conditional mutation kind: edge.kind === 'maybeAlias' ? MutationKind.Conditional : kind, + skipRangeExtension: false, }); } for (const [alias, when] of node.createdFrom) { @@ -775,6 +879,8 @@ class AliasingState { transitive: true, direction: 'backwards', kind, + // Propagate parent's skipRangeExtension but don't initiate it + skipRangeExtension, }); } if (direction === 'backwards' || node.value.kind !== 'Phi') { @@ -797,6 +903,16 @@ class AliasingState { transitive, direction: 'backwards', kind, + /* + * Skip range extension when traversing backward through alias + * edges from a conditional method-call mutation. This prevents + * the receiver's creation-site values from having their ranges + * extended to cover the method call instruction, which would + * otherwise cause them to be merged into the same scope. + */ + skipRangeExtension: + skipRangeExtension || + (effectiveSkipAliases && current === start), }); } /** @@ -815,6 +931,9 @@ class AliasingState { transitive, direction: 'backwards', kind: MutationKind.Conditional, + skipRangeExtension: + skipRangeExtension || + (effectiveSkipAliases && current === start), }); } } @@ -831,6 +950,13 @@ class AliasingState { transitive, direction: 'backwards', kind, + /* + * Reset skipRangeExtension for capture edges. Captures + * represent structural containment (e.g. a function closing + * over a value), and the captured value's range must be + * extended to cover the mutation point for correctness. + */ + skipRangeExtension: false, }); } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/index.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/index.ts index eb645cc218dc..6ff5a0c53bf4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/index.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/index.ts @@ -9,4 +9,3 @@ export {default as analyseFunctions} from './AnalyseFunctions'; export {dropManualMemoization} from './DropManualMemoization'; export {inferReactivePlaces} from './InferReactivePlaces'; export {inlineImmediatelyInvokedFunctionExpressions} from './InlineImmediatelyInvokedFunctionExpressions'; -export {inferEffectDependencies} from './InferEffectDependencies'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts deleted file mode 100644 index 3588cf32f9f6..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts +++ /dev/null @@ -1,790 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import { - BasicBlock, - BlockId, - BuiltinTag, - DeclarationId, - Effect, - forkTemporaryIdentifier, - GotoTerminal, - GotoVariant, - HIRFunction, - Identifier, - IfTerminal, - Instruction, - InstructionKind, - JsxAttribute, - makeInstructionId, - makePropertyLiteral, - ObjectProperty, - Phi, - Place, - promoteTemporary, - SpreadPattern, -} from '../HIR'; -import { - createTemporaryPlace, - fixScopeAndIdentifierRanges, - markInstructionIds, - markPredecessors, - reversePostorderBlocks, -} from '../HIR/HIRBuilder'; -import {CompilerError, EnvironmentConfig} from '..'; -import { - mapInstructionLValues, - mapInstructionOperands, - mapInstructionValueOperands, - mapTerminalOperands, -} from '../HIR/visitors'; -import {ErrorCategory} from '../CompilerError'; - -type InlinedJsxDeclarationMap = Map< - DeclarationId, - {identifier: Identifier; blockIdsToIgnore: Set} ->; - -/** - * A prod-only, RN optimization to replace JSX with inlined ReactElement object literals - * - * Example: - * <>foo - * _______________ - * let t1; - * if (__DEV__) { - * t1 = <>foo - * } else { - * t1 = {...} - * } - * - */ -export function inlineJsxTransform( - fn: HIRFunction, - inlineJsxTransformConfig: NonNullable< - EnvironmentConfig['inlineJsxTransform'] - >, -): void { - const inlinedJsxDeclarations: InlinedJsxDeclarationMap = new Map(); - /** - * Step 1: Codegen the conditional and ReactElement object literal - */ - for (const [_, currentBlock] of [...fn.body.blocks]) { - let fallthroughBlockInstructions: Array | null = null; - const instructionCount = currentBlock.instructions.length; - for (let i = 0; i < instructionCount; i++) { - const instr = currentBlock.instructions[i]!; - // TODO: Support value blocks - if (currentBlock.kind === 'value') { - fn.env.logger?.logEvent(fn.env.filename, { - kind: 'CompileDiagnostic', - fnLoc: null, - detail: { - category: ErrorCategory.Todo, - reason: 'JSX Inlining is not supported on value blocks', - loc: instr.loc, - }, - }); - continue; - } - switch (instr.value.kind) { - case 'JsxExpression': - case 'JsxFragment': { - /** - * Split into blocks for new IfTerminal: - * current, then, else, fallthrough - */ - const currentBlockInstructions = currentBlock.instructions.slice( - 0, - i, - ); - const thenBlockInstructions = currentBlock.instructions.slice( - i, - i + 1, - ); - const elseBlockInstructions: Array = []; - fallthroughBlockInstructions ??= currentBlock.instructions.slice( - i + 1, - ); - - const fallthroughBlockId = fn.env.nextBlockId; - const fallthroughBlock: BasicBlock = { - kind: currentBlock.kind, - id: fallthroughBlockId, - instructions: fallthroughBlockInstructions, - terminal: currentBlock.terminal, - preds: new Set(), - phis: new Set(), - }; - - /** - * Complete current block - * - Add instruction for variable declaration - * - Add instruction for LoadGlobal used by conditional - * - End block with a new IfTerminal - */ - const varPlace = createTemporaryPlace(fn.env, instr.value.loc); - promoteTemporary(varPlace.identifier); - const varLValuePlace = createTemporaryPlace(fn.env, instr.value.loc); - const thenVarPlace = { - ...varPlace, - identifier: forkTemporaryIdentifier( - fn.env.nextIdentifierId, - varPlace.identifier, - ), - }; - const elseVarPlace = { - ...varPlace, - identifier: forkTemporaryIdentifier( - fn.env.nextIdentifierId, - varPlace.identifier, - ), - }; - const varInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...varLValuePlace}, - value: { - kind: 'DeclareLocal', - lvalue: {place: {...varPlace}, kind: InstructionKind.Let}, - type: null, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - currentBlockInstructions.push(varInstruction); - - const devGlobalPlace = createTemporaryPlace(fn.env, instr.value.loc); - const devGlobalInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...devGlobalPlace, effect: Effect.Mutate}, - value: { - kind: 'LoadGlobal', - binding: { - kind: 'Global', - name: inlineJsxTransformConfig.globalDevVar, - }, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - currentBlockInstructions.push(devGlobalInstruction); - const thenBlockId = fn.env.nextBlockId; - const elseBlockId = fn.env.nextBlockId; - const ifTerminal: IfTerminal = { - kind: 'if', - test: {...devGlobalPlace, effect: Effect.Read}, - consequent: thenBlockId, - alternate: elseBlockId, - fallthrough: fallthroughBlockId, - loc: instr.loc, - id: makeInstructionId(0), - }; - currentBlock.instructions = currentBlockInstructions; - currentBlock.terminal = ifTerminal; - - /** - * Set up then block where we put the original JSX return - */ - const thenBlock: BasicBlock = { - id: thenBlockId, - instructions: thenBlockInstructions, - kind: 'block', - phis: new Set(), - preds: new Set(), - terminal: { - kind: 'goto', - block: fallthroughBlockId, - variant: GotoVariant.Break, - id: makeInstructionId(0), - loc: instr.loc, - }, - }; - fn.body.blocks.set(thenBlockId, thenBlock); - - const resassignElsePlace = createTemporaryPlace( - fn.env, - instr.value.loc, - ); - const reassignElseInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...resassignElsePlace}, - value: { - kind: 'StoreLocal', - lvalue: { - place: elseVarPlace, - kind: InstructionKind.Reassign, - }, - value: {...instr.lvalue}, - type: null, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - thenBlockInstructions.push(reassignElseInstruction); - - /** - * Set up else block where we add new codegen - */ - const elseBlockTerminal: GotoTerminal = { - kind: 'goto', - block: fallthroughBlockId, - variant: GotoVariant.Break, - id: makeInstructionId(0), - loc: instr.loc, - }; - const elseBlock: BasicBlock = { - id: elseBlockId, - instructions: elseBlockInstructions, - kind: 'block', - phis: new Set(), - preds: new Set(), - terminal: elseBlockTerminal, - }; - fn.body.blocks.set(elseBlockId, elseBlock); - - /** - * ReactElement object literal codegen - */ - const {refProperty, keyProperty, propsProperty} = - createPropsProperties( - fn, - instr, - elseBlockInstructions, - instr.value.kind === 'JsxExpression' ? instr.value.props : [], - instr.value.children, - ); - const reactElementInstructionPlace = createTemporaryPlace( - fn.env, - instr.value.loc, - ); - const reactElementInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...reactElementInstructionPlace, effect: Effect.Store}, - value: { - kind: 'ObjectExpression', - properties: [ - createSymbolProperty( - fn, - instr, - elseBlockInstructions, - '$$typeof', - inlineJsxTransformConfig.elementSymbol, - ), - instr.value.kind === 'JsxExpression' - ? createTagProperty( - fn, - instr, - elseBlockInstructions, - instr.value.tag, - ) - : createSymbolProperty( - fn, - instr, - elseBlockInstructions, - 'type', - 'react.fragment', - ), - refProperty, - keyProperty, - propsProperty, - ], - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - elseBlockInstructions.push(reactElementInstruction); - - const reassignConditionalInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...createTemporaryPlace(fn.env, instr.value.loc)}, - value: { - kind: 'StoreLocal', - lvalue: { - place: {...elseVarPlace}, - kind: InstructionKind.Reassign, - }, - value: {...reactElementInstruction.lvalue}, - type: null, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - elseBlockInstructions.push(reassignConditionalInstruction); - - /** - * Create phis to reassign the var - */ - const operands: Map = new Map(); - operands.set(thenBlockId, { - ...elseVarPlace, - }); - operands.set(elseBlockId, { - ...thenVarPlace, - }); - - const phiIdentifier = forkTemporaryIdentifier( - fn.env.nextIdentifierId, - varPlace.identifier, - ); - const phiPlace = { - ...createTemporaryPlace(fn.env, instr.value.loc), - identifier: phiIdentifier, - }; - const phis: Set = new Set([ - { - kind: 'Phi', - operands, - place: phiPlace, - }, - ]); - fallthroughBlock.phis = phis; - fn.body.blocks.set(fallthroughBlockId, fallthroughBlock); - - /** - * Track this JSX instruction so we can replace references in step 2 - */ - inlinedJsxDeclarations.set(instr.lvalue.identifier.declarationId, { - identifier: phiIdentifier, - blockIdsToIgnore: new Set([thenBlockId, elseBlockId]), - }); - break; - } - case 'FunctionExpression': - case 'ObjectMethod': { - inlineJsxTransform( - instr.value.loweredFunc.func, - inlineJsxTransformConfig, - ); - break; - } - } - } - } - - /** - * Step 2: Replace declarations with new phi values - */ - for (const [blockId, block] of fn.body.blocks) { - for (const instr of block.instructions) { - mapInstructionOperands(instr, place => - handlePlace(place, blockId, inlinedJsxDeclarations), - ); - - mapInstructionLValues(instr, lvalue => - handlelValue(lvalue, blockId, inlinedJsxDeclarations), - ); - - mapInstructionValueOperands(instr.value, place => - handlePlace(place, blockId, inlinedJsxDeclarations), - ); - } - - mapTerminalOperands(block.terminal, place => - handlePlace(place, blockId, inlinedJsxDeclarations), - ); - - if (block.terminal.kind === 'scope') { - const scope = block.terminal.scope; - for (const dep of scope.dependencies) { - dep.identifier = handleIdentifier( - dep.identifier, - inlinedJsxDeclarations, - ); - } - - for (const [origId, decl] of [...scope.declarations]) { - const newDecl = handleIdentifier( - decl.identifier, - inlinedJsxDeclarations, - ); - if (newDecl.id !== origId) { - scope.declarations.delete(origId); - scope.declarations.set(decl.identifier.id, { - identifier: newDecl, - scope: decl.scope, - }); - } - } - } - } - - /** - * Step 3: Fixup the HIR - * Restore RPO, ensure correct predecessors, renumber instructions, fix scope and ranges. - */ - reversePostorderBlocks(fn.body); - markPredecessors(fn.body); - markInstructionIds(fn.body); - fixScopeAndIdentifierRanges(fn.body); -} - -function createSymbolProperty( - fn: HIRFunction, - instr: Instruction, - nextInstructions: Array, - propertyName: string, - symbolName: string, -): ObjectProperty { - const symbolPlace = createTemporaryPlace(fn.env, instr.value.loc); - const symbolInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...symbolPlace, effect: Effect.Mutate}, - value: { - kind: 'LoadGlobal', - binding: {kind: 'Global', name: 'Symbol'}, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - nextInstructions.push(symbolInstruction); - - const symbolForPlace = createTemporaryPlace(fn.env, instr.value.loc); - const symbolForInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...symbolForPlace, effect: Effect.Read}, - value: { - kind: 'PropertyLoad', - object: {...symbolInstruction.lvalue}, - property: makePropertyLiteral('for'), - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - nextInstructions.push(symbolForInstruction); - - const symbolValuePlace = createTemporaryPlace(fn.env, instr.value.loc); - const symbolValueInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...symbolValuePlace, effect: Effect.Mutate}, - value: { - kind: 'Primitive', - value: symbolName, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - nextInstructions.push(symbolValueInstruction); - - const $$typeofPlace = createTemporaryPlace(fn.env, instr.value.loc); - const $$typeofInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...$$typeofPlace, effect: Effect.Mutate}, - value: { - kind: 'MethodCall', - receiver: symbolInstruction.lvalue, - property: symbolForInstruction.lvalue, - args: [symbolValueInstruction.lvalue], - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - const $$typeofProperty: ObjectProperty = { - kind: 'ObjectProperty', - key: {name: propertyName, kind: 'string'}, - type: 'property', - place: {...$$typeofPlace, effect: Effect.Capture}, - }; - nextInstructions.push($$typeofInstruction); - return $$typeofProperty; -} - -function createTagProperty( - fn: HIRFunction, - instr: Instruction, - nextInstructions: Array, - componentTag: BuiltinTag | Place, -): ObjectProperty { - let tagProperty: ObjectProperty; - switch (componentTag.kind) { - case 'BuiltinTag': { - const tagPropertyPlace = createTemporaryPlace(fn.env, instr.value.loc); - const tagInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...tagPropertyPlace, effect: Effect.Mutate}, - value: { - kind: 'Primitive', - value: componentTag.name, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - tagProperty = { - kind: 'ObjectProperty', - key: {name: 'type', kind: 'string'}, - type: 'property', - place: {...tagPropertyPlace, effect: Effect.Capture}, - }; - nextInstructions.push(tagInstruction); - break; - } - case 'Identifier': { - tagProperty = { - kind: 'ObjectProperty', - key: {name: 'type', kind: 'string'}, - type: 'property', - place: {...componentTag, effect: Effect.Capture}, - }; - break; - } - } - - return tagProperty; -} - -function createPropsProperties( - fn: HIRFunction, - instr: Instruction, - nextInstructions: Array, - propAttributes: Array, - children: Array | null, -): { - refProperty: ObjectProperty; - keyProperty: ObjectProperty; - propsProperty: ObjectProperty; -} { - let refProperty: ObjectProperty | undefined; - let keyProperty: ObjectProperty | undefined; - const props: Array = []; - const jsxAttributesWithoutKey = propAttributes.filter( - p => p.kind === 'JsxAttribute' && p.name !== 'key', - ); - const jsxSpreadAttributes = propAttributes.filter( - p => p.kind === 'JsxSpreadAttribute', - ); - const spreadPropsOnly = - jsxAttributesWithoutKey.length === 0 && jsxSpreadAttributes.length === 1; - propAttributes.forEach(prop => { - switch (prop.kind) { - case 'JsxAttribute': { - switch (prop.name) { - case 'key': { - keyProperty = { - kind: 'ObjectProperty', - key: {name: 'key', kind: 'string'}, - type: 'property', - place: {...prop.place}, - }; - break; - } - case 'ref': { - /** - * In the current JSX implementation, ref is both - * a property on the element and a property on props. - */ - refProperty = { - kind: 'ObjectProperty', - key: {name: 'ref', kind: 'string'}, - type: 'property', - place: {...prop.place}, - }; - const refPropProperty: ObjectProperty = { - kind: 'ObjectProperty', - key: {name: 'ref', kind: 'string'}, - type: 'property', - place: {...prop.place}, - }; - props.push(refPropProperty); - break; - } - default: { - const attributeProperty: ObjectProperty = { - kind: 'ObjectProperty', - key: {name: prop.name, kind: 'string'}, - type: 'property', - place: {...prop.place}, - }; - props.push(attributeProperty); - } - } - break; - } - case 'JsxSpreadAttribute': { - props.push({ - kind: 'Spread', - place: {...prop.argument}, - }); - break; - } - } - }); - - const propsPropertyPlace = createTemporaryPlace(fn.env, instr.value.loc); - if (children) { - let childrenPropProperty: ObjectProperty; - if (children.length === 1) { - childrenPropProperty = { - kind: 'ObjectProperty', - key: {name: 'children', kind: 'string'}, - type: 'property', - place: {...children[0], effect: Effect.Capture}, - }; - } else { - const childrenPropPropertyPlace = createTemporaryPlace( - fn.env, - instr.value.loc, - ); - - const childrenPropInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...childrenPropPropertyPlace, effect: Effect.Mutate}, - value: { - kind: 'ArrayExpression', - elements: [...children], - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - nextInstructions.push(childrenPropInstruction); - childrenPropProperty = { - kind: 'ObjectProperty', - key: {name: 'children', kind: 'string'}, - type: 'property', - place: {...childrenPropPropertyPlace, effect: Effect.Capture}, - }; - } - props.push(childrenPropProperty); - } - - if (refProperty == null) { - const refPropertyPlace = createTemporaryPlace(fn.env, instr.value.loc); - const refInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...refPropertyPlace, effect: Effect.Mutate}, - value: { - kind: 'Primitive', - value: null, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - refProperty = { - kind: 'ObjectProperty', - key: {name: 'ref', kind: 'string'}, - type: 'property', - place: {...refPropertyPlace, effect: Effect.Capture}, - }; - nextInstructions.push(refInstruction); - } - - if (keyProperty == null) { - const keyPropertyPlace = createTemporaryPlace(fn.env, instr.value.loc); - const keyInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...keyPropertyPlace, effect: Effect.Mutate}, - value: { - kind: 'Primitive', - value: null, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - keyProperty = { - kind: 'ObjectProperty', - key: {name: 'key', kind: 'string'}, - type: 'property', - place: {...keyPropertyPlace, effect: Effect.Capture}, - }; - nextInstructions.push(keyInstruction); - } - - let propsProperty: ObjectProperty; - if (spreadPropsOnly) { - const spreadProp = jsxSpreadAttributes[0]; - CompilerError.invariant(spreadProp.kind === 'JsxSpreadAttribute', { - reason: 'Spread prop attribute must be of kind JSXSpreadAttribute', - loc: instr.loc, - }); - propsProperty = { - kind: 'ObjectProperty', - key: {name: 'props', kind: 'string'}, - type: 'property', - place: {...spreadProp.argument, effect: Effect.Mutate}, - }; - } else { - const propsInstruction: Instruction = { - id: makeInstructionId(0), - lvalue: {...propsPropertyPlace, effect: Effect.Mutate}, - value: { - kind: 'ObjectExpression', - properties: props, - loc: instr.value.loc, - }, - effects: null, - loc: instr.loc, - }; - propsProperty = { - kind: 'ObjectProperty', - key: {name: 'props', kind: 'string'}, - type: 'property', - place: {...propsPropertyPlace, effect: Effect.Capture}, - }; - nextInstructions.push(propsInstruction); - } - - return {refProperty, keyProperty, propsProperty}; -} - -function handlePlace( - place: Place, - blockId: BlockId, - inlinedJsxDeclarations: InlinedJsxDeclarationMap, -): Place { - const inlinedJsxDeclaration = inlinedJsxDeclarations.get( - place.identifier.declarationId, - ); - if ( - inlinedJsxDeclaration == null || - inlinedJsxDeclaration.blockIdsToIgnore.has(blockId) - ) { - return place; - } - - return {...place, identifier: inlinedJsxDeclaration.identifier}; -} - -function handlelValue( - lvalue: Place, - blockId: BlockId, - inlinedJsxDeclarations: InlinedJsxDeclarationMap, -): Place { - const inlinedJsxDeclaration = inlinedJsxDeclarations.get( - lvalue.identifier.declarationId, - ); - if ( - inlinedJsxDeclaration == null || - inlinedJsxDeclaration.blockIdsToIgnore.has(blockId) - ) { - return lvalue; - } - - return {...lvalue, identifier: inlinedJsxDeclaration.identifier}; -} - -function handleIdentifier( - identifier: Identifier, - inlinedJsxDeclarations: InlinedJsxDeclarationMap, -): Identifier { - const inlinedJsxDeclaration = inlinedJsxDeclarations.get( - identifier.declarationId, - ); - return inlinedJsxDeclaration == null - ? identifier - : inlinedJsxDeclaration.identifier; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InstructionReordering.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/InstructionReordering.ts deleted file mode 100644 index f0c038247e87..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InstructionReordering.ts +++ /dev/null @@ -1,503 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {CompilerError} from '..'; -import { - BasicBlock, - Environment, - GeneratedSource, - HIRFunction, - IdentifierId, - Instruction, - InstructionId, - Place, - isExpressionBlockKind, - makeInstructionId, - markInstructionIds, -} from '../HIR'; -import {printInstruction} from '../HIR/PrintHIR'; -import { - eachInstructionLValue, - eachInstructionValueLValue, - eachInstructionValueOperand, - eachTerminalOperand, -} from '../HIR/visitors'; -import {getOrInsertWith} from '../Utils/utils'; - -/** - * This pass implements conservative instruction reordering to move instructions closer to - * to where their produced values are consumed. The goal is to group instructions in a way that - * is more optimal for future optimizations. Notably, MergeReactiveScopesThatAlwaysInvalidateTogether - * can only merge two candidate scopes if there are no intervenining instructions that are used by - * some later code: instruction reordering can move those intervening instructions later in many cases, - * thereby allowing more scopes to merge together. - * - * The high-level approach is to build a dependency graph where nodes correspond either to - * instructions OR to a particular lvalue assignment of another instruction. So - * `Destructure [x, y] = z` creates 3 nodes: one for the instruction, and one each for x and y. - * The lvalue nodes depend on the instruction node that assigns them. - * - * Dependency edges are added for all the lvalues and rvalues of each instruction, so for example - * the node for `t$2 = CallExpression t$0 ( t$1 )` will take dependencies on the nodes for t$0 and t$1. - * - * Individual instructions are grouped into two categories: - * - "Reorderable" instructions include a safe set of instructions that we know are fine to reorder. - * This includes JSX elements/fragments/text, primitives, template literals, and globals. - * These instructions are never emitted until they are referenced, and can even be moved across - * basic blocks until they are used. - * - All other instructions are non-reorderable, and take an explicit dependency on the last such - * non-reorderable instruction in their block. This largely ensures that mutations are serialized, - * since all potentially mutating instructions are in this category. - * - * The only remaining mutation not handled by the above is variable reassignment. To ensure that all - * reads/writes of a variable access the correct version, all references (lvalues and rvalues) to - * each named variable are serialized. Thus `x = 1; y = x; x = 2; z = x` will establish a chain - * of dependencies and retain the correct ordering. - * - * The algorithm proceeds one basic block at a time, first building up the dependnecy graph and then - * reordering. - * - * The reordering weights nodes according to their transitive dependencies, and whether a particular node - * needs memoization or not. Larger dependencies go first, followed by smaller dependencies, which in - * testing seems to allow scopes to merge more effectively. Over time we can likely continue to improve - * the reordering heuristic. - * - * An obvious area for improvement is to allow reordering of LoadLocals that occur after the last write - * of the named variable. We can add this in a follow-up. - */ -export function instructionReordering(fn: HIRFunction): void { - // Shared nodes are emitted when they are first used - const shared: Nodes = new Map(); - const references = findReferencedRangeOfTemporaries(fn); - for (const [, block] of fn.body.blocks) { - reorderBlock(fn.env, block, shared, references); - } - CompilerError.invariant(shared.size === 0, { - reason: `InstructionReordering: expected all reorderable nodes to have been emitted`, - loc: - [...shared.values()] - .map(node => node.instruction?.loc) - .filter(loc => loc != null)[0] ?? GeneratedSource, - }); - markInstructionIds(fn.body); -} - -const DEBUG = false; - -type Nodes = Map; -type Node = { - instruction: Instruction | null; - dependencies: Set; - reorderability: Reorderability; - depth: number | null; -}; - -// Inclusive start and end -type References = { - singleUseIdentifiers: SingleUseIdentifiers; - lastAssignments: LastAssignments; -}; -type LastAssignments = Map; -type SingleUseIdentifiers = Set; -enum ReferenceKind { - Read, - Write, -} -function findReferencedRangeOfTemporaries(fn: HIRFunction): References { - const singleUseIdentifiers = new Map(); - const lastAssignments: LastAssignments = new Map(); - function reference( - instr: InstructionId, - place: Place, - kind: ReferenceKind, - ): void { - if ( - place.identifier.name !== null && - place.identifier.name.kind === 'named' - ) { - if (kind === ReferenceKind.Write) { - const name = place.identifier.name.value; - const previous = lastAssignments.get(name); - if (previous === undefined) { - lastAssignments.set(name, instr); - } else { - lastAssignments.set( - name, - makeInstructionId(Math.max(previous, instr)), - ); - } - } - return; - } else if (kind === ReferenceKind.Read) { - const previousCount = singleUseIdentifiers.get(place.identifier.id) ?? 0; - singleUseIdentifiers.set(place.identifier.id, previousCount + 1); - } - } - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - for (const operand of eachInstructionValueLValue(instr.value)) { - reference(instr.id, operand, ReferenceKind.Read); - } - for (const lvalue of eachInstructionLValue(instr)) { - reference(instr.id, lvalue, ReferenceKind.Write); - } - } - for (const operand of eachTerminalOperand(block.terminal)) { - reference(block.terminal.id, operand, ReferenceKind.Read); - } - } - return { - singleUseIdentifiers: new Set( - [...singleUseIdentifiers] - .filter(([, count]) => count === 1) - .map(([id]) => id), - ), - lastAssignments, - }; -} - -function reorderBlock( - env: Environment, - block: BasicBlock, - shared: Nodes, - references: References, -): void { - const locals: Nodes = new Map(); - const named: Map = new Map(); - let previous: IdentifierId | null = null; - for (const instr of block.instructions) { - const {lvalue, value} = instr; - // Get or create a node for this lvalue - const reorderability = getReorderability(instr, references); - const node = getOrInsertWith( - locals, - lvalue.identifier.id, - () => - ({ - instruction: instr, - dependencies: new Set(), - reorderability, - depth: null, - }) as Node, - ); - /** - * Ensure non-reoderable instructions have their order retained by - * adding explicit dependencies to the previous such instruction. - */ - if (reorderability === Reorderability.Nonreorderable) { - if (previous !== null) { - node.dependencies.add(previous); - } - previous = lvalue.identifier.id; - } - /** - * Establish dependencies on operands - */ - for (const operand of eachInstructionValueOperand(value)) { - const {name, id} = operand.identifier; - if (name !== null && name.kind === 'named') { - // Serialize all accesses to named variables - const previous = named.get(name.value); - if (previous !== undefined) { - node.dependencies.add(previous); - } - named.set(name.value, lvalue.identifier.id); - } else if (locals.has(id) || shared.has(id)) { - node.dependencies.add(id); - } - } - /** - * Establish nodes for lvalues, with dependencies on the node - * for the instruction itself. This ensures that any consumers - * of the lvalue will take a dependency through to the original - * instruction. - */ - for (const lvalueOperand of eachInstructionValueLValue(value)) { - const lvalueNode = getOrInsertWith( - locals, - lvalueOperand.identifier.id, - () => - ({ - instruction: null, - dependencies: new Set(), - depth: null, - }) as Node, - ); - lvalueNode.dependencies.add(lvalue.identifier.id); - const name = lvalueOperand.identifier.name; - if (name !== null && name.kind === 'named') { - const previous = named.get(name.value); - if (previous !== undefined) { - node.dependencies.add(previous); - } - named.set(name.value, lvalue.identifier.id); - } - } - } - - const nextInstructions: Array = []; - const seen = new Set(); - - DEBUG && console.log(`bb${block.id}`); - - /** - * The ideal order for emitting instructions may change the final instruction, - * but value blocks have special semantics for the final instruction of a block - - * that's the expression's value!. So we choose between a less optimal strategy - * for value blocks which preserves the final instruction order OR a more optimal - * ordering for statement-y blocks. - */ - if (isExpressionBlockKind(block.kind)) { - // First emit everything that can't be reordered - if (previous !== null) { - DEBUG && console.log(`(last non-reorderable instruction)`); - DEBUG && print(env, locals, shared, seen, previous); - emit(env, locals, shared, nextInstructions, previous); - } - /* - * For "value" blocks the final instruction represents its value, so we have to be - * careful to not change the ordering. Emit the last instruction explicitly. - * Any non-reorderable instructions will get emitted first, and any unused - * reorderable instructions can be deferred to the shared node list. - */ - if (block.instructions.length !== 0) { - DEBUG && console.log(`(block value)`); - DEBUG && - print( - env, - locals, - shared, - seen, - block.instructions.at(-1)!.lvalue.identifier.id, - ); - emit( - env, - locals, - shared, - nextInstructions, - block.instructions.at(-1)!.lvalue.identifier.id, - ); - } - /* - * Then emit the dependencies of the terminal operand. In many cases they will have - * already been emitted in the previous step and this is a no-op. - * TODO: sort the dependencies based on weight, like we do for other nodes. Not a big - * deal though since most terminals have a single operand - */ - for (const operand of eachTerminalOperand(block.terminal)) { - DEBUG && console.log(`(terminal operand)`); - DEBUG && print(env, locals, shared, seen, operand.identifier.id); - emit(env, locals, shared, nextInstructions, operand.identifier.id); - } - // Anything not emitted yet is globally reorderable - for (const [id, node] of locals) { - if (node.instruction == null) { - continue; - } - CompilerError.invariant( - node.reorderability === Reorderability.Reorderable, - { - reason: `Expected all remaining instructions to be reorderable`, - description: - node.instruction != null - ? `Instruction [${node.instruction.id}] was not emitted yet but is not reorderable` - : `Lvalue $${id} was not emitted yet but is not reorderable`, - loc: node.instruction?.loc ?? block.terminal.loc, - }, - ); - - DEBUG && console.log(`save shared: $${id}`); - shared.set(id, node); - } - } else { - /** - * If this is not a value block, then the order within the block doesn't matter - * and we can optimize more. The observation is that blocks often have instructions - * such as: - * - * ``` - * t$0 = nonreorderable - * t$1 = nonreorderable <-- this gets in the way of merging t$0 and t$2 - * t$2 = reorderable deps[ t$0 ] - * return t$2 - * ``` - * - * Ie where there is some pair of nonreorderable+reorderable values, with some intervening - * also non-reorderable instruction. If we emit all non-reorderable instructions first, - * then we'll keep the original order. But reordering instructions doesn't just mean moving - * them later: we can also move them _earlier_. By starting from terminal operands we - * end up emitting: - * - * ``` - * t$0 = nonreorderable // dep of t$2 - * t$2 = reorderable deps[ t$0 ] - * t$1 = nonreorderable <-- not in the way of merging anymore! - * return t$2 - * ``` - * - * Ie all nonreorderable transitive deps of the terminal operands will get emitted first, - * but we'll be able to intersperse the depending reorderable instructions in between - * them in a way that works better with scope merging. - */ - for (const operand of eachTerminalOperand(block.terminal)) { - DEBUG && console.log(`(terminal operand)`); - DEBUG && print(env, locals, shared, seen, operand.identifier.id); - emit(env, locals, shared, nextInstructions, operand.identifier.id); - } - // Anything not emitted yet is globally reorderable - for (const id of Array.from(locals.keys()).reverse()) { - const node = locals.get(id); - if (node === undefined) { - continue; - } - if (node.reorderability === Reorderability.Reorderable) { - DEBUG && console.log(`save shared: $${id}`); - shared.set(id, node); - } else { - DEBUG && console.log('leftover'); - DEBUG && print(env, locals, shared, seen, id); - emit(env, locals, shared, nextInstructions, id); - } - } - } - - block.instructions = nextInstructions; - DEBUG && console.log(); -} - -function getDepth(env: Environment, nodes: Nodes, id: IdentifierId): number { - const node = nodes.get(id)!; - if (node == null) { - return 0; - } - if (node.depth != null) { - return node.depth; - } - node.depth = 0; // in case of cycles - let depth = node.reorderability === Reorderability.Reorderable ? 1 : 10; - for (const dep of node.dependencies) { - depth += getDepth(env, nodes, dep); - } - node.depth = depth; - return depth; -} - -function print( - env: Environment, - locals: Nodes, - shared: Nodes, - seen: Set, - id: IdentifierId, - depth: number = 0, -): void { - if (seen.has(id)) { - DEBUG && console.log(`${'| '.repeat(depth)}$${id} `); - return; - } - seen.add(id); - const node = locals.get(id) ?? shared.get(id); - if (node == null) { - return; - } - const deps = [...node.dependencies]; - deps.sort((a, b) => { - const aDepth = getDepth(env, locals, a); - const bDepth = getDepth(env, locals, b); - return bDepth - aDepth; - }); - for (const dep of deps) { - print(env, locals, shared, seen, dep, depth + 1); - } - DEBUG && - console.log( - `${'| '.repeat(depth)}$${id} ${printNode(node)} deps=[${deps - .map(x => `$${x}`) - .join(', ')}] depth=${node.depth}`, - ); -} - -function printNode(node: Node): string { - const {instruction} = node; - if (instruction === null) { - return ''; - } - switch (instruction.value.kind) { - case 'FunctionExpression': - case 'ObjectMethod': { - return `[${instruction.id}] ${instruction.value.kind}`; - } - default: { - return printInstruction(instruction); - } - } -} - -function emit( - env: Environment, - locals: Nodes, - shared: Nodes, - instructions: Array, - id: IdentifierId, -): void { - const node = locals.get(id) ?? shared.get(id); - if (node == null) { - return; - } - locals.delete(id); - shared.delete(id); - const deps = [...node.dependencies]; - deps.sort((a, b) => { - const aDepth = getDepth(env, locals, a); - const bDepth = getDepth(env, locals, b); - return bDepth - aDepth; - }); - for (const dep of deps) { - emit(env, locals, shared, instructions, dep); - } - if (node.instruction !== null) { - instructions.push(node.instruction); - } -} - -enum Reorderability { - Reorderable, - Nonreorderable, -} -function getReorderability( - instr: Instruction, - references: References, -): Reorderability { - switch (instr.value.kind) { - case 'JsxExpression': - case 'JsxFragment': - case 'JSXText': - case 'LoadGlobal': - case 'Primitive': - case 'TemplateLiteral': - case 'BinaryExpression': - case 'UnaryExpression': { - return Reorderability.Reorderable; - } - case 'LoadLocal': { - const name = instr.value.place.identifier.name; - if (name !== null && name.kind === 'named') { - const lastAssignment = references.lastAssignments.get(name.value); - if ( - lastAssignment !== undefined && - lastAssignment < instr.id && - references.singleUseIdentifiers.has(instr.lvalue.identifier.id) - ) { - return Reorderability.Reorderable; - } - } - return Reorderability.Nonreorderable; - } - default: { - return Reorderability.Nonreorderable; - } - } -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts deleted file mode 100644 index 50f00427205b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts +++ /dev/null @@ -1,308 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import { - ArrayExpression, - BasicBlock, - CallExpression, - Destructure, - Environment, - ExternalFunction, - GeneratedSource, - HIRFunction, - IdentifierId, - Instruction, - LoadGlobal, - LoadLocal, - NonLocalImportSpecifier, - Place, - PropertyLoad, - isUseContextHookType, - makeBlockId, - makeInstructionId, - makePropertyLiteral, - markInstructionIds, - promoteTemporary, - reversePostorderBlocks, -} from '../HIR'; -import {createTemporaryPlace} from '../HIR/HIRBuilder'; -import {enterSSA} from '../SSA'; -import {inferTypes} from '../TypeInference'; - -export function lowerContextAccess( - fn: HIRFunction, - loweredContextCalleeConfig: ExternalFunction, -): void { - const contextAccess: Map = new Map(); - const contextKeys: Map> = new Map(); - - // collect context access and keys - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - const {value, lvalue} = instr; - - if ( - value.kind === 'CallExpression' && - isUseContextHookType(value.callee.identifier) - ) { - contextAccess.set(lvalue.identifier.id, value); - continue; - } - - if (value.kind !== 'Destructure') { - continue; - } - - const destructureId = value.value.identifier.id; - if (!contextAccess.has(destructureId)) { - continue; - } - - const keys = getContextKeys(value); - if (keys === null) { - return; - } - - if (contextKeys.has(destructureId)) { - /* - * TODO(gsn): Add support for accessing context over multiple - * statements. - */ - return; - } else { - contextKeys.set(destructureId, keys); - } - } - } - - let importLoweredContextCallee: NonLocalImportSpecifier | null = null; - - if (contextAccess.size > 0 && contextKeys.size > 0) { - for (const [, block] of fn.body.blocks) { - let nextInstructions: Array | null = null; - - for (let i = 0; i < block.instructions.length; i++) { - const instr = block.instructions[i]; - const {lvalue, value} = instr; - if ( - value.kind === 'CallExpression' && - isUseContextHookType(value.callee.identifier) && - contextKeys.has(lvalue.identifier.id) - ) { - importLoweredContextCallee ??= - fn.env.programContext.addImportSpecifier( - loweredContextCalleeConfig, - ); - const loweredContextCalleeInstr = emitLoadLoweredContextCallee( - fn.env, - importLoweredContextCallee, - ); - - if (nextInstructions === null) { - nextInstructions = block.instructions.slice(0, i); - } - nextInstructions.push(loweredContextCalleeInstr); - - const keys = contextKeys.get(lvalue.identifier.id)!; - const selectorFnInstr = emitSelectorFn(fn.env, keys); - nextInstructions.push(selectorFnInstr); - - const lowerContextCallId = loweredContextCalleeInstr.lvalue; - value.callee = lowerContextCallId; - - const selectorFn = selectorFnInstr.lvalue; - value.args.push(selectorFn); - } - - if (nextInstructions) { - nextInstructions.push(instr); - } - } - if (nextInstructions) { - block.instructions = nextInstructions; - } - } - markInstructionIds(fn.body); - inferTypes(fn); - } -} - -function emitLoadLoweredContextCallee( - env: Environment, - importedLowerContextCallee: NonLocalImportSpecifier, -): Instruction { - const loadGlobal: LoadGlobal = { - kind: 'LoadGlobal', - binding: {...importedLowerContextCallee}, - loc: GeneratedSource, - }; - - return { - id: makeInstructionId(0), - loc: GeneratedSource, - lvalue: createTemporaryPlace(env, GeneratedSource), - effects: null, - value: loadGlobal, - }; -} - -function getContextKeys(value: Destructure): Array | null { - const keys = []; - const pattern = value.lvalue.pattern; - - switch (pattern.kind) { - case 'ArrayPattern': { - return null; - } - - case 'ObjectPattern': { - for (const place of pattern.properties) { - if ( - place.kind !== 'ObjectProperty' || - place.type !== 'property' || - place.key.kind !== 'identifier' || - place.place.identifier.name === null || - place.place.identifier.name.kind !== 'named' - ) { - return null; - } - keys.push(place.key.name); - } - return keys; - } - } -} - -function emitPropertyLoad( - env: Environment, - obj: Place, - property: string, -): {instructions: Array; element: Place} { - const loadObj: LoadLocal = { - kind: 'LoadLocal', - place: obj, - loc: GeneratedSource, - }; - const object: Place = createTemporaryPlace(env, GeneratedSource); - const loadLocalInstr: Instruction = { - lvalue: object, - value: loadObj, - id: makeInstructionId(0), - effects: null, - loc: GeneratedSource, - }; - - const loadProp: PropertyLoad = { - kind: 'PropertyLoad', - object, - property: makePropertyLiteral(property), - loc: GeneratedSource, - }; - const element: Place = createTemporaryPlace(env, GeneratedSource); - const loadPropInstr: Instruction = { - lvalue: element, - value: loadProp, - id: makeInstructionId(0), - effects: null, - loc: GeneratedSource, - }; - return { - instructions: [loadLocalInstr, loadPropInstr], - element: element, - }; -} - -function emitSelectorFn(env: Environment, keys: Array): Instruction { - const obj: Place = createTemporaryPlace(env, GeneratedSource); - promoteTemporary(obj.identifier); - const instr: Array = []; - const elements = []; - for (const key of keys) { - const {instructions, element: prop} = emitPropertyLoad(env, obj, key); - instr.push(...instructions); - elements.push(prop); - } - - const arrayInstr = emitArrayInstr(elements, env); - instr.push(arrayInstr); - - const block: BasicBlock = { - kind: 'block', - id: makeBlockId(0), - instructions: instr, - terminal: { - id: makeInstructionId(0), - kind: 'return', - returnVariant: 'Explicit', - loc: GeneratedSource, - value: arrayInstr.lvalue, - effects: null, - }, - preds: new Set(), - phis: new Set(), - }; - - const fn: HIRFunction = { - loc: GeneratedSource, - id: null, - nameHint: null, - fnType: 'Other', - env, - params: [obj], - returnTypeAnnotation: null, - returns: createTemporaryPlace(env, GeneratedSource), - context: [], - body: { - entry: block.id, - blocks: new Map([[block.id, block]]), - }, - generator: false, - async: false, - directives: [], - aliasingEffects: [], - }; - - reversePostorderBlocks(fn.body); - markInstructionIds(fn.body); - enterSSA(fn); - inferTypes(fn); - - const fnInstr: Instruction = { - id: makeInstructionId(0), - value: { - kind: 'FunctionExpression', - name: null, - nameHint: null, - loweredFunc: { - func: fn, - }, - type: 'ArrowFunctionExpression', - loc: GeneratedSource, - }, - lvalue: createTemporaryPlace(env, GeneratedSource), - effects: null, - loc: GeneratedSource, - }; - return fnInstr; -} - -function emitArrayInstr(elements: Array, env: Environment): Instruction { - const array: ArrayExpression = { - kind: 'ArrayExpression', - elements, - loc: GeneratedSource, - }; - const arrayLvalue: Place = createTemporaryPlace(env, GeneratedSource); - const arrayInstr: Instruction = { - id: makeInstructionId(0), - value: array, - lvalue: arrayLvalue, - effects: null, - loc: GeneratedSource, - }; - return arrayInstr; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/index.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/index.ts index bb060b8dc285..722b05a80996 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/index.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/index.ts @@ -8,4 +8,3 @@ export {constantPropagation} from './ConstantPropagation'; export {deadCodeElimination} from './DeadCodeElimination'; export {pruneMaybeThrows} from './PruneMaybeThrows'; -export {inlineJsxTransform} from './InlineJsxTransform'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts index f5b2a654ec4f..f53f7d15e0cb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts @@ -1007,11 +1007,10 @@ class Driver { const test = this.visitValueBlock(testBlockId, loc); const testBlock = this.cx.ir.blocks.get(test.block)!; if (testBlock.terminal.kind !== 'branch') { - CompilerError.throwTodo({ - reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for ${terminalKind} test block`, - description: null, + CompilerError.invariant(false, { + reason: `Expected a branch terminal for ${terminalKind} test block`, + description: `Got \`${testBlock.terminal.kind}\``, loc: testBlock.terminal.loc, - suggestions: null, }); } return { diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts index c60e8fb95967..486773d5eb91 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -13,7 +13,11 @@ import { pruneUnusedLabels, renameVariables, } from '.'; -import {CompilerError, ErrorCategory} from '../CompilerError'; +import { + CompilerError, + CompilerErrorDetail, + ErrorCategory, +} from '../CompilerError'; import {Environment, ExternalFunction} from '../HIR'; import { ArrayPattern, @@ -46,13 +50,13 @@ import { } from '../HIR/HIR'; import {printIdentifier, printInstruction, printPlace} from '../HIR/PrintHIR'; import {eachPatternOperand} from '../HIR/visitors'; -import {Err, Ok, Result} from '../Utils/Result'; + import {GuardKind} from '../Utils/RuntimeDiagnosticConstants'; import {assertExhaustive} from '../Utils/utils'; import {buildReactiveFunction} from './BuildReactiveFunction'; import {SINGLE_CHILD_FBT_TAGS} from './MemoizeFbtAndMacroOperandsInSameScope'; import {ReactiveFunctionVisitor, visitReactiveFunction} from './visitors'; -import {EMIT_FREEZE_GLOBAL_GATING, ReactFunctionType} from '../HIR/Environment'; +import {ReactFunctionType} from '../HIR/Environment'; import {ProgramContext} from '../Entrypoint'; export const MEMO_CACHE_SENTINEL = 'react.memo_cache_sentinel'; @@ -100,17 +104,6 @@ export type CodegenFunction = { fn: CodegenFunction; type: ReactFunctionType | null; }>; - - /** - * This is true if the compiler has compiled inferred effect dependencies - */ - hasInferredEffect: boolean; - inferredEffectLocations: Set; - - /** - * This is true if the compiler has compiled a fire to a useFire call - */ - hasFireRewrite: boolean; }; export function codegenFunction( @@ -122,7 +115,7 @@ export function codegenFunction( uniqueIdentifiers: Set; fbtOperands: Set; }, -): Result { +): CodegenFunction { const cx = new Context( fn.env, fn.id ?? '[[ anonymous ]]', @@ -152,11 +145,7 @@ export function codegenFunction( }; } - const compileResult = codegenReactiveFunction(cx, fn); - if (compileResult.isErr()) { - return compileResult; - } - const compiled = compileResult.unwrap(); + const compiled = codegenReactiveFunction(cx, fn); const hookGuard = fn.env.config.enableEmitHookGuards; if (hookGuard != null && fn.env.outputMode === 'client') { @@ -284,7 +273,7 @@ export function codegenFunction( emitInstrumentForget.globalGating, ); if (assertResult.isErr()) { - return assertResult; + fn.env.recordErrors(assertResult.unwrapErr()); } } @@ -334,20 +323,17 @@ export function codegenFunction( ), reactiveFunction, ); - if (codegen.isErr()) { - return codegen; - } - outlined.push({fn: codegen.unwrap(), type}); + outlined.push({fn: codegen, type}); } compiled.outlined = outlined; - return compileResult; + return compiled; } function codegenReactiveFunction( cx: Context, fn: ReactiveFunction, -): Result { +): CodegenFunction { for (const param of fn.params) { const place = param.kind === 'Identifier' ? param : param.place; cx.temp.set(place.identifier.declarationId, null); @@ -365,14 +351,10 @@ function codegenReactiveFunction( } } - if (cx.errors.hasAnyErrors()) { - return Err(cx.errors); - } - const countMemoBlockVisitor = new CountMemoBlockVisitor(fn.env); visitReactiveFunction(fn, countMemoBlockVisitor, undefined); - return Ok({ + return { type: 'CodegenFunction', loc: fn.loc, id: fn.id !== null ? t.identifier(fn.id) : null, @@ -387,10 +369,7 @@ function codegenReactiveFunction( prunedMemoBlocks: countMemoBlockVisitor.prunedMemoBlocks, prunedMemoValues: countMemoBlockVisitor.prunedMemoValues, outlined: [], - hasFireRewrite: fn.env.hasFireRewrite, - hasInferredEffect: fn.env.hasInferredEffect, - inferredEffectLocations: fn.env.inferredEffectLocations, - }); + }; } class CountMemoBlockVisitor extends ReactiveFunctionVisitor { @@ -441,7 +420,6 @@ class Context { */ #declarations: Set = new Set(); temp: Temporaries; - errors: CompilerError = new CompilerError(); objectMethods: Map = new Map(); uniqueIdentifiers: Set; fbtOperands: Set; @@ -460,6 +438,11 @@ class Context { this.fbtOperands = fbtOperands; this.temp = temporaries !== null ? new Map(temporaries) : new Map(); } + + recordError(error: CompilerErrorDetail): void { + this.env.recordError(error); + } + get nextCacheIndex(): number { return this.#nextCacheIndex++; } @@ -574,30 +557,6 @@ function codegenBlockNoReset( return t.blockStatement(statements); } -function wrapCacheDep(cx: Context, value: t.Expression): t.Expression { - if ( - cx.env.config.enableEmitFreeze != null && - cx.env.outputMode === 'client' - ) { - const emitFreezeIdentifier = cx.env.programContext.addImportSpecifier( - cx.env.config.enableEmitFreeze, - ).name; - cx.env.programContext - .assertGlobalBinding(EMIT_FREEZE_GLOBAL_GATING, cx.env.scope) - .unwrap(); - return t.conditionalExpression( - t.identifier(EMIT_FREEZE_GLOBAL_GATING), - t.callExpression(t.identifier(emitFreezeIdentifier), [ - value, - t.stringLiteral(cx.fnName), - ]), - value, - ); - } else { - return value; - } -} - function codegenReactiveScope( cx: Context, statements: Array, @@ -612,12 +571,9 @@ function codegenReactiveScope( value: t.Expression; }> = []; const changeExpressions: Array = []; - const changeExpressionComments: Array = []; - const outputComments: Array = []; for (const dep of [...scope.dependencies].sort(compareScopeDependency)) { const index = cx.nextCacheIndex; - changeExpressionComments.push(printDependencyComment(dep)); const comparison = t.binaryExpression( '!==', t.memberExpression( @@ -627,18 +583,7 @@ function codegenReactiveScope( ), codegenDependency(cx, dep), ); - - if (cx.env.config.enableChangeVariableCodegen) { - const changeIdentifier = t.identifier(cx.synthesizeName(`c_${index}`)); - statements.push( - t.variableDeclaration('const', [ - t.variableDeclarator(changeIdentifier, comparison), - ]), - ); - changeExpressions.push(changeIdentifier); - } else { - changeExpressions.push(comparison); - } + changeExpressions.push(comparison); /* * Adding directly to cacheStoreStatements rather than cacheLoads, because there * is no corresponding cacheLoadStatement for dependencies @@ -676,13 +621,12 @@ function codegenReactiveScope( }); const name = convertIdentifier(identifier); - outputComments.push(name.name); if (!cx.hasDeclared(identifier)) { statements.push( t.variableDeclaration('let', [createVariableDeclarator(name, null)]), ); } - cacheLoads.push({name, index, value: wrapCacheDep(cx, name)}); + cacheLoads.push({name, index, value: name}); cx.declare(identifier); } for (const reassignment of scope.reassignments) { @@ -691,8 +635,7 @@ function codegenReactiveScope( firstOutputIndex = index; } const name = convertIdentifier(reassignment); - outputComments.push(name.name); - cacheLoads.push({name, index, value: wrapCacheDep(cx, name)}); + cacheLoads.push({name, index, value: name}); } let testCondition = (changeExpressions as Array).reduce( @@ -724,187 +667,44 @@ function codegenReactiveScope( ); } - if (cx.env.config.disableMemoizationForDebugging) { - CompilerError.invariant( - cx.env.config.enableChangeDetectionForDebugging == null, - { - reason: `Expected to not have both change detection enabled and memoization disabled`, - description: `Incompatible config options`, - loc: GeneratedSource, - }, - ); - testCondition = t.logicalExpression( - '||', - testCondition, - t.booleanLiteral(true), - ); - } let computationBlock = codegenBlock(cx, block); let memoStatement; - const detectionFunction = cx.env.config.enableChangeDetectionForDebugging; - if (detectionFunction != null && changeExpressions.length > 0) { - const loc = - typeof scope.loc === 'symbol' - ? 'unknown location' - : `(${scope.loc.start.line}:${scope.loc.end.line})`; - const importedDetectionFunctionIdentifier = - cx.env.programContext.addImportSpecifier(detectionFunction).name; - const cacheLoadOldValueStatements: Array = []; - const changeDetectionStatements: Array = []; - const idempotenceDetectionStatements: Array = []; - - for (const {name, index, value} of cacheLoads) { - const loadName = cx.synthesizeName(`old$${name.name}`); - const slot = t.memberExpression( - t.identifier(cx.synthesizeName('$')), - t.numericLiteral(index), - true, - ); - cacheStoreStatements.push( - t.expressionStatement(t.assignmentExpression('=', slot, value)), - ); - cacheLoadOldValueStatements.push( - t.variableDeclaration('let', [ - t.variableDeclarator(t.identifier(loadName), slot), - ]), - ); - changeDetectionStatements.push( - t.expressionStatement( - t.callExpression(t.identifier(importedDetectionFunctionIdentifier), [ - t.identifier(loadName), - t.cloneNode(name, true), - t.stringLiteral(name.name), - t.stringLiteral(cx.fnName), - t.stringLiteral('cached'), - t.stringLiteral(loc), - ]), - ), - ); - idempotenceDetectionStatements.push( - t.expressionStatement( - t.callExpression(t.identifier(importedDetectionFunctionIdentifier), [ - t.cloneNode(slot, true), - t.cloneNode(name, true), - t.stringLiteral(name.name), - t.stringLiteral(cx.fnName), - t.stringLiteral('recomputed'), - t.stringLiteral(loc), - ]), - ), - ); - idempotenceDetectionStatements.push( - t.expressionStatement(t.assignmentExpression('=', name, slot)), - ); - } - const condition = cx.synthesizeName('condition'); - const recomputationBlock = t.cloneNode(computationBlock, true); - memoStatement = t.blockStatement([ - ...computationBlock.body, - t.variableDeclaration('let', [ - t.variableDeclarator(t.identifier(condition), testCondition), - ]), - t.ifStatement( - t.unaryExpression('!', t.identifier(condition)), - t.blockStatement([ - ...cacheLoadOldValueStatements, - ...changeDetectionStatements, - ]), - ), - ...cacheStoreStatements, - t.ifStatement( - t.identifier(condition), - t.blockStatement([ - ...recomputationBlock.body, - ...idempotenceDetectionStatements, - ]), - ), - ]); - } else { - for (const {name, index, value} of cacheLoads) { - cacheStoreStatements.push( - t.expressionStatement( - t.assignmentExpression( - '=', - t.memberExpression( - t.identifier(cx.synthesizeName('$')), - t.numericLiteral(index), - true, - ), - value, + for (const {name, index, value} of cacheLoads) { + cacheStoreStatements.push( + t.expressionStatement( + t.assignmentExpression( + '=', + t.memberExpression( + t.identifier(cx.synthesizeName('$')), + t.numericLiteral(index), + true, ), + value, ), - ); - cacheLoadStatements.push( - t.expressionStatement( - t.assignmentExpression( - '=', - name, - t.memberExpression( - t.identifier(cx.synthesizeName('$')), - t.numericLiteral(index), - true, - ), + ), + ); + cacheLoadStatements.push( + t.expressionStatement( + t.assignmentExpression( + '=', + name, + t.memberExpression( + t.identifier(cx.synthesizeName('$')), + t.numericLiteral(index), + true, ), ), - ); - } - computationBlock.body.push(...cacheStoreStatements); - memoStatement = t.ifStatement( - testCondition, - computationBlock, - t.blockStatement(cacheLoadStatements), + ), ); } + computationBlock.body.push(...cacheStoreStatements); + memoStatement = t.ifStatement( + testCondition, + computationBlock, + t.blockStatement(cacheLoadStatements), + ); - if (cx.env.config.enableMemoizationComments) { - if (changeExpressionComments.length) { - t.addComment( - memoStatement, - 'leading', - ` check if ${printDelimitedCommentList( - changeExpressionComments, - 'or', - )} changed`, - true, - ); - t.addComment( - memoStatement, - 'leading', - ` "useMemo" for ${printDelimitedCommentList(outputComments, 'and')}:`, - true, - ); - } else { - t.addComment( - memoStatement, - 'leading', - ' cache value with no dependencies', - true, - ); - t.addComment( - memoStatement, - 'leading', - ` "useMemo" for ${printDelimitedCommentList(outputComments, 'and')}:`, - true, - ); - } - if (computationBlock.body.length > 0) { - t.addComment( - computationBlock.body[0]!, - 'leading', - ` Inputs changed, recompute`, - true, - ); - } - if (cacheLoadStatements.length > 0) { - t.addComment( - cacheLoadStatements[0]!, - 'leading', - ` Inputs did not change, use cached value`, - true, - ); - } - } statements.push(memoStatement); const earlyReturnValue = scope.earlyReturnValue; @@ -979,12 +779,15 @@ function codegenTerminal( loc: terminal.init.loc, }); if (terminal.init.instructions.length !== 2) { - CompilerError.throwTodo({ - reason: 'Support non-trivial for..in inits', - description: null, - loc: terminal.init.loc, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: 'Support non-trivial for..in inits', + category: ErrorCategory.Todo, + loc: terminal.init.loc, + suggestions: null, + }), + ); + return t.emptyStatement(); } const iterableCollection = terminal.init.instructions[0]; const iterableItem = terminal.init.instructions[1]; @@ -999,12 +802,15 @@ function codegenTerminal( break; } case 'StoreContext': { - CompilerError.throwTodo({ - reason: 'Support non-trivial for..in inits', - description: null, - loc: terminal.init.loc, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: 'Support non-trivial for..in inits', + category: ErrorCategory.Todo, + loc: terminal.init.loc, + suggestions: null, + }), + ); + return t.emptyStatement(); } default: CompilerError.invariant(false, { @@ -1074,12 +880,15 @@ function codegenTerminal( loc: terminal.test.loc, }); if (terminal.test.instructions.length !== 2) { - CompilerError.throwTodo({ - reason: 'Support non-trivial for..of inits', - description: null, - loc: terminal.init.loc, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: 'Support non-trivial for..of inits', + category: ErrorCategory.Todo, + loc: terminal.init.loc, + suggestions: null, + }), + ); + return t.emptyStatement(); } const iterableItem = terminal.test.instructions[1]; let lval: t.LVal; @@ -1093,12 +902,15 @@ function codegenTerminal( break; } case 'StoreContext': { - CompilerError.throwTodo({ - reason: 'Support non-trivial for..of inits', - description: null, - loc: terminal.init.loc, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: 'Support non-trivial for..of inits', + category: ErrorCategory.Todo, + loc: terminal.init.loc, + suggestions: null, + }), + ); + return t.emptyStatement(); } default: CompilerError.invariant(false, { @@ -1431,41 +1243,6 @@ function codegenForInit( } } -function printDependencyComment(dependency: ReactiveScopeDependency): string { - const identifier = convertIdentifier(dependency.identifier); - let name = identifier.name; - if (dependency.path !== null) { - for (const path of dependency.path) { - name += `.${path.property}`; - } - } - return name; -} - -function printDelimitedCommentList( - items: Array, - finalCompletion: string, -): string { - if (items.length === 2) { - return items.join(` ${finalCompletion} `); - } else if (items.length <= 1) { - return items.join(''); - } - - let output = []; - for (let i = 0; i < items.length; i++) { - const item = items[i]!; - if (i < items.length - 2) { - output.push(`${item}, `); - } else if (i === items.length - 2) { - output.push(`${item}, ${finalCompletion} `); - } else { - output.push(item); - } - } - return output.join(''); -} - function codegenDependency( cx: Context, dependency: ReactiveScopeDependency, @@ -1897,7 +1674,7 @@ function codegenInstructionValue( cx.temp, ), reactiveFunction, - ).unwrap(); + ); /* * ObjectMethod builder must be backwards compatible with older versions of babel. @@ -2096,7 +1873,7 @@ function codegenInstructionValue( cx.temp, ), reactiveFunction, - ).unwrap(); + ); if (instrValue.type === 'ArrowFunctionExpression') { let body: t.BlockStatement | t.Expression = fn.body; @@ -2192,22 +1969,26 @@ function codegenInstructionValue( } else { if (t.isVariableDeclaration(stmt)) { const declarator = stmt.declarations[0]; - cx.errors.push({ - reason: `(CodegenReactiveFunction::codegenInstructionValue) Cannot declare variables in a value block, tried to declare '${ - (declarator.id as t.Identifier).name - }'`, - category: ErrorCategory.Todo, - loc: declarator.loc ?? null, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: `(CodegenReactiveFunction::codegenInstructionValue) Cannot declare variables in a value block, tried to declare '${ + (declarator.id as t.Identifier).name + }'`, + category: ErrorCategory.Todo, + loc: declarator.loc ?? null, + suggestions: null, + }), + ); return t.stringLiteral(`TODO handle ${declarator.id}`); } else { - cx.errors.push({ - reason: `(CodegenReactiveFunction::codegenInstructionValue) Handle conversion of ${stmt.type} to expression`, - category: ErrorCategory.Todo, - loc: stmt.loc ?? null, - suggestions: null, - }); + cx.recordError( + new CompilerErrorDetail({ + reason: `(CodegenReactiveFunction::codegenInstructionValue) Handle conversion of ${stmt.type} to expression`, + category: ErrorCategory.Todo, + loc: stmt.loc ?? null, + suggestions: null, + }), + ); return t.stringLiteral(`TODO handle ${stmt.type}`); } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts index bdf2f29284aa..f6cf491220c1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts @@ -470,6 +470,7 @@ function canMergeScopes( identifier: declaration.identifier, reactive: true, path: [], + loc: GeneratedSource, })), ), next.scope.dependencies, diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts index ffb25082c20f..ddc65423f38d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts @@ -22,6 +22,7 @@ import { printIdentifier, printInstructionValue, printPlace, + printSourceLocation, printType, } from '../HIR/PrintHIR'; import {assertExhaustive} from '../Utils/utils'; @@ -114,7 +115,7 @@ export function printDependency(dependency: ReactiveScopeDependency): string { const identifier = printIdentifier(dependency.identifier) + printType(dependency.identifier.type); - return `${identifier}${dependency.path.map(token => `${token.optional ? '?.' : '.'}${token.property}`).join('')}`; + return `${identifier}${dependency.path.map(token => `${token.optional ? '?.' : '.'}${token.property}`).join('')}_${printSourceLocation(dependency.loc)}`; } export function printReactiveInstructions( diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneInitializationDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneInitializationDependencies.ts deleted file mode 100644 index 9ef9d382c283..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneInitializationDependencies.ts +++ /dev/null @@ -1,294 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {CompilerError} from '../CompilerError'; -import { - Environment, - Identifier, - IdentifierId, - InstructionId, - Place, - PropertyLiteral, - ReactiveBlock, - ReactiveFunction, - ReactiveInstruction, - ReactiveScopeBlock, - ReactiveTerminalStatement, - getHookKind, - isUseRefType, - isUseStateType, -} from '../HIR'; -import {eachCallArgument, eachInstructionLValue} from '../HIR/visitors'; -import DisjointSet from '../Utils/DisjointSet'; -import {assertExhaustive} from '../Utils/utils'; -import {ReactiveFunctionVisitor, visitReactiveFunction} from './visitors'; - -/** - * This pass is built based on the observation by @jbrown215 that arguments - * to useState and useRef are only used the first time a component is rendered. - * Any subsequent times, the arguments will be evaluated but ignored. In this pass, - * we use this fact to improve the output of the compiler by not recomputing values that - * are only used as arguments (or inputs to arguments to) useState and useRef. - * - * This pass isn't yet stress-tested so it's not enabled by default. It's only enabled - * to support certain debug modes that detect non-idempotent code, since non-idempotent - * code can "safely" be used if its only passed to useState and useRef. We plan to rewrite - * this pass in HIR and enable it as an optimization in the future. - * - * Algorithm: - * We take two passes over the reactive function AST. In the first pass, we gather - * aliases and build relationships between property accesses--the key thing we need - * to do here is to find that, e.g., $0.x and $1 refer to the same value if - * $1 = PropertyLoad $0.x. - * - * In the second pass, we traverse the AST in reverse order and track how each place - * is used. If a place is read from in any Terminal, we mark the place as "Update", meaning - * it is used whenever the component is updated/re-rendered. If a place is read from in - * a useState or useRef hook call, we mark it as "Create", since it is only used when the - * component is created. In other instructions, we propagate the inferred place for the - * instructions lvalues onto any other instructions that are read. - * - * Whenever we finish this reverse pass over a reactive block, we can look at the blocks - * dependencies and see whether the dependencies are used in an "Update" context or only - * in a "Create" context. If a dependency is create-only, then we can remove that dependency - * from the block. - */ - -type CreateUpdate = 'Create' | 'Update' | 'Unknown'; - -type KindMap = Map; - -class Visitor extends ReactiveFunctionVisitor { - map: KindMap = new Map(); - aliases: DisjointSet; - paths: Map>; - env: Environment; - - constructor( - env: Environment, - aliases: DisjointSet, - paths: Map>, - ) { - super(); - this.aliases = aliases; - this.paths = paths; - this.env = env; - } - - join(values: Array): CreateUpdate { - function join2(l: CreateUpdate, r: CreateUpdate): CreateUpdate { - if (l === 'Update' || r === 'Update') { - return 'Update'; - } else if (l === 'Create' || r === 'Create') { - return 'Create'; - } else if (l === 'Unknown' || r === 'Unknown') { - return 'Unknown'; - } - assertExhaustive(r, `Unhandled variable kind ${r}`); - } - return values.reduce(join2, 'Unknown'); - } - - isCreateOnlyHook(id: Identifier): boolean { - return isUseStateType(id) || isUseRefType(id); - } - - override visitPlace( - _: InstructionId, - place: Place, - state: CreateUpdate, - ): void { - this.map.set( - place.identifier.id, - this.join([state, this.map.get(place.identifier.id) ?? 'Unknown']), - ); - } - - override visitBlock(block: ReactiveBlock, state: CreateUpdate): void { - super.visitBlock([...block].reverse(), state); - } - - override visitInstruction(instruction: ReactiveInstruction): void { - const state = this.join( - [...eachInstructionLValue(instruction)].map( - operand => this.map.get(operand.identifier.id) ?? 'Unknown', - ), - ); - - const visitCallOrMethodNonArgs = (): void => { - switch (instruction.value.kind) { - case 'CallExpression': { - this.visitPlace(instruction.id, instruction.value.callee, state); - break; - } - case 'MethodCall': { - this.visitPlace(instruction.id, instruction.value.property, state); - this.visitPlace(instruction.id, instruction.value.receiver, state); - break; - } - } - }; - - const isHook = (): boolean => { - let callee = null; - switch (instruction.value.kind) { - case 'CallExpression': { - callee = instruction.value.callee.identifier; - break; - } - case 'MethodCall': { - callee = instruction.value.property.identifier; - break; - } - } - return callee != null && getHookKind(this.env, callee) != null; - }; - - switch (instruction.value.kind) { - case 'CallExpression': - case 'MethodCall': { - if ( - instruction.lvalue && - this.isCreateOnlyHook(instruction.lvalue.identifier) - ) { - [...eachCallArgument(instruction.value.args)].forEach(operand => - this.visitPlace(instruction.id, operand, 'Create'), - ); - visitCallOrMethodNonArgs(); - } else { - this.traverseInstruction(instruction, isHook() ? 'Update' : state); - } - break; - } - default: { - this.traverseInstruction(instruction, state); - } - } - } - - override visitScope(scope: ReactiveScopeBlock): void { - const state = this.join( - [ - ...scope.scope.declarations.keys(), - ...[...scope.scope.reassignments.values()].map(ident => ident.id), - ].map(id => this.map.get(id) ?? 'Unknown'), - ); - super.visitScope(scope, state); - [...scope.scope.dependencies].forEach(ident => { - let target: undefined | IdentifierId = - this.aliases.find(ident.identifier.id) ?? ident.identifier.id; - ident.path.forEach(token => { - target &&= this.paths.get(target)?.get(token.property); - }); - if (target && this.map.get(target) === 'Create') { - scope.scope.dependencies.delete(ident); - } - }); - } - - override visitTerminal( - stmt: ReactiveTerminalStatement, - state: CreateUpdate, - ): void { - CompilerError.invariant(state !== 'Create', { - reason: "Visiting a terminal statement with state 'Create'", - loc: stmt.terminal.loc, - }); - super.visitTerminal(stmt, state); - } - - override visitReactiveFunctionValue( - _id: InstructionId, - _dependencies: Array, - fn: ReactiveFunction, - state: CreateUpdate, - ): void { - visitReactiveFunction(fn, this, state); - } -} - -export default function pruneInitializationDependencies( - fn: ReactiveFunction, -): void { - const [aliases, paths] = getAliases(fn); - visitReactiveFunction(fn, new Visitor(fn.env, aliases, paths), 'Update'); -} - -function update( - map: Map>, - key: IdentifierId, - path: PropertyLiteral, - value: IdentifierId, -): void { - const inner = map.get(key) ?? new Map(); - inner.set(path, value); - map.set(key, inner); -} - -class AliasVisitor extends ReactiveFunctionVisitor { - scopeIdentifiers: DisjointSet = new DisjointSet(); - scopePaths: Map> = new Map(); - - override visitInstruction(instr: ReactiveInstruction): void { - if ( - instr.value.kind === 'StoreLocal' || - instr.value.kind === 'StoreContext' - ) { - this.scopeIdentifiers.union([ - instr.value.lvalue.place.identifier.id, - instr.value.value.identifier.id, - ]); - } else if ( - instr.value.kind === 'LoadLocal' || - instr.value.kind === 'LoadContext' - ) { - instr.lvalue && - this.scopeIdentifiers.union([ - instr.lvalue.identifier.id, - instr.value.place.identifier.id, - ]); - } else if (instr.value.kind === 'PropertyLoad') { - instr.lvalue && - update( - this.scopePaths, - instr.value.object.identifier.id, - instr.value.property, - instr.lvalue.identifier.id, - ); - } else if (instr.value.kind === 'PropertyStore') { - update( - this.scopePaths, - instr.value.object.identifier.id, - instr.value.property, - instr.value.value.identifier.id, - ); - } - } -} - -function getAliases( - fn: ReactiveFunction, -): [ - DisjointSet, - Map>, -] { - const visitor = new AliasVisitor(); - visitReactiveFunction(fn, visitor, null); - let disjoint = visitor.scopeIdentifiers; - let scopePaths = new Map>(); - for (const [key, value] of visitor.scopePaths) { - for (const [path, id] of value) { - update( - scopePaths, - disjoint.find(key) ?? key, - path, - disjoint.find(id) ?? id, - ); - } - } - return [disjoint, scopePaths]; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts b/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts deleted file mode 100644 index 6793df5710bf..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts +++ /dev/null @@ -1,739 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {CompilerError, CompilerErrorDetailOptions, SourceLocation} from '..'; -import { - ArrayExpression, - CallExpression, - Effect, - Environment, - FunctionExpression, - GeneratedSource, - HIRFunction, - Identifier, - IdentifierId, - Instruction, - InstructionId, - InstructionKind, - InstructionValue, - isUseEffectHookType, - LoadLocal, - makeInstructionId, - NonLocalImportSpecifier, - Place, - promoteTemporary, -} from '../HIR'; -import {createTemporaryPlace, markInstructionIds} from '../HIR/HIRBuilder'; -import {getOrInsertWith} from '../Utils/utils'; -import { - BuiltInFireFunctionId, - BuiltInFireId, - DefaultNonmutatingHook, -} from '../HIR/ObjectShape'; -import {eachInstructionOperand} from '../HIR/visitors'; -import {printSourceLocationLine} from '../HIR/PrintHIR'; -import {USE_FIRE_FUNCTION_NAME} from '../HIR/Environment'; -import {ErrorCategory} from '../CompilerError'; - -/* - * TODO(jmbrown): - * - traverse object methods - * - method calls - * - React.useEffect calls - */ - -const CANNOT_COMPILE_FIRE = 'Cannot compile `fire`'; - -export function transformFire(fn: HIRFunction): void { - const context = new Context(fn.env); - replaceFireFunctions(fn, context); - if (!context.hasErrors()) { - ensureNoMoreFireUses(fn, context); - } - context.throwIfErrorsFound(); -} - -function replaceFireFunctions(fn: HIRFunction, context: Context): void { - let importedUseFire: NonLocalImportSpecifier | null = null; - let hasRewrite = false; - for (const [, block] of fn.body.blocks) { - const rewriteInstrs = new Map>(); - const deleteInstrs = new Set(); - for (const instr of block.instructions) { - const {value, lvalue} = instr; - if ( - value.kind === 'CallExpression' && - isUseEffectHookType(value.callee.identifier) && - value.args.length > 0 && - value.args[0].kind === 'Identifier' - ) { - const lambda = context.getFunctionExpression( - value.args[0].identifier.id, - ); - if (lambda != null) { - const capturedCallees = - visitFunctionExpressionAndPropagateFireDependencies( - lambda, - context, - true, - ); - - // Add useFire calls for all fire calls in found in the lambda - const newInstrs = []; - for (const [ - fireCalleePlace, - fireCalleeInfo, - ] of capturedCallees.entries()) { - if (!context.hasCalleeWithInsertedFire(fireCalleePlace)) { - context.addCalleeWithInsertedFire(fireCalleePlace); - - importedUseFire ??= fn.env.programContext.addImportSpecifier({ - source: fn.env.programContext.reactRuntimeModule, - importSpecifierName: USE_FIRE_FUNCTION_NAME, - }); - const loadUseFireInstr = makeLoadUseFireInstruction( - fn.env, - importedUseFire, - ); - const loadFireCalleeInstr = makeLoadFireCalleeInstruction( - fn.env, - fireCalleeInfo.capturedCalleeIdentifier, - ); - const callUseFireInstr = makeCallUseFireInstruction( - fn.env, - loadUseFireInstr.lvalue, - loadFireCalleeInstr.lvalue, - ); - const storeUseFireInstr = makeStoreUseFireInstruction( - fn.env, - callUseFireInstr.lvalue, - fireCalleeInfo.fireFunctionBinding, - ); - newInstrs.push( - loadUseFireInstr, - loadFireCalleeInstr, - callUseFireInstr, - storeUseFireInstr, - ); - - // We insert all of these instructions before the useEffect is loaded - const loadUseEffectInstrId = context.getLoadGlobalInstrId( - value.callee.identifier.id, - ); - if (loadUseEffectInstrId == null) { - context.pushError({ - loc: value.loc, - description: null, - category: ErrorCategory.Invariant, - reason: '[InsertFire] No LoadGlobal found for useEffect call', - suggestions: null, - }); - continue; - } - rewriteInstrs.set(loadUseEffectInstrId, newInstrs); - } - } - ensureNoRemainingCalleeCaptures( - lambda.loweredFunc.func, - context, - capturedCallees, - ); - - if ( - value.args.length > 1 && - value.args[1] != null && - value.args[1].kind === 'Identifier' - ) { - const depArray = value.args[1]; - const depArrayExpression = context.getArrayExpression( - depArray.identifier.id, - ); - if (depArrayExpression != null) { - for (const dependency of depArrayExpression.elements) { - if (dependency.kind === 'Identifier') { - const loadOfDependency = context.getLoadLocalInstr( - dependency.identifier.id, - ); - if (loadOfDependency != null) { - const replacedDepArrayItem = capturedCallees.get( - loadOfDependency.place.identifier.id, - ); - if (replacedDepArrayItem != null) { - loadOfDependency.place = - replacedDepArrayItem.fireFunctionBinding; - } - } - } - } - } else { - context.pushError({ - loc: value.args[1].loc, - description: - 'You must use an array literal for an effect dependency array when that effect uses `fire()`', - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } else if (value.args.length > 1 && value.args[1].kind === 'Spread') { - context.pushError({ - loc: value.args[1].place.loc, - description: - 'You must use an array literal for an effect dependency array when that effect uses `fire()`', - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } - } else if ( - value.kind === 'CallExpression' && - value.callee.identifier.type.kind === 'Function' && - value.callee.identifier.type.shapeId === BuiltInFireId && - context.inUseEffectLambda() - ) { - /* - * We found a fire(callExpr()) call. We remove the `fire()` call and replace the callExpr() - * with a freshly generated fire function binding. We'll insert the useFire call before the - * useEffect call, which happens in the CallExpression (useEffect) case above. - */ - - /* - * We only allow fire to be called with a CallExpression: `fire(f())` - * TODO: add support for method calls: `fire(this.method())` - */ - if (value.args.length === 1 && value.args[0].kind === 'Identifier') { - const callExpr = context.getCallExpression( - value.args[0].identifier.id, - ); - - if (callExpr != null) { - const calleeId = callExpr.callee.identifier.id; - const loadLocal = context.getLoadLocalInstr(calleeId); - if (loadLocal == null) { - context.pushError({ - loc: value.loc, - description: null, - category: ErrorCategory.Invariant, - reason: - '[InsertFire] No loadLocal found for fire call argument', - suggestions: null, - }); - continue; - } - - const fireFunctionBinding = - context.getOrGenerateFireFunctionBinding( - loadLocal.place, - value.loc, - ); - - loadLocal.place = {...fireFunctionBinding}; - - // Delete the fire call expression - deleteInstrs.add(instr.id); - } else { - context.pushError({ - loc: value.loc, - description: - '`fire()` can only receive a function call such as `fire(fn(a,b)). Method calls and other expressions are not allowed', - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } else { - let description: string = - 'fire() can only take in a single call expression as an argument'; - if (value.args.length === 0) { - description += ' but received none'; - } else if (value.args.length > 1) { - description += ' but received multiple arguments'; - } else if (value.args[0].kind === 'Spread') { - description += ' but received a spread argument'; - } - context.pushError({ - loc: value.loc, - description, - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } else if (value.kind === 'CallExpression') { - context.addCallExpression(lvalue.identifier.id, value); - } else if ( - value.kind === 'FunctionExpression' && - context.inUseEffectLambda() - ) { - visitFunctionExpressionAndPropagateFireDependencies( - value, - context, - false, - ); - } else if (value.kind === 'FunctionExpression') { - context.addFunctionExpression(lvalue.identifier.id, value); - } else if (value.kind === 'LoadLocal') { - context.addLoadLocalInstr(lvalue.identifier.id, value); - } else if ( - value.kind === 'LoadGlobal' && - value.binding.kind === 'ImportSpecifier' && - value.binding.module === 'react' && - value.binding.imported === 'fire' && - context.inUseEffectLambda() - ) { - deleteInstrs.add(instr.id); - } else if (value.kind === 'LoadGlobal') { - context.addLoadGlobalInstrId(lvalue.identifier.id, instr.id); - } else if (value.kind === 'ArrayExpression') { - context.addArrayExpression(lvalue.identifier.id, value); - } - } - block.instructions = rewriteInstructions(rewriteInstrs, block.instructions); - block.instructions = deleteInstructions(deleteInstrs, block.instructions); - - if (rewriteInstrs.size > 0 || deleteInstrs.size > 0) { - hasRewrite = true; - fn.env.hasFireRewrite = true; - } - } - - if (hasRewrite) { - markInstructionIds(fn.body); - } -} - -/** - * Traverses a function expression to find fire calls fire(foo()) and replaces them with - * fireFoo(). - * - * When a function captures a fire call we need to update its context to reflect the newly created - * fire function bindings and update the LoadLocals referenced by the function's dependencies. - * - * @param isUseEffect is necessary so we can keep track of when we should additionally insert - * useFire hooks calls. - */ -function visitFunctionExpressionAndPropagateFireDependencies( - fnExpr: FunctionExpression, - context: Context, - enteringUseEffect: boolean, -): FireCalleesToFireFunctionBinding { - let withScope = enteringUseEffect - ? context.withUseEffectLambdaScope.bind(context) - : context.withFunctionScope.bind(context); - - const calleesCapturedByFnExpression = withScope(() => - replaceFireFunctions(fnExpr.loweredFunc.func, context), - ); - - // For each replaced callee, update the context of the function expression to track it - for ( - let contextIdx = 0; - contextIdx < fnExpr.loweredFunc.func.context.length; - contextIdx++ - ) { - const contextItem = fnExpr.loweredFunc.func.context[contextIdx]; - const replacedCallee = calleesCapturedByFnExpression.get( - contextItem.identifier.id, - ); - if (replacedCallee != null) { - fnExpr.loweredFunc.func.context[contextIdx] = { - ...replacedCallee.fireFunctionBinding, - }; - } - } - - context.mergeCalleesFromInnerScope(calleesCapturedByFnExpression); - - return calleesCapturedByFnExpression; -} - -/* - * eachInstructionOperand is not sufficient for our cases because: - * 1. fire is a global, which will not appear - * 2. The HIR may be malformed, so can't rely on function deps and must - * traverse the whole function. - */ -function* eachReachablePlace(fn: HIRFunction): Iterable { - for (const [, block] of fn.body.blocks) { - for (const instr of block.instructions) { - if ( - instr.value.kind === 'FunctionExpression' || - instr.value.kind === 'ObjectMethod' - ) { - yield* eachReachablePlace(instr.value.loweredFunc.func); - } else { - yield* eachInstructionOperand(instr); - } - } - } -} - -function ensureNoRemainingCalleeCaptures( - fn: HIRFunction, - context: Context, - capturedCallees: FireCalleesToFireFunctionBinding, -): void { - for (const place of eachReachablePlace(fn)) { - const calleeInfo = capturedCallees.get(place.identifier.id); - if (calleeInfo != null) { - const calleeName = - calleeInfo.capturedCalleeIdentifier.name?.kind === 'named' - ? calleeInfo.capturedCalleeIdentifier.name.value - : ''; - context.pushError({ - loc: place.loc, - description: `All uses of ${calleeName} must be either used with a fire() call in \ -this effect or not used with a fire() call at all. ${calleeName} was used with fire() on line \ -${printSourceLocationLine(calleeInfo.fireLoc)} in this effect`, - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } -} - -function ensureNoMoreFireUses(fn: HIRFunction, context: Context): void { - for (const place of eachReachablePlace(fn)) { - if ( - place.identifier.type.kind === 'Function' && - place.identifier.type.shapeId === BuiltInFireId - ) { - context.pushError({ - loc: place.identifier.loc, - description: 'Cannot use `fire` outside of a useEffect function', - category: ErrorCategory.Fire, - reason: CANNOT_COMPILE_FIRE, - suggestions: null, - }); - } - } -} - -function makeLoadUseFireInstruction( - env: Environment, - importedLoadUseFire: NonLocalImportSpecifier, -): Instruction { - const useFirePlace = createTemporaryPlace(env, GeneratedSource); - useFirePlace.effect = Effect.Read; - useFirePlace.identifier.type = DefaultNonmutatingHook; - const instrValue: InstructionValue = { - kind: 'LoadGlobal', - binding: {...importedLoadUseFire}, - loc: GeneratedSource, - }; - return { - id: makeInstructionId(0), - value: instrValue, - lvalue: {...useFirePlace}, - loc: GeneratedSource, - effects: null, - }; -} - -function makeLoadFireCalleeInstruction( - env: Environment, - fireCalleeIdentifier: Identifier, -): Instruction { - const loadedFireCallee = createTemporaryPlace(env, GeneratedSource); - const fireCallee: Place = { - kind: 'Identifier', - identifier: fireCalleeIdentifier, - reactive: false, - effect: Effect.Unknown, - loc: fireCalleeIdentifier.loc, - }; - return { - id: makeInstructionId(0), - value: { - kind: 'LoadLocal', - loc: GeneratedSource, - place: {...fireCallee}, - }, - lvalue: {...loadedFireCallee}, - loc: GeneratedSource, - effects: null, - }; -} - -function makeCallUseFireInstruction( - env: Environment, - useFirePlace: Place, - argPlace: Place, -): Instruction { - const useFireCallResultPlace = createTemporaryPlace(env, GeneratedSource); - useFireCallResultPlace.effect = Effect.Read; - - const useFireCall: CallExpression = { - kind: 'CallExpression', - callee: {...useFirePlace}, - args: [argPlace], - loc: GeneratedSource, - }; - - return { - id: makeInstructionId(0), - value: useFireCall, - lvalue: {...useFireCallResultPlace}, - loc: GeneratedSource, - effects: null, - }; -} - -function makeStoreUseFireInstruction( - env: Environment, - useFireCallResultPlace: Place, - fireFunctionBindingPlace: Place, -): Instruction { - promoteTemporary(fireFunctionBindingPlace.identifier); - - const fireFunctionBindingLValuePlace = createTemporaryPlace( - env, - GeneratedSource, - ); - return { - id: makeInstructionId(0), - value: { - kind: 'StoreLocal', - lvalue: { - kind: InstructionKind.Const, - place: {...fireFunctionBindingPlace}, - }, - value: {...useFireCallResultPlace}, - type: null, - loc: GeneratedSource, - }, - lvalue: fireFunctionBindingLValuePlace, - loc: GeneratedSource, - effects: null, - }; -} - -type FireCalleesToFireFunctionBinding = Map< - IdentifierId, - { - fireFunctionBinding: Place; - capturedCalleeIdentifier: Identifier; - fireLoc: SourceLocation; - } ->; - -class Context { - #env: Environment; - - #errors: CompilerError = new CompilerError(); - - /* - * Used to look up the call expression passed to a `fire(callExpr())`. Gives back - * the `callExpr()`. - */ - #callExpressions = new Map(); - - /* - * We keep track of function expressions so that we can traverse them when - * we encounter a lambda passed to a useEffect call - */ - #functionExpressions = new Map(); - - /* - * Mapping from lvalue ids to the LoadLocal for it. Allows us to replace dependency LoadLocals. - */ - #loadLocals = new Map(); - - /* - * Maps all of the fire callees found in a component/hook to the generated fire function places - * we create for them. Allows us to reuse already-inserted useFire results - */ - #fireCalleesToFireFunctions: Map = new Map(); - - /* - * The callees for which we have already created fire bindings. Used to skip inserting a new - * useFire call for a fire callee if one has already been created. - */ - #calleesWithInsertedFire = new Set(); - - /* - * A mapping from fire callees to the created fire function bindings that are reachable from this - * scope. - * - * We additionally keep track of the captured callee identifier so that we can properly reference - * it in the place where we LoadLocal the callee as an argument to useFire. - */ - #capturedCalleeIdentifierIds: FireCalleesToFireFunctionBinding = new Map(); - - /* - * We only transform fire calls if we're syntactically within a useEffect lambda (for now) - */ - #inUseEffectLambda = false; - - /* - * Mapping from useEffect callee identifier ids to the instruction id of the - * load global instruction for the useEffect call. We use this to insert the - * useFire calls before the useEffect call - */ - #loadGlobalInstructionIds = new Map(); - - constructor(env: Environment) { - this.#env = env; - } - - /* - * We keep track of array expressions so we can rewrite dependency arrays passed to useEffect - * to use the fire functions - */ - #arrayExpressions = new Map(); - - pushError(error: CompilerErrorDetailOptions): void { - this.#errors.push(error); - } - - withFunctionScope(fn: () => void): FireCalleesToFireFunctionBinding { - fn(); - return this.#capturedCalleeIdentifierIds; - } - - withUseEffectLambdaScope(fn: () => void): FireCalleesToFireFunctionBinding { - const capturedCalleeIdentifierIds = this.#capturedCalleeIdentifierIds; - const inUseEffectLambda = this.#inUseEffectLambda; - - this.#capturedCalleeIdentifierIds = new Map(); - this.#inUseEffectLambda = true; - - const resultCapturedCalleeIdentifierIds = this.withFunctionScope(fn); - - this.#capturedCalleeIdentifierIds = capturedCalleeIdentifierIds; - this.#inUseEffectLambda = inUseEffectLambda; - - return resultCapturedCalleeIdentifierIds; - } - - addCallExpression(id: IdentifierId, callExpr: CallExpression): void { - this.#callExpressions.set(id, callExpr); - } - - getCallExpression(id: IdentifierId): CallExpression | undefined { - return this.#callExpressions.get(id); - } - - addLoadLocalInstr(id: IdentifierId, loadLocal: LoadLocal): void { - this.#loadLocals.set(id, loadLocal); - } - - getLoadLocalInstr(id: IdentifierId): LoadLocal | undefined { - return this.#loadLocals.get(id); - } - getOrGenerateFireFunctionBinding( - callee: Place, - fireLoc: SourceLocation, - ): Place { - const fireFunctionBinding = getOrInsertWith( - this.#fireCalleesToFireFunctions, - callee.identifier.id, - () => createTemporaryPlace(this.#env, GeneratedSource), - ); - - fireFunctionBinding.identifier.type = { - kind: 'Function', - shapeId: BuiltInFireFunctionId, - return: {kind: 'Poly'}, - isConstructor: false, - }; - - this.#capturedCalleeIdentifierIds.set(callee.identifier.id, { - fireFunctionBinding, - capturedCalleeIdentifier: callee.identifier, - fireLoc, - }); - - return fireFunctionBinding; - } - - mergeCalleesFromInnerScope( - innerCallees: FireCalleesToFireFunctionBinding, - ): void { - for (const [id, calleeInfo] of innerCallees.entries()) { - this.#capturedCalleeIdentifierIds.set(id, calleeInfo); - } - } - - addCalleeWithInsertedFire(id: IdentifierId): void { - this.#calleesWithInsertedFire.add(id); - } - - hasCalleeWithInsertedFire(id: IdentifierId): boolean { - return this.#calleesWithInsertedFire.has(id); - } - - inUseEffectLambda(): boolean { - return this.#inUseEffectLambda; - } - - addFunctionExpression(id: IdentifierId, fn: FunctionExpression): void { - this.#functionExpressions.set(id, fn); - } - - getFunctionExpression(id: IdentifierId): FunctionExpression | undefined { - return this.#functionExpressions.get(id); - } - - addLoadGlobalInstrId(id: IdentifierId, instrId: InstructionId): void { - this.#loadGlobalInstructionIds.set(id, instrId); - } - - getLoadGlobalInstrId(id: IdentifierId): InstructionId | undefined { - return this.#loadGlobalInstructionIds.get(id); - } - - addArrayExpression(id: IdentifierId, array: ArrayExpression): void { - this.#arrayExpressions.set(id, array); - } - - getArrayExpression(id: IdentifierId): ArrayExpression | undefined { - return this.#arrayExpressions.get(id); - } - - hasErrors(): boolean { - return this.#errors.hasAnyErrors(); - } - - throwIfErrorsFound(): void { - if (this.hasErrors()) throw this.#errors; - } -} - -function deleteInstructions( - deleteInstrs: Set, - instructions: Array, -): Array { - if (deleteInstrs.size > 0) { - const newInstrs = instructions.filter(instr => !deleteInstrs.has(instr.id)); - return newInstrs; - } - return instructions; -} - -function rewriteInstructions( - rewriteInstrs: Map>, - instructions: Array, -): Array { - if (rewriteInstrs.size > 0) { - const newInstrs = []; - for (const instr of instructions) { - const newInstrsAtId = rewriteInstrs.get(instr.id); - if (newInstrsAtId != null) { - newInstrs.push(...newInstrsAtId, instr); - } else { - newInstrs.push(instr); - } - } - - return newInstrs; - } - - return instructions; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Transform/index.ts b/compiler/packages/babel-plugin-react-compiler/src/Transform/index.ts index 4f142104f210..a265a953eed3 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Transform/index.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Transform/index.ts @@ -4,5 +4,3 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ - -export {transformFire} from './TransformFire'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts index 50fdfb356cd9..190e3d3a7a3e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts @@ -8,7 +8,6 @@ import * as t from '@babel/types'; import {CompilerError} from '../CompilerError'; import {Environment} from '../HIR'; -import {lowerType} from '../HIR/BuildHIR'; import { GeneratedSource, HIRFunction, @@ -26,7 +25,6 @@ import { } from '../HIR/HIR'; import { BuiltInArrayId, - BuiltInEventHandlerId, BuiltInFunctionId, BuiltInJsxId, BuiltInMixedReadonlyId, @@ -223,22 +221,11 @@ function* generateInstructionTypes( } case 'StoreLocal': { - if (env.config.enableUseTypeAnnotations) { - yield equation( - value.lvalue.place.identifier.type, - value.value.identifier.type, - ); - const valueType = - value.type === null ? makeType() : lowerType(value.type); - yield equation(valueType, value.lvalue.place.identifier.type); - yield equation(left, valueType); - } else { - yield equation(left, value.value.identifier.type); - yield equation( - value.lvalue.place.identifier.type, - value.value.identifier.type, - ); - } + yield equation(left, value.value.identifier.type); + yield equation( + value.lvalue.place.identifier.type, + value.value.identifier.type, + ); break; } @@ -422,12 +409,7 @@ function* generateInstructionTypes( } case 'TypeCastExpression': { - if (env.config.enableUseTypeAnnotations) { - yield equation(value.type, value.value.identifier.type); - yield equation(left, value.type); - } else { - yield equation(left, value.value.identifier.type); - } + yield equation(left, value.value.identifier.type); break; } @@ -473,41 +455,6 @@ function* generateInstructionTypes( } } } - if (env.config.enableInferEventHandlers) { - if ( - value.kind === 'JsxExpression' && - value.tag.kind === 'BuiltinTag' && - !value.tag.name.includes('-') - ) { - /* - * Infer event handler types for built-in DOM elements. - * Props starting with "on" (e.g., onClick, onSubmit) on primitive tags - * are inferred as event handlers. This allows functions with ref access - * to be passed to these props, since DOM event handlers are guaranteed - * by React to only execute in response to events, never during render. - * - * We exclude tags with hyphens to avoid web components (custom elements), - * which are required by the HTML spec to contain a hyphen. Web components - * may call event handler props during their lifecycle methods (e.g., - * connectedCallback), which would be unsafe for ref access. - */ - for (const prop of value.props) { - if ( - prop.kind === 'JsxAttribute' && - prop.name.startsWith('on') && - prop.name.length > 2 && - prop.name[2] === prop.name[2].toUpperCase() - ) { - yield equation(prop.place.identifier.type, { - kind: 'Function', - shapeId: BuiltInEventHandlerId, - return: makeType(), - isConstructor: false, - }); - } - } - } - } yield equation(left, {kind: 'Object', shapeId: BuiltInJsxId}); break; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Utils/TestUtils.ts b/compiler/packages/babel-plugin-react-compiler/src/Utils/TestUtils.ts index 68f73c7f9972..ac5f40e00d9e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Utils/TestUtils.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Utils/TestUtils.ts @@ -39,14 +39,6 @@ function tryParseTestPragmaValue(val: string): Result { const testComplexConfigDefaults: PartialEnvironmentConfig = { validateNoCapitalizedCalls: [], - enableChangeDetectionForDebugging: { - source: 'react-compiler-runtime', - importSpecifierName: '$structuralCheck', - }, - enableEmitFreeze: { - source: 'react-compiler-runtime', - importSpecifierName: 'makeReadOnly', - }, enableEmitInstrumentForget: { fn: { source: 'react-compiler-runtime', @@ -62,37 +54,6 @@ const testComplexConfigDefaults: PartialEnvironmentConfig = { source: 'react-compiler-runtime', importSpecifierName: '$dispatcherGuard', }, - inlineJsxTransform: { - elementSymbol: 'react.transitional.element', - globalDevVar: 'DEV', - }, - lowerContextAccess: { - source: 'react-compiler-runtime', - importSpecifierName: 'useContext_withSelector', - }, - inferEffectDependencies: [ - { - function: { - source: 'react', - importSpecifierName: 'useEffect', - }, - autodepsIndex: 1, - }, - { - function: { - source: 'shared-runtime', - importSpecifierName: 'useSpecialEffect', - }, - autodepsIndex: 2, - }, - { - function: { - source: 'useEffectWrapper', - importSpecifierName: 'default', - }, - autodepsIndex: 1, - }, - ], }; function* splitPragma( diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateContextVariableLValues.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateContextVariableLValues.ts index a0d14ab16ba4..933a10f5c676 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateContextVariableLValues.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateContextVariableLValues.ts @@ -5,7 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import {CompilerError} from '..'; +import {CompilerDiagnostic, CompilerError} from '..'; +import {ErrorCategory} from '../CompilerError'; +import {Environment} from '../HIR/Environment'; import {HIRFunction, IdentifierId, Place} from '../HIR'; import {printPlace} from '../HIR/PrintHIR'; import {eachInstructionValueLValue, eachPatternOperand} from '../HIR/visitors'; @@ -17,12 +19,13 @@ import {eachInstructionValueLValue, eachPatternOperand} from '../HIR/visitors'; */ export function validateContextVariableLValues(fn: HIRFunction): void { const identifierKinds: IdentifierKinds = new Map(); - validateContextVariableLValuesImpl(fn, identifierKinds); + validateContextVariableLValuesImpl(fn, identifierKinds, fn.env); } function validateContextVariableLValuesImpl( fn: HIRFunction, identifierKinds: IdentifierKinds, + env: Environment, ): void { for (const [, block] of fn.body.blocks) { for (const instr of block.instructions) { @@ -30,30 +33,30 @@ function validateContextVariableLValuesImpl( switch (value.kind) { case 'DeclareContext': case 'StoreContext': { - visit(identifierKinds, value.lvalue.place, 'context'); + visit(identifierKinds, value.lvalue.place, 'context', env); break; } case 'LoadContext': { - visit(identifierKinds, value.place, 'context'); + visit(identifierKinds, value.place, 'context', env); break; } case 'StoreLocal': case 'DeclareLocal': { - visit(identifierKinds, value.lvalue.place, 'local'); + visit(identifierKinds, value.lvalue.place, 'local', env); break; } case 'LoadLocal': { - visit(identifierKinds, value.place, 'local'); + visit(identifierKinds, value.place, 'local', env); break; } case 'PostfixUpdate': case 'PrefixUpdate': { - visit(identifierKinds, value.lvalue, 'local'); + visit(identifierKinds, value.lvalue, 'local', env); break; } case 'Destructure': { for (const lvalue of eachPatternOperand(value.lvalue.pattern)) { - visit(identifierKinds, lvalue, 'destructure'); + visit(identifierKinds, lvalue, 'destructure', env); } break; } @@ -62,18 +65,24 @@ function validateContextVariableLValuesImpl( validateContextVariableLValuesImpl( value.loweredFunc.func, identifierKinds, + env, ); break; } default: { for (const _ of eachInstructionValueLValue(value)) { - CompilerError.throwTodo({ - reason: - 'ValidateContextVariableLValues: unhandled instruction variant', - loc: value.loc, - description: `Handle '${value.kind} lvalues`, - suggestions: null, - }); + fn.env.recordError( + CompilerDiagnostic.create({ + category: ErrorCategory.Todo, + reason: + 'ValidateContextVariableLValues: unhandled instruction variant', + description: `Handle '${value.kind} lvalues`, + }).withDetails({ + kind: 'error', + loc: value.loc, + message: null, + }), + ); } } } @@ -90,6 +99,7 @@ function visit( identifiers: IdentifierKinds, place: Place, kind: 'local' | 'context' | 'destructure', + env: Environment, ): void { const prev = identifiers.get(place.identifier.id); if (prev !== undefined) { @@ -97,12 +107,18 @@ function visit( const isContext = kind === 'context'; if (wasContext !== isContext) { if (prev.kind === 'destructure' || kind === 'destructure') { - CompilerError.throwTodo({ - reason: `Support destructuring of context variables`, - loc: kind === 'destructure' ? place.loc : prev.place.loc, - description: null, - suggestions: null, - }); + env.recordError( + CompilerDiagnostic.create({ + category: ErrorCategory.Todo, + reason: `Support destructuring of context variables`, + description: null, + }).withDetails({ + kind: 'error', + loc: kind === 'destructure' ? place.loc : prev.place.loc, + message: null, + }), + ); + return; } CompilerError.invariant(false, { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts index b8647ec7c9bd..c418b7770387 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts @@ -29,6 +29,9 @@ import { isStableType, isSubPath, isSubPathIgnoringOptionals, + isUseEffectHookType, + isUseInsertionEffectHookType, + isUseLayoutEffectHookType, isUseRefType, LoadGlobal, ManualMemoDependency, @@ -41,9 +44,7 @@ import { eachInstructionValueOperand, eachTerminalOperand, } from '../HIR/visitors'; -import {Result} from '../Utils/Result'; import {retainWhere} from '../Utils/utils'; -import {isEffectHook} from './ValidateMemoizedEffectDependencies'; const DEBUG = false; @@ -86,9 +87,7 @@ const DEBUG = false; * When we go to compute the dependencies, we then think that the user's manual dep * logic is part of what the memo computation logic. */ -export function validateExhaustiveDependencies( - fn: HIRFunction, -): Result { +export function validateExhaustiveDependencies(fn: HIRFunction): void { const env = fn.env; const reactive = collectReactiveIdentifiersHIR(fn); @@ -103,7 +102,6 @@ export function validateExhaustiveDependencies( loc: place.loc, }); } - const error = new CompilerError(); let startMemo: StartMemoize | null = null; function onStartMemoize( @@ -144,7 +142,8 @@ export function validateExhaustiveDependencies( 'all', ); if (diagnostic != null) { - error.pushDiagnostic(diagnostic); + fn.env.recordError(diagnostic); + startMemo.hasInvalidDeps = true; } } @@ -209,13 +208,12 @@ export function validateExhaustiveDependencies( effectReportMode, ); if (diagnostic != null) { - error.pushDiagnostic(diagnostic); + fn.env.recordError(diagnostic); } }, }, false, // isFunctionExpression ); - return error.asResult(); } function validateDependencies( @@ -756,6 +754,7 @@ function collectDependencies( { optional, property: value.property, + loc: value.loc, }, ], loc: value.loc, @@ -1110,3 +1109,11 @@ function createDiagnostic( suggestions: suggestion != null ? [suggestion] : null, }); } + +export function isEffectHook(identifier: Identifier): boolean { + return ( + isUseEffectHookType(identifier) || + isUseLayoutEffectHookType(identifier) || + isUseInsertionEffectHookType(identifier) + ); +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts index 7259041ec29a..a243929ddefd 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts @@ -6,13 +6,9 @@ */ import * as t from '@babel/types'; -import { - CompilerError, - CompilerErrorDetail, - ErrorCategory, -} from '../CompilerError'; +import {CompilerErrorDetail, ErrorCategory} from '../CompilerError'; import {computeUnconditionalBlocks} from '../HIR/ComputeUnconditionalBlocks'; -import {isHookName} from '../HIR/Environment'; +import {Environment, isHookName} from '../HIR/Environment'; import { HIRFunction, IdentifierId, @@ -26,7 +22,6 @@ import { eachTerminalOperand, } from '../HIR/visitors'; import {assertExhaustive} from '../Utils/utils'; -import {Result} from '../Utils/Result'; /** * Represents the possible kinds of value which may be stored at a given Place during @@ -88,20 +83,17 @@ function joinKinds(a: Kind, b: Kind): Kind { * may not appear as the callee of a conditional call. * See the note for Kind.PotentialHook for sources of potential hooks */ -export function validateHooksUsage( - fn: HIRFunction, -): Result { +export function validateHooksUsage(fn: HIRFunction): void { const unconditionalBlocks = computeUnconditionalBlocks(fn); - const errors = new CompilerError(); const errorsByPlace = new Map(); - function recordError( + function trackError( loc: SourceLocation, errorDetail: CompilerErrorDetail, ): void { if (typeof loc === 'symbol') { - errors.pushErrorDetail(errorDetail); + fn.env.recordError(errorDetail); } else { errorsByPlace.set(loc, errorDetail); } @@ -121,7 +113,7 @@ export function validateHooksUsage( * If that same place is also used as a conditional call, upgrade the error to a conditonal hook error */ if (previousError === undefined || previousError.reason !== reason) { - recordError( + trackError( place.loc, new CompilerErrorDetail({ category: ErrorCategory.Hooks, @@ -137,7 +129,7 @@ export function validateHooksUsage( const previousError = typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined; if (previousError === undefined) { - recordError( + trackError( place.loc, new CompilerErrorDetail({ category: ErrorCategory.Hooks, @@ -154,7 +146,7 @@ export function validateHooksUsage( const previousError = typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined; if (previousError === undefined) { - recordError( + trackError( place.loc, new CompilerErrorDetail({ category: ErrorCategory.Hooks, @@ -399,7 +391,7 @@ export function validateHooksUsage( } case 'ObjectMethod': case 'FunctionExpression': { - visitFunctionExpression(errors, instr.value.loweredFunc.func); + visitFunctionExpression(fn.env, instr.value.loweredFunc.func); break; } default: { @@ -424,18 +416,17 @@ export function validateHooksUsage( } for (const [, error] of errorsByPlace) { - errors.pushErrorDetail(error); + fn.env.recordError(error); } - return errors.asResult(); } -function visitFunctionExpression(errors: CompilerError, fn: HIRFunction): void { +function visitFunctionExpression(env: Environment, fn: HIRFunction): void { for (const [, block] of fn.body.blocks) { for (const instr of block.instructions) { switch (instr.value.kind) { case 'ObjectMethod': case 'FunctionExpression': { - visitFunctionExpression(errors, instr.value.loweredFunc.func); + visitFunctionExpression(env, instr.value.loweredFunc.func); break; } case 'MethodCall': @@ -446,7 +437,7 @@ function visitFunctionExpression(errors: CompilerError, fn: HIRFunction): void { : instr.value.property; const hookKind = getHookKind(fn.env, callee.identifier); if (hookKind != null) { - errors.pushErrorDetail( + env.recordError( new CompilerErrorDetail({ category: ErrorCategory.Hooks, reason: diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts index 0bdb48357005..77b921512aa1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts @@ -7,6 +7,7 @@ import {CompilerDiagnostic, CompilerError, Effect} from '..'; import {ErrorCategory} from '../CompilerError'; +import {Environment} from '../HIR/Environment'; import {HIRFunction, IdentifierId, Place} from '../HIR'; import { eachInstructionLValue, @@ -27,15 +28,15 @@ export function validateLocalsNotReassignedAfterRender(fn: HIRFunction): void { contextVariables, false, false, + fn.env, ); if (reassignment !== null) { - const errors = new CompilerError(); const variable = reassignment.identifier.name != null && reassignment.identifier.name.kind === 'named' ? `\`${reassignment.identifier.name.value}\`` : 'variable'; - errors.pushDiagnostic( + fn.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Immutability, reason: 'Cannot reassign variable after render completes', @@ -46,7 +47,6 @@ export function validateLocalsNotReassignedAfterRender(fn: HIRFunction): void { message: `Cannot reassign ${variable} after render completes`, }), ); - throw errors; } } @@ -55,6 +55,7 @@ function getContextReassignment( contextVariables: Set, isFunctionExpression: boolean, isAsync: boolean, + env: Environment, ): Place | null { const reassigningFunctions = new Map(); for (const [, block] of fn.body.blocks) { @@ -68,6 +69,7 @@ function getContextReassignment( contextVariables, true, isAsync || value.loweredFunc.func.async, + env, ); if (reassignment === null) { // If the function itself doesn't reassign, does one of its dependencies? @@ -84,13 +86,12 @@ function getContextReassignment( // if the function or its depends reassign, propagate that fact on the lvalue if (reassignment !== null) { if (isAsync || value.loweredFunc.func.async) { - const errors = new CompilerError(); const variable = reassignment.identifier.name !== null && reassignment.identifier.name.kind === 'named' ? `\`${reassignment.identifier.name.value}\`` : 'variable'; - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Immutability, reason: 'Cannot reassign variable in async function', @@ -102,7 +103,7 @@ function getContextReassignment( message: `Cannot reassign ${variable}`, }), ); - throw errors; + return null; } reassigningFunctions.set(lvalue.identifier.id, reassignment); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateMemoizedEffectDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateMemoizedEffectDependencies.ts deleted file mode 100644 index 15d0be57fcf7..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateMemoizedEffectDependencies.ts +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {CompilerError} from '..'; -import {ErrorCategory} from '../CompilerError'; -import { - Identifier, - Instruction, - ReactiveFunction, - ReactiveInstruction, - ReactiveScopeBlock, - ScopeId, - isUseEffectHookType, - isUseInsertionEffectHookType, - isUseLayoutEffectHookType, -} from '../HIR'; -import {isMutable} from '../ReactiveScopes/InferReactiveScopeVariables'; -import { - ReactiveFunctionVisitor, - visitReactiveFunction, -} from '../ReactiveScopes/visitors'; -import {Result} from '../Utils/Result'; - -/** - * Validates that all known effect dependencies are memoized. The algorithm checks two things: - * - Disallow effect dependencies that should be memoized (have a reactive scope assigned) but - * where that reactive scope does not exist. This checks for cases where a reactive scope was - * pruned for some reason, such as spanning a hook. - * - Disallow effect dependencies whose a mutable range that encompasses the effect call. - * - * This latter check corresponds to any values which Forget knows may be mutable and may be mutated - * after the effect. Note that it's possible Forget may miss not memoize a value for some other reason, - * but in general this is a bug. The only reason Forget would _choose_ to skip memoization of an - * effect dependency is because it's mutated later. - * - * Example: - * - * ```javascript - * const object = {}; // mutable range starts here... - * - * useEffect(() => { - * console.log('hello'); - * }, [object]); // the dependency array picks up the mutable range of its mutable contents - * - * mutate(object); // ... mutable range ends here after this mutation - * ``` - */ -export function validateMemoizedEffectDependencies( - fn: ReactiveFunction, -): Result { - const errors = new CompilerError(); - visitReactiveFunction(fn, new Visitor(), errors); - return errors.asResult(); -} - -class Visitor extends ReactiveFunctionVisitor { - scopes: Set = new Set(); - - override visitScope( - scopeBlock: ReactiveScopeBlock, - state: CompilerError, - ): void { - this.traverseScope(scopeBlock, state); - - /* - * Record scopes that exist in the AST so we can later check to see if - * effect dependencies which should be memoized (have a scope assigned) - * actually are memoized (that scope exists). - * However, we only record scopes if *their* dependencies are also - * memoized, allowing a transitive memoization check. - */ - let areDependenciesMemoized = true; - for (const dep of scopeBlock.scope.dependencies) { - if (isUnmemoized(dep.identifier, this.scopes)) { - areDependenciesMemoized = false; - break; - } - } - if (areDependenciesMemoized) { - this.scopes.add(scopeBlock.scope.id); - for (const id of scopeBlock.scope.merged) { - this.scopes.add(id); - } - } - } - - override visitInstruction( - instruction: ReactiveInstruction, - state: CompilerError, - ): void { - this.traverseInstruction(instruction, state); - if ( - instruction.value.kind === 'CallExpression' && - isEffectHook(instruction.value.callee.identifier) && - instruction.value.args.length >= 2 - ) { - const deps = instruction.value.args[1]!; - if ( - deps.kind === 'Identifier' && - /* - * TODO: isMutable is not safe to call here as it relies on identifier mutableRange which is no longer valid at this point - * in the pipeline - */ - (isMutable(instruction as Instruction, deps) || - isUnmemoized(deps.identifier, this.scopes)) - ) { - state.push({ - category: ErrorCategory.EffectDependencies, - reason: - 'React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior', - description: null, - loc: typeof instruction.loc !== 'symbol' ? instruction.loc : null, - suggestions: null, - }); - } - } - } -} - -function isUnmemoized(operand: Identifier, scopes: Set): boolean { - return operand.scope != null && !scopes.has(operand.scope.id); -} - -export function isEffectHook(identifier: Identifier): boolean { - return ( - isUseEffectHookType(identifier) || - isUseLayoutEffectHookType(identifier) || - isUseInsertionEffectHookType(identifier) - ); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts index 0d0242d25c9d..c0c6b6d9f9af 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts @@ -5,33 +5,21 @@ * LICENSE file in the root directory of this source tree. */ -import {CompilerError, EnvironmentConfig} from '..'; +import {CompilerErrorDetail, EnvironmentConfig} from '..'; import {ErrorCategory} from '../CompilerError'; import {HIRFunction, IdentifierId} from '../HIR'; import {DEFAULT_GLOBALS} from '../HIR/Globals'; -import {Result} from '../Utils/Result'; -export function validateNoCapitalizedCalls( - fn: HIRFunction, -): Result { +export function validateNoCapitalizedCalls(fn: HIRFunction): void { const envConfig: EnvironmentConfig = fn.env.config; const ALLOW_LIST = new Set([ ...DEFAULT_GLOBALS.keys(), ...(envConfig.validateNoCapitalizedCalls ?? []), ]); - /* - * The hook pattern may allow uppercase names, like React$useState, so we need to be sure that we - * do not error in those cases - */ - const hookPattern = - envConfig.hookPattern != null ? new RegExp(envConfig.hookPattern) : null; const isAllowed = (name: string): boolean => { - return ( - ALLOW_LIST.has(name) || (hookPattern != null && hookPattern.test(name)) - ); + return ALLOW_LIST.has(name); }; - const errors = new CompilerError(); const capitalLoadGlobals = new Map(); const capitalizedProperties = new Map(); const reason = @@ -56,13 +44,16 @@ export function validateNoCapitalizedCalls( const calleeIdentifier = value.callee.identifier.id; const calleeName = capitalLoadGlobals.get(calleeIdentifier); if (calleeName != null) { - CompilerError.throwInvalidReact({ - category: ErrorCategory.CapitalizedCalls, - reason, - description: `${calleeName} may be a component`, - loc: value.loc, - suggestions: null, - }); + fn.env.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.CapitalizedCalls, + reason, + description: `${calleeName} may be a component`, + loc: value.loc, + suggestions: null, + }), + ); + continue; } break; } @@ -80,18 +71,19 @@ export function validateNoCapitalizedCalls( const propertyIdentifier = value.property.identifier.id; const propertyName = capitalizedProperties.get(propertyIdentifier); if (propertyName != null) { - errors.push({ - category: ErrorCategory.CapitalizedCalls, - reason, - description: `${propertyName} may be a component`, - loc: value.loc, - suggestions: null, - }); + fn.env.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.CapitalizedCalls, + reason, + description: `${propertyName} may be a component`, + loc: value.loc, + suggestions: null, + }), + ); } break; } } } } - return errors.asResult(); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts index a80f1efc636e..380f24433178 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.ts @@ -6,7 +6,7 @@ */ import {CompilerError, SourceLocation} from '..'; -import {ErrorCategory} from '../CompilerError'; +import {CompilerErrorDetail, ErrorCategory} from '../CompilerError'; import { ArrayExpression, BlockId, @@ -20,6 +20,7 @@ import { eachInstructionValueOperand, eachTerminalOperand, } from '../HIR/visitors'; +import {Environment} from '../HIR/Environment'; /** * Validates that useEffect is not used for derived computations which could/should @@ -49,8 +50,6 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void { const functions: Map = new Map(); const locals: Map = new Map(); - const errors = new CompilerError(); - for (const block of fn.body.blocks.values()) { for (const instr of block.instructions) { const {lvalue, value} = instr; @@ -90,22 +89,19 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void { validateEffect( effectFunction.loweredFunc.func, dependencies, - errors, + fn.env, ); } } } } } - if (errors.hasAnyErrors()) { - throw errors; - } } function validateEffect( effectFunction: HIRFunction, effectDeps: Array, - errors: CompilerError, + env: Environment, ): void { for (const operand of effectFunction.context) { if (isSetStateType(operand.identifier)) { @@ -219,13 +215,15 @@ function validateEffect( } for (const loc of setStateLocations) { - errors.push({ - category: ErrorCategory.EffectDerivationsOfState, - reason: - 'Values derived from props and state should be calculated during render, not in an effect. (https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state)', - description: null, - loc, - suggestions: null, - }); + env.recordError( + new CompilerErrorDetail({ + category: ErrorCategory.EffectDerivationsOfState, + reason: + 'Values derived from props and state should be calculated during render, not in an effect. (https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state)', + description: null, + loc, + suggestions: null, + }), + ); } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts index 0c5b5a9a3193..3fc28ffb9e76 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {CompilerDiagnostic, CompilerError, Effect} from '..'; +import {CompilerDiagnostic, Effect} from '..'; import {ErrorCategory} from '../CompilerError'; import { HIRFunction, @@ -18,7 +18,6 @@ import { eachTerminalOperand, } from '../HIR/visitors'; import {AliasingEffect} from '../Inference/AliasingEffects'; -import {Result} from '../Utils/Result'; /** * Validates that functions with known mutations (ie due to types) cannot be passed @@ -43,10 +42,7 @@ import {Result} from '../Utils/Result'; * This pass detects functions with *known* mutations (Store or Mutate, not ConditionallyMutate) * that are passed where a frozen value is expected and rejects them. */ -export function validateNoFreezingKnownMutableFunctions( - fn: HIRFunction, -): Result { - const errors = new CompilerError(); +export function validateNoFreezingKnownMutableFunctions(fn: HIRFunction): void { const contextMutationEffects: Map< IdentifierId, Extract @@ -63,7 +59,7 @@ export function validateNoFreezingKnownMutableFunctions( place.identifier.name.kind === 'named' ? `\`${place.identifier.name.value}\`` : 'a local variable'; - errors.pushDiagnostic( + fn.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Immutability, reason: 'Cannot modify local variables after render completes', @@ -162,5 +158,4 @@ export function validateNoFreezingKnownMutableFunctions( visitOperand(operand); } } - return errors.asResult(); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoImpureFunctionsInRender.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoImpureFunctionsInRender.ts index ca0612d80ce2..ba089fbd1bc4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoImpureFunctionsInRender.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoImpureFunctionsInRender.ts @@ -5,11 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import {CompilerDiagnostic, CompilerError} from '..'; +import {CompilerDiagnostic} from '..'; import {ErrorCategory} from '../CompilerError'; import {HIRFunction} from '../HIR'; import {getFunctionCallSignature} from '../Inference/InferMutationAliasingEffects'; -import {Result} from '../Utils/Result'; /** * Checks that known-impure functions are not called during render. Examples of invalid functions to @@ -20,10 +19,7 @@ import {Result} from '../Utils/Result'; * this in several of our validation passes and should unify those analyses into a reusable helper * and use it here. */ -export function validateNoImpureFunctionsInRender( - fn: HIRFunction, -): Result { - const errors = new CompilerError(); +export function validateNoImpureFunctionsInRender(fn: HIRFunction): void { for (const [, block] of fn.body.blocks) { for (const instr of block.instructions) { const value = instr.value; @@ -35,7 +31,7 @@ export function validateNoImpureFunctionsInRender( callee.identifier.type, ); if (signature != null && signature.impure === true) { - errors.pushDiagnostic( + fn.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Purity, reason: 'Cannot call impure function during render', @@ -55,5 +51,4 @@ export function validateNoImpureFunctionsInRender( } } } - return errors.asResult(); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts index dd7b04a11d69..7da564205475 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoRefAccessInRender.ts @@ -15,21 +15,18 @@ import { GeneratedSource, HIRFunction, IdentifierId, - Identifier, Place, SourceLocation, getHookKindForType, isRefValueType, isUseRefType, } from '../HIR'; -import {BuiltInEventHandlerId} from '../HIR/ObjectShape'; import { eachInstructionOperand, eachInstructionValueOperand, eachPatternOperand, eachTerminalOperand, } from '../HIR/visitors'; -import {Err, Ok, Result} from '../Utils/Result'; import {retainWhere} from '../Utils/utils'; /** @@ -122,12 +119,14 @@ class Env { } } -export function validateNoRefAccessInRender( - fn: HIRFunction, -): Result { +export function validateNoRefAccessInRender(fn: HIRFunction): void { const env = new Env(); collectTemporariesSidemap(fn, env); - return validateNoRefAccessInRenderImpl(fn, env).map(_ => undefined); + const errors = new CompilerError(); + validateNoRefAccessInRenderImpl(fn, env, errors); + for (const detail of errors.details) { + fn.env.recordError(detail); + } } function collectTemporariesSidemap(fn: HIRFunction, env: Env): void { @@ -178,11 +177,6 @@ function refTypeOfType(place: Place): RefAccessType { } } -function isEventHandlerType(identifier: Identifier): boolean { - const type = identifier.type; - return type.kind === 'Function' && type.shapeId === BuiltInEventHandlerId; -} - function tyEqual(a: RefAccessType, b: RefAccessType): boolean { if (a.kind !== b.kind) { return false; @@ -312,7 +306,8 @@ function joinRefAccessTypes(...types: Array): RefAccessType { function validateNoRefAccessInRenderImpl( fn: HIRFunction, env: Env, -): Result { + errors: CompilerError, +): RefAccessType { let returnValues: Array = []; let place; for (const param of fn.params) { @@ -343,7 +338,6 @@ function validateNoRefAccessInRenderImpl( env.resetChanged(); returnValues = []; const safeBlocks: Array<{block: BlockId; ref: RefId}> = []; - const errors = new CompilerError(); for (const [, block] of fn.body.blocks) { retainWhere(safeBlocks, entry => entry.block !== block.id); for (const phi of block.phis) { @@ -439,13 +433,15 @@ function validateNoRefAccessInRenderImpl( case 'FunctionExpression': { let returnType: RefAccessType = {kind: 'None'}; let readRefEffect = false; + const innerErrors = new CompilerError(); const result = validateNoRefAccessInRenderImpl( instr.value.loweredFunc.func, env, + innerErrors, ); - if (result.isOk()) { - returnType = result.unwrap(); - } else if (result.isErr()) { + if (!innerErrors.hasAnyErrors()) { + returnType = result; + } else { readRefEffect = true; } env.set(instr.lvalue.identifier.id, { @@ -491,29 +487,26 @@ function validateNoRefAccessInRenderImpl( */ if (!didError) { const isRefLValue = isUseRefType(instr.lvalue.identifier); - const isEventHandlerLValue = isEventHandlerType( - instr.lvalue.identifier, - ); - for (const operand of eachInstructionValueOperand(instr.value)) { - /** - * By default we check that function call operands are not refs, - * ref values, or functions that can access refs. - */ - if ( - isRefLValue || - isEventHandlerLValue || - (hookKind != null && - hookKind !== 'useState' && - hookKind !== 'useReducer') - ) { + if ( + isRefLValue || + (hookKind != null && + hookKind !== 'useState' && + hookKind !== 'useReducer') + ) { + for (const operand of eachInstructionValueOperand( + instr.value, + )) { /** * Allow passing refs or ref-accessing functions when: * 1. lvalue is a ref (mergeRefs pattern: `mergeRefs(ref1, ref2)`) - * 2. lvalue is an event handler (DOM events execute outside render) - * 3. calling hooks (independently validated for ref safety) + * 2. calling hooks (independently validated for ref safety) */ validateNoDirectRefValueAccess(errors, operand, env); - } else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) { + } + } else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) { + for (const operand of eachInstructionValueOperand( + instr.value, + )) { /** * Special case: the lvalue is passed as a jsx child * @@ -522,7 +515,98 @@ function validateNoRefAccessInRenderImpl( * render function which attempts to obey the rules. */ validateNoRefValueAccess(errors, env, operand); - } else { + } + } else if (hookKind == null && instr.effects != null) { + /** + * For non-hook functions with known aliasing effects, use the + * effects to determine what validation to apply for each place. + * Track visited id:kind pairs to avoid duplicate errors. + */ + const visitedEffects: Set = new Set(); + for (const effect of instr.effects) { + let place: Place | null = null; + let validation: 'ref-passed' | 'direct-ref' | 'none' = 'none'; + switch (effect.kind) { + case 'Freeze': { + place = effect.value; + validation = 'direct-ref'; + break; + } + case 'Mutate': + case 'MutateTransitive': + case 'MutateConditionally': + case 'MutateTransitiveConditionally': { + place = effect.value; + validation = 'ref-passed'; + break; + } + case 'Render': { + place = effect.place; + validation = 'ref-passed'; + break; + } + case 'Capture': + case 'Alias': + case 'MaybeAlias': + case 'Assign': + case 'CreateFrom': { + place = effect.from; + validation = 'ref-passed'; + break; + } + case 'ImmutableCapture': { + /** + * ImmutableCapture can come from two sources: + * 1. A known signature that explicitly freezes the operand + * (e.g. PanResponder.create) — safe, the function doesn't + * call callbacks during render. + * 2. Downgraded defaults when the operand is already frozen + * (e.g. foo(propRef)) — the function is unknown and may + * access the ref. + * + * We distinguish these by checking whether the same operand + * also has a Freeze effect on this instruction, which only + * comes from known signatures. + */ + place = effect.from; + const isFrozen = instr.effects.some( + e => + e.kind === 'Freeze' && + e.value.identifier.id === effect.from.identifier.id, + ); + validation = isFrozen ? 'direct-ref' : 'ref-passed'; + break; + } + case 'Create': + case 'CreateFunction': + case 'Apply': + case 'Impure': + case 'MutateFrozen': + case 'MutateGlobal': { + break; + } + } + if (place !== null && validation !== 'none') { + const key = `${place.identifier.id}:${validation}`; + if (!visitedEffects.has(key)) { + visitedEffects.add(key); + if (validation === 'direct-ref') { + validateNoDirectRefValueAccess(errors, place, env); + } else { + validateNoRefPassedToFunction( + errors, + env, + place, + place.loc, + ); + } + } + } + } + } else { + for (const operand of eachInstructionValueOperand( + instr.value, + )) { validateNoRefPassedToFunction( errors, env, @@ -741,7 +825,7 @@ function validateNoRefAccessInRenderImpl( } if (errors.hasAnyErrors()) { - return Err(errors); + return {kind: 'None'}; } } @@ -750,10 +834,8 @@ function validateNoRefAccessInRenderImpl( loc: GeneratedSource, }); - return Ok( - joinRefAccessTypes( - ...returnValues.filter((env): env is RefAccessType => env !== undefined), - ), + return joinRefAccessTypes( + ...returnValues.filter((env): env is RefAccessType => env !== undefined), ); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInRender.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInRender.ts index 28a1de4235ae..43db75110798 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInRender.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInRender.ts @@ -13,7 +13,6 @@ import { import {HIRFunction, IdentifierId, isSetStateType} from '../HIR'; import {computeUnconditionalBlocks} from '../HIR/ComputeUnconditionalBlocks'; import {eachInstructionValueOperand} from '../HIR/visitors'; -import {Result} from '../Utils/Result'; /** * Validates that the given function does not have an infinite update loop @@ -43,17 +42,21 @@ import {Result} from '../Utils/Result'; * y(); * ``` */ -export function validateNoSetStateInRender( - fn: HIRFunction, -): Result { +export function validateNoSetStateInRender(fn: HIRFunction): void { const unconditionalSetStateFunctions: Set = new Set(); - return validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions); + const errors = validateNoSetStateInRenderImpl( + fn, + unconditionalSetStateFunctions, + ); + for (const detail of errors.details) { + fn.env.recordError(detail); + } } function validateNoSetStateInRenderImpl( fn: HIRFunction, unconditionalSetStateFunctions: Set, -): Result { +): CompilerError { const unconditionalBlocks = computeUnconditionalBlocks(fn); let activeManualMemoId: number | null = null; const errors = new CompilerError(); @@ -92,7 +95,7 @@ function validateNoSetStateInRenderImpl( validateNoSetStateInRenderImpl( instr.value.loweredFunc.func, unconditionalSetStateFunctions, - ).isErr() + ).hasAnyErrors() ) { // This function expression unconditionally calls a setState unconditionalSetStateFunctions.add(instr.lvalue.identifier.id); @@ -183,5 +186,5 @@ function validateNoSetStateInRenderImpl( } } - return errors.asResult(); + return errors; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts index 5591a6a29c8d..d39aa307dfb3 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts @@ -27,6 +27,7 @@ import { ScopeId, SourceLocation, } from '../HIR'; +import {Environment} from '../HIR/Environment'; import {printIdentifier, printManualMemoDependency} from '../HIR/PrintHIR'; import { eachInstructionValueLValue, @@ -37,7 +38,6 @@ import { ReactiveFunctionVisitor, visitReactiveFunction, } from '../ReactiveScopes/visitors'; -import {Result} from '../Utils/Result'; import {getOrInsertDefault} from '../Utils/utils'; /** @@ -47,15 +47,12 @@ import {getOrInsertDefault} from '../Utils/utils'; * This can occur if a value's mutable range somehow extended to include a hook and * was pruned. */ -export function validatePreservedManualMemoization( - fn: ReactiveFunction, -): Result { +export function validatePreservedManualMemoization(fn: ReactiveFunction): void { const state = { - errors: new CompilerError(), + env: fn.env, manualMemoState: null, }; visitReactiveFunction(fn, new Visitor(), state); - return state.errors.asResult(); } const DEBUG = false; @@ -113,7 +110,7 @@ type ManualMemoBlockState = { }; type VisitorState = { - errors: CompilerError; + env: Environment; manualMemoState: ManualMemoBlockState | null; }; @@ -233,7 +230,7 @@ function validateInferredDep( temporaries: Map, declsWithinMemoBlock: Set, validDepsInMemoBlock: Array, - errorState: CompilerError, + env: Environment, memoLocation: SourceLocation, ): void { let normalizedDep: ManualMemoDependency; @@ -283,7 +280,7 @@ function validateInferredDep( errorDiagnostic = merge(errorDiagnostic ?? compareResult, compareResult); } } - errorState.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.PreserveManualMemo, reason: 'Existing memoization could not be preserved', @@ -429,7 +426,7 @@ class Visitor extends ReactiveFunctionVisitor { this.temporaries, state.manualMemoState.decls, state.manualMemoState.depsFromSource, - state.errors, + state.env, state.manualMemoState.loc, ); } @@ -489,16 +486,25 @@ class Visitor extends ReactiveFunctionVisitor { ids.add(value.place.identifier); } if (value.kind === 'StartMemoize') { - let depsFromSource: Array | null = null; - if (value.deps != null) { - depsFromSource = value.deps; - } CompilerError.invariant(state.manualMemoState == null, { reason: 'Unexpected nested StartMemoize instructions', description: `Bad manual memoization ids: ${state.manualMemoState?.manualMemoId}, ${value.manualMemoId}`, loc: value.loc, }); + if (value.hasInvalidDeps === true) { + /* + * ValidateExhaustiveDependencies already reported an error for this + * memo block, skip validation to avoid duplicate errors + */ + return; + } + + let depsFromSource: Array | null = null; + if (value.deps != null) { + depsFromSource = value.deps; + } + state.manualMemoState = { loc: instruction.loc, decls: new Set(), @@ -532,7 +538,7 @@ class Visitor extends ReactiveFunctionVisitor { !this.scopes.has(identifier.scope.id) && !this.prunedScopes.has(identifier.scope.id) ) { - state.errors.pushDiagnostic( + state.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.PreserveManualMemo, reason: 'Existing memoization could not be preserved', @@ -550,12 +556,15 @@ class Visitor extends ReactiveFunctionVisitor { } } if (value.kind === 'FinishMemoize') { + if (state.manualMemoState == null) { + // StartMemoize had invalid deps, skip validation + return; + } CompilerError.invariant( - state.manualMemoState != null && - state.manualMemoState.manualMemoId === value.manualMemoId, + state.manualMemoState.manualMemoId === value.manualMemoId, { reason: 'Unexpected mismatch between StartMemoize and FinishMemoize', - description: `Encountered StartMemoize id=${state.manualMemoState?.manualMemoId} followed by FinishMemoize id=${value.manualMemoId}`, + description: `Encountered StartMemoize id=${state.manualMemoState.manualMemoId} followed by FinishMemoize id=${value.manualMemoId}`, loc: value.loc, }, ); @@ -578,7 +587,7 @@ class Visitor extends ReactiveFunctionVisitor { for (const identifier of decls) { if (isUnmemoized(identifier, this.scopes)) { - state.errors.pushDiagnostic( + state.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.PreserveManualMemo, reason: 'Existing memoization could not be preserved', diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateSourceLocations.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateSourceLocations.ts index 24804386aac8..50f4c0e16038 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateSourceLocations.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateSourceLocations.ts @@ -7,9 +7,9 @@ import {NodePath} from '@babel/traverse'; import * as t from '@babel/types'; -import {CompilerDiagnostic, CompilerError, ErrorCategory} from '..'; +import {CompilerDiagnostic, ErrorCategory} from '..'; import {CodegenFunction} from '../ReactiveScopes'; -import {Result} from '../Utils/Result'; +import {Environment} from '../HIR/Environment'; /** * IMPORTANT: This validation is only intended for use in unit tests. @@ -123,9 +123,8 @@ export function validateSourceLocations( t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression >, generatedAst: CodegenFunction, -): Result { - const errors = new CompilerError(); - + env: Environment, +): void { /* * Step 1: Collect important locations from the original source * Note: Multiple node types can share the same location (e.g. VariableDeclarator and Identifier) @@ -240,7 +239,7 @@ export function validateSourceLocations( loc: t.SourceLocation, nodeType: string, ): void => { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Todo, reason: 'Important source location missing in generated code', @@ -260,7 +259,7 @@ export function validateSourceLocations( expectedType: string, actualTypes: Set, ): void => { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.Todo, reason: @@ -308,6 +307,4 @@ export function validateSourceLocations( } } } - - return errors.asResult(); } diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateUseMemo.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateUseMemo.ts index 2bccda3a2e93..87c6ebd1a2a4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateUseMemo.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateUseMemo.ts @@ -16,14 +16,13 @@ import { IdentifierId, SourceLocation, } from '../HIR'; +import {Environment} from '../HIR/Environment'; import { eachInstructionValueOperand, eachTerminalOperand, } from '../HIR/visitors'; -import {Result} from '../Utils/Result'; -export function validateUseMemo(fn: HIRFunction): Result { - const errors = new CompilerError(); +export function validateUseMemo(fn: HIRFunction): void { const voidMemoErrors = new CompilerError(); const useMemos = new Set(); const react = new Set(); @@ -91,7 +90,7 @@ export function validateUseMemo(fn: HIRFunction): Result { firstParam.kind === 'Identifier' ? firstParam.loc : firstParam.place.loc; - errors.pushDiagnostic( + fn.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: 'useMemo() callbacks may not accept parameters', @@ -107,7 +106,7 @@ export function validateUseMemo(fn: HIRFunction): Result { } if (body.loweredFunc.func.async || body.loweredFunc.func.generator) { - errors.pushDiagnostic( + fn.env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: @@ -123,7 +122,7 @@ export function validateUseMemo(fn: HIRFunction): Result { ); } - validateNoContextVariableAssignment(body.loweredFunc.func, errors); + validateNoContextVariableAssignment(body.loweredFunc.func, fn.env); if (fn.env.config.validateNoVoidUseMemo) { if (!hasNonVoidReturn(body.loweredFunc.func)) { @@ -177,12 +176,11 @@ export function validateUseMemo(fn: HIRFunction): Result { } } fn.env.logErrors(voidMemoErrors.asResult()); - return errors.asResult(); } function validateNoContextVariableAssignment( fn: HIRFunction, - errors: CompilerError, + env: Environment, ): void { const context = new Set(fn.context.map(place => place.identifier.id)); for (const block of fn.body.blocks.values()) { @@ -191,7 +189,7 @@ function validateNoContextVariableAssignment( switch (value.kind) { case 'StoreContext': { if (context.has(value.lvalue.place.identifier.id)) { - errors.pushDiagnostic( + env.recordError( CompilerDiagnostic.create({ category: ErrorCategory.UseMemo, reason: diff --git a/compiler/packages/babel-plugin-react-compiler/src/Validation/index.ts b/compiler/packages/babel-plugin-react-compiler/src/Validation/index.ts index 44abf3241d35..3169bd63558a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Validation/index.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Validation/index.ts @@ -7,7 +7,6 @@ export {validateContextVariableLValues} from './ValidateContextVariableLValues'; export {validateHooksUsage} from './ValidateHooksUsage'; -export {validateMemoizedEffectDependencies} from './ValidateMemoizedEffectDependencies'; export {validateNoCapitalizedCalls} from './ValidateNoCapitalizedCalls'; export {validateNoRefAccessInRender} from './ValidateNoRefAccessInRender'; export {validateNoSetStateInRender} from './ValidateNoSetStateInRender'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/envConfig-test.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/envConfig-test.ts index 933990b6bdd9..406b5cd3c422 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/envConfig-test.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/envConfig-test.ts @@ -24,24 +24,6 @@ describe('parseConfigPragma()', () => { ); }); - it('effect autodeps config must have at least 1 required argument', () => { - expect(() => { - validateEnvironmentConfig({ - inferEffectDependencies: [ - { - function: { - source: 'react', - importSpecifierName: 'useEffect', - }, - autodepsIndex: 0, - }, - ], - } as any); - }).toThrowErrorMatchingInlineSnapshot( - `"Error: Could not validate environment config. Update React Compiler config to fix the error. Validation error: AutodepsIndex must be > 0 at "inferEffectDependencies[0].autodepsIndex"."`, - ); - }); - it('can parse stringy enums', () => { const stringyHook = { effectKind: 'freeze', diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md index aff7a2e7b999..a945e86baafb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/align-scopes-within-nested-valueblock-in-array.expect.md @@ -51,9 +51,9 @@ import { Stringify, identity, makeArray, mutate } from "shared-runtime"; function Foo(t0) { const $ = _c(3); const { cond1, cond2 } = t0; + const arr = makeArray({ a: 2 }, 2, []); let t1; if ($[0] !== cond1 || $[1] !== cond2) { - const arr = makeArray({ a: 2 }, 2, []); t1 = cond1 ? ( <>
{identity("foo")}
diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.expect.md deleted file mode 100644 index 5cee9a68ceba..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.expect.md +++ /dev/null @@ -1,148 +0,0 @@ - -## Input - -```javascript -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates react-hook-form's handleSubmit -function handleSubmit(callback: (data: T) => void | Promise) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -// Simulates an upload function -async function upload(file: any): Promise<{blob: {url: string}}> { - return {blob: {url: 'https://example.com/file.jpg'}}; -} - -interface SignatureRef { - toFile(): any; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = async (value: any) => { - // This should be allowed: accessing ref.current in an async event handler - // that's wrapped and passed to onSubmit prop - let sigUrl: string; - if (value.hasSignature) { - const {blob} = await upload(ref.current?.toFile()); - sigUrl = blob?.url || ''; - } else { - sigUrl = value.signature; - } - console.log('Signature URL:', sigUrl); - }; - - return ( -
- - -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableInferEventHandlers -import { useRef } from "react"; - -// Simulates react-hook-form's handleSubmit -function handleSubmit(callback) { - const $ = _c(2); - let t0; - if ($[0] !== callback) { - t0 = (event) => { - event.preventDefault(); - callback({} as T); - }; - $[0] = callback; - $[1] = t0; - } else { - t0 = $[1]; - } - return t0; -} - -// Simulates an upload function -async function upload(file) { - const $ = _c(1); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = { blob: { url: "https://example.com/file.jpg" } }; - $[0] = t0; - } else { - t0 = $[0]; - } - return t0; -} - -interface SignatureRef { - toFile(): any; -} - -function Component() { - const $ = _c(4); - const ref = useRef(null); - - const onSubmit = async (value) => { - let sigUrl; - if (value.hasSignature) { - const { blob } = await upload(ref.current?.toFile()); - sigUrl = blob?.url || ""; - } else { - sigUrl = value.signature; - } - - console.log("Signature URL:", sigUrl); - }; - - const t0 = handleSubmit(onSubmit); - let t1; - let t2; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - t2 = ; - $[0] = t1; - $[1] = t2; - } else { - t1 = $[0]; - t2 = $[1]; - } - let t3; - if ($[2] !== t0) { - t3 = ( -
- {t1} - {t2} -
- ); - $[2] = t0; - $[3] = t3; - } else { - t3 = $[3]; - } - return t3; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -### Eval output -(kind: ok)
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.tsx deleted file mode 100644 index be6f6656e18f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-async-event-handler-wrapper.tsx +++ /dev/null @@ -1,48 +0,0 @@ -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates react-hook-form's handleSubmit -function handleSubmit(callback: (data: T) => void | Promise) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -// Simulates an upload function -async function upload(file: any): Promise<{blob: {url: string}}> { - return {blob: {url: 'https://example.com/file.jpg'}}; -} - -interface SignatureRef { - toFile(): any; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = async (value: any) => { - // This should be allowed: accessing ref.current in an async event handler - // that's wrapped and passed to onSubmit prop - let sigUrl: string; - if (value.hasSignature) { - const {blob} = await upload(ref.current?.toFile()); - sigUrl = blob?.url || ''; - } else { - sigUrl = value.signature; - } - console.log('Signature URL:', sigUrl); - }; - - return ( -
- - -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.expect.md deleted file mode 100644 index 5a2727965879..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.expect.md +++ /dev/null @@ -1,100 +0,0 @@ - -## Input - -```javascript -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates react-hook-form's handleSubmit or similar event handler wrappers -function handleSubmit(callback: (data: T) => void) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = (data: any) => { - // This should be allowed: accessing ref.current in an event handler - // that's wrapped by handleSubmit and passed to onSubmit prop - if (ref.current !== null) { - console.log(ref.current.value); - } - }; - - return ( - <> - -
- -
- - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableInferEventHandlers -import { useRef } from "react"; - -// Simulates react-hook-form's handleSubmit or similar event handler wrappers -function handleSubmit(callback) { - const $ = _c(2); - let t0; - if ($[0] !== callback) { - t0 = (event) => { - event.preventDefault(); - callback({} as T); - }; - $[0] = callback; - $[1] = t0; - } else { - t0 = $[1]; - } - return t0; -} - -function Component() { - const $ = _c(1); - const ref = useRef(null); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - const onSubmit = (data) => { - if (ref.current !== null) { - console.log(ref.current.value); - } - }; - t0 = ( - <> - -
- -
- - ); - $[0] = t0; - } else { - t0 = $[0]; - } - return t0; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -### Eval output -(kind: ok)
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.tsx deleted file mode 100644 index f305a1f9ac6e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-access-in-event-handler-wrapper.tsx +++ /dev/null @@ -1,36 +0,0 @@ -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates react-hook-form's handleSubmit or similar event handler wrappers -function handleSubmit(callback: (data: T) => void) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = (data: any) => { - // This should be allowed: accessing ref.current in an event handler - // that's wrapped by handleSubmit and passed to onSubmit prop - if (ref.current !== null) { - console.log(ref.current.value); - } - }; - - return ( - <> - -
- -
- - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.expect.md deleted file mode 100644 index 1d95318e5c08..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.expect.md +++ /dev/null @@ -1,53 +0,0 @@ - -## Input - -```javascript -// @validateNoCapitalizedCalls @hookPattern:".*\b(use[^$]+)$" -import * as React from 'react'; -const React$useState = React.useState; -const THIS_IS_A_CONSTANT = () => {}; -function Component() { - const b = Boolean(true); // OK - const n = Number(3); // OK - const s = String('foo'); // OK - const [state, setState] = React$useState(0); // OK - const [state2, setState2] = React.useState(1); // OK - const constant = THIS_IS_A_CONSTANT(); // OK - return 3; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [], - isComponent: true, -}; - -``` - -## Code - -```javascript -// @validateNoCapitalizedCalls @hookPattern:".*\b(use[^$]+)$" -import * as React from "react"; -const React$useState = React.useState; -const THIS_IS_A_CONSTANT = () => {}; -function Component() { - Boolean(true); - Number(3); - String("foo"); - React$useState(0); - React.useState(1); - THIS_IS_A_CONSTANT(); - return 3; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [], - isComponent: true, -}; - -``` - -### Eval output -(kind: ok) 3 \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.js deleted file mode 100644 index 59fdf7c5d07f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capitalized-function-allowlist.js +++ /dev/null @@ -1,19 +0,0 @@ -// @validateNoCapitalizedCalls @hookPattern:".*\b(use[^$]+)$" -import * as React from 'react'; -const React$useState = React.useState; -const THIS_IS_A_CONSTANT = () => {}; -function Component() { - const b = Boolean(true); // OK - const n = Number(3); // OK - const s = String('foo'); // OK - const [state, setState] = React$useState(0); // OK - const [state2, setState2] = React.useState(1); // OK - const constant = THIS_IS_A_CONSTANT(); // OK - return 3; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [], - isComponent: true, -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.expect.md deleted file mode 100644 index cc2e8ee27b25..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @enableChangeDetectionForDebugging -function Component(props) { - let x = null; - if (props.cond) { - x = []; - x.push(props.value); - } - return x; -} - -``` - -## Code - -```javascript -import { $structuralCheck } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @enableChangeDetectionForDebugging -function Component(props) { - const $ = _c(2); - let x = null; - if (props.cond) { - { - x = []; - x.push(props.value); - let condition = $[0] !== props.value; - if (!condition) { - let old$x = $[1]; - $structuralCheck(old$x, x, "x", "Component", "cached", "(3:6)"); - } - $[0] = props.value; - $[1] = x; - if (condition) { - x = []; - x.push(props.value); - $structuralCheck($[1], x, "x", "Component", "recomputed", "(3:6)"); - x = $[1]; - } - } - } - - return x; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.js deleted file mode 100644 index 8ccc3d30f091..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/change-detect-reassign.js +++ /dev/null @@ -1,9 +0,0 @@ -// @enableChangeDetectionForDebugging -function Component(props) { - let x = null; - if (props.cond) { - x = []; - x.push(props.value); - } - return x; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.expect.md deleted file mode 100644 index 6ec8520f9ec9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.expect.md +++ /dev/null @@ -1,39 +0,0 @@ - -## Input - -```javascript -// @enableEmitFreeze @enableEmitInstrumentForget - -function useFoo(props) { - return foo(props.x); -} - -``` - -## Code - -```javascript -import { - makeReadOnly, - shouldInstrument, - useRenderCounter, -} from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @enableEmitFreeze @enableEmitInstrumentForget - -function useFoo(props) { - if (DEV && shouldInstrument) - useRenderCounter("useFoo", "/codegen-emit-imports-same-source.ts"); - const $ = _c(2); - let t0; - if ($[0] !== props.x) { - t0 = foo(props.x); - $[0] = props.x; - $[1] = __DEV__ ? makeReadOnly(t0, "useFoo") : t0; - } else { - t0 = $[1]; - } - return t0; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.js deleted file mode 100644 index bd66353319d6..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-imports-same-source.js +++ /dev/null @@ -1,5 +0,0 @@ -// @enableEmitFreeze @enableEmitInstrumentForget - -function useFoo(props) { - return foo(props.x); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md deleted file mode 100644 index 602e49672e26..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.expect.md +++ /dev/null @@ -1,44 +0,0 @@ - -## Input - -```javascript -// @enableEmitFreeze true - -function MyComponentName(props) { - let x = {}; - foo(x, props.a); - foo(x, props.b); - - let y = []; - y.push(x); - return y; -} - -``` - -## Code - -```javascript -import { makeReadOnly } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @enableEmitFreeze true - -function MyComponentName(props) { - const $ = _c(3); - let y; - if ($[0] !== props.a || $[1] !== props.b) { - const x = {}; - foo(x, props.a); - foo(x, props.b); - y = []; - y.push(x); - $[0] = props.a; - $[1] = props.b; - $[2] = __DEV__ ? makeReadOnly(y, "MyComponentName") : y; - } else { - y = $[2]; - } - return y; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.js deleted file mode 100644 index 8ad3e859e6ea..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-emit-make-read-only.js +++ /dev/null @@ -1,11 +0,0 @@ -// @enableEmitFreeze true - -function MyComponentName(props) { - let x = {}; - foo(x, props.a); - foo(x, props.b); - - let y = []; - y.push(x); - return y; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md index deb87c9d8a0c..a6ee8a798b58 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md @@ -24,18 +24,9 @@ function useThing(fn) { ``` Found 1 error: -Compilation Skipped: `this` is not supported syntax - -React Compiler does not support compiling functions that use `this`. - -error.reserved-words.ts:8:28 - 6 | - 7 | if (ref.current === null) { -> 8 | ref.current = function (this: unknown, ...args) { - | ^^^^^^^^^^^^^ `this` was used here - 9 | return fnRef.current.call(this, ...args); - 10 | }; - 11 | } +Error: Expected a non-reserved identifier name + +`this` is a reserved word in JavaScript and cannot be used as an identifier name. ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.expect.md deleted file mode 100644 index 9b30c1b8cbbd..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @enableEmitFreeze @instrumentForget - -let makeReadOnly = 'conflicting identifier'; -function useFoo(props) { - return foo(props.x); -} - -``` - -## Code - -```javascript -import { makeReadOnly as _makeReadOnly } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @enableEmitFreeze @instrumentForget - -let makeReadOnly = "conflicting identifier"; -function useFoo(props) { - const $ = _c(2); - let t0; - if ($[0] !== props.x) { - t0 = foo(props.x); - $[0] = props.x; - $[1] = __DEV__ ? _makeReadOnly(t0, "useFoo") : t0; - } else { - t0 = $[1]; - } - return t0; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.js deleted file mode 100644 index b30c56eb5537..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-conflicting-imports.js +++ /dev/null @@ -1,6 +0,0 @@ -// @enableEmitFreeze @instrumentForget - -let makeReadOnly = 'conflicting identifier'; -function useFoo(props) { - return foo(props.x); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.expect.md deleted file mode 100644 index ee18d3d1a2d1..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -// @enableEmitFreeze @instrumentForget -function useFoo(props) { - return foo(props.x, __DEV__); -} - -``` - -## Code - -```javascript -import { makeReadOnly } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @enableEmitFreeze @instrumentForget -function useFoo(props) { - const $ = _c(2); - let t0; - if ($[0] !== props.x) { - t0 = foo(props.x, __DEV__); - $[0] = props.x; - $[1] = __DEV__ ? makeReadOnly(t0, "useFoo") : t0; - } else { - t0 = $[1]; - } - return t0; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.js deleted file mode 100644 index 62c313b67d33..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/emit-freeze-nonconflicting-global-reference.js +++ /dev/null @@ -1,4 +0,0 @@ -// @enableEmitFreeze @instrumentForget -function useFoo(props) { - return foo(props.x, __DEV__); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md index 2d633a3d0fdd..026b9f2f1101 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md @@ -17,16 +17,17 @@ function Component(props) { ``` Found 1 error: -Todo: (BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized -error._todo.computed-lval-in-destructure.ts:3:9 - 1 | function Component(props) { - 2 | const computedKey = props.key; -> 3 | const {[computedKey]: x} = props.val; - | ^^^^^^^^^^^^^^^^ (BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern + x$8. + +error._todo.computed-lval-in-destructure.ts:5:9 + 3 | const {[computedKey]: x} = props.val; 4 | - 5 | return x; +> 5 | return x; + | ^ this is uninitialized 6 | } + 7 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.expect.md deleted file mode 100644 index d8436fa2c046..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.expect.md +++ /dev/null @@ -1,34 +0,0 @@ - -## Input - -```javascript -// @enableEmitFreeze @instrumentForget -function useFoo(props) { - const __DEV__ = 'conflicting global'; - console.log(__DEV__); - return foo(props.x); -} - -``` - - -## Error - -``` -Found 1 error: - -Todo: Encountered conflicting global in generated program - -Conflict from local binding __DEV__. - -error.emit-freeze-conflicting-global.ts:3:8 - 1 | // @enableEmitFreeze @instrumentForget - 2 | function useFoo(props) { -> 3 | const __DEV__ = 'conflicting global'; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Encountered conflicting global in generated program - 4 | console.log(__DEV__); - 5 | return foo(props.x); - 6 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.js deleted file mode 100644 index 4391ad76e70a..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.emit-freeze-conflicting-global.js +++ /dev/null @@ -1,6 +0,0 @@ -// @enableEmitFreeze @instrumentForget -function useFoo(props) { - const __DEV__ = 'conflicting global'; - console.log(__DEV__); - return foo(props.x); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.expect.md new file mode 100644 index 000000000000..e66e99524fad --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +// @validateRefAccessDuringRender +/** + * This fixture tests fault tolerance: the compiler should report + * multiple independent errors rather than stopping at the first one. + * + * Error 1: Ref access during render (ref.current) + * Error 2: Mutation of frozen value (props) + */ +function Component(props) { + const ref = useRef(null); + + // Error: reading ref during render + const value = ref.current; + + // Error: mutating frozen value (props, which is frozen after hook call) + props.items = []; + + return
{value}
; +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.fault-tolerance-reports-multiple-errors.ts:16:2 + 14 | + 15 | // Error: mutating frozen value (props, which is frozen after hook call) +> 16 | props.items = []; + | ^^^^^ value cannot be modified + 17 | + 18 | return
{value}
; + 19 | } + +Error: Cannot access refs during render + +React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). + +error.fault-tolerance-reports-multiple-errors.ts:13:16 + 11 | + 12 | // Error: reading ref during render +> 13 | const value = ref.current; + | ^^^^^^^^^^^ Cannot access ref value during render + 14 | + 15 | // Error: mutating frozen value (props, which is frozen after hook call) + 16 | props.items = []; +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.js new file mode 100644 index 000000000000..f478540250ed --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.fault-tolerance-reports-multiple-errors.js @@ -0,0 +1,19 @@ +// @validateRefAccessDuringRender +/** + * This fixture tests fault tolerance: the compiler should report + * multiple independent errors rather than stopping at the first one. + * + * Error 1: Ref access during render (ref.current) + * Error 2: Mutation of frozen value (props) + */ +function Component(props) { + const ref = useRef(null); + + // Error: reading ref during render + const value = ref.current; + + // Error: mutating frozen value (props, which is frozen after hook call) + props.items = []; + + return
{value}
; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.hook-call-freezes-captured-memberexpr.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.hook-call-freezes-captured-memberexpr.expect.md index c57d55e29a3d..a7f36aac18bb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.hook-call-freezes-captured-memberexpr.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.hook-call-freezes-captured-memberexpr.expect.md @@ -29,7 +29,7 @@ export const FIXTURE_ENTRYPOINT = { ## Error ``` -Found 1 error: +Found 2 errors: Error: This value cannot be modified @@ -43,6 +43,32 @@ error.hook-call-freezes-captured-memberexpr.ts:13:2 14 | return ; 15 | } 16 | + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `x` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.hook-call-freezes-captured-memberexpr.ts:9:25 + 7 | * After this custom hook call, it's no longer valid to mutate x. + 8 | */ +> 9 | const cb = useIdentity(() => { + | ^^^^^^^ +> 10 | x.value++; + | ^^^^^^^^^^^^^^ +> 11 | }); + | ^^^^ This function may (indirectly) reassign or modify `x` after render + 12 | + 13 | x.value += count; + 14 | return ; + +error.hook-call-freezes-captured-memberexpr.ts:10:4 + 8 | */ + 9 | const cb = useIdentity(() => { +> 10 | x.value++; + | ^ This modifies `x` + 11 | }); + 12 | + 13 | x.value += count; ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.expect.md index 4aac70a93327..733a62a0a2e9 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-ReactUseMemo-async-callback.expect.md @@ -15,7 +15,7 @@ function component(a, b) { ## Error ``` -Found 1 error: +Found 2 errors: Error: useMemo() callbacks may not be async or generator functions @@ -32,6 +32,21 @@ error.invalid-ReactUseMemo-async-callback.ts:2:24 5 | return x; 6 | } 7 | + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + +error.invalid-ReactUseMemo-async-callback.ts:3:10 + 1 | function component(a, b) { + 2 | let x = React.useMemo(async () => { +> 3 | await a; + | ^ Missing dependency `a` + 4 | }, []); + 5 | return x; + 6 | } + +Inferred dependencies: `[a]` ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-conditional-setState-in-useMemo.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-conditional-setState-in-useMemo.expect.md index e4a94249623b..ee2c56fe223d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-conditional-setState-in-useMemo.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-conditional-setState-in-useMemo.expect.md @@ -22,7 +22,7 @@ function Component({item, cond}) { ## Error ``` -Found 2 errors: +Found 3 errors: Error: Calling setState from useMemo may trigger an infinite loop @@ -49,6 +49,39 @@ error.invalid-conditional-setState-in-useMemo.ts:8:6 9 | } 10 | }, [cond, key, init]); 11 | + +Error: Found missing/extra memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. Extra dependencies can cause a value to update more often than it should, resulting in performance problems such as excessive renders or effects firing too often. + +error.invalid-conditional-setState-in-useMemo.ts:7:18 + 5 | useMemo(() => { + 6 | if (cond) { +> 7 | setPrevItem(item); + | ^^^^ Missing dependency `item` + 8 | setState(0); + 9 | } + 10 | }, [cond, key, init]); + +error.invalid-conditional-setState-in-useMemo.ts:10:12 + 8 | setState(0); + 9 | } +> 10 | }, [cond, key, init]); + | ^^^ Unnecessary dependency `key`. Values declared outside of a component/hook should not be listed as dependencies as the component will not re-render if they change + 11 | + 12 | return state; + 13 | } + +error.invalid-conditional-setState-in-useMemo.ts:10:17 + 8 | setState(0); + 9 | } +> 10 | }, [cond, key, init]); + | ^^^^ Unnecessary dependency `init`. Values declared outside of a component/hook should not be listed as dependencies as the component will not re-render if they change + 11 | + 12 | return state; + 13 | } + +Inferred dependencies: `[cond, item]` ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-mutation-in-closure.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-mutation-in-closure.expect.md index 62792df4d857..bd0d587f554d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-mutation-in-closure.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-mutation-in-closure.expect.md @@ -16,7 +16,7 @@ function useInvalidMutation(options) { ## Error ``` -Found 1 error: +Found 2 errors: Error: This value cannot be modified @@ -30,6 +30,27 @@ error.invalid-mutation-in-closure.ts:4:4 5 | } 6 | return test; 7 | } + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `options` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-mutation-in-closure.ts:6:9 + 4 | options.foo = 'bar'; + 5 | } +> 6 | return test; + | ^^^^ This function may (indirectly) reassign or modify `options` after render + 7 | } + 8 | + +error.invalid-mutation-in-closure.ts:4:4 + 2 | function test() { + 3 | foo(options.foo); // error should not point on this line +> 4 | options.foo = 'bar'; + | ^^^^^^^ This modifies `options` + 5 | } + 6 | return test; + 7 | } ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-in-hook-return-value.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-in-hook-return-value.expect.md index 6379515a057d..8eb3f6ce6e6d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-in-hook-return-value.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-in-hook-return-value.expect.md @@ -15,7 +15,7 @@ function useFoo() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -29,6 +29,31 @@ error.invalid-reassign-local-in-hook-return-value.ts:4:4 5 | }; 6 | } 7 | + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `x` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-in-hook-return-value.ts:3:9 + 1 | function useFoo() { + 2 | let x = 0; +> 3 | return value => { + | ^^^^^^^^^^ +> 4 | x = value; + | ^^^^^^^^^^^^^^ +> 5 | }; + | ^^^^ This function may (indirectly) reassign or modify `x` after render + 6 | } + 7 | + +error.invalid-reassign-local-in-hook-return-value.ts:4:4 + 2 | let x = 0; + 3 | return value => { +> 4 | x = value; + | ^ This modifies `x` + 5 | }; + 6 | } + 7 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-effect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-effect.expect.md index 368b312022ff..1f87cf411bef 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-effect.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-effect.expect.md @@ -47,7 +47,7 @@ function Component() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -61,6 +61,32 @@ error.invalid-reassign-local-variable-in-effect.ts:7:4 8 | }; 9 | 10 | const onMount = newValue => { + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `local` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-effect.ts:33:12 + 31 | }; + 32 | +> 33 | useEffect(() => { + | ^^^^^^^ +> 34 | onMount(); + | ^^^^^^^^^^^^^^ +> 35 | }, [onMount]); + | ^^^^ This function may (indirectly) reassign or modify `local` after render + 36 | + 37 | return 'ok'; + 38 | } + +error.invalid-reassign-local-variable-in-effect.ts:7:4 + 5 | + 6 | const reassignLocal = newValue => { +> 7 | local = newValue; + | ^^^^^ This modifies `local` + 8 | }; + 9 | + 10 | const onMount = newValue => { ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-hook-argument.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-hook-argument.expect.md index 8c7973377d87..61b8ef46c56b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-hook-argument.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-hook-argument.expect.md @@ -48,7 +48,7 @@ function Component() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -62,6 +62,32 @@ error.invalid-reassign-local-variable-in-hook-argument.ts:8:4 9 | }; 10 | 11 | const callback = newValue => { + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `local` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-hook-argument.ts:34:14 + 32 | }; + 33 | +> 34 | useIdentity(() => { + | ^^^^^^^ +> 35 | callback(); + | ^^^^^^^^^^^^^^^ +> 36 | }); + | ^^^^ This function may (indirectly) reassign or modify `local` after render + 37 | + 38 | return 'ok'; + 39 | } + +error.invalid-reassign-local-variable-in-hook-argument.ts:8:4 + 6 | + 7 | const reassignLocal = newValue => { +> 8 | local = newValue; + | ^^^^^ This modifies `local` + 9 | }; + 10 | + 11 | const callback = newValue => { ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-jsx-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-jsx-callback.expect.md index 3ecbcc97c32f..feb3449befb3 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-jsx-callback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-reassign-local-variable-in-jsx-callback.expect.md @@ -41,7 +41,7 @@ function Component() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -55,6 +55,27 @@ error.invalid-reassign-local-variable-in-jsx-callback.ts:5:4 6 | }; 7 | 8 | const onClick = newValue => { + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `local` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-jsx-callback.ts:31:26 + 29 | }; + 30 | +> 31 | return ; + | ^^^^^^^ This function may (indirectly) reassign or modify `local` after render + 32 | } + 33 | + +error.invalid-reassign-local-variable-in-jsx-callback.ts:5:4 + 3 | + 4 | const reassignLocal = newValue => { +> 5 | local = newValue; + | ^^^^^ This modifies `local` + 6 | }; + 7 | + 8 | const onClick = newValue => { ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-setState-in-useMemo-indirect-useCallback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-setState-in-useMemo-indirect-useCallback.expect.md index e284a9367fbc..b245c5324f3f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-setState-in-useMemo-indirect-useCallback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-setState-in-useMemo-indirect-useCallback.expect.md @@ -26,7 +26,7 @@ function useKeyedState({key, init}) { ## Error ``` -Found 1 error: +Found 3 errors: Error: Calling setState from useMemo may trigger an infinite loop @@ -40,6 +40,61 @@ error.invalid-setState-in-useMemo-indirect-useCallback.ts:13:4 14 | }, [key, init]); 15 | 16 | return state; + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + +error.invalid-setState-in-useMemo-indirect-useCallback.ts:9:13 + 7 | const fn = useCallback(() => { + 8 | setPrevKey(key); +> 9 | setState(init); + | ^^^^ Missing dependency `init` + 10 | }); + 11 | + 12 | useMemo(() => { + +error.invalid-setState-in-useMemo-indirect-useCallback.ts:8:15 + 6 | + 7 | const fn = useCallback(() => { +> 8 | setPrevKey(key); + | ^^^ Missing dependency `key` + 9 | setState(init); + 10 | }); + 11 | + +Error: Found missing/extra memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. Extra dependencies can cause a value to update more often than it should, resulting in performance problems such as excessive renders or effects firing too often. + +error.invalid-setState-in-useMemo-indirect-useCallback.ts:13:4 + 11 | + 12 | useMemo(() => { +> 13 | fn(); + | ^^ Missing dependency `fn` + 14 | }, [key, init]); + 15 | + 16 | return state; + +error.invalid-setState-in-useMemo-indirect-useCallback.ts:14:6 + 12 | useMemo(() => { + 13 | fn(); +> 14 | }, [key, init]); + | ^^^ Unnecessary dependency `key` + 15 | + 16 | return state; + 17 | } + +error.invalid-setState-in-useMemo-indirect-useCallback.ts:14:11 + 12 | useMemo(() => { + 13 | fn(); +> 14 | }, [key, init]); + | ^^^^ Unnecessary dependency `init` + 15 | + 16 | return state; + 17 | } + +Inferred dependencies: `[fn]` ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.expect.md deleted file mode 100644 index 1e13064b7220..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.expect.md +++ /dev/null @@ -1,44 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies -function Component(props) { - // Items cannot be memoized bc its mutation spans a hook call - const items = [props.value]; - const [state, _setState] = useState(null); - mutate(items); - - // Items is no longer mutable here, but it hasn't been memoized - useEffect(() => { - console.log(items); - }, [items]); - - return [items, state]; -} - -``` - - -## Error - -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - -error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.ts:9:2 - 7 | - 8 | // Items is no longer mutable here, but it hasn't been memoized -> 9 | useEffect(() => { - | ^^^^^^^^^^^^^^^^^ -> 10 | console.log(items); - | ^^^^^^^^^^^^^^^^^^^^^^^ -> 11 | }, [items]); - | ^^^^^^^^^^^^^^ React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - 12 | - 13 | return [items, state]; - 14 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.js deleted file mode 100644 index 20a0d9b606f2..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized-bc-range-overlaps-hook.js +++ /dev/null @@ -1,14 +0,0 @@ -// @validateMemoizedEffectDependencies -function Component(props) { - // Items cannot be memoized bc its mutation spans a hook call - const items = [props.value]; - const [state, _setState] = useState(null); - mutate(items); - - // Items is no longer mutable here, but it hasn't been memoized - useEffect(() => { - console.log(items); - }, [items]); - - return [items, state]; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.expect.md deleted file mode 100644 index 02712424842f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.expect.md +++ /dev/null @@ -1,41 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies -import {useEffect} from 'react'; - -function Component(props) { - const data = {}; - useEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} - -``` - - -## Error - -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - -error.invalid-useEffect-dep-not-memoized.ts:6:2 - 4 | function Component(props) { - 5 | const data = {}; -> 6 | useEffect(() => { - | ^^^^^^^^^^^^^^^^^ -> 7 | console.log(props.value); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 8 | }, [data]); - | ^^^^^^^^^^^^^ React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - 9 | mutate(data); - 10 | return data; - 11 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.js deleted file mode 100644 index fee0f630a073..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useEffect-dep-not-memoized.js +++ /dev/null @@ -1,11 +0,0 @@ -// @validateMemoizedEffectDependencies -import {useEffect} from 'react'; - -function Component(props) { - const data = {}; - useEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.expect.md deleted file mode 100644 index 9f98a7a2273f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.expect.md +++ /dev/null @@ -1,41 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies -import {useInsertionEffect} from 'react'; - -function Component(props) { - const data = {}; - useInsertionEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} - -``` - - -## Error - -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - -error.invalid-useInsertionEffect-dep-not-memoized.ts:6:2 - 4 | function Component(props) { - 5 | const data = {}; -> 6 | useInsertionEffect(() => { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 7 | console.log(props.value); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 8 | }, [data]); - | ^^^^^^^^^^^^^ React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - 9 | mutate(data); - 10 | return data; - 11 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.js deleted file mode 100644 index 7d1ed16544b0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useInsertionEffect-dep-not-memoized.js +++ /dev/null @@ -1,11 +0,0 @@ -// @validateMemoizedEffectDependencies -import {useInsertionEffect} from 'react'; - -function Component(props) { - const data = {}; - useInsertionEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.expect.md deleted file mode 100644 index 55ba0876d761..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.expect.md +++ /dev/null @@ -1,41 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies -import {useLayoutEffect} from 'react'; - -function Component(props) { - const data = {}; - useLayoutEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} - -``` - - -## Error - -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - -error.invalid-useLayoutEffect-dep-not-memoized.ts:6:2 - 4 | function Component(props) { - 5 | const data = {}; -> 6 | useLayoutEffect(() => { - | ^^^^^^^^^^^^^^^^^^^^^^^ -> 7 | console.log(props.value); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 8 | }, [data]); - | ^^^^^^^^^^^^^ React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - 9 | mutate(data); - 10 | return data; - 11 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.js deleted file mode 100644 index 3925334795ee..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useLayoutEffect-dep-not-memoized.js +++ /dev/null @@ -1,11 +0,0 @@ -// @validateMemoizedEffectDependencies -import {useLayoutEffect} from 'react'; - -function Component(props) { - const data = {}; - useLayoutEffect(() => { - console.log(props.value); - }, [data]); - mutate(data); - return data; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-async-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-async-callback.expect.md index 7146a57869f4..273da427a062 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-async-callback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-async-callback.expect.md @@ -15,7 +15,7 @@ function component(a, b) { ## Error ``` -Found 1 error: +Found 2 errors: Error: useMemo() callbacks may not be async or generator functions @@ -32,6 +32,21 @@ error.invalid-useMemo-async-callback.ts:2:18 5 | return x; 6 | } 7 | + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + +error.invalid-useMemo-async-callback.ts:3:10 + 1 | function component(a, b) { + 2 | let x = useMemo(async () => { +> 3 | await a; + | ^ Missing dependency `a` + 4 | }, []); + 5 | return x; + 6 | } + +Inferred dependencies: `[a]` ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-callback-args.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-callback-args.expect.md index 933315073f13..ecde7590abe9 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-callback-args.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.invalid-useMemo-callback-args.expect.md @@ -13,7 +13,7 @@ function component(a, b) { ## Error ``` -Found 1 error: +Found 2 errors: Error: useMemo() callbacks may not accept parameters @@ -26,6 +26,20 @@ error.invalid-useMemo-callback-args.ts:2:18 3 | return x; 4 | } 5 | + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + +error.invalid-useMemo-callback-args.ts:2:23 + 1 | function component(a, b) { +> 2 | let x = useMemo(c => a, []); + | ^ Missing dependency `a` + 3 | return x; + 4 | } + 5 | + +Inferred dependencies: `[a]` ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.mutable-range-shared-inner-outer-function.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.mutable-range-shared-inner-outer-function.expect.md index 337b9dd30c08..b0a1a6712305 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.mutable-range-shared-inner-outer-function.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.mutable-range-shared-inner-outer-function.expect.md @@ -32,7 +32,7 @@ export const FIXTURE_ENTRYPOINT = { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -46,6 +46,28 @@ error.mutable-range-shared-inner-outer-function.ts:8:6 9 | b = []; 10 | } else { 11 | a = {}; + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `a` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.mutable-range-shared-inner-outer-function.ts:17:23 + 15 | b.push(false); + 16 | }; +> 17 | return
; + | ^ This function may (indirectly) reassign or modify `a` after render + 18 | } + 19 | + 20 | export const FIXTURE_ENTRYPOINT = { + +error.mutable-range-shared-inner-outer-function.ts:8:6 + 6 | const f = () => { + 7 | if (cond) { +> 8 | a = {}; + | ^ This modifies `a` + 9 | b = []; + 10 | } else { + 11 | a = {}; ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.expect.md deleted file mode 100644 index 69ce796faed3..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.expect.md +++ /dev/null @@ -1,54 +0,0 @@ - -## Input - -```javascript -// @validateNoDynamicallyCreatedComponentsOrHooks -export function getInput(a) { - const Wrapper = () => { - const handleChange = () => { - a.onChange(); - }; - - return ; - }; - - return Wrapper; -} - -export const FIXTURE_ENTRYPOINT = { - fn: getInput, - isComponent: false, - params: [{onChange() {}}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Components and hooks cannot be created dynamically - -The function `Wrapper` appears to be a React component, but it's defined inside `getInput`. Components and Hooks should always be declared at module scope. - -error.nested-component-in-normal-function.ts:2:16 - 1 | // @validateNoDynamicallyCreatedComponentsOrHooks -> 2 | export function getInput(a) { - | ^^^^^^^^ this function dynamically created a component/hook - 3 | const Wrapper = () => { - 4 | const handleChange = () => { - 5 | a.onChange(); - -error.nested-component-in-normal-function.ts:3:8 - 1 | // @validateNoDynamicallyCreatedComponentsOrHooks - 2 | export function getInput(a) { -> 3 | const Wrapper = () => { - | ^^^^^^^ the component is created here - 4 | const handleChange = () => { - 5 | a.onChange(); - 6 | }; -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.js deleted file mode 100644 index 36be05e3ee8b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-component-in-normal-function.js +++ /dev/null @@ -1,18 +0,0 @@ -// @validateNoDynamicallyCreatedComponentsOrHooks -export function getInput(a) { - const Wrapper = () => { - const handleChange = () => { - a.onChange(); - }; - - return ; - }; - - return Wrapper; -} - -export const FIXTURE_ENTRYPOINT = { - fn: getInput, - isComponent: false, - params: [{onChange() {}}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.expect.md deleted file mode 100644 index 652fc2feb009..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.expect.md +++ /dev/null @@ -1,59 +0,0 @@ - -## Input - -```javascript -// @validateNoDynamicallyCreatedComponentsOrHooks -import {useState} from 'react'; - -function createCustomHook(config) { - function useConfiguredState() { - const [state, setState] = useState(0); - - const increment = () => { - setState(state + config.step); - }; - - return [state, increment]; - } - - return useConfiguredState; -} - -export const FIXTURE_ENTRYPOINT = { - fn: createCustomHook, - isComponent: false, - params: [{step: 1}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Components and hooks cannot be created dynamically - -The function `useConfiguredState` appears to be a React hook, but it's defined inside `createCustomHook`. Components and Hooks should always be declared at module scope. - -error.nested-hook-in-normal-function.ts:4:9 - 2 | import {useState} from 'react'; - 3 | -> 4 | function createCustomHook(config) { - | ^^^^^^^^^^^^^^^^ this function dynamically created a component/hook - 5 | function useConfiguredState() { - 6 | const [state, setState] = useState(0); - 7 | - -error.nested-hook-in-normal-function.ts:5:11 - 3 | - 4 | function createCustomHook(config) { -> 5 | function useConfiguredState() { - | ^^^^^^^^^^^^^^^^^^ the component is created here - 6 | const [state, setState] = useState(0); - 7 | - 8 | const increment = () => { -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.js deleted file mode 100644 index 306e78f7b1c6..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nested-hook-in-normal-function.js +++ /dev/null @@ -1,22 +0,0 @@ -// @validateNoDynamicallyCreatedComponentsOrHooks -import {useState} from 'react'; - -function createCustomHook(config) { - function useConfiguredState() { - const [state, setState] = useState(0); - - const increment = () => { - setState(state + config.step); - }; - - return [state, increment]; - } - - return useConfiguredState; -} - -export const FIXTURE_ENTRYPOINT = { - fn: createCustomHook, - isComponent: false, - params: [{step: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.expect.md deleted file mode 100644 index c0c369e11d1c..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.expect.md +++ /dev/null @@ -1,19 +0,0 @@ - -## Input - -```javascript -// @disableMemoizationForDebugging @enableChangeDetectionForDebugging -function Component(props) {} - -``` - - -## Error - -``` -Found 1 error: - -Error: Invalid environment config: the 'disableMemoizationForDebugging' and 'enableChangeDetectionForDebugging' options cannot be used together -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.js deleted file mode 100644 index ce93cd29f1af..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.nomemo-and-change-detect.js +++ /dev/null @@ -1,2 +0,0 @@ -// @disableMemoizationForDebugging @enableChangeDetectionForDebugging -function Component(props) {} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.expect.md deleted file mode 100644 index 2a3657eda34f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.expect.md +++ /dev/null @@ -1,69 +0,0 @@ - -## Input - -```javascript -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates a custom component wrapper -function CustomForm({onSubmit, children}: any) { - return
{children}
; -} - -// Simulates react-hook-form's handleSubmit -function handleSubmit(callback: (data: T) => void) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = (data: any) => { - // This should error: passing function with ref access to custom component - // event handler, even though it would be safe on a native
- if (ref.current !== null) { - console.log(ref.current.value); - } - }; - - return ( - <> - - - - - - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot access refs during render - -React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). - -error.ref-value-in-custom-component-event-handler-wrapper.ts:31:41 - 29 | <> - 30 | -> 31 | - | ^^^^^^^^ Passing a ref to a function may read its value during render - 32 | - 33 | - 34 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.tsx deleted file mode 100644 index b90a12171654..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-custom-component-event-handler-wrapper.tsx +++ /dev/null @@ -1,41 +0,0 @@ -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates a custom component wrapper -function CustomForm({onSubmit, children}: any) { - return {children}
; -} - -// Simulates react-hook-form's handleSubmit -function handleSubmit(callback: (data: T) => void) { - return (event: any) => { - event.preventDefault(); - callback({} as T); - }; -} - -function Component() { - const ref = useRef(null); - - const onSubmit = (data: any) => { - // This should error: passing function with ref access to custom component - // event handler, even though it would be safe on a native
- if (ref.current !== null) { - console.log(ref.current.value); - } - }; - - return ( - <> - - - - - - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.expect.md deleted file mode 100644 index 718e2c81419f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.expect.md +++ /dev/null @@ -1,55 +0,0 @@ - -## Input - -```javascript -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates a handler wrapper -function handleClick(value: any) { - return () => { - console.log(value); - }; -} - -function Component() { - const ref = useRef(null); - - // This should still error: passing ref.current directly to a wrapper - // The ref value is accessed during render, not in the event handler - return ( - <> - - - - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot access refs during render - -React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). - -error.ref-value-in-event-handler-wrapper.ts:19:35 - 17 | <> - 18 | -> 19 | - | ^^^^^^^^^^^ Cannot access ref value during render - 20 | - 21 | ); - 22 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.tsx deleted file mode 100644 index 58313e560ce7..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.ref-value-in-event-handler-wrapper.tsx +++ /dev/null @@ -1,27 +0,0 @@ -// @enableInferEventHandlers -import {useRef} from 'react'; - -// Simulates a handler wrapper -function handleClick(value: any) { - return () => { - console.log(value); - }; -} - -function Component() { - const ref = useRef(null); - - // This should still error: passing ref.current directly to a wrapper - // The ref value is accessed during render, not in the event handler - return ( - <> - - - - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-allow-assigning-to-inferred-ref-prop-in-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-allow-assigning-to-inferred-ref-prop-in-callback.expect.md index ec187f6bc23b..757e038c676b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-allow-assigning-to-inferred-ref-prop-in-callback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-allow-assigning-to-inferred-ref-prop-in-callback.expect.md @@ -29,7 +29,7 @@ function useHook(parentRef) { ## Error ``` -Found 1 error: +Found 2 errors: Error: This value cannot be modified @@ -43,6 +43,27 @@ error.todo-allow-assigning-to-inferred-ref-prop-in-callback.ts:15:8 16 | } 17 | } 18 | }; + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `parentRef` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.todo-allow-assigning-to-inferred-ref-prop-in-callback.ts:19:9 + 17 | } + 18 | }; +> 19 | return handler; + | ^^^^^^^ This function may (indirectly) reassign or modify `parentRef` after render + 20 | } + 21 | + +error.todo-allow-assigning-to-inferred-ref-prop-in-callback.ts:15:8 + 13 | } else { + 14 | // So this assignment fails since we don't know its a ref +> 15 | parentRef.current = instance; + | ^^^^^^^^^ This modifies `parentRef` + 16 | } + 17 | } + 18 | }; ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.expect.md index a88d43b35282..0cd493836b69 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-function-expression-references-later-variable-declaration.expect.md @@ -17,7 +17,7 @@ function Component() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -31,6 +31,27 @@ error.todo-function-expression-references-later-variable-declaration.ts:3:4 4 | }; 5 | let onClick; 6 | + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `onClick` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.todo-function-expression-references-later-variable-declaration.ts:7:23 + 5 | let onClick; + 6 | +> 7 | return
; + | ^^^^^^^^ This function may (indirectly) reassign or modify `onClick` after render + 8 | } + 9 | + +error.todo-function-expression-references-later-variable-declaration.ts:3:4 + 1 | function Component() { + 2 | let callback = () => { +> 3 | onClick = () => {}; + | ^^^^^^^ This modifies `onClick` + 4 | }; + 5 | let onClick; + 6 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-hoisted-function-in-unreachable-code.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-hoisted-function-in-unreachable-code.expect.md index c9152496f2de..6cd0945d74d5 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-hoisted-function-in-unreachable-code.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-hoisted-function-in-unreachable-code.expect.md @@ -18,15 +18,18 @@ function Component() { ``` Found 1 error: -Todo: Support functions with unreachable code that may contain hoisted declarations +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized -error.todo-hoisted-function-in-unreachable-code.ts:6:2 + Foo$0. + +error.todo-hoisted-function-in-unreachable-code.ts:3:10 + 1 | // @compilationMode:"infer" + 2 | function Component() { +> 3 | return ; + | ^^^ this is uninitialized 4 | 5 | // This is unreachable from a control-flow perspective, but it gets hoisted -> 6 | function Foo() {} - | ^^^^^^^^^^^^^^^^^ Support functions with unreachable code that may contain hoisted declarations - 7 | } - 8 | + 6 | function Foo() {} ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md index 32db5b2e7caa..60fbf9637ff4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md @@ -79,43 +79,11 @@ let moduleLocal = false; ## Error ``` -Found 10 errors: - -Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration - -error.todo-kitchensink.ts:3:2 - 1 | function foo([a, b], {c, d, e = 'e'}, f = 'f', ...args) { - 2 | let i = 0; -> 3 | var x = []; - | ^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration - 4 | - 5 | class Bar { - 6 | #secretSauce = 42; - -Compilation Skipped: Inline `class` declarations are not supported - -Move class declarations outside of components/hooks. - -error.todo-kitchensink.ts:5:2 - 3 | var x = []; - 4 | -> 5 | class Bar { - | ^^^^^^^^^^^ -> 6 | #secretSauce = 42; - | ^^^^^^^^^^^^^^^^^^^^^^ -> 7 | constructor() { - | ^^^^^^^^^^^^^^^^^^^^^^ -> 8 | console.log(this.#secretSauce); - | ^^^^^^^^^^^^^^^^^^^^^^ -> 9 | } - | ^^^^^^^^^^^^^^^^^^^^^^ -> 10 | } - | ^^^^ Inline `class` declarations are not supported - 11 | - 12 | const g = {b() {}, c: () => {}}; - 13 | const {z, aa = 'aa'} = useCustom(); - -Todo: (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement +Found 1 error: + +Invariant: Expected a variable declaration + +Got ExpressionStatement. error.todo-kitchensink.ts:20:2 18 | const j = function bar([quz, qux], ...args) {}; @@ -125,103 +93,10 @@ error.todo-kitchensink.ts:20:2 > 21 | x.push(i); | ^^^^^^^^^^^^^^ > 22 | } - | ^^^^ (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement + | ^^^^ Expected a variable declaration 23 | for (; i < 3; ) { 24 | break; 25 | } - -Todo: (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement - -error.todo-kitchensink.ts:23:2 - 21 | x.push(i); - 22 | } -> 23 | for (; i < 3; ) { - | ^^^^^^^^^^^^^^^^^ -> 24 | break; - | ^^^^^^^^^^ -> 25 | } - | ^^^^ (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement - 26 | for (;;) { - 27 | break; - 28 | } - -Todo: (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement - -error.todo-kitchensink.ts:26:2 - 24 | break; - 25 | } -> 26 | for (;;) { - | ^^^^^^^^^^ -> 27 | break; - | ^^^^^^^^^^ -> 28 | } - | ^^^^ (BuildHIR::lowerStatement) Handle non-variable initialization in ForStatement - 29 | - 30 | graphql` - 31 | ${g} - -Todo: (BuildHIR::lowerStatement) Handle empty test in ForStatement - -error.todo-kitchensink.ts:26:2 - 24 | break; - 25 | } -> 26 | for (;;) { - | ^^^^^^^^^^ -> 27 | break; - | ^^^^^^^^^^ -> 28 | } - | ^^^^ (BuildHIR::lowerStatement) Handle empty test in ForStatement - 29 | - 30 | graphql` - 31 | ${g} - -Todo: (BuildHIR::lowerExpression) Handle tagged template with interpolations - -error.todo-kitchensink.ts:30:2 - 28 | } - 29 | -> 30 | graphql` - | ^^^^^^^^ -> 31 | ${g} - | ^^^^^^^^ -> 32 | `; - | ^^^^ (BuildHIR::lowerExpression) Handle tagged template with interpolations - 33 | - 34 | graphql`\\t\n`; - 35 | - -Todo: (BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value - -error.todo-kitchensink.ts:34:2 - 32 | `; - 33 | -> 34 | graphql`\\t\n`; - | ^^^^^^^^^^^^^^ (BuildHIR::lowerExpression) Handle tagged template where cooked value is different from raw value - 35 | - 36 | for (c of [1, 2]) { - 37 | } - -Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `MemberExpression` cannot be safely reordered - -error.todo-kitchensink.ts:57:9 - 55 | case foo(): { - 56 | } -> 57 | case x.y: { - | ^^^ (BuildHIR::node.lowerReorderableExpression) Expression type `MemberExpression` cannot be safely reordered - 58 | } - 59 | default: { - 60 | } - -Todo: (BuildHIR::node.lowerReorderableExpression) Expression type `BinaryExpression` cannot be safely reordered - -error.todo-kitchensink.ts:53:9 - 51 | - 52 | switch (i) { -> 53 | case 1 + 1: { - | ^^^^^ (BuildHIR::node.lowerReorderableExpression) Expression type `BinaryExpression` cannot be safely reordered - 54 | } - 55 | case foo(): { - 56 | } ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-optional-call-chain-in-optional.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-optional-call-chain-in-optional.expect.md deleted file mode 100644 index 6551bb8d40fd..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-optional-call-chain-in-optional.expect.md +++ /dev/null @@ -1,39 +0,0 @@ - -## Input - -```javascript -function useFoo(props: {value: {x: string; y: string} | null}) { - const value = props.value; - return createArray(value?.x, value?.y)?.join(', '); -} - -function createArray(...args: Array): Array { - return args; -} - -export const FIXTURE_ENTRYPONT = { - fn: useFoo, - props: [{value: null}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Todo: Unexpected terminal kind `optional` for optional fallthrough block - -error.todo-optional-call-chain-in-optional.ts:3:21 - 1 | function useFoo(props: {value: {x: string; y: string} | null}) { - 2 | const value = props.value; -> 3 | return createArray(value?.x, value?.y)?.join(', '); - | ^^^^^^^^ Unexpected terminal kind `optional` for optional fallthrough block - 4 | } - 5 | - 6 | function createArray(...args: Array): Array { -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.expect.md index 45e3d365a8d3..10af9368c594 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.js index 12f8ebf3ce40..ec7c5811d69a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-preserve-memo-deps-mixed-optional-nonoptional-property-chain.js @@ -1,4 +1,4 @@ -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-reassign-const.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-reassign-const.expect.md index 4b8ee0e4ed96..e594880ef92f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-reassign-const.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-reassign-const.expect.md @@ -21,7 +21,7 @@ function Component({foo}) { ## Error ``` -Found 1 error: +Found 3 errors: Todo: Support destructuring of context variables @@ -29,10 +29,34 @@ error.todo-reassign-const.ts:3:20 1 | import {Stringify} from 'shared-runtime'; 2 | > 3 | function Component({foo}) { - | ^^^ Support destructuring of context variables + | ^^^ 4 | let bar = foo.bar; 5 | return ( 6 | 3 | function Component({foo}) { + | ^^^ + 4 | let bar = foo.bar; + 5 | return ( + 6 | { +> 8 | foo = true; + | ^^^ `foo` cannot be modified + 9 | }} + 10 | /> + 11 | ); ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md index b96648b00f5c..a3aae8768031 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.useMemo-callback-generator.expect.md @@ -18,7 +18,7 @@ function component(a, b) { ## Error ``` -Found 1 error: +Found 2 errors: Todo: (BuildHIR::lowerExpression) Handle YieldExpression expressions @@ -30,6 +30,23 @@ error.useMemo-callback-generator.ts:6:4 7 | }, []); 8 | return x; 9 | } + +Error: useMemo() callbacks may not be async or generator functions + +useMemo() callbacks are called once and must synchronously return a value. + +error.useMemo-callback-generator.ts:5:18 + 3 | // useful for now, but adding this test in case we do + 4 | // add support for generators in the future. +> 5 | let x = useMemo(function* () { + | ^^^^^^^^^^^^^^ +> 6 | yield a; + | ^^^^^^^^^^^^ +> 7 | }, []); + | ^^^^ Async and generator functions are not supported + 8 | return x; + 9 | } + 10 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.expect.md deleted file mode 100644 index 7fdadfbc896b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies -import {useHook} from 'shared-runtime'; - -function Component(props) { - const x = []; - useHook(); // intersperse a hook call to prevent memoization of x - x.push(props.value); - - const y = [x]; - - useEffect(() => { - console.log(y); - }, [y]); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 'sathya'}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Compilation Skipped: React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - -error.validate-memoized-effect-deps-invalidated-dep-value.ts:11:2 - 9 | const y = [x]; - 10 | -> 11 | useEffect(() => { - | ^^^^^^^^^^^^^^^^^ -> 12 | console.log(y); - | ^^^^^^^^^^^^^^^^^^^ -> 13 | }, [y]); - | ^^^^^^^^^^ React Compiler has skipped optimizing this component because the effect dependencies could not be memoized. Unmemoized effect dependencies can trigger an infinite loop or other unexpected behavior - 14 | } - 15 | - 16 | export const FIXTURE_ENTRYPOINT = { -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.js deleted file mode 100644 index e0944d35e635..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-memoized-effect-deps-invalidated-dep-value.js +++ /dev/null @@ -1,19 +0,0 @@ -// @validateMemoizedEffectDependencies -import {useHook} from 'shared-runtime'; - -function Component(props) { - const x = []; - useHook(); // intersperse a hook call to prevent memoization of x - x.push(props.value); - - const y = [x]; - - useEffect(() => { - console.log(y); - }, [y]); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 'sathya'}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.expect.md index 1d5a4a2284cf..47e9fedd37f6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.expect.md @@ -3,8 +3,10 @@ ```javascript // @validateRefAccessDuringRender:true +import {mutate} from 'shared-runtime'; + function Foo(props, ref) { - console.log(ref.current); + mutate(ref.current); return
{props.bar}
; } @@ -26,14 +28,14 @@ Error: Cannot access refs during render React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). -error.validate-mutate-ref-arg-in-render.ts:3:14 - 1 | // @validateRefAccessDuringRender:true - 2 | function Foo(props, ref) { -> 3 | console.log(ref.current); - | ^^^^^^^^^^^ Passing a ref to a function may read its value during render - 4 | return
{props.bar}
; - 5 | } - 6 | +error.validate-mutate-ref-arg-in-render.ts:5:9 + 3 | + 4 | function Foo(props, ref) { +> 5 | mutate(ref.current); + | ^^^^^^^^^^^ Passing a ref to a function may read its value during render + 6 | return
{props.bar}
; + 7 | } + 8 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.js index 10218fc61633..8e75ec2107c6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.validate-mutate-ref-arg-in-render.js @@ -1,6 +1,8 @@ // @validateRefAccessDuringRender:true +import {mutate} from 'shared-runtime'; + function Foo(props, ref) { - console.log(ref.current); + mutate(ref.current); return
{props.bar}
; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.expect.md new file mode 100644 index 000000000000..750f35d7eb95 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + // Error: try/finally (Todo from BuildHIR) + try { + doWork(); + } finally { + doCleanup(); + } + + // Error: mutating frozen props + props.value = 1; + + return
{props.value}
; +} + +``` + + +## Error + +``` +Found 2 errors: + +Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + +error.try-finally-and-mutation-of-props.ts:9:2 + 7 | function Component(props) { + 8 | // Error: try/finally (Todo from BuildHIR) +> 9 | try { + | ^^^^^ +> 10 | doWork(); + | ^^^^^^^^^^^^^ +> 11 | } finally { + | ^^^^^^^^^^^^^ +> 12 | doCleanup(); + | ^^^^^^^^^^^^^ +> 13 | } + | ^^^^ (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + 14 | + 15 | // Error: mutating frozen props + 16 | props.value = 1; + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.try-finally-and-mutation-of-props.ts:16:2 + 14 | + 15 | // Error: mutating frozen props +> 16 | props.value = 1; + | ^^^^^ value cannot be modified + 17 | + 18 | return
{props.value}
; + 19 | } +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.js new file mode 100644 index 000000000000..a26724daf68c --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-mutation-of-props.js @@ -0,0 +1,19 @@ +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + // Error: try/finally (Todo from BuildHIR) + try { + doWork(); + } finally { + doCleanup(); + } + + // Error: mutating frozen props + props.value = 1; + + return
{props.value}
; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.expect.md new file mode 100644 index 000000000000..45b637f104e5 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +// @validateRefAccessDuringRender +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + */ +function Component() { + const ref = useRef(null); + + // Error: try/finally (Todo from BuildHIR) + try { + doSomething(); + } finally { + cleanup(); + } + + // Error: reading ref during render + const value = ref.current; + + return
{value}
; +} + +``` + + +## Error + +``` +Found 2 errors: + +Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + +error.try-finally-and-ref-access.ts:12:2 + 10 | + 11 | // Error: try/finally (Todo from BuildHIR) +> 12 | try { + | ^^^^^ +> 13 | doSomething(); + | ^^^^^^^^^^^^^^^^^^ +> 14 | } finally { + | ^^^^^^^^^^^^^^^^^^ +> 15 | cleanup(); + | ^^^^^^^^^^^^^^^^^^ +> 16 | } + | ^^^^ (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + 17 | + 18 | // Error: reading ref during render + 19 | const value = ref.current; + +Error: Cannot access refs during render + +React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). + +error.try-finally-and-ref-access.ts:19:16 + 17 | + 18 | // Error: reading ref during render +> 19 | const value = ref.current; + | ^^^^^^^^^^^ Cannot access ref value during render + 20 | + 21 | return
{value}
; + 22 | } +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.js new file mode 100644 index 000000000000..3d247c2c0b7c --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-and-ref-access.js @@ -0,0 +1,22 @@ +// @validateRefAccessDuringRender +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + */ +function Component() { + const ref = useRef(null); + + // Error: try/finally (Todo from BuildHIR) + try { + doSomething(); + } finally { + cleanup(); + } + + // Error: reading ref during render + const value = ref.current; + + return
{value}
; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.expect.md new file mode 100644 index 000000000000..a21c72635b3a --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.expect.md @@ -0,0 +1,86 @@ + +## Input + +```javascript +// @validateRefAccessDuringRender +/** + * Fault tolerance test: three independent errors should all be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + * Error 3 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + const ref = useRef(null); + + // Error: try/finally (Todo from BuildHIR) + try { + doWork(); + } finally { + cleanup(); + } + + // Error: reading ref during render + const value = ref.current; + + // Error: mutating frozen props + props.items = []; + + return
{value}
; +} + +``` + + +## Error + +``` +Found 3 errors: + +Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + +error.try-finally-ref-access-and-mutation.ts:13:2 + 11 | + 12 | // Error: try/finally (Todo from BuildHIR) +> 13 | try { + | ^^^^^ +> 14 | doWork(); + | ^^^^^^^^^^^^^ +> 15 | } finally { + | ^^^^^^^^^^^^^ +> 16 | cleanup(); + | ^^^^^^^^^^^^^ +> 17 | } + | ^^^^ (BuildHIR::lowerStatement) Handle TryStatement without a catch clause + 18 | + 19 | // Error: reading ref during render + 20 | const value = ref.current; + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.try-finally-ref-access-and-mutation.ts:23:2 + 21 | + 22 | // Error: mutating frozen props +> 23 | props.items = []; + | ^^^^^ value cannot be modified + 24 | + 25 | return
{value}
; + 26 | } + +Error: Cannot access refs during render + +React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). + +error.try-finally-ref-access-and-mutation.ts:20:16 + 18 | + 19 | // Error: reading ref during render +> 20 | const value = ref.current; + | ^^^^^^^^^^^ Cannot access ref value during render + 21 | + 22 | // Error: mutating frozen props + 23 | props.items = []; +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.js new file mode 100644 index 000000000000..f25a59c7653b --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.try-finally-ref-access-and-mutation.js @@ -0,0 +1,26 @@ +// @validateRefAccessDuringRender +/** + * Fault tolerance test: three independent errors should all be reported. + * + * Error 1 (BuildHIR): `try/finally` is not supported + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + * Error 3 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + const ref = useRef(null); + + // Error: try/finally (Todo from BuildHIR) + try { + doWork(); + } finally { + cleanup(); + } + + // Error: reading ref during render + const value = ref.current; + + // Error: mutating frozen props + props.items = []; + + return
{value}
; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.expect.md new file mode 100644 index 000000000000..ecb65622d432 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `var` declarations are not supported (treated as `let`) + * Error 2 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + // Error: var declaration (Todo from BuildHIR) + var items = props.items; + + // Error: mutating frozen props (detected during inference) + props.items = []; + + return
{items.length}
; +} + +``` + + +## Error + +``` +Found 2 errors: + +Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + +error.var-declaration-and-mutation-of-props.ts:9:2 + 7 | function Component(props) { + 8 | // Error: var declaration (Todo from BuildHIR) +> 9 | var items = props.items; + | ^^^^^^^^^^^^^^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + 10 | + 11 | // Error: mutating frozen props (detected during inference) + 12 | props.items = []; + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.var-declaration-and-mutation-of-props.ts:12:2 + 10 | + 11 | // Error: mutating frozen props (detected during inference) +> 12 | props.items = []; + | ^^^^^ value cannot be modified + 13 | + 14 | return
{items.length}
; + 15 | } +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.js new file mode 100644 index 000000000000..c0fd6a34fb8f --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-mutation-of-props.js @@ -0,0 +1,15 @@ +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `var` declarations are not supported (treated as `let`) + * Error 2 (InferMutationAliasingEffects): Mutation of frozen props + */ +function Component(props) { + // Error: var declaration (Todo from BuildHIR) + var items = props.items; + + // Error: mutating frozen props (detected during inference) + props.items = []; + + return
{items.length}
; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.expect.md new file mode 100644 index 000000000000..c86e6ffe6ab6 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @validateRefAccessDuringRender +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `var` declarations are not supported (treated as `let`) + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + */ +function Component() { + const ref = useRef(null); + + // Error: var declaration (Todo from BuildHIR) + var items = [1, 2, 3]; + + // Error: reading ref during render + const value = ref.current; + + return ( +
+ {value} + {items.length} +
+ ); +} + +``` + + +## Error + +``` +Found 2 errors: + +Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + +error.var-declaration-and-ref-access.ts:12:2 + 10 | + 11 | // Error: var declaration (Todo from BuildHIR) +> 12 | var items = [1, 2, 3]; + | ^^^^^^^^^^^^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + 13 | + 14 | // Error: reading ref during render + 15 | const value = ref.current; + +Error: Cannot access refs during render + +React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). + +error.var-declaration-and-ref-access.ts:15:16 + 13 | + 14 | // Error: reading ref during render +> 15 | const value = ref.current; + | ^^^^^^^^^^^ Cannot access ref value during render + 16 | + 17 | return ( + 18 |
+``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.js new file mode 100644 index 000000000000..60a14ecec3d9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fault-tolerance/error.var-declaration-and-ref-access.js @@ -0,0 +1,23 @@ +// @validateRefAccessDuringRender +/** + * Fault tolerance test: two independent errors should both be reported. + * + * Error 1 (BuildHIR): `var` declarations are not supported (treated as `let`) + * Error 2 (ValidateNoRefAccessInRender): reading ref.current during render + */ +function Component() { + const ref = useRef(null); + + // Error: var declaration (Todo from BuildHIR) + var items = [1, 2, 3]; + + // Error: reading ref during render + const value = ref.current; + + return ( +
+ {value} + {items.length} +
+ ); +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-fbt-as-local.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-fbt-as-local.expect.md index bb86d3bc42e0..c2cc0a19506c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-fbt-as-local.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-fbt-as-local.expect.md @@ -50,7 +50,7 @@ export const FIXTURE_ENTRYPOINT = { ## Error ``` -Found 1 error: +Found 4 errors: Todo: Support local variables named `fbt` @@ -60,10 +60,49 @@ error.todo-fbt-as-local.ts:18:19 16 | 17 | function Foo(props) { > 18 | const getText1 = fbt => - | ^^^ Rename to avoid conflict with fbt plugin + | ^^^ Support local variables named `fbt` 19 | fbt( 20 | `Hello, ${fbt.param('(key) name', identity(props.name))}!`, 21 | '(description) Greeting' + +Todo: Support local variables named `fbt` + +Local variables named `fbt` may conflict with the fbt plugin and are not yet supported. + +error.todo-fbt-as-local.ts:18:19 + 16 | + 17 | function Foo(props) { +> 18 | const getText1 = fbt => + | ^^^ Support local variables named `fbt` + 19 | fbt( + 20 | `Hello, ${fbt.param('(key) name', identity(props.name))}!`, + 21 | '(description) Greeting' + +Todo: Support local variables named `fbt` + +Local variables named `fbt` may conflict with the fbt plugin and are not yet supported. + +error.todo-fbt-as-local.ts:18:19 + 16 | + 17 | function Foo(props) { +> 18 | const getText1 = fbt => + | ^^^ Support local variables named `fbt` + 19 | fbt( + 20 | `Hello, ${fbt.param('(key) name', identity(props.name))}!`, + 21 | '(description) Greeting' + +Todo: Support local variables named `fbt` + +Local variables named `fbt` may conflict with the fbt plugin and are not yet supported. + +error.todo-fbt-as-local.ts:24:19 + 22 | ); + 23 | +> 24 | const getText2 = fbt => + | ^^^ Support local variables named `fbt` + 25 | fbt( + 26 | `Goodbye, ${fbt.param('(key) name', identity(props.name))}!`, + 27 | '(description) Greeting2' ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-locally-require-fbt.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-locally-require-fbt.expect.md index 62605e5896ce..2847ad9d5a29 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-locally-require-fbt.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-locally-require-fbt.expect.md @@ -16,17 +16,15 @@ function Component(props) { ``` Found 1 error: -Todo: Support local variables named `fbt` +Invariant: tags should be module-level imports -Local variables named `fbt` may conflict with the fbt plugin and are not yet supported. - -error.todo-locally-require-fbt.ts:2:8 - 1 | function Component(props) { -> 2 | const fbt = require('fbt'); - | ^^^ Rename to avoid conflict with fbt plugin +error.todo-locally-require-fbt.ts:4:10 + 2 | const fbt = require('fbt'); 3 | - 4 | return {'Text'}; +> 4 | return {'Text'}; + | ^^^ tags should be module-level imports 5 | } + 6 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.expect.md deleted file mode 100644 index 5614560c6c4d..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.expect.md +++ /dev/null @@ -1,67 +0,0 @@ - -## Input - -```javascript -// @enableTreatFunctionDepsAsConditional -import {Stringify} from 'shared-runtime'; - -function Component({props}) { - const f = () => props.a.b; - - return {} : f} />; -} -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{props: null}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableTreatFunctionDepsAsConditional -import { Stringify } from "shared-runtime"; - -function Component(t0) { - const $ = _c(7); - const { props } = t0; - let t1; - if ($[0] !== props) { - t1 = () => props.a.b; - $[0] = props; - $[1] = t1; - } else { - t1 = $[1]; - } - const f = t1; - let t2; - if ($[2] !== f || $[3] !== props) { - t2 = props == null ? _temp : f; - $[2] = f; - $[3] = props; - $[4] = t2; - } else { - t2 = $[4]; - } - let t3; - if ($[5] !== t2) { - t3 = ; - $[5] = t2; - $[6] = t3; - } else { - t3 = $[6]; - } - return t3; -} -function _temp() {} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ props: null }], -}; - -``` - -### Eval output -(kind: ok)
{"f":"[[ function params=0 ]]"}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.tsx deleted file mode 100644 index ab3e00f9ba2b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr-conditional-access-2.tsx +++ /dev/null @@ -1,12 +0,0 @@ -// @enableTreatFunctionDepsAsConditional -import {Stringify} from 'shared-runtime'; - -function Component({props}) { - const f = () => props.a.b; - - return {} : f} />; -} -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{props: null}], -}; diff --git "a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.expect.md" "b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.expect.md" deleted file mode 100644 index c7aa3e3b7544..000000000000 --- "a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.expect.md" +++ /dev/null @@ -1,58 +0,0 @@ - -## Input - -```javascript -// @enableTreatFunctionDepsAsConditional -function Component(props) { - function getLength() { - return props.bar.length; - } - - return props.bar && getLength(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{bar: null}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableTreatFunctionDepsAsConditional -function Component(props) { - const $ = _c(5); - let t0; - if ($[0] !== props.bar) { - t0 = function getLength() { - return props.bar.length; - }; - $[0] = props.bar; - $[1] = t0; - } else { - t0 = $[1]; - } - const getLength = t0; - let t1; - if ($[2] !== getLength || $[3] !== props.bar) { - t1 = props.bar && getLength(); - $[2] = getLength; - $[3] = props.bar; - $[4] = t1; - } else { - t1 = $[4]; - } - return t1; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ bar: null }], -}; - -``` - -### Eval output -(kind: ok) null \ No newline at end of file diff --git "a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.js" "b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.js" deleted file mode 100644 index 6e59fb947d15..000000000000 --- "a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/functionexpr\342\200\223conditional-access.js" +++ /dev/null @@ -1,13 +0,0 @@ -// @enableTreatFunctionDepsAsConditional -function Component(props) { - function getLength() { - return props.bar.length; - } - - return props.bar && getLength(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{bar: null}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.expect.md deleted file mode 100644 index 05b7218de741..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.expect.md +++ /dev/null @@ -1,42 +0,0 @@ - -## Input - -```javascript -// @dynamicGating:{"source":"shared-runtime"} @panicThreshold:"none" @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveVariable({propVal}) { - 'use memo if(invalid identifier)'; - const arr = [propVal]; - useEffect(() => print(arr), AUTODEPS); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveVariable, - params: [{}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.dynamic-gating-invalid-identifier-nopanic-required-feature.ts:8:2 - 6 | 'use memo if(invalid identifier)'; - 7 | const arr = [propVal]; -> 8 | useEffect(() => print(arr), AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 9 | } - 10 | - 11 | export const FIXTURE_ENTRYPOINT = { -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.js deleted file mode 100644 index c753bc263827..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/error.dynamic-gating-invalid-identifier-nopanic-required-feature.js +++ /dev/null @@ -1,14 +0,0 @@ -// @dynamicGating:{"source":"shared-runtime"} @panicThreshold:"none" @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveVariable({propVal}) { - 'use memo if(invalid identifier)'; - const arr = [propVal]; - useEffect(() => print(arr), AUTODEPS); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveVariable, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/global-types/set-add-mutate.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/global-types/set-add-mutate.expect.md index cb829ffea217..d0218bb8696b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/global-types/set-add-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/global-types/set-add-mutate.expect.md @@ -33,24 +33,32 @@ import { c as _c } from "react/compiler-runtime"; import { makeArray } from "shared-runtime"; function useHook(t0) { - const $ = _c(5); + const $ = _c(7); const { el1, el2 } = t0; let s; if ($[0] !== el1 || $[1] !== el2) { s = new Set(); - const arr = makeArray(el1); - s.add(arr); - - arr.push(el2); let t1; - if ($[3] !== el2) { - t1 = makeArray(el2); - $[3] = el2; + if ($[3] !== el1) { + t1 = makeArray(el1); + $[3] = el1; $[4] = t1; } else { t1 = $[4]; } - s.add(t1); + const arr = t1; + s.add(arr); + + arr.push(el2); + let t2; + if ($[5] !== el2) { + t2 = makeArray(el2); + $[5] = el2; + $[6] = t2; + } else { + t2 = $[6]; + } + s.add(t2); $[0] = el1; $[1] = el2; $[2] = s; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md deleted file mode 100644 index 1170f6a60a81..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md +++ /dev/null @@ -1,95 +0,0 @@ - -## Input - -```javascript -// @hookPattern:".*\b(use[^$]+)$" @enablePreserveExistingMemoizationGuarantees:false - -import * as React from 'react'; -import {makeArray, useHook} from 'shared-runtime'; - -const React$useState = React.useState; -const React$useMemo = React.useMemo; -const Internal$Reassigned$useHook = useHook; - -function Component() { - const [state, setState] = React$useState(0); - const object = Internal$Reassigned$useHook(); - const json = JSON.stringify(object); - const doubledArray = React$useMemo(() => { - return makeArray(state); - }, [state]); - return ( -
- {doubledArray.join('')} - {json} -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @hookPattern:".*\b(use[^$]+)$" @enablePreserveExistingMemoizationGuarantees:false - -import * as React from "react"; -import { makeArray, useHook } from "shared-runtime"; - -const React$useState = React.useState; -const React$useMemo = React.useMemo; -const Internal$Reassigned$useHook = useHook; - -function Component() { - const $ = _c(7); - const [state] = React$useState(0); - const object = Internal$Reassigned$useHook(); - let t0; - if ($[0] !== object) { - t0 = JSON.stringify(object); - $[0] = object; - $[1] = t0; - } else { - t0 = $[1]; - } - const json = t0; - let t1; - if ($[2] !== state) { - const doubledArray = makeArray(state); - t1 = doubledArray.join(""); - $[2] = state; - $[3] = t1; - } else { - t1 = $[3]; - } - let t2; - if ($[4] !== json || $[5] !== t1) { - t2 = ( -
- {t1} - {json} -
- ); - $[4] = json; - $[5] = t1; - $[6] = t2; - } else { - t2 = $[6]; - } - return t2; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; - -``` - -### Eval output -(kind: ok)
0{"a":0,"b":"value1","c":true}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.js deleted file mode 100644 index 4db8451bc855..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hooks-with-prefix.js +++ /dev/null @@ -1,28 +0,0 @@ -// @hookPattern:".*\b(use[^$]+)$" @enablePreserveExistingMemoizationGuarantees:false - -import * as React from 'react'; -import {makeArray, useHook} from 'shared-runtime'; - -const React$useState = React.useState; -const React$useMemo = React.useMemo; -const Internal$Reassigned$useHook = useHook; - -function Component() { - const [state, setState] = React$useState(0); - const object = Internal$Reassigned$useHook(); - const json = JSON.stringify(object); - const doubledArray = React$useMemo(() => { - return makeArray(state); - }, [state]); - return ( -
- {doubledArray.join('')} - {json} -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.expect.md deleted file mode 100644 index c85f4d4468f9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.expect.md +++ /dev/null @@ -1,34 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @compilationMode:"infer" @panicThreshold:"none" -import useMyEffect from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function nonReactFn(arg) { - useMyEffect(() => [1, 2, arg], AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.callsite-in-non-react-fn-default-import.ts:6:2 - 4 | - 5 | function nonReactFn(arg) { -> 6 | useMyEffect(() => [1, 2, arg], AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 7 | } - 8 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.js deleted file mode 100644 index adfe3ffadd59..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn-default-import.js +++ /dev/null @@ -1,7 +0,0 @@ -// @inferEffectDependencies @compilationMode:"infer" @panicThreshold:"none" -import useMyEffect from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function nonReactFn(arg) { - useMyEffect(() => [1, 2, arg], AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.expect.md deleted file mode 100644 index a372aeb6b290..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @compilationMode:"infer" @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -function nonReactFn(arg) { - useEffect(() => [1, 2, arg], AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.callsite-in-non-react-fn.ts:5:2 - 3 | - 4 | function nonReactFn(arg) { -> 5 | useEffect(() => [1, 2, arg], AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 6 | } - 7 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.js deleted file mode 100644 index 9cbc47086b23..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.callsite-in-non-react-fn.js +++ /dev/null @@ -1,6 +0,0 @@ -// @inferEffectDependencies @compilationMode:"infer" @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -function nonReactFn(arg) { - useEffect(() => [1, 2, arg], AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.expect.md deleted file mode 100644 index ac56c25cb324..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.expect.md +++ /dev/null @@ -1,48 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -/** - * Error on non-inlined effect functions: - * 1. From the effect hook callee's perspective, it only makes sense - * to either - * (a) never hard error (i.e. failing to infer deps is acceptable) or - * (b) always hard error, - * regardless of whether the callback function is an inline fn. - * 2. (Technical detail) it's harder to support detecting cases in which - * function (pre-Forget transform) was inline but becomes memoized - */ -function Component({foo}) { - function f() { - console.log(foo); - } - - // No inferred dep array, the argument is not a lambda - useEffect(f, AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.non-inlined-effect-fn.ts:20:2 - 18 | - 19 | // No inferred dep array, the argument is not a lambda -> 20 | useEffect(f, AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 21 | } - 22 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.js deleted file mode 100644 index c113fe363c53..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.non-inlined-effect-fn.js +++ /dev/null @@ -1,21 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -/** - * Error on non-inlined effect functions: - * 1. From the effect hook callee's perspective, it only makes sense - * to either - * (a) never hard error (i.e. failing to infer deps is acceptable) or - * (b) always hard error, - * regardless of whether the callback function is an inline fn. - * 2. (Technical detail) it's harder to support detecting cases in which - * function (pre-Forget transform) was inline but becomes memoized - */ -function Component({foo}) { - function f() { - console.log(foo); - } - - // No inferred dep array, the argument is not a lambda - useEffect(f, AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.expect.md deleted file mode 100644 index 251d7c76c900..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.expect.md +++ /dev/null @@ -1,50 +0,0 @@ - -## Input - -```javascript -// @dynamicGating:{"source":"shared-runtime"} @inferEffectDependencies @panicThreshold:"none" - -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -/** - * TODO: run the non-forget enabled version through the effect inference - * pipeline. - */ -function Component({foo}) { - 'use memo if(getTrue)'; - const arr = []; - useEffectWrapper(() => arr.push(foo), AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], - sequentialRenders: [{foo: 1}, {foo: 2}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.todo-dynamic-gating.ts:13:2 - 11 | 'use memo if(getTrue)'; - 12 | const arr = []; -> 13 | useEffectWrapper(() => arr.push(foo), AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 14 | arr.push(2); - 15 | return arr; - 16 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.js deleted file mode 100644 index 667abfea6f26..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-dynamic-gating.js +++ /dev/null @@ -1,22 +0,0 @@ -// @dynamicGating:{"source":"shared-runtime"} @inferEffectDependencies @panicThreshold:"none" - -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -/** - * TODO: run the non-forget enabled version through the effect inference - * pipeline. - */ -function Component({foo}) { - 'use memo if(getTrue)'; - const arr = []; - useEffectWrapper(() => arr.push(foo), AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], - sequentialRenders: [{foo: 1}, {foo: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.expect.md deleted file mode 100644 index 041a0f4e8bc8..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.expect.md +++ /dev/null @@ -1,48 +0,0 @@ - -## Input - -```javascript -// @gating @inferEffectDependencies @panicThreshold:"none" -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -/** - * TODO: run the non-forget enabled version through the effect inference - * pipeline. - */ -function Component({foo}) { - const arr = []; - useEffectWrapper(() => arr.push(foo), AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], - sequentialRenders: [{foo: 1}, {foo: 2}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.todo-gating.ts:11:2 - 9 | function Component({foo}) { - 10 | const arr = []; -> 11 | useEffectWrapper(() => arr.push(foo), AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 12 | arr.push(2); - 13 | return arr; - 14 | } -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.js deleted file mode 100644 index 60bd9a362e30..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-gating.js +++ /dev/null @@ -1,20 +0,0 @@ -// @gating @inferEffectDependencies @panicThreshold:"none" -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -/** - * TODO: run the non-forget enabled version through the effect inference - * pipeline. - */ -function Component({foo}) { - const arr = []; - useEffectWrapper(() => arr.push(foo), AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], - sequentialRenders: [{foo: 1}, {foo: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.expect.md deleted file mode 100644 index 3c7fa047f8b5..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.expect.md +++ /dev/null @@ -1,34 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import React from 'react'; - -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - React.useEffect(() => print(obj), React.AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.todo-import-default-property-useEffect.ts:6:2 - 4 | function NonReactiveDepInEffect() { - 5 | const obj = makeObject_Primitives(); -> 6 | React.useEffect(() => print(obj), React.AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 7 | } - 8 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.js deleted file mode 100644 index c3044274cd25..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-import-default-property-useEffect.js +++ /dev/null @@ -1,7 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import React from 'react'; - -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - React.useEffect(() => print(obj), React.AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.expect.md deleted file mode 100644 index 00af7ec6ad59..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.expect.md +++ /dev/null @@ -1,60 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {useSpecialEffect} from 'shared-runtime'; -import {AUTODEPS} from 'react'; - -/** - * Note that a react compiler-based transform still has limitations on JS syntax. - * We should surface these as actionable lint / build errors to devs. - */ -function Component({prop1}) { - 'use memo'; - useSpecialEffect( - () => { - try { - console.log(prop1); - } finally { - console.log('exiting'); - } - }, - [prop1], - AUTODEPS - ); - return
{prop1}
; -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics Todo: (BuildHIR::lowerStatement) Handle TryStatement without a catch clause (13:6). - -error.todo-syntax.ts:11:2 - 9 | function Component({prop1}) { - 10 | 'use memo'; -> 11 | useSpecialEffect( - | ^^^^^^^^^^^^^^^^^ -> 12 | () => { - | ^^^^^^^^^^^ -> 13 | try { - … - | ^^^^^^^^^^^ -> 20 | AUTODEPS - | ^^^^^^^^^^^ -> 21 | ); - | ^^^^ Cannot infer dependencies - 22 | return
{prop1}
; - 23 | } - 24 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.js deleted file mode 100644 index ad1e58532953..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.todo-syntax.js +++ /dev/null @@ -1,23 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {useSpecialEffect} from 'shared-runtime'; -import {AUTODEPS} from 'react'; - -/** - * Note that a react compiler-based transform still has limitations on JS syntax. - * We should surface these as actionable lint / build errors to devs. - */ -function Component({prop1}) { - 'use memo'; - useSpecialEffect( - () => { - try { - console.log(prop1); - } finally { - console.log('exiting'); - } - }, - [prop1], - AUTODEPS - ); - return
{prop1}
; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.expect.md deleted file mode 100644 index 8371ba004034..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.expect.md +++ /dev/null @@ -1,34 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -function Component({propVal}) { - 'use no memo'; - useEffect(() => [propVal], AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.use-no-memo.ts:6:2 - 4 | function Component({propVal}) { - 5 | 'use no memo'; -> 6 | useEffect(() => [propVal], AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 7 | } - 8 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.js deleted file mode 100644 index 30fbd8c2a61e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/error.use-no-memo.js +++ /dev/null @@ -1,7 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; - -function Component({propVal}) { - 'use no memo'; - useEffect(() => [propVal], AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.expect.md deleted file mode 100644 index b8cf1bc40aa8..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.expect.md +++ /dev/null @@ -1,39 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0].value), AUTODEPS); - arr.push({value: foo}); - return arr; -} - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function Component(t0) { - const { foo } = t0; - const arr = []; - - useEffect(() => print(arr[0].value), [arr[0].value]); - arr.push({ value: foo }); - return arr; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.js deleted file mode 100644 index 9ec840ab623e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-granular-access.js +++ /dev/null @@ -1,12 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0].value), AUTODEPS); - arr.push({value: foo}); - return arr; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.expect.md deleted file mode 100644 index 4606d49f37fa..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.expect.md +++ /dev/null @@ -1,58 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0]?.value), AUTODEPS); - arr.push({value: foo}); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function Component(t0) { - const { foo } = t0; - const arr = []; - - useEffect(() => print(arr[0]?.value), [arr[0]?.value]); - arr.push({ value: foo }); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ foo: 1 }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":149},"end":{"line":12,"column":1,"index":404},"filename":"mutate-after-useeffect-optional-chain.ts"},"detail":{"options":{"category":"Immutability","reason":"This value cannot be modified","description":"Modifying a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the modification before calling useEffect()","details":[{"kind":"error","loc":{"start":{"line":10,"column":2,"index":365},"end":{"line":10,"column":5,"index":368},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"},"message":"value cannot be modified"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":9,"column":2,"index":314},"end":{"line":9,"column":49,"index":361},"filename":"mutate-after-useeffect-optional-chain.ts"},"decorations":[{"start":{"line":9,"column":24,"index":336},"end":{"line":9,"column":27,"index":339},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":149},"end":{"line":12,"column":1,"index":404},"filename":"mutate-after-useeffect-optional-chain.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) [{"value":1}] -logs: [1] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.js deleted file mode 100644 index c78251bf2d3b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-optional-chain.js +++ /dev/null @@ -1,17 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0]?.value), AUTODEPS); - arr.push({value: foo}); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.expect.md deleted file mode 100644 index ea5a887b8bf7..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.expect.md +++ /dev/null @@ -1,57 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly - -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({arrRef}) { - // Avoid taking arr.current as a dependency - useEffect(() => print(arrRef.current), AUTODEPS); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{arrRef: {current: {val: 'initial ref value'}}}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly - -import { useEffect, useRef, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function Component(t0) { - const { arrRef } = t0; - - useEffect(() => print(arrRef.current), [arrRef]); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ arrRef: { current: { val: "initial ref value" } } }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":6,"column":0,"index":158},"end":{"line":11,"column":1,"index":331},"filename":"mutate-after-useeffect-ref-access.ts"},"detail":{"options":{"category":"Refs","reason":"Cannot access refs during render","description":"React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef)","details":[{"kind":"error","loc":{"start":{"line":9,"column":2,"index":289},"end":{"line":9,"column":16,"index":303},"filename":"mutate-after-useeffect-ref-access.ts"},"message":"Cannot update ref during render"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":8,"column":2,"index":237},"end":{"line":8,"column":50,"index":285},"filename":"mutate-after-useeffect-ref-access.ts"},"decorations":[{"start":{"line":8,"column":24,"index":259},"end":{"line":8,"column":30,"index":265},"filename":"mutate-after-useeffect-ref-access.ts","identifierName":"arrRef"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":6,"column":0,"index":158},"end":{"line":11,"column":1,"index":331},"filename":"mutate-after-useeffect-ref-access.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) {"current":{"val":2}} -logs: [{ val: 2 }] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.js deleted file mode 100644 index d972d6d001c8..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect-ref-access.js +++ /dev/null @@ -1,16 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly - -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({arrRef}) { - // Avoid taking arr.current as a dependency - useEffect(() => print(arrRef.current), AUTODEPS); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{arrRef: {current: {val: 'initial ref value'}}}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.expect.md deleted file mode 100644 index 71cd9fb62085..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.expect.md +++ /dev/null @@ -1,56 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - const arr = []; - useEffect(() => { - arr.push(foo); - }, AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import { useEffect, AUTODEPS } from "react"; - -function Component(t0) { - const { foo } = t0; - const arr = []; - useEffect(() => { - arr.push(foo); - }, [arr, foo]); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ foo: 1 }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":4,"column":0,"index":111},"end":{"line":11,"column":1,"index":242},"filename":"mutate-after-useeffect.ts"},"detail":{"options":{"category":"Immutability","reason":"This value cannot be modified","description":"Modifying a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the modification before calling useEffect()","details":[{"kind":"error","loc":{"start":{"line":9,"column":2,"index":214},"end":{"line":9,"column":5,"index":217},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},"message":"value cannot be modified"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":6,"column":2,"index":159},"end":{"line":8,"column":14,"index":210},"filename":"mutate-after-useeffect.ts"},"decorations":[{"start":{"line":7,"column":4,"index":181},"end":{"line":7,"column":7,"index":184},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":4,"index":181},"end":{"line":7,"column":7,"index":184},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":13,"index":190},"end":{"line":7,"column":16,"index":193},"filename":"mutate-after-useeffect.ts","identifierName":"foo"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":111},"end":{"line":11,"column":1,"index":242},"filename":"mutate-after-useeffect.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) [2] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.js deleted file mode 100644 index 965f64fd7aeb..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/bailout-retry/mutate-after-useeffect.js +++ /dev/null @@ -1,16 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - const arr = []; - useEffect(() => { - arr.push(foo); - }, AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.expect.md deleted file mode 100644 index 4b2f94a263a1..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - useEffect(AUTODEPS); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.wrong-index-no-func.ts:5:2 - 3 | - 4 | function Component({foo}) { -> 5 | useEffect(AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 6 | } - 7 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.js deleted file mode 100644 index 973798b5731b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index-no-func.js +++ /dev/null @@ -1,6 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - useEffect(AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.expect.md deleted file mode 100644 index 0a5cde5cd65d..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.expect.md +++ /dev/null @@ -1,52 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {AUTODEPS} from 'react'; -import useEffectWrapper from 'useEffectWrapper'; - -function Component({foo}) { - useEffectWrapper( - () => { - console.log(foo); - }, - [foo], - AUTODEPS - ); -} - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.wrong-index.ts:6:2 - 4 | - 5 | function Component({foo}) { -> 6 | useEffectWrapper( - | ^^^^^^^^^^^^^^^^^ -> 7 | () => { - | ^^^^^^^^^^^ -> 8 | console.log(foo); - | ^^^^^^^^^^^ -> 9 | }, - | ^^^^^^^^^^^ -> 10 | [foo], - | ^^^^^^^^^^^ -> 11 | AUTODEPS - | ^^^^^^^^^^^ -> 12 | ); - | ^^^^ Cannot infer dependencies - 13 | } - 14 | -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.js deleted file mode 100644 index b0898e3dbde9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/error.wrong-index.js +++ /dev/null @@ -1,13 +0,0 @@ -// @inferEffectDependencies -import {AUTODEPS} from 'react'; -import useEffectWrapper from 'useEffectWrapper'; - -function Component({foo}) { - useEffectWrapper( - () => { - console.log(foo); - }, - [foo], - AUTODEPS - ); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.expect.md deleted file mode 100644 index ae2b018d36dd..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -function useCustomRef() { - const ref = useRef(); - return ref; -} -function NonReactiveWrapper() { - const ref = useCustomRef(); - useEffect(() => { - print(ref); - }, AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useRef, AUTODEPS } from "react"; -function useCustomRef() { - const ref = useRef(); - return ref; -} - -function NonReactiveWrapper() { - const $ = _c(2); - const ref = useCustomRef(); - let t0; - if ($[0] !== ref) { - t0 = () => { - print(ref); - }; - $[0] = ref; - $[1] = t0; - } else { - t0 = $[1]; - } - useEffect(t0, [ref]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.js deleted file mode 100644 index 18de64bc08b4..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/helper-nonreactive.js +++ /dev/null @@ -1,12 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -function useCustomRef() { - const ref = useRef(); - return ref; -} -function NonReactiveWrapper() { - const ref = useCustomRef(); - useEffect(() => { - print(ref); - }, AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.expect.md deleted file mode 100644 index a221475b95bb..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.expect.md +++ /dev/null @@ -1,59 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import * as React from 'react'; -import * as SharedRuntime from 'shared-runtime'; - -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - React.useEffect(() => print(obj), React.AUTODEPS); - SharedRuntime.useSpecialEffect(() => print(obj), [obj], React.AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import * as React from "react"; -import * as SharedRuntime from "shared-runtime"; - -function NonReactiveDepInEffect() { - const $ = _c(4); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = makeObject_Primitives(); - $[0] = t0; - } else { - t0 = $[0]; - } - const obj = t0; - let t1; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = () => print(obj); - $[1] = t1; - } else { - t1 = $[1]; - } - React.useEffect(t1, [obj]); - let t2; - let t3; - if ($[2] === Symbol.for("react.memo_cache_sentinel")) { - t2 = () => print(obj); - t3 = [obj]; - $[2] = t2; - $[3] = t3; - } else { - t2 = $[2]; - t3 = $[3]; - } - SharedRuntime.useSpecialEffect(t2, t3, [obj]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.js deleted file mode 100644 index 3ef0bf400e5b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/import-namespace-useEffect.js +++ /dev/null @@ -1,9 +0,0 @@ -// @inferEffectDependencies -import * as React from 'react'; -import * as SharedRuntime from 'shared-runtime'; - -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - React.useEffect(() => print(obj), React.AUTODEPS); - SharedRuntime.useSpecialEffect(() => print(obj), [obj], React.AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.expect.md deleted file mode 100644 index 03d9ba04e90c..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.expect.md +++ /dev/null @@ -1,63 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {print, useSpecialEffect} from 'shared-runtime'; -import {AUTODEPS} from 'react'; - -function CustomConfig({propVal}) { - // Insertion - useSpecialEffect(() => print(propVal), [propVal], AUTODEPS); - // No insertion - useSpecialEffect(() => print(propVal), [propVal], [propVal]); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { print, useSpecialEffect } from "shared-runtime"; -import { AUTODEPS } from "react"; - -function CustomConfig(t0) { - const $ = _c(7); - const { propVal } = t0; - let t1; - let t2; - if ($[0] !== propVal) { - t1 = () => print(propVal); - t2 = [propVal]; - $[0] = propVal; - $[1] = t1; - $[2] = t2; - } else { - t1 = $[1]; - t2 = $[2]; - } - useSpecialEffect(t1, t2, [propVal]); - let t3; - let t4; - let t5; - if ($[3] !== propVal) { - t3 = () => print(propVal); - t4 = [propVal]; - t5 = [propVal]; - $[3] = propVal; - $[4] = t3; - $[5] = t4; - $[6] = t5; - } else { - t3 = $[4]; - t4 = $[5]; - t5 = $[6]; - } - useSpecialEffect(t3, t4, t5); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.js deleted file mode 100644 index efb821239841..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-deps-custom-config.js +++ /dev/null @@ -1,10 +0,0 @@ -// @inferEffectDependencies -import {print, useSpecialEffect} from 'shared-runtime'; -import {AUTODEPS} from 'react'; - -function CustomConfig({propVal}) { - // Insertion - useSpecialEffect(() => print(propVal), [propVal], AUTODEPS); - // No insertion - useSpecialEffect(() => print(propVal), [propVal], [propVal]); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.expect.md deleted file mode 100644 index e09c0ef8e8a4..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.expect.md +++ /dev/null @@ -1,129 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import useEffectWrapper from 'useEffectWrapper'; - -const moduleNonReactive = 0; - -function Component({foo, bar}) { - const localNonreactive = 0; - const ref = useRef(0); - const localNonPrimitiveReactive = { - foo, - }; - const localNonPrimitiveNonreactive = {}; - useEffect(() => { - console.log(foo); - console.log(bar); - console.log(moduleNonReactive); - console.log(localNonreactive); - console.log(globalValue); - console.log(ref.current); - console.log(localNonPrimitiveReactive); - console.log(localNonPrimitiveNonreactive); - }, AUTODEPS); - - // Optional chains and property accesses - // TODO: we may be able to save bytes by omitting property accesses if the - // object of the member expression is already included in the inferred deps - useEffect(() => { - console.log(bar?.baz); - console.log(bar.qux); - }, AUTODEPS); - - useEffectWrapper(() => { - console.log(foo); - }, AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useRef, AUTODEPS } from "react"; -import useEffectWrapper from "useEffectWrapper"; - -const moduleNonReactive = 0; - -function Component(t0) { - const $ = _c(12); - const { foo, bar } = t0; - - const ref = useRef(0); - let t1; - if ($[0] !== foo) { - t1 = { foo }; - $[0] = foo; - $[1] = t1; - } else { - t1 = $[1]; - } - const localNonPrimitiveReactive = t1; - let t2; - if ($[2] === Symbol.for("react.memo_cache_sentinel")) { - t2 = {}; - $[2] = t2; - } else { - t2 = $[2]; - } - const localNonPrimitiveNonreactive = t2; - let t3; - if ($[3] !== bar || $[4] !== foo || $[5] !== localNonPrimitiveReactive) { - t3 = () => { - console.log(foo); - console.log(bar); - console.log(moduleNonReactive); - console.log(0); - console.log(globalValue); - console.log(ref.current); - console.log(localNonPrimitiveReactive); - console.log(localNonPrimitiveNonreactive); - }; - $[3] = bar; - $[4] = foo; - $[5] = localNonPrimitiveReactive; - $[6] = t3; - } else { - t3 = $[6]; - } - useEffect(t3, [ - foo, - bar, - localNonPrimitiveReactive, - localNonPrimitiveNonreactive, - ]); - let t4; - if ($[7] !== bar.baz || $[8] !== bar.qux) { - t4 = () => { - console.log(bar?.baz); - console.log(bar.qux); - }; - $[7] = bar.baz; - $[8] = bar.qux; - $[9] = t4; - } else { - t4 = $[9]; - } - useEffect(t4, [bar.baz, bar.qux]); - let t5; - if ($[10] !== foo) { - t5 = () => { - console.log(foo); - }; - $[10] = foo; - $[11] = t5; - } else { - t5 = $[11]; - } - useEffectWrapper(t5, [foo]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.js deleted file mode 100644 index bf02842d25ff..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/infer-effect-dependencies.js +++ /dev/null @@ -1,36 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import useEffectWrapper from 'useEffectWrapper'; - -const moduleNonReactive = 0; - -function Component({foo, bar}) { - const localNonreactive = 0; - const ref = useRef(0); - const localNonPrimitiveReactive = { - foo, - }; - const localNonPrimitiveNonreactive = {}; - useEffect(() => { - console.log(foo); - console.log(bar); - console.log(moduleNonReactive); - console.log(localNonreactive); - console.log(globalValue); - console.log(ref.current); - console.log(localNonPrimitiveReactive); - console.log(localNonPrimitiveNonreactive); - }, AUTODEPS); - - // Optional chains and property accesses - // TODO: we may be able to save bytes by omitting property accesses if the - // object of the member expression is already included in the inferred deps - useEffect(() => { - console.log(bar?.baz); - console.log(bar.qux); - }, AUTODEPS); - - useEffectWrapper(() => { - console.log(foo); - }, AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.expect.md deleted file mode 100644 index 2730f8941b51..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.expect.md +++ /dev/null @@ -1,80 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {makeObject_Primitives, print} from 'shared-runtime'; - -/** - * Note that `obj` is currently added to the effect dependency array, even - * though it's non-reactive due to memoization. - * - * This is a TODO in effect dependency inference. Note that we cannot simply - * filter out non-reactive effect dependencies, as some non-reactive (by data - * flow) values become reactive due to scope pruning. See the - * `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. - * - * Realizing that this `useEffect` should have an empty dependency array - * requires effect dependency inference to be structured similarly to memo - * dependency inference. - * Pass 1: add all potential dependencies regardless of dataflow reactivity - * Pass 2: (todo) prune non-reactive dependencies - * - * Note that instruction reordering should significantly reduce scope pruning - */ -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - useEffect(() => print(obj), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { makeObject_Primitives, print } from "shared-runtime"; - -/** - * Note that `obj` is currently added to the effect dependency array, even - * though it's non-reactive due to memoization. - * - * This is a TODO in effect dependency inference. Note that we cannot simply - * filter out non-reactive effect dependencies, as some non-reactive (by data - * flow) values become reactive due to scope pruning. See the - * `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. - * - * Realizing that this `useEffect` should have an empty dependency array - * requires effect dependency inference to be structured similarly to memo - * dependency inference. - * Pass 1: add all potential dependencies regardless of dataflow reactivity - * Pass 2: (todo) prune non-reactive dependencies - * - * Note that instruction reordering should significantly reduce scope pruning - */ -function NonReactiveDepInEffect() { - const $ = _c(2); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = makeObject_Primitives(); - $[0] = t0; - } else { - t0 = $[0]; - } - const obj = t0; - let t1; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = () => print(obj); - $[1] = t1; - } else { - t1 = $[1]; - } - useEffect(t1, [obj]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.js deleted file mode 100644 index f6e44034c51f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-dep.js +++ /dev/null @@ -1,25 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {makeObject_Primitives, print} from 'shared-runtime'; - -/** - * Note that `obj` is currently added to the effect dependency array, even - * though it's non-reactive due to memoization. - * - * This is a TODO in effect dependency inference. Note that we cannot simply - * filter out non-reactive effect dependencies, as some non-reactive (by data - * flow) values become reactive due to scope pruning. See the - * `infer-effect-deps/pruned-nonreactive-obj` fixture for why this matters. - * - * Realizing that this `useEffect` should have an empty dependency array - * requires effect dependency inference to be structured similarly to memo - * dependency inference. - * Pass 1: add all potential dependencies regardless of dataflow reactivity - * Pass 2: (todo) prune non-reactive dependencies - * - * Note that instruction reordering should significantly reduce scope pruning - */ -function NonReactiveDepInEffect() { - const obj = makeObject_Primitives(); - useEffect(() => print(obj), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.expect.md deleted file mode 100644 index c95f9625bbb3..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useEffectEvent, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * We do not include effect events in dep arrays. - */ -function NonReactiveEffectEvent() { - const fn = useEffectEvent(() => print('hello world')); - useEffect(() => fn(), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useEffectEvent, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/** - * We do not include effect events in dep arrays. - */ -function NonReactiveEffectEvent() { - const $ = _c(2); - const fn = useEffectEvent(_temp); - let t0; - if ($[0] !== fn) { - t0 = () => fn(); - $[0] = fn; - $[1] = t0; - } else { - t0 = $[1]; - } - useEffect(t0, []); -} -function _temp() { - return print("hello world"); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.js deleted file mode 100644 index fc78581e6d52..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-effect-event.js +++ /dev/null @@ -1,11 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useEffectEvent, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * We do not include effect events in dep arrays. - */ -function NonReactiveEffectEvent() { - const fn = useEffectEvent(() => print('hello world')); - useEffect(() => fn(), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.expect.md deleted file mode 100644 index 05dcab77968a..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.expect.md +++ /dev/null @@ -1,89 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * We never include a .current access in a dep array because it may be a ref access. - * This might over-capture objects that are not refs and happen to have fields named - * current, but that should be a rare case and the result would still be correct - * (assuming the effect is idempotent). In the worst case, you can always write a manual - * dep array. - */ -function RefsInEffects() { - const ref = useRefHelper(); - const wrapped = useDeeperRefHelper(); - useEffect(() => { - print(ref.current); - print(wrapped.foo.current); - }, AUTODEPS); -} - -function useRefHelper() { - return useRef(0); -} - -function useDeeperRefHelper() { - return {foo: useRefHelper()}; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/** - * We never include a .current access in a dep array because it may be a ref access. - * This might over-capture objects that are not refs and happen to have fields named - * current, but that should be a rare case and the result would still be correct - * (assuming the effect is idempotent). In the worst case, you can always write a manual - * dep array. - */ -function RefsInEffects() { - const $ = _c(3); - const ref = useRefHelper(); - const wrapped = useDeeperRefHelper(); - let t0; - if ($[0] !== ref || $[1] !== wrapped.foo.current) { - t0 = () => { - print(ref.current); - print(wrapped.foo.current); - }; - $[0] = ref; - $[1] = wrapped.foo.current; - $[2] = t0; - } else { - t0 = $[2]; - } - useEffect(t0, [ref, wrapped.foo]); -} - -function useRefHelper() { - return useRef(0); -} - -function useDeeperRefHelper() { - const $ = _c(2); - const t0 = useRefHelper(); - let t1; - if ($[0] !== t0) { - t1 = { foo: t0 }; - $[0] = t0; - $[1] = t1; - } else { - t1 = $[1]; - } - return t1; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.js deleted file mode 100644 index 92a905061fe0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref-helper.js +++ /dev/null @@ -1,27 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * We never include a .current access in a dep array because it may be a ref access. - * This might over-capture objects that are not refs and happen to have fields named - * current, but that should be a rare case and the result would still be correct - * (assuming the effect is idempotent). In the worst case, you can always write a manual - * dep array. - */ -function RefsInEffects() { - const ref = useRefHelper(); - const wrapped = useDeeperRefHelper(); - useEffect(() => { - print(ref.current); - print(wrapped.foo.current); - }, AUTODEPS); -} - -function useRefHelper() { - return useRef(0); -} - -function useDeeperRefHelper() { - return {foo: useRefHelper()}; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.expect.md deleted file mode 100644 index 3c5835e75550..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.expect.md +++ /dev/null @@ -1,51 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveRefInEffect() { - const ref = useRef('initial value'); - useEffect(() => print(ref.current), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useRef, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveRefInEffect() { - const $ = _c(1); - const ref = useRef("initial value"); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = () => print(ref.current); - $[0] = t0; - } else { - t0 = $[0]; - } - useEffect(t0, []); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.js deleted file mode 100644 index 38b83b9b8f81..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-ref.js +++ /dev/null @@ -1,14 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveRefInEffect() { - const ref = useRef('initial value'); - useEffect(() => print(ref.current), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.expect.md deleted file mode 100644 index e0a1a95b339f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.expect.md +++ /dev/null @@ -1,51 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveSetStateInEffect() { - const [_, setState] = useState('initial value'); - useEffect(() => print(setState), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useState, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveSetStateInEffect() { - const $ = _c(1); - const [, setState] = useState("initial value"); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = () => print(setState); - $[0] = t0; - } else { - t0 = $[0]; - } - useEffect(t0, []); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.js deleted file mode 100644 index 0f8abceca0ec..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/nonreactive-setState.js +++ /dev/null @@ -1,14 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/** - * Special case of `infer-effect-deps/nonreactive-dep`. - * - * We know that local `useRef` return values are stable, regardless of - * inferred memoization. - */ -function NonReactiveSetStateInEffect() { - const [_, setState] = useState('initial value'); - useEffect(() => print(setState), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.expect.md deleted file mode 100644 index a0b0594afa1b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.expect.md +++ /dev/null @@ -1,46 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; -/** - * This compiled output is technically incorrect but this is currently the same - * case as a bailout (an effect that overfires). - * - * To ensure an empty deps array is passed, we need special case - * `InferEffectDependencies` for outlined functions (likely easier) or run it - * before OutlineFunctions - */ -function OutlinedFunctionInEffect() { - useEffect(() => print('hello world!'), AUTODEPS); -} - -``` - -## Code - -```javascript -// @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; -/** - * This compiled output is technically incorrect but this is currently the same - * case as a bailout (an effect that overfires). - * - * To ensure an empty deps array is passed, we need special case - * `InferEffectDependencies` for outlined functions (likely easier) or run it - * before OutlineFunctions - */ -function OutlinedFunctionInEffect() { - useEffect(_temp, []); -} -function _temp() { - return print("hello world!"); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.js deleted file mode 100644 index 5ec2f7d8f9ed..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/outlined-function.js +++ /dev/null @@ -1,14 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; -/** - * This compiled output is technically incorrect but this is currently the same - * case as a bailout (an effect that overfires). - * - * To ensure an empty deps array is passed, we need special case - * `InferEffectDependencies` for outlined functions (likely easier) or run it - * before OutlineFunctions - */ -function OutlinedFunctionInEffect() { - useEffect(() => print('hello world!'), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.expect.md deleted file mode 100644 index c6c0087a774f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.expect.md +++ /dev/null @@ -1,119 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useIdentity, mutate, makeObject} from 'shared-runtime'; -import {useEffect, AUTODEPS} from 'react'; - -/** - * When a semantically non-reactive value has a pruned scope (i.e. the object - * identity becomes reactive, but the underlying value it represents should be - * constant), the compiler can choose to either - * - add it as a dependency (and rerun the effect) - * - not add it as a dependency - * - * We keep semantically non-reactive values in both memo block and effect - * dependency arrays to avoid versioning invariants e.g. `x !== y.aliasedX`. - * ```js - * function Component() { - * // obj is semantically non-reactive, but its memo scope is pruned due to - * // the interleaving hook call - * const obj = {}; - * useHook(); - * write(obj); - * - * const ref = useRef(); - * - * // this effect needs to be rerun when obj's referential identity changes, - * // because it might alias obj to a useRef / mutable store. - * useEffect(() => ref.current = obj, ???); - * - * // in a custom hook (or child component), the user might expect versioning - * // invariants to hold - * useHook(ref, obj); - * } - * - * // defined elsewhere - * function useHook(someRef, obj) { - * useEffect( - * () => assert(someRef.current === obj), - * [someRef, obj] - * ); - * } - * ``` - */ -function PrunedNonReactive() { - const obj = makeObject(); - useIdentity(null); - mutate(obj); - - useEffect(() => print(obj.value), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useIdentity, mutate, makeObject } from "shared-runtime"; -import { useEffect, AUTODEPS } from "react"; - -/** - * When a semantically non-reactive value has a pruned scope (i.e. the object - * identity becomes reactive, but the underlying value it represents should be - * constant), the compiler can choose to either - * - add it as a dependency (and rerun the effect) - * - not add it as a dependency - * - * We keep semantically non-reactive values in both memo block and effect - * dependency arrays to avoid versioning invariants e.g. `x !== y.aliasedX`. - * ```js - * function Component() { - * // obj is semantically non-reactive, but its memo scope is pruned due to - * // the interleaving hook call - * const obj = {}; - * useHook(); - * write(obj); - * - * const ref = useRef(); - * - * // this effect needs to be rerun when obj's referential identity changes, - * // because it might alias obj to a useRef / mutable store. - * useEffect(() => ref.current = obj, ???); - * - * // in a custom hook (or child component), the user might expect versioning - * // invariants to hold - * useHook(ref, obj); - * } - * - * // defined elsewhere - * function useHook(someRef, obj) { - * useEffect( - * () => assert(someRef.current === obj), - * [someRef, obj] - * ); - * } - * ``` - */ -function PrunedNonReactive() { - const $ = _c(2); - const obj = makeObject(); - useIdentity(null); - mutate(obj); - let t0; - if ($[0] !== obj.value) { - t0 = () => print(obj.value); - $[0] = obj.value; - $[1] = t0; - } else { - t0 = $[1]; - } - useEffect(t0, [obj.value]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.js deleted file mode 100644 index f210f2779a18..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/pruned-nonreactive-obj.js +++ /dev/null @@ -1,48 +0,0 @@ -// @inferEffectDependencies -import {useIdentity, mutate, makeObject} from 'shared-runtime'; -import {useEffect, AUTODEPS} from 'react'; - -/** - * When a semantically non-reactive value has a pruned scope (i.e. the object - * identity becomes reactive, but the underlying value it represents should be - * constant), the compiler can choose to either - * - add it as a dependency (and rerun the effect) - * - not add it as a dependency - * - * We keep semantically non-reactive values in both memo block and effect - * dependency arrays to avoid versioning invariants e.g. `x !== y.aliasedX`. - * ```js - * function Component() { - * // obj is semantically non-reactive, but its memo scope is pruned due to - * // the interleaving hook call - * const obj = {}; - * useHook(); - * write(obj); - * - * const ref = useRef(); - * - * // this effect needs to be rerun when obj's referential identity changes, - * // because it might alias obj to a useRef / mutable store. - * useEffect(() => ref.current = obj, ???); - * - * // in a custom hook (or child component), the user might expect versioning - * // invariants to hold - * useHook(ref, obj); - * } - * - * // defined elsewhere - * function useHook(someRef, obj) { - * useEffect( - * () => assert(someRef.current === obj), - * [someRef, obj] - * ); - * } - * ``` - */ -function PrunedNonReactive() { - const obj = makeObject(); - useIdentity(null); - mutate(obj); - - useEffect(() => print(obj.value), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.expect.md deleted file mode 100644 index 983c474bdeae..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExprMerge({propVal}) { - const obj = {a: {b: propVal}}; - useEffect(() => print(obj.a, obj.a.b), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function ReactiveMemberExprMerge(t0) { - const $ = _c(4); - const { propVal } = t0; - let t1; - if ($[0] !== propVal) { - t1 = { a: { b: propVal } }; - $[0] = propVal; - $[1] = t1; - } else { - t1 = $[1]; - } - const obj = t1; - let t2; - if ($[2] !== obj.a) { - t2 = () => print(obj.a, obj.a.b); - $[2] = obj.a; - $[3] = t2; - } else { - t2 = $[3]; - } - useEffect(t2, [obj.a]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.js deleted file mode 100644 index 41c18d937a2e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr-merge.js +++ /dev/null @@ -1,8 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExprMerge({propVal}) { - const obj = {a: {b: propVal}}; - useEffect(() => print(obj.a, obj.a.b), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.expect.md deleted file mode 100644 index 50bceb09aef1..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExpr({propVal}) { - const obj = {a: {b: propVal}}; - useEffect(() => print(obj.a.b), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function ReactiveMemberExpr(t0) { - const $ = _c(4); - const { propVal } = t0; - let t1; - if ($[0] !== propVal) { - t1 = { a: { b: propVal } }; - $[0] = propVal; - $[1] = t1; - } else { - t1 = $[1]; - } - const obj = t1; - let t2; - if ($[2] !== obj.a.b) { - t2 = () => print(obj.a.b); - $[2] = obj.a.b; - $[3] = t2; - } else { - t2 = $[3]; - } - useEffect(t2, [obj.a.b]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.js deleted file mode 100644 index ca6e7d4f0f01..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-memberexpr.js +++ /dev/null @@ -1,8 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExpr({propVal}) { - const obj = {a: {b: propVal}}; - useEffect(() => print(obj.a.b), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.expect.md deleted file mode 100644 index a886842c8eb0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.expect.md +++ /dev/null @@ -1,100 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print, shallowCopy} from 'shared-runtime'; - -function ReactiveMemberExpr({cond, propVal}) { - const obj = {a: cond ? {b: propVal} : null, c: null}; - const other = shallowCopy({a: {b: {c: {d: {e: {f: propVal + 1}}}}}}); - const primitive = shallowCopy(propVal); - useEffect( - () => print(obj.a?.b, other?.a?.b?.c?.d?.e.f, primitive.a?.b.c?.d?.e.f), - AUTODEPS - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{cond: true, propVal: 1}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print, shallowCopy } from "shared-runtime"; - -function ReactiveMemberExpr(t0) { - const $ = _c(13); - const { cond, propVal } = t0; - let t1; - if ($[0] !== cond || $[1] !== propVal) { - t1 = cond ? { b: propVal } : null; - $[0] = cond; - $[1] = propVal; - $[2] = t1; - } else { - t1 = $[2]; - } - let t2; - if ($[3] !== t1) { - t2 = { a: t1, c: null }; - $[3] = t1; - $[4] = t2; - } else { - t2 = $[4]; - } - const obj = t2; - const t3 = propVal + 1; - let t4; - if ($[5] !== t3) { - t4 = shallowCopy({ a: { b: { c: { d: { e: { f: t3 } } } } } }); - $[5] = t3; - $[6] = t4; - } else { - t4 = $[6]; - } - const other = t4; - let t5; - if ($[7] !== propVal) { - t5 = shallowCopy(propVal); - $[7] = propVal; - $[8] = t5; - } else { - t5 = $[8]; - } - const primitive = t5; - let t6; - if ( - $[9] !== obj.a?.b || - $[10] !== other?.a?.b?.c?.d?.e.f || - $[11] !== primitive.a?.b.c?.d?.e.f - ) { - t6 = () => - print(obj.a?.b, other?.a?.b?.c?.d?.e.f, primitive.a?.b.c?.d?.e.f); - $[9] = obj.a?.b; - $[10] = other?.a?.b?.c?.d?.e.f; - $[11] = primitive.a?.b.c?.d?.e.f; - $[12] = t6; - } else { - t6 = $[12]; - } - useEffect(t6, [obj.a?.b, other?.a?.b?.c?.d?.e.f, primitive.a?.b.c?.d?.e.f]); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{ cond: true, propVal: 1 }], -}; - -``` - -### Eval output -(kind: ok) -logs: [1,2,undefined] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.js deleted file mode 100644 index 42dc585cbe9a..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain-complex.js +++ /dev/null @@ -1,18 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print, shallowCopy} from 'shared-runtime'; - -function ReactiveMemberExpr({cond, propVal}) { - const obj = {a: cond ? {b: propVal} : null, c: null}; - const other = shallowCopy({a: {b: {c: {d: {e: {f: propVal + 1}}}}}}); - const primitive = shallowCopy(propVal); - useEffect( - () => print(obj.a?.b, other?.a?.b?.c?.d?.e.f, primitive.a?.b.c?.d?.e.f), - AUTODEPS - ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{cond: true, propVal: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.expect.md deleted file mode 100644 index 7dfad84f7959..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.expect.md +++ /dev/null @@ -1,79 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExpr({cond, propVal}) { - const obj = {a: cond ? {b: propVal} : null, c: null}; - useEffect(() => print(obj.a?.b), AUTODEPS); - useEffect(() => print(obj.c?.d), AUTODEPS); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{cond: true, propVal: 1}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function ReactiveMemberExpr(t0) { - const $ = _c(9); - const { cond, propVal } = t0; - let t1; - if ($[0] !== cond || $[1] !== propVal) { - t1 = cond ? { b: propVal } : null; - $[0] = cond; - $[1] = propVal; - $[2] = t1; - } else { - t1 = $[2]; - } - let t2; - if ($[3] !== t1) { - t2 = { a: t1, c: null }; - $[3] = t1; - $[4] = t2; - } else { - t2 = $[4]; - } - const obj = t2; - let t3; - if ($[5] !== obj.a?.b) { - t3 = () => print(obj.a?.b); - $[5] = obj.a?.b; - $[6] = t3; - } else { - t3 = $[6]; - } - useEffect(t3, [obj.a?.b]); - let t4; - if ($[7] !== obj.c?.d) { - t4 = () => print(obj.c?.d); - $[7] = obj.c?.d; - $[8] = t4; - } else { - t4 = $[8]; - } - useEffect(t4, [obj.c?.d]); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{ cond: true, propVal: 1 }], -}; - -``` - -### Eval output -(kind: ok) -logs: [1,undefined] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.js deleted file mode 100644 index 882f9026613c..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-optional-chain.js +++ /dev/null @@ -1,14 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveMemberExpr({cond, propVal}) { - const obj = {a: cond ? {b: propVal} : null, c: null}; - useEffect(() => print(obj.a?.b), AUTODEPS); - useEffect(() => print(obj.c?.d), AUTODEPS); -} - -export const FIXTURE_ENTRYPOINT = { - fn: ReactiveMemberExpr, - params: [{cond: true, propVal: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.expect.md deleted file mode 100644 index 97fab06c1f37..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.expect.md +++ /dev/null @@ -1,69 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useRef, useEffect, AUTODEPS} from 'react'; -import {print, mutate} from 'shared-runtime'; - -function Component({cond}) { - const arr = useRef([]); - const other = useRef([]); - // Although arr and other are both stable, derived is not - const derived = cond ? arr : other; - useEffect(() => { - mutate(derived.current); - print(derived.current); - }, AUTODEPS); - return arr; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useRef, useEffect, AUTODEPS } from "react"; -import { print, mutate } from "shared-runtime"; - -function Component(t0) { - const $ = _c(4); - const { cond } = t0; - let t1; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t1 = []; - $[0] = t1; - } else { - t1 = $[0]; - } - const arr = useRef(t1); - let t2; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t2 = []; - $[1] = t2; - } else { - t2 = $[1]; - } - const other = useRef(t2); - - const derived = cond ? arr : other; - let t3; - if ($[2] !== derived) { - t3 = () => { - mutate(derived.current); - print(derived.current); - }; - $[2] = derived; - $[3] = t3; - } else { - t3 = $[3]; - } - useEffect(t3, [derived]); - return arr; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.js deleted file mode 100644 index 32a9037a9f7e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref-ternary.js +++ /dev/null @@ -1,15 +0,0 @@ -// @inferEffectDependencies -import {useRef, useEffect, AUTODEPS} from 'react'; -import {print, mutate} from 'shared-runtime'; - -function Component({cond}) { - const arr = useRef([]); - const other = useRef([]); - // Although arr and other are both stable, derived is not - const derived = cond ? arr : other; - useEffect(() => { - mutate(derived.current); - print(derived.current); - }, AUTODEPS); - return arr; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.expect.md deleted file mode 100644 index 7e5371dba355..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.expect.md +++ /dev/null @@ -1,66 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * Ref types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const ref1 = useRef('initial value'); - const ref2 = useRef('initial value'); - let ref; - if (props.foo) { - ref = ref1; - } else { - ref = ref2; - } - useEffect(() => print(ref), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useRef, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/* - * Ref types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const $ = _c(4); - const ref1 = useRef("initial value"); - const ref2 = useRef("initial value"); - let ref; - if ($[0] !== props.foo) { - if (props.foo) { - ref = ref1; - } else { - ref = ref2; - } - $[0] = props.foo; - $[1] = ref; - } else { - ref = $[1]; - } - let t0; - if ($[2] !== ref) { - t0 = () => print(ref); - $[2] = ref; - $[3] = t0; - } else { - t0 = $[3]; - } - useEffect(t0, [ref]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.js deleted file mode 100644 index ee98a93c70bf..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-ref.js +++ /dev/null @@ -1,18 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * Ref types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const ref1 = useRef('initial value'); - const ref2 = useRef('initial value'); - let ref; - if (props.foo) { - ref = ref1; - } else { - ref = ref2; - } - useEffect(() => print(ref), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.expect.md deleted file mode 100644 index db79dda30103..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.expect.md +++ /dev/null @@ -1,66 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const [_state1, setState1] = useRef('initial value'); - const [_state2, setState2] = useRef('initial value'); - let setState; - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - useEffect(() => print(setState), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, useState, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const $ = _c(4); - const [, setState1] = useRef("initial value"); - const [, setState2] = useRef("initial value"); - let setState; - if ($[0] !== props.foo) { - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - $[0] = props.foo; - $[1] = setState; - } else { - setState = $[1]; - } - let t0; - if ($[2] !== setState) { - t0 = () => print(setState); - $[2] = setState; - $[3] = t0; - } else { - t0 = $[3]; - } - useEffect(t0, [setState]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.js deleted file mode 100644 index f69efd650cd0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-setState.js +++ /dev/null @@ -1,18 +0,0 @@ -// @inferEffectDependencies -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const [_state1, setState1] = useRef('initial value'); - const [_state2, setState2] = useRef('initial value'); - let setState; - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - useEffect(() => print(setState), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.expect.md deleted file mode 100644 index 8646a926aa72..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveVariable({propVal}) { - const arr = [propVal]; - useEffect(() => print(arr), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function ReactiveVariable(t0) { - const $ = _c(4); - const { propVal } = t0; - let t1; - if ($[0] !== propVal) { - t1 = [propVal]; - $[0] = propVal; - $[1] = t1; - } else { - t1 = $[1]; - } - const arr = t1; - let t2; - if ($[2] !== arr) { - t2 = () => print(arr); - $[2] = arr; - $[3] = t2; - } else { - t2 = $[3]; - } - useEffect(t2, [arr]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.js deleted file mode 100644 index 510ff6e1242c..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/reactive-variable.js +++ /dev/null @@ -1,8 +0,0 @@ -// @inferEffectDependencies -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function ReactiveVariable({propVal}) { - const arr = [propVal]; - useEffect(() => print(arr), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.expect.md deleted file mode 100644 index b8d213b63740..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.expect.md +++ /dev/null @@ -1,48 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @outputMode:"lint" @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.infer-effect-deps-with-rule-violation--lint.ts:8:2 - 6 | function Foo({propVal}) { - 7 | const arr = [propVal]; -> 8 | useEffectWrapper(() => print(arr), AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 9 | - 10 | const arr2 = []; - 11 | useEffectWrapper(() => arr2.push(propVal), AUTODEPS); -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.js deleted file mode 100644 index 7df86cdfd254..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation--lint.js +++ /dev/null @@ -1,20 +0,0 @@ -// @inferEffectDependencies @outputMode:"lint" @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.expect.md deleted file mode 100644 index 80d8af00e610..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.expect.md +++ /dev/null @@ -1,49 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @outputMode:"lint" @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - 'use memo'; - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Error: Cannot infer dependencies of this effect. This will break your build! - -To resolve, either pass a dependency array or fix reported compiler bailout diagnostics. - -error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.ts:9:2 - 7 | 'use memo'; - 8 | const arr = [propVal]; -> 9 | useEffectWrapper(() => print(arr), AUTODEPS); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot infer dependencies - 10 | - 11 | const arr2 = []; - 12 | useEffectWrapper(() => arr2.push(propVal), AUTODEPS); -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.js deleted file mode 100644 index 42bbf4c994bf..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/error.infer-effect-deps-with-rule-violation-use-memo-opt-in--lint.js +++ /dev/null @@ -1,21 +0,0 @@ -// @inferEffectDependencies @outputMode:"lint" @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - 'use memo'; - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.expect.md deleted file mode 100644 index 47de4a1d1932..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.expect.md +++ /dev/null @@ -1,58 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import { print } from "shared-runtime"; -import useEffectWrapper from "useEffectWrapper"; -import { AUTODEPS } from "react"; - -function Foo(t0) { - const { propVal } = t0; - const arr = [propVal]; - useEffectWrapper(() => print(arr), [arr]); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), [arr2, propVal]); - arr2.push(2); - return { arr, arr2 }; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{ propVal: 1 }], - sequentialRenders: [{ propVal: 1 }, { propVal: 2 }], -}; - -``` - -### Eval output -(kind: ok) {"arr":[1],"arr2":[2]} -{"arr":[2],"arr2":[2]} -logs: [[ 1 ],[ 2 ]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.js deleted file mode 100644 index 6cca9a833d81..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation--compile.js +++ /dev/null @@ -1,20 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.expect.md deleted file mode 100644 index 610ad3489025..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.expect.md +++ /dev/null @@ -1,61 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - 'use memo'; - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" -import { print } from "shared-runtime"; -import useEffectWrapper from "useEffectWrapper"; -import { AUTODEPS } from "react"; - -function Foo(t0) { - "use memo"; - const { propVal } = t0; - - const arr = [propVal]; - useEffectWrapper(() => print(arr), [arr]); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), [arr2, propVal]); - arr2.push(2); - return { arr, arr2 }; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{ propVal: 1 }], - sequentialRenders: [{ propVal: 1 }, { propVal: 2 }], -}; - -``` - -### Eval output -(kind: ok) {"arr":[1],"arr2":[2]} -{"arr":[2],"arr2":[2]} -logs: [[ 1 ],[ 2 ]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.js deleted file mode 100644 index efa5db194060..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/infer-effect-deps-with-rule-violation-use-memo-opt-in--compile.js +++ /dev/null @@ -1,21 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function Foo({propVal}) { - 'use memo'; - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); - - const arr2 = []; - useEffectWrapper(() => arr2.push(propVal), AUTODEPS); - arr2.push(2); - return {arr, arr2}; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Foo, - params: [{propVal: 1}], - sequentialRenders: [{propVal: 1}, {propVal: 2}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.expect.md deleted file mode 100644 index 1d767ce3db71..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @outputMode:"lint" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function ReactiveVariable({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); -} - -``` - -## Code - -```javascript -// @inferEffectDependencies @outputMode:"lint" -import { print } from "shared-runtime"; -import useEffectWrapper from "useEffectWrapper"; -import { AUTODEPS } from "react"; - -function ReactiveVariable({ propVal }) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.js deleted file mode 100644 index 011cc535c215..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-effect-dependencies/retry-lint-comparison/lint-repro.js +++ /dev/null @@ -1,9 +0,0 @@ -// @inferEffectDependencies @outputMode:"lint" -import {print} from 'shared-runtime'; -import useEffectWrapper from 'useEffectWrapper'; -import {AUTODEPS} from 'react'; - -function ReactiveVariable({propVal}) { - const arr = [propVal]; - useEffectWrapper(() => print(arr), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.expect.md deleted file mode 100644 index ae93de0b1e7e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.expect.md +++ /dev/null @@ -1,478 +0,0 @@ - -## Input - -```javascript -// @inlineJsxTransform - -function Parent({children, a: _a, b: _b, c: _c, ref}) { - return
{children}
; -} - -function Child({children}) { - return <>{children}; -} - -function GrandChild({className}) { - return ( - - Hello world - - ); -} - -function ParentAndRefAndKey(props) { - const testRef = useRef(); - return ; -} - -function ParentAndChildren(props) { - const render = () => { - return
{props.foo}
; - }; - return ( - - - - - {render()} - - - ); -} - -const propsToSpread = {a: 'a', b: 'b', c: 'c'}; -function PropsSpread() { - return ( - <> - - - - ); -} - -function ConditionalJsx({shouldWrap}) { - let content =
Hello
; - - if (shouldWrap) { - content = {content}; - } - - return content; -} - -function ComponentWithSpreadPropsAndRef({ref, ...other}) { - return ; -} - -// TODO: Support value blocks -function TernaryJsx({cond}) { - return cond ?
: null; -} - -global.DEV = true; -export const FIXTURE_ENTRYPOINT = { - fn: ParentAndChildren, - params: [{foo: 'abc'}], -}; - -``` - -## Code - -```javascript -import { c as _c2 } from "react/compiler-runtime"; // @inlineJsxTransform - -function Parent(t0) { - const $ = _c2(3); - const { children, ref } = t0; - let t1; - if ($[0] !== children || $[1] !== ref) { - if (DEV) { - t1 =
{children}
; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: "div", - ref: ref, - key: null, - props: { ref: ref, children: children }, - }; - } - $[0] = children; - $[1] = ref; - $[2] = t1; - } else { - t1 = $[2]; - } - return t1; -} - -function Child(t0) { - const $ = _c2(2); - const { children } = t0; - let t1; - if ($[0] !== children) { - if (DEV) { - t1 = <>{children}; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Symbol.for("react.fragment"), - ref: null, - key: null, - props: { children: children }, - }; - } - $[0] = children; - $[1] = t1; - } else { - t1 = $[1]; - } - return t1; -} - -function GrandChild(t0) { - const $ = _c2(3); - const { className } = t0; - let t1; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - if (DEV) { - t1 = Hello world; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: React.Fragment, - ref: null, - key: "fragmentKey", - props: { children: "Hello world" }, - }; - } - $[0] = t1; - } else { - t1 = $[0]; - } - let t2; - if ($[1] !== className) { - if (DEV) { - t2 = {t1}; - } else { - t2 = { - $$typeof: Symbol.for("react.transitional.element"), - type: "span", - ref: null, - key: null, - props: { className: className, children: t1 }, - }; - } - $[1] = className; - $[2] = t2; - } else { - t2 = $[2]; - } - return t2; -} - -function ParentAndRefAndKey(props) { - const $ = _c2(1); - const testRef = useRef(); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - if (DEV) { - t0 = ; - } else { - t0 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Parent, - ref: testRef, - key: "testKey", - props: { a: "a", b: { b: "b" }, c: C, ref: testRef }, - }; - } - $[0] = t0; - } else { - t0 = $[0]; - } - return t0; -} - -function ParentAndChildren(props) { - const $ = _c2(14); - let t0; - if ($[0] !== props.foo) { - t0 = () => { - let t1; - if (DEV) { - t1 =
{props.foo}
; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: "div", - ref: null, - key: "d", - props: { children: props.foo }, - }; - } - return t1; - }; - $[0] = props.foo; - $[1] = t0; - } else { - t0 = $[1]; - } - const render = t0; - let t1; - if ($[2] !== props) { - if (DEV) { - t1 = ; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Child, - ref: null, - key: "a", - props: props, - }; - } - $[2] = props; - $[3] = t1; - } else { - t1 = $[3]; - } - - const t2 = props.foo; - let t3; - if ($[4] !== props) { - if (DEV) { - t3 = ; - } else { - t3 = { - $$typeof: Symbol.for("react.transitional.element"), - type: GrandChild, - ref: null, - key: "c", - props: { className: t2, ...props }, - }; - } - $[4] = props; - $[5] = t3; - } else { - t3 = $[5]; - } - let t4; - if ($[6] !== render) { - t4 = render(); - $[6] = render; - $[7] = t4; - } else { - t4 = $[7]; - } - let t5; - if ($[8] !== t3 || $[9] !== t4) { - if (DEV) { - t5 = ( - - {t3} - {t4} - - ); - } else { - t5 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Child, - ref: null, - key: "b", - props: { children: [t3, t4] }, - }; - } - $[8] = t3; - $[9] = t4; - $[10] = t5; - } else { - t5 = $[10]; - } - let t6; - if ($[11] !== t1 || $[12] !== t5) { - if (DEV) { - t6 = ( - - {t1} - {t5} - - ); - } else { - t6 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Parent, - ref: null, - key: null, - props: { children: [t1, t5] }, - }; - } - $[11] = t1; - $[12] = t5; - $[13] = t6; - } else { - t6 = $[13]; - } - return t6; -} - -const propsToSpread = { a: "a", b: "b", c: "c" }; -function PropsSpread() { - const $ = _c2(1); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - let t1; - if (DEV) { - t1 = ; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Test, - ref: null, - key: "a", - props: propsToSpread, - }; - } - let t2; - if (DEV) { - t2 = ; - } else { - t2 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Test, - ref: null, - key: "b", - props: { ...propsToSpread, a: "z" }, - }; - } - if (DEV) { - t0 = ( - <> - {t1} - {t2} - - ); - } else { - t0 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Symbol.for("react.fragment"), - ref: null, - key: null, - props: { children: [t1, t2] }, - }; - } - $[0] = t0; - } else { - t0 = $[0]; - } - return t0; -} - -function ConditionalJsx(t0) { - const $ = _c2(2); - const { shouldWrap } = t0; - let t1; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - if (DEV) { - t1 =
Hello
; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: "div", - ref: null, - key: null, - props: { children: "Hello" }, - }; - } - $[0] = t1; - } else { - t1 = $[0]; - } - let content = t1; - - if (shouldWrap) { - const t2 = content; - let t3; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - if (DEV) { - t3 = {t2}; - } else { - t3 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Parent, - ref: null, - key: null, - props: { children: t2 }, - }; - } - $[1] = t3; - } else { - t3 = $[1]; - } - content = t3; - } - - return content; -} - -function ComponentWithSpreadPropsAndRef(t0) { - const $ = _c2(6); - let other; - let ref; - if ($[0] !== t0) { - ({ ref, ...other } = t0); - $[0] = t0; - $[1] = other; - $[2] = ref; - } else { - other = $[1]; - ref = $[2]; - } - let t1; - if ($[3] !== other || $[4] !== ref) { - if (DEV) { - t1 = ; - } else { - t1 = { - $$typeof: Symbol.for("react.transitional.element"), - type: Foo, - ref: ref, - key: null, - props: { ref: ref, ...other }, - }; - } - $[3] = other; - $[4] = ref; - $[5] = t1; - } else { - t1 = $[5]; - } - return t1; -} - -// TODO: Support value blocks -function TernaryJsx(t0) { - const $ = _c2(2); - const { cond } = t0; - let t1; - if ($[0] !== cond) { - t1 = cond ?
: null; - $[0] = cond; - $[1] = t1; - } else { - t1 = $[1]; - } - return t1; -} - -global.DEV = true; -export const FIXTURE_ENTRYPOINT = { - fn: ParentAndChildren, - params: [{ foo: "abc" }], -}; - -``` - -### Eval output -(kind: ok)
Hello world
abc
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.js deleted file mode 100644 index 2ab1efef794b..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.js +++ /dev/null @@ -1,72 +0,0 @@ -// @inlineJsxTransform - -function Parent({children, a: _a, b: _b, c: _c, ref}) { - return
{children}
; -} - -function Child({children}) { - return <>{children}; -} - -function GrandChild({className}) { - return ( - - Hello world - - ); -} - -function ParentAndRefAndKey(props) { - const testRef = useRef(); - return ; -} - -function ParentAndChildren(props) { - const render = () => { - return
{props.foo}
; - }; - return ( - - - - - {render()} - - - ); -} - -const propsToSpread = {a: 'a', b: 'b', c: 'c'}; -function PropsSpread() { - return ( - <> - - - - ); -} - -function ConditionalJsx({shouldWrap}) { - let content =
Hello
; - - if (shouldWrap) { - content = {content}; - } - - return content; -} - -function ComponentWithSpreadPropsAndRef({ref, ...other}) { - return ; -} - -// TODO: Support value blocks -function TernaryJsx({cond}) { - return cond ?
: null; -} - -global.DEV = true; -export const FIXTURE_ENTRYPOINT = { - fn: ParentAndChildren, - params: [{foo: 'abc'}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.expect.md deleted file mode 100644 index c6e179a6e7fe..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.expect.md +++ /dev/null @@ -1,66 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess @enableEmitHookGuards -function App() { - const {foo} = useContext(MyContext); - const {bar} = useContext(MyContext); - return ; -} - -``` - -## Code - -```javascript -import { - $dispatcherGuard, - useContext_withSelector, -} from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess @enableEmitHookGuards -function App() { - const $ = _c(3); - try { - $dispatcherGuard(0); - const { foo } = (function () { - try { - $dispatcherGuard(2); - return useContext_withSelector(MyContext, _temp); - } finally { - $dispatcherGuard(3); - } - })(); - const { bar } = (function () { - try { - $dispatcherGuard(2); - return useContext_withSelector(MyContext, _temp2); - } finally { - $dispatcherGuard(3); - } - })(); - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; - } finally { - $dispatcherGuard(1); - } -} -function _temp2(t0) { - return [t0.bar]; -} -function _temp(t0) { - return [t0.foo]; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.js deleted file mode 100644 index da881ea124a4..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-access-hook-guard.js +++ /dev/null @@ -1,6 +0,0 @@ -// @lowerContextAccess @enableEmitHookGuards -function App() { - const {foo} = useContext(MyContext); - const {bar} = useContext(MyContext); - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.expect.md deleted file mode 100644 index af9b1df36ad0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.expect.md +++ /dev/null @@ -1,42 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const {foo} = useContext(MyContext); - const {bar} = useContext(MyContext); - return ; -} - -``` - -## Code - -```javascript -import { useContext_withSelector } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const { foo } = useContext_withSelector(MyContext, _temp); - const { bar } = useContext_withSelector(MyContext, _temp2); - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} -function _temp2(t0) { - return [t0.bar]; -} -function _temp(t0) { - return [t0.foo]; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.js deleted file mode 100644 index 314ff41c280e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-acess-multiple.js +++ /dev/null @@ -1,6 +0,0 @@ -// @lowerContextAccess -function App() { - const {foo} = useContext(MyContext); - const {bar} = useContext(MyContext); - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.expect.md deleted file mode 100644 index d13682467b3e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const {foo, bar} = useContext(MyContext); - return ; -} - -``` - -## Code - -```javascript -import { useContext_withSelector } from "react-compiler-runtime"; -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const { foo, bar } = useContext_withSelector(MyContext, _temp); - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} -function _temp(t0) { - return [t0.foo, t0.bar]; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.js deleted file mode 100644 index 0d53548e67c5..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/lower-context-selector-simple.js +++ /dev/null @@ -1,5 +0,0 @@ -// @lowerContextAccess -function App() { - const {foo, bar} = useContext(MyContext); - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.expect.md deleted file mode 100644 index eae0b280f02e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.expect.md +++ /dev/null @@ -1,78 +0,0 @@ - -## Input - -```javascript -// @enableMemoizationComments -import {addOne, getNumber, identity} from 'shared-runtime'; - -function Component(props) { - const x = identity(props.a); - const y = addOne(x); - const z = identity(props.b); - return [x, y, z]; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{a: 1, b: 10}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableMemoizationComments -import { addOne, getNumber, identity } from "shared-runtime"; - -function Component(props) { - const $ = _c(9); - let t0; - let x; // "useMemo" for t0 and x: - // check if props.a changed - if ($[0] !== props.a) { - // Inputs changed, recompute - x = identity(props.a); - t0 = addOne(x); - $[0] = props.a; - $[1] = t0; - $[2] = x; - } else { - // Inputs did not change, use cached value - t0 = $[1]; - x = $[2]; - } - const y = t0; - let t1; // "useMemo" for t1: - // check if props.b changed - if ($[3] !== props.b) { - // Inputs changed, recompute - t1 = identity(props.b); - $[3] = props.b; - $[4] = t1; - } else { - // Inputs did not change, use cached value - t1 = $[4]; - } - const z = t1; - let t2; // "useMemo" for t2: - // check if x, y, or z changed - if ($[5] !== x || $[6] !== y || $[7] !== z) { - // Inputs changed, recompute - t2 = [x, y, z]; - $[5] = x; - $[6] = y; - $[7] = z; - $[8] = t2; - } else { - // Inputs did not change, use cached value - t2 = $[8]; - } - return t2; -} -export const FIXTURE_ENTRYPOINT = { fn: Component, params: [{ a: 1, b: 10 }] }; - -``` - -### Eval output -(kind: ok) [1,2,10] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.js deleted file mode 100644 index d35993ad053e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/memoization-comments.js +++ /dev/null @@ -1,14 +0,0 @@ -// @enableMemoizationComments -import {addOne, getNumber, identity} from 'shared-runtime'; - -function Component(props) { - const x = identity(props.a); - const y = addOne(x); - const z = identity(props.b); - return [x, y, z]; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{a: 1, b: 10}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.expect.md deleted file mode 100644 index e5a9081137a2..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.expect.md +++ /dev/null @@ -1,88 +0,0 @@ - -## Input - -```javascript -// @enableInstructionReordering -import {useState} from 'react'; -import {Stringify} from 'shared-runtime'; - -function Component() { - let [state, setState] = useState(0); - return ( -
- - {state} - -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 42}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableInstructionReordering -import { useState } from "react"; -import { Stringify } from "shared-runtime"; - -function Component() { - const $ = _c(7); - const [state, setState] = useState(0); - let t0; - let t1; - if ($[0] !== state) { - t0 = ( - - ); - t1 = {state}; - $[0] = state; - $[1] = t0; - $[2] = t1; - } else { - t0 = $[1]; - t1 = $[2]; - } - let t2; - if ($[3] === Symbol.for("react.memo_cache_sentinel")) { - t2 = ; - $[3] = t2; - } else { - t2 = $[3]; - } - let t3; - if ($[4] !== t0 || $[5] !== t1) { - t3 = ( -
- {t2} - {t1} - {t0} -
- ); - $[4] = t0; - $[5] = t1; - $[6] = t3; - } else { - t3 = $[6]; - } - return t3; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ value: 42 }], -}; - -``` - -### Eval output -(kind: ok)
{"text":"Counter"}
0
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.js deleted file mode 100644 index 137c161b2478..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-reordering.js +++ /dev/null @@ -1,21 +0,0 @@ -// @enableInstructionReordering -import {useState} from 'react'; -import {Stringify} from 'shared-runtime'; - -function Component() { - let [state, setState] = useState(0); - return ( -
- - {state} - -
- ); -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 42}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.expect.md deleted file mode 100644 index 0ff9773f76e0..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.expect.md +++ /dev/null @@ -1,71 +0,0 @@ - -## Input - -```javascript -// @enableInstructionReordering -import {useState} from 'react'; - -function Component() { - const [state, setState] = useState(0); - const onClick = () => { - setState(s => s + 1); - }; - return ( - <> - Count: {state} - - - ); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableInstructionReordering -import { useState } from "react"; - -function Component() { - const $ = _c(4); - const [state, setState] = useState(0); - let t0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = () => { - setState(_temp); - }; - $[0] = t0; - } else { - t0 = $[0]; - } - const onClick = t0; - let t1; - if ($[1] === Symbol.for("react.memo_cache_sentinel")) { - t1 = ; - $[1] = t1; - } else { - t1 = $[1]; - } - let t2; - if ($[2] !== state) { - t2 = ( - <> - Count: {state} - {t1} - - ); - $[2] = state; - $[3] = t2; - } else { - t2 = $[3]; - } - return t2; -} -function _temp(s) { - return s + 1; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.js deleted file mode 100644 index 33dfe347119e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-scopes-callback.js +++ /dev/null @@ -1,15 +0,0 @@ -// @enableInstructionReordering -import {useState} from 'react'; - -function Component() { - const [state, setState] = useState(0); - const onClick = () => { - setState(s => s + 1); - }; - return ( - <> - Count: {state} - - - ); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.expect.md deleted file mode 100644 index bcf1aa0165d8..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.expect.md +++ /dev/null @@ -1,74 +0,0 @@ - -## Input - -```javascript -// @validateMemoizedEffectDependencies - -import {useEffect} from 'react'; - -function Component(props) { - const y = [[props.value]]; // merged w scope for inner array - - useEffect(() => { - console.log(y); - }, [y]); // should still be a valid dependency here - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 42}], - isComponent: false, -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @validateMemoizedEffectDependencies - -import { useEffect } from "react"; - -function Component(props) { - const $ = _c(5); - let t0; - if ($[0] !== props.value) { - t0 = [[props.value]]; - $[0] = props.value; - $[1] = t0; - } else { - t0 = $[1]; - } - const y = t0; - let t1; - let t2; - if ($[2] !== y) { - t1 = () => { - console.log(y); - }; - t2 = [y]; - $[2] = y; - $[3] = t1; - $[4] = t2; - } else { - t1 = $[3]; - t2 = $[4]; - } - useEffect(t1, t2); - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ value: 42 }], - isComponent: false, -}; - -``` - -### Eval output -(kind: ok) [[42]] -logs: [[ [ 42 ] ]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.js deleted file mode 100644 index 1ede9dd4ccca..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merged-scopes-are-valid-effect-deps.js +++ /dev/null @@ -1,19 +0,0 @@ -// @validateMemoizedEffectDependencies - -import {useEffect} from 'react'; - -function Component(props) { - const y = [[props.value]]; // merged w scope for inner array - - useEffect(() => { - console.log(y); - }, [y]); // should still be a valid dependency here - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{value: 42}], - isComponent: false, -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md index babb4e896975..5cd2cf7b9798 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md @@ -42,7 +42,7 @@ function Component() { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot reassign variable after render completes @@ -56,6 +56,27 @@ error.invalid-reassign-local-variable-in-jsx-callback.ts:6:4 7 | }; 8 | 9 | const onClick = newValue => { + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `local` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-jsx-callback.ts:32:26 + 30 | }; + 31 | +> 32 | return ; + | ^^^^^^^ This function may (indirectly) reassign or modify `local` after render + 33 | } + 34 | + +error.invalid-reassign-local-variable-in-jsx-callback.ts:6:4 + 4 | + 5 | const reassignLocal = newValue => { +> 6 | local = newValue; + | ^^^^^ This modifies `local` + 7 | }; + 8 | + 9 | const onClick = newValue => { ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md index d78e4becec78..d50943f67784 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md @@ -31,7 +31,7 @@ function Component({content, refetch}) { ## Error ``` -Found 1 error: +Found 2 errors: Error: Cannot access variable before it is declared @@ -52,6 +52,18 @@ Error: Cannot access variable before it is declared 20 | 21 | return ; 22 | } + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + + 9 | // TDZ violation! + 10 | const onRefetch = useCallback(() => { +> 11 | refetch(data); + | ^^^^ Missing dependency `data` + 12 | }, [refetch]); + 13 | + 14 | // The context variable gets frozen here since it's passed to a hook ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.expect.md deleted file mode 100644 index edc266b9f334..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.expect.md +++ /dev/null @@ -1,58 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0]?.value), AUTODEPS); - arr.push({value: foo}); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import { useEffect, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function Component(t0) { - const { foo } = t0; - const arr = []; - - useEffect(() => print(arr[0]?.value), [arr[0]?.value]); - arr.push({ value: foo }); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ foo: 1 }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":5,"column":0,"index":181},"end":{"line":12,"column":1,"index":436},"filename":"mutate-after-useeffect-optional-chain.ts"},"detail":{"options":{"category":"Immutability","reason":"This value cannot be modified","description":"Modifying a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the modification before calling useEffect()","details":[{"kind":"error","loc":{"start":{"line":10,"column":2,"index":397},"end":{"line":10,"column":5,"index":400},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"},"message":"value cannot be modified"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":9,"column":2,"index":346},"end":{"line":9,"column":49,"index":393},"filename":"mutate-after-useeffect-optional-chain.ts"},"decorations":[{"start":{"line":9,"column":24,"index":368},"end":{"line":9,"column":27,"index":371},"filename":"mutate-after-useeffect-optional-chain.ts","identifierName":"arr"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":181},"end":{"line":12,"column":1,"index":436},"filename":"mutate-after-useeffect-optional-chain.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) [{"value":1}] -logs: [1] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.js deleted file mode 100644 index ee59d8fe4ea9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-optional-chain.js +++ /dev/null @@ -1,17 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import {useEffect, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({foo}) { - const arr = []; - // Taking either arr[0].value or arr as a dependency is reasonable - // as long as developers know what to expect. - useEffect(() => print(arr[0]?.value), AUTODEPS); - arr.push({value: foo}); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.expect.md deleted file mode 100644 index acf9c28cabdf..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.expect.md +++ /dev/null @@ -1,57 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel - -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({arrRef}) { - // Avoid taking arr.current as a dependency - useEffect(() => print(arrRef.current), AUTODEPS); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{arrRef: {current: {val: 'initial ref value'}}}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel - -import { useEffect, useRef, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -function Component(t0) { - const { arrRef } = t0; - - useEffect(() => print(arrRef.current), [arrRef]); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ arrRef: { current: { val: "initial ref value" } } }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":6,"column":0,"index":190},"end":{"line":11,"column":1,"index":363},"filename":"mutate-after-useeffect-ref-access.ts"},"detail":{"options":{"category":"Refs","reason":"Cannot access refs during render","description":"React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef)","details":[{"kind":"error","loc":{"start":{"line":9,"column":2,"index":321},"end":{"line":9,"column":16,"index":335},"filename":"mutate-after-useeffect-ref-access.ts"},"message":"Cannot update ref during render"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":8,"column":2,"index":269},"end":{"line":8,"column":50,"index":317},"filename":"mutate-after-useeffect-ref-access.ts"},"decorations":[{"start":{"line":8,"column":24,"index":291},"end":{"line":8,"column":30,"index":297},"filename":"mutate-after-useeffect-ref-access.ts","identifierName":"arrRef"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":6,"column":0,"index":190},"end":{"line":11,"column":1,"index":363},"filename":"mutate-after-useeffect-ref-access.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) {"current":{"val":2}} -logs: [{ val: 2 }] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.js deleted file mode 100644 index 9d0f7e194d0e..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect-ref-access.js +++ /dev/null @@ -1,16 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel - -import {useEffect, useRef, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -function Component({arrRef}) { - // Avoid taking arr.current as a dependency - useEffect(() => print(arrRef.current), AUTODEPS); - arrRef.current.val = 2; - return arrRef; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{arrRef: {current: {val: 'initial ref value'}}}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.expect.md deleted file mode 100644 index 93e7f1e06096..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.expect.md +++ /dev/null @@ -1,56 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - const arr = []; - useEffect(() => { - arr.push(foo); - }, AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; - -``` - -## Code - -```javascript -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import { useEffect, AUTODEPS } from "react"; - -function Component(t0) { - const { foo } = t0; - const arr = []; - useEffect(() => { - arr.push(foo); - }, [arr, foo]); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ foo: 1 }], -}; - -``` - -## Logs - -``` -{"kind":"CompileError","fnLoc":{"start":{"line":4,"column":0,"index":143},"end":{"line":11,"column":1,"index":274},"filename":"mutate-after-useeffect.ts"},"detail":{"options":{"category":"Immutability","reason":"This value cannot be modified","description":"Modifying a value used previously in an effect function or as an effect dependency is not allowed. Consider moving the modification before calling useEffect()","details":[{"kind":"error","loc":{"start":{"line":9,"column":2,"index":246},"end":{"line":9,"column":5,"index":249},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},"message":"value cannot be modified"}]}}} -{"kind":"AutoDepsDecorations","fnLoc":{"start":{"line":6,"column":2,"index":191},"end":{"line":8,"column":14,"index":242},"filename":"mutate-after-useeffect.ts"},"decorations":[{"start":{"line":7,"column":4,"index":213},"end":{"line":7,"column":7,"index":216},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":4,"index":213},"end":{"line":7,"column":7,"index":216},"filename":"mutate-after-useeffect.ts","identifierName":"arr"},{"start":{"line":7,"column":13,"index":222},"end":{"line":7,"column":16,"index":225},"filename":"mutate-after-useeffect.ts","identifierName":"foo"}]} -{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":143},"end":{"line":11,"column":1,"index":274},"filename":"mutate-after-useeffect.ts"},"fnName":"Component","memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} -``` - -### Eval output -(kind: ok) [2] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.js deleted file mode 100644 index 21298604043a..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-after-useeffect.js +++ /dev/null @@ -1,16 +0,0 @@ -// @inferEffectDependencies @panicThreshold:"none" @loggerTestOnly @enableNewMutationAliasingModel -import {useEffect, AUTODEPS} from 'react'; - -function Component({foo}) { - const arr = []; - useEffect(() => { - arr.push(foo); - }, AUTODEPS); - arr.push(2); - return arr; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{foo: 1}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.expect.md deleted file mode 100644 index 9f9786e4be83..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.expect.md +++ /dev/null @@ -1,66 +0,0 @@ - -## Input - -```javascript -// @inferEffectDependencies @enableNewMutationAliasingModel -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const [_state1, setState1] = useRef('initial value'); - const [_state2, setState2] = useRef('initial value'); - let setState; - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - useEffect(() => print(setState), AUTODEPS); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies @enableNewMutationAliasingModel -import { useEffect, useState, AUTODEPS } from "react"; -import { print } from "shared-runtime"; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const $ = _c(4); - const [, setState1] = useRef("initial value"); - const [, setState2] = useRef("initial value"); - let setState; - if ($[0] !== props.foo) { - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - $[0] = props.foo; - $[1] = setState; - } else { - setState = $[1]; - } - let t0; - if ($[2] !== setState) { - t0 = () => print(setState); - $[2] = setState; - $[3] = t0; - } else { - t0 = $[3]; - } - useEffect(t0, [setState]); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.js deleted file mode 100644 index d35199803243..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-setState.js +++ /dev/null @@ -1,18 +0,0 @@ -// @inferEffectDependencies @enableNewMutationAliasingModel -import {useEffect, useState, AUTODEPS} from 'react'; -import {print} from 'shared-runtime'; - -/* - * setState types are not enough to determine to omit from deps. Must also take reactivity into account. - */ -function ReactiveRefInEffect(props) { - const [_state1, setState1] = useRef('initial value'); - const [_state2, setState2] = useRef('initial value'); - let setState; - if (props.foo) { - setState = setState1; - } else { - setState = setState2; - } - useEffect(() => print(setState), AUTODEPS); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md index 955c4e070579..d8217ea9b686 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md @@ -21,24 +21,32 @@ function useHook({el1, el2}) { ```javascript import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel function useHook(t0) { - const $ = _c(5); + const $ = _c(7); const { el1, el2 } = t0; let s; if ($[0] !== el1 || $[1] !== el2) { s = new Set(); - const arr = makeArray(el1); - s.add(arr); - - arr.push(el2); let t1; - if ($[3] !== el2) { - t1 = makeArray(el2); - $[3] = el2; + if ($[3] !== el1) { + t1 = makeArray(el1); + $[3] = el1; $[4] = t1; } else { t1 = $[4]; } - s.add(t1); + const arr = t1; + s.add(arr); + + arr.push(el2); + let t2; + if ($[5] !== el2) { + t2 = makeArray(el2); + $[5] = el2; + $[6] = t2; + } else { + t2 = $[6]; + } + s.add(t2); $[0] = el1; $[1] = el2; $[2] = s; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.expect.md deleted file mode 100644 index 05405df219f9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.expect.md +++ /dev/null @@ -1,81 +0,0 @@ - -## Input - -```javascript -// @enableFire @enableNewMutationAliasingModel -import {fire} from 'react'; - -function Component({bar, baz}) { - const foo = () => { - console.log(bar); - }; - useEffect(() => { - fire(foo(bar)); - fire(baz(bar)); - }); - - useEffect(() => { - fire(foo(bar)); - }); - - return null; -} - -``` - -## Code - -```javascript -import { c as _c, useFire } from "react/compiler-runtime"; // @enableFire @enableNewMutationAliasingModel -import { fire } from "react"; - -function Component(t0) { - const $ = _c(9); - const { bar, baz } = t0; - let t1; - if ($[0] !== bar) { - t1 = () => { - console.log(bar); - }; - $[0] = bar; - $[1] = t1; - } else { - t1 = $[1]; - } - const foo = t1; - const t2 = useFire(foo); - const t3 = useFire(baz); - let t4; - if ($[2] !== bar || $[3] !== t2 || $[4] !== t3) { - t4 = () => { - t2(bar); - t3(bar); - }; - $[2] = bar; - $[3] = t2; - $[4] = t3; - $[5] = t4; - } else { - t4 = $[5]; - } - useEffect(t4); - let t5; - if ($[6] !== bar || $[7] !== t2) { - t5 = () => { - t2(bar); - }; - $[6] = bar; - $[7] = t2; - $[8] = t5; - } else { - t5 = $[8]; - } - useEffect(t5); - - return null; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.js deleted file mode 100644 index 54d4cf83fe31..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/shared-hook-calls.js +++ /dev/null @@ -1,18 +0,0 @@ -// @enableFire @enableNewMutationAliasingModel -import {fire} from 'react'; - -function Component({bar, baz}) { - const foo = () => { - console.log(bar); - }; - useEffect(() => { - fire(foo(bar)); - fire(baz(bar)); - }); - - useEffect(() => { - fire(foo(bar)); - }); - - return null; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.expect.md deleted file mode 100644 index 82f71f4a0501..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.expect.md +++ /dev/null @@ -1,47 +0,0 @@ - -## Input - -```javascript -// @enableChangeVariableCodegen -function Component(props) { - const c_0 = [props.a, props.b.c]; - return c_0; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{a: 3.14, b: {c: true}}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableChangeVariableCodegen -function Component(props) { - const $ = _c(3); - const c_00 = $[0] !== props.a; - const c_1 = $[1] !== props.b.c; - let t0; - if (c_00 || c_1) { - t0 = [props.a, props.b.c]; - $[0] = props.a; - $[1] = props.b.c; - $[2] = t0; - } else { - t0 = $[2]; - } - const c_0 = t0; - return c_0; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{ a: 3.14, b: { c: true } }], -}; - -``` - -### Eval output -(kind: ok) [3.14,true] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.js deleted file mode 100644 index 4b89fddcf3c6..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/option-enable-change-variable-codegen.js +++ /dev/null @@ -1,10 +0,0 @@ -// @enableChangeVariableCodegen -function Component(props) { - const c_0 = [props.a, props.b.c]; - return c_0; -} - -export const FIXTURE_ENTRYPOINT = { - fn: Component, - params: [{a: 3.14, b: {c: true}}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.expect.md new file mode 100644 index 000000000000..92831f065943 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.expect.md @@ -0,0 +1,77 @@ + +## Input + +```javascript +// @flow +import {PanResponder, Stringify} from 'shared-runtime'; + +export default component Playground() { + const onDragEndRef = useRef(() => {}); + useEffect(() => { + onDragEndRef.current = () => { + console.log('drag ended'); + }; + }); + const panResponder = useMemo( + () => + PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }), + [] + ); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { PanResponder, Stringify } from "shared-runtime"; + +export default function Playground() { + const $ = _c(3); + const onDragEndRef = useRef(_temp); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = () => { + onDragEndRef.current = _temp2; + }; + $[0] = t0; + } else { + t0 = $[0]; + } + useEffect(t0); + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }); + $[1] = t1; + } else { + t1 = $[1]; + } + const panResponder = t1; + let t2; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t2 = ; + $[2] = t2; + } else { + t2 = $[2]; + } + return t2; +} +function _temp2() { + console.log("drag ended"); +} +function _temp() {} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.js new file mode 100644 index 000000000000..93614bfeec0b --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.js @@ -0,0 +1,21 @@ +// @flow +import {PanResponder, Stringify} from 'shared-runtime'; + +export default component Playground() { + const onDragEndRef = useRef(() => {}); + useEffect(() => { + onDragEndRef.current = () => { + console.log('drag ended'); + }; + }); + const panResponder = useMemo( + () => + PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }), + [] + ); + return ; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.expect.md index 84c611dec355..d676bb8dad2e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; @@ -43,7 +43,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import { useMemo } from "react"; import { identity, ValidateMemoization } from "shared-runtime"; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.js index 373fdc53fa27..445a908c39ac 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain-less-precise-deps.js @@ -1,4 +1,4 @@ -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.expect.md index 82c11f7783d4..9f16154c7011 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; @@ -39,7 +39,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import { useMemo } from "react"; import { identity, ValidateMemoization } from "shared-runtime"; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.js index 6b55e68bb018..a601957052dd 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-conditional-property-chain.js @@ -1,4 +1,4 @@ -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.expect.md index ab4940bcc3ce..13fdd5c15002 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; @@ -44,7 +44,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import { useMemo } from "react"; import { identity, ValidateMemoization } from "shared-runtime"; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.js index 820cef20cb2a..85122e62be9e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-deps-optional-property-chain.js @@ -1,4 +1,4 @@ -// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies @enableTreatFunctionDepsAsConditional:false +// @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies import {useMemo} from 'react'; import {identity, ValidateMemoization} from 'shared-runtime'; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/error.todo-optional-call-chain-in-optional.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/error.todo-optional-call-chain-in-optional.expect.md deleted file mode 100644 index 5da7122c76fe..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/error.todo-optional-call-chain-in-optional.expect.md +++ /dev/null @@ -1,40 +0,0 @@ - -## Input - -```javascript -// @enablePropagateDepsInHIR -function useFoo(props: {value: {x: string; y: string} | null}) { - const value = props.value; - return createArray(value?.x, value?.y)?.join(', '); -} - -function createArray(...args: Array): Array { - return args; -} - -export const FIXTURE_ENTRYPONT = { - fn: useFoo, - props: [{value: null}], -}; - -``` - - -## Error - -``` -Found 1 error: - -Todo: Unexpected terminal kind `optional` for optional fallthrough block - -error.todo-optional-call-chain-in-optional.ts:4:21 - 2 | function useFoo(props: {value: {x: string; y: string} | null}) { - 3 | const value = props.value; -> 4 | return createArray(value?.x, value?.y)?.join(', '); - | ^^^^^^^^ Unexpected terminal kind `optional` for optional fallthrough block - 5 | } - 6 | - 7 | function createArray(...args: Array): Array { -``` - - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/todo-optional-call-chain-in-optional.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/todo-optional-call-chain-in-optional.expect.md new file mode 100644 index 000000000000..af046d58b713 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/todo-optional-call-chain-in-optional.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +// @enablePropagateDepsInHIR +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return createArray(value?.x, value?.y)?.join(', '); +} + +function createArray(...args: Array): Array { + return args; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR +function useFoo(props) { + const $ = _c(3); + const value = props.value; + let t0; + if ($[0] !== value?.x || $[1] !== value?.y) { + t0 = createArray(value?.x, value?.y)?.join(", "); + $[0] = value?.x; + $[1] = value?.y; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +function createArray(...t0) { + const args = t0; + return args; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{ value: null }], +}; + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/error.todo-optional-call-chain-in-optional.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/todo-optional-call-chain-in-optional.ts similarity index 100% rename from compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/error.todo-optional-call-chain-in-optional.ts rename to compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/todo-optional-call-chain-in-optional.ts diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.expect.md deleted file mode 100644 index e8d1d04b0618..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.expect.md +++ /dev/null @@ -1,78 +0,0 @@ - -## Input - -```javascript -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const a = () => { - const b = () => { - const c = () => { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }; - return c; - }; - return b; - }; - return a()()(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 42}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableChangeVariableCodegen -import { identity } from "shared-runtime"; - -const $ = "module_$"; -const t0 = "module_t0"; -const c_0 = "module_c_0"; -function useFoo(props) { - const $0 = _c(2); - const c_00 = $0[0] !== props.value; - let t1; - if (c_00) { - const a = () => { - const b = () => { - const c = () => { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }; - return c; - }; - return b; - }; - t1 = a()()(); - $0[0] = props.value; - $0[1] = t1; - } else { - t1 = $0[1]; - } - return t1; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{ value: 42 }], -}; - -``` - -### Eval output -(kind: ok) 42 -logs: ['module_$','module_t0','module_c_0'] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.js deleted file mode 100644 index 5a3d6a3ef3ac..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-function.js +++ /dev/null @@ -1,26 +0,0 @@ -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const a = () => { - const b = () => { - const c = () => { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }; - return c; - }; - return b; - }; - return a()()(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 42}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.expect.md deleted file mode 100644 index 01130cc3a376..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.expect.md +++ /dev/null @@ -1,80 +0,0 @@ - -## Input - -```javascript -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const a = { - foo() { - const b = { - bar() { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }, - }; - return b; - }, - }; - return a.foo().bar(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 42}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableChangeVariableCodegen -import { identity } from "shared-runtime"; - -const $ = "module_$"; -const t0 = "module_t0"; -const c_0 = "module_c_0"; -function useFoo(props) { - const $0 = _c(2); - const c_00 = $0[0] !== props; - let t1; - if (c_00) { - const a = { - foo() { - const b = { - bar() { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }, - }; - return b; - }, - }; - t1 = a.foo().bar(); - $0[0] = props; - $0[1] = t1; - } else { - t1 = $0[1]; - } - return t1; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{ value: 42 }], -}; - -``` - -### Eval output -(kind: ok) 42 -logs: ['module_$','module_t0','module_c_0'] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.js deleted file mode 100644 index c1432148a970..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables-nested-object-method.js +++ /dev/null @@ -1,27 +0,0 @@ -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const a = { - foo() { - const b = { - bar() { - console.log($); - console.log(t0); - console.log(c_0); - return identity(props.value); - }, - }; - return b; - }, - }; - return a.foo().bar(); -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 42}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.expect.md deleted file mode 100644 index 71e9680c4e77..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.expect.md +++ /dev/null @@ -1,62 +0,0 @@ - -## Input - -```javascript -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const results = identity(props.value); - console.log($); - console.log(t0); - console.log(c_0); - return results; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 0}], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enableChangeVariableCodegen -import { identity } from "shared-runtime"; - -const $ = "module_$"; -const t0 = "module_t0"; -const c_0 = "module_c_0"; -function useFoo(props) { - const $0 = _c(2); - const c_00 = $0[0] !== props.value; - let t1; - if (c_00) { - t1 = identity(props.value); - $0[0] = props.value; - $0[1] = t1; - } else { - t1 = $0[1]; - } - const results = t1; - console.log($); - console.log(t0); - console.log(c_0); - return results; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{ value: 0 }], -}; - -``` - -### Eval output -(kind: ok) 0 -logs: ['module_$','module_t0','module_c_0'] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.ts deleted file mode 100644 index 38e75a821906..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rename-source-variables.ts +++ /dev/null @@ -1,18 +0,0 @@ -// @enableChangeVariableCodegen -import {identity} from 'shared-runtime'; - -const $ = 'module_$'; -const t0 = 'module_t0'; -const c_0 = 'module_c_0'; -function useFoo(props: {value: number}): number { - const results = identity(props.value); - console.log($); - console.log(t0); - console.log(c_0); - return results; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [{value: 0}], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.expect.md new file mode 100644 index 000000000000..27da22eb0fa5 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.expect.md @@ -0,0 +1,87 @@ + +## Input + +```javascript +// @flow +import {Stringify} from 'shared-runtime'; + +/** + * Example fixture demonstrating a case where we could hoist dependencies + * and reuse them across scopes. Here we extract a temporary for `item.value` + * and reference it both in the scope for `a`. Then the scope for `c` could + * use `.inner` as its dependency, avoiding reloading + * `item.value`. + */ +function Test({item, index}: {item: {value: {inner: any}}, index: number}) { + // These scopes have the same dependency, `item.value`, and could + // share a hoisted expression to evaluate it + const a = []; + if (index) { + a.push({value: item.value, index}); + } + const b = [item.value]; + + // This dependency is more precise (nested property), the outer + // `item.value` portion could use a hoisted dep for `item.value + const c = [item.value.inner]; + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Test(t0) { + const $ = _c(11); + const { item, index } = t0; + let a; + if ($[0] !== index || $[1] !== item.value) { + a = []; + if (index) { + a.push({ value: item.value, index }); + } + $[0] = index; + $[1] = item.value; + $[2] = a; + } else { + a = $[2]; + } + let t1; + if ($[3] !== item.value) { + t1 = [item.value]; + $[3] = item.value; + $[4] = t1; + } else { + t1 = $[4]; + } + const b = t1; + let t2; + if ($[5] !== item.value.inner) { + t2 = [item.value.inner]; + $[5] = item.value.inner; + $[6] = t2; + } else { + t2 = $[6]; + } + const c = t2; + let t3; + if ($[7] !== a || $[8] !== b || $[9] !== c) { + t3 = ; + $[7] = a; + $[8] = b; + $[9] = c; + $[10] = t3; + } else { + t3 = $[10]; + } + return t3; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.js new file mode 100644 index 000000000000..63ebbdd91b7b --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repeated-dependencies-more-precise.js @@ -0,0 +1,24 @@ +// @flow +import {Stringify} from 'shared-runtime'; + +/** + * Example fixture demonstrating a case where we could hoist dependencies + * and reuse them across scopes. Here we extract a temporary for `item.value` + * and reference it both in the scope for `a`. Then the scope for `c` could + * use `.inner` as its dependency, avoiding reloading + * `item.value`. + */ +function Test({item, index}: {item: {value: {inner: any}}, index: number}) { + // These scopes have the same dependency, `item.value`, and could + // share a hoisted expression to evaluate it + const a = []; + if (index) { + a.push({value: item.value, index}); + } + const b = [item.value]; + + // This dependency is more precise (nested property), the outer + // `item.value` portion could use a hoisted dep for `item.value + const c = [item.value.inner]; + return ; +} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.expect.md deleted file mode 100644 index b19a06519f75..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.expect.md +++ /dev/null @@ -1,27 +0,0 @@ - -## Input - -```javascript -// @flow @enableEmitHookGuards @panicThreshold:"none" @enableFire - -component Foo(useDynamicHook) { - useDynamicHook(); - return
hello world
; -} - -``` - -## Code - -```javascript -function Foo({ - useDynamicHook, -}: $ReadOnly<{ useDynamicHook: any }>): React.Node { - useDynamicHook(); - return
hello world
; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.js deleted file mode 100644 index 54b18182133d..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-dont-add-hook-guards-on-retry.js +++ /dev/null @@ -1,6 +0,0 @@ -// @flow @enableEmitHookGuards @panicThreshold:"none" @enableFire - -component Foo(useDynamicHook) { - useDynamicHook(); - return
hello world
; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-independently-memoized-property-load-for-method-call.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-independently-memoized-property-load-for-method-call.expect.md index ea711a5baf4b..64fb486fdaab 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-independently-memoized-property-load-for-method-call.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-independently-memoized-property-load-for-method-call.expect.md @@ -54,40 +54,54 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { c as _c } from "react/compiler-runtime"; function Component(t0) { - const $ = _c(8); + const $ = _c(12); const { label, highlightedItem } = t0; const serverTime = useServerTime(); let t1; + if ($[0] !== highlightedItem) { + t1 = new Highlight(highlightedItem); + $[0] = highlightedItem; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; let timestampLabel; - if ($[0] !== highlightedItem || $[1] !== label || $[2] !== serverTime) { - const highlight = new Highlight(highlightedItem); + if ($[2] !== label || $[3] !== serverTime || $[4] !== t1) { + const highlight = t1; const time = serverTime.get(); timestampLabel = time / 1000 || label; - t1 = highlight.render(); - $[0] = highlightedItem; - $[1] = label; - $[2] = serverTime; - $[3] = t1; - $[4] = timestampLabel; + if ($[7] !== highlight) { + t2 = highlight.render(); + $[7] = highlight; + $[8] = t2; + } else { + t2 = $[8]; + } + $[2] = label; + $[3] = serverTime; + $[4] = t1; + $[5] = t2; + $[6] = timestampLabel; } else { - t1 = $[3]; - timestampLabel = $[4]; + t2 = $[5]; + timestampLabel = $[6]; } - let t2; - if ($[5] !== t1 || $[6] !== timestampLabel) { - t2 = ( + let t3; + if ($[9] !== t2 || $[10] !== timestampLabel) { + t3 = ( <> - {t1} + {t2} {timestampLabel} ); - $[5] = t1; - $[6] = timestampLabel; - $[7] = t2; + $[9] = t2; + $[10] = timestampLabel; + $[11] = t3; } else { - t2 = $[7]; + t3 = $[11]; } - return t2; + return t3; } function useServerTime() { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.expect.md deleted file mode 100644 index 15bc857ac76f..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.expect.md +++ /dev/null @@ -1,90 +0,0 @@ - -## Input - -```javascript -// @flow @validatePreserveExistingMemoizationGuarantees @enableUseTypeAnnotations -import {useMemo} from 'react'; -import {useFragment} from 'shared-runtime'; - -// This is a version of error.todo-repro-missing-memoization-lack-of-phi-types -// with explicit type annotations and using enableUseTypeAnnotations to demonstrate -// that type information is sufficient to preserve memoization in this example -function Component() { - const data = useFragment(); - const nodes: Array = data.nodes ?? []; - const flatMap: Array = nodes.flatMap(node => node.items); - const filtered: Array = flatMap.filter(item => item != null); - const map: Array = useMemo(() => filtered.map(), [filtered]); - const index: Array = filtered.findIndex(x => x === null); - - return ( -
- {map} - {index} -
- ); -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; -import { useMemo } from "react"; -import { useFragment } from "shared-runtime"; - -function Component() { - const $ = _c(7); - const data = useFragment(); - let t0; - if ($[0] !== data.nodes) { - const nodes = data.nodes ?? []; - const flatMap = nodes.flatMap(_temp); - t0 = flatMap.filter(_temp2); - $[0] = data.nodes; - $[1] = t0; - } else { - t0 = $[1]; - } - const filtered = t0; - let t1; - if ($[2] !== filtered) { - t1 = filtered.map(); - $[2] = filtered; - $[3] = t1; - } else { - t1 = $[3]; - } - const map = t1; - const index = filtered.findIndex(_temp3); - let t2; - if ($[4] !== index || $[5] !== map) { - t2 = ( -
- {map} - {index} -
- ); - $[4] = index; - $[5] = map; - $[6] = t2; - } else { - t2 = $[6]; - } - return t2; -} -function _temp3(x) { - return x === null; -} -function _temp2(item) { - return item != null; -} -function _temp(node) { - return node.items; -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.js deleted file mode 100644 index 914ef6199d36..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-missing-memoization-lack-of-phi-types-explicit-types.js +++ /dev/null @@ -1,22 +0,0 @@ -// @flow @validatePreserveExistingMemoizationGuarantees @enableUseTypeAnnotations -import {useMemo} from 'react'; -import {useFragment} from 'shared-runtime'; - -// This is a version of error.todo-repro-missing-memoization-lack-of-phi-types -// with explicit type annotations and using enableUseTypeAnnotations to demonstrate -// that type information is sufficient to preserve memoization in this example -function Component() { - const data = useFragment(); - const nodes: Array = data.nodes ?? []; - const flatMap: Array = nodes.flatMap(node => node.items); - const filtered: Array = flatMap.filter(item => item != null); - const map: Array = useMemo(() => filtered.map(), [filtered]); - const index: Array = filtered.findIndex(x => x === null); - - return ( -
- {map} - {index} -
- ); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-unmerged-fbt-call-merge-overlapping-reactive-scopes.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-unmerged-fbt-call-merge-overlapping-reactive-scopes.expect.md index ee2c82048a9d..2c7e3f7a4703 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-unmerged-fbt-call-merge-overlapping-reactive-scopes.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-unmerged-fbt-call-merge-overlapping-reactive-scopes.expect.md @@ -36,27 +36,35 @@ import fbt from "fbt"; import { Stringify } from "shared-runtime"; function Component(props) { - const $ = _c(3); + const $ = _c(5); let t0; - if ($[0] !== props.cond || $[1] !== props.value.length) { - const label = fbt._( + if ($[0] !== props.value.length) { + t0 = fbt._( { "*": "{number} bars", _1: "1 bar" }, [fbt._plural(props.value.length, "number")], { hk: "4mUen7" }, ); - t0 = props.cond ? ( + $[0] = props.value.length; + $[1] = t0; + } else { + t0 = $[1]; + } + const label = t0; + let t1; + if ($[2] !== label || $[3] !== props.cond) { + t1 = props.cond ? ( ) : null; - $[0] = props.cond; - $[1] = props.value.length; - $[2] = t0; + $[2] = label; + $[3] = props.cond; + $[4] = t1; } else { - t0 = $[2]; + t1 = $[4]; } - return t0; + return t1; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.invalid-hook-for.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.invalid-hook-for.expect.md index 84db054148e4..cd05dccfe6dd 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.invalid-hook-for.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/rules-of-hooks/error.invalid-hook-for.expect.md @@ -16,29 +16,24 @@ function Component(props) { ## Error ``` -Found 2 errors: +Found 1 error: -Error: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) +Invariant: Unexpected empty block with `goto` terminal -error.invalid-hook-for.ts:4:9 - 2 | let i = 0; - 3 | for (let x = 0; useHook(x) < 10; useHook(i), x++) { -> 4 | i += useHook(x); - | ^^^^^^^ Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) - 5 | } - 6 | return i; - 7 | } +Block bb5 is empty. -Error: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) - -error.invalid-hook-for.ts:3:35 +error.invalid-hook-for.ts:3:2 1 | function Component(props) { 2 | let i = 0; > 3 | for (let x = 0; useHook(x) < 10; useHook(i), x++) { - | ^^^^^^^ Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) - 4 | i += useHook(x); - 5 | } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +> 4 | i += useHook(x); + | ^^^^^^^^^^^^^^^^^^^^ +> 5 | } + | ^^^^ Unexpected empty block with `goto` terminal 6 | return i; + 7 | } + 8 | ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-load-cached.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-load-cached.expect.md index a2a0e3bef9b7..9c2aa913af92 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-load-cached.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-load-cached.expect.md @@ -35,31 +35,47 @@ import { makeArray } from "shared-runtime"; * and avoid adding them to `temporariesUsedOutsideDefiningScope`. */ function Component(t0) { - const $ = _c(6); + const $ = _c(12); const { num } = t0; - let T0; let t1; if ($[0] !== num) { - const arr = makeArray(num); - T0 = Stringify; - t1 = arr.push(num); + t1 = makeArray(num); $[0] = num; - $[1] = T0; - $[2] = t1; + $[1] = t1; } else { - T0 = $[1]; - t1 = $[2]; + t1 = $[1]; } + let T0; let t2; - if ($[3] !== T0 || $[4] !== t1) { - t2 = ; - $[3] = T0; - $[4] = t1; + if ($[2] !== num || $[3] !== t1) { + const arr = t1; + T0 = Stringify; + if ($[6] !== arr || $[7] !== num) { + t2 = arr.push(num); + $[6] = arr; + $[7] = num; + $[8] = t2; + } else { + t2 = $[8]; + } + $[2] = num; + $[3] = t1; + $[4] = T0; $[5] = t2; } else { + T0 = $[4]; t2 = $[5]; } - return t2; + let t3; + if ($[9] !== T0 || $[10] !== t2) { + t3 = ; + $[9] = T0; + $[10] = t2; + $[11] = t3; + } else { + t3 = $[11]; + } + return t3; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-property-load-cached.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-property-load-cached.expect.md index 058f5ab0a986..301c5756599d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-property-load-cached.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-global-property-load-cached.expect.md @@ -39,31 +39,47 @@ import { makeArray } from "shared-runtime"; * and avoid adding them to `temporariesUsedOutsideDefiningScope`. */ function Component(t0) { - const $ = _c(6); + const $ = _c(12); const { num } = t0; - let T0; let t1; if ($[0] !== num) { - const arr = makeArray(num); - T0 = SharedRuntime.Stringify; - t1 = arr.push(num); + t1 = makeArray(num); $[0] = num; - $[1] = T0; - $[2] = t1; + $[1] = t1; } else { - T0 = $[1]; - t1 = $[2]; + t1 = $[1]; } + let T0; let t2; - if ($[3] !== T0 || $[4] !== t1) { - t2 = ; - $[3] = T0; - $[4] = t1; + if ($[2] !== num || $[3] !== t1) { + const arr = t1; + T0 = SharedRuntime.Stringify; + if ($[6] !== arr || $[7] !== num) { + t2 = arr.push(num); + $[6] = arr; + $[7] = num; + $[8] = t2; + } else { + t2 = $[8]; + } + $[2] = num; + $[3] = t1; + $[4] = T0; $[5] = t2; } else { + T0 = $[4]; t2 = $[5]; } - return t2; + let t3; + if ($[9] !== T0 || $[10] !== t2) { + t3 = ; + $[9] = T0; + $[10] = t2; + $[11] = t3; + } else { + t3 = $[11]; + } + return t3; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-optional-call-chain-in-optional.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-optional-call-chain-in-optional.expect.md new file mode 100644 index 000000000000..c9d71a8c3b79 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-optional-call-chain-in-optional.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return createArray(value?.x, value?.y)?.join(', '); +} + +function createArray(...args: Array): Array { + return args; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function useFoo(props) { + const $ = _c(3); + const value = props.value; + let t0; + if ($[0] !== value?.x || $[1] !== value?.y) { + t0 = createArray(value?.x, value?.y)?.join(", "); + $[0] = value?.x; + $[1] = value?.y; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +function createArray(...t0) { + const args = t0; + return args; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{ value: null }], +}; + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-optional-call-chain-in-optional.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-optional-call-chain-in-optional.ts similarity index 100% rename from compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-optional-call-chain-in-optional.ts rename to compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-optional-call-chain-in-optional.ts diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.error.object-pattern-computed-key.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.error.object-pattern-computed-key.expect.md index 7bc1e49069b6..930be997fad8 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.error.object-pattern-computed-key.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.error.object-pattern-computed-key.expect.md @@ -23,16 +23,18 @@ export const FIXTURE_ENTRYPOINT = { ``` Found 1 error: -Todo: (BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized -todo.error.object-pattern-computed-key.ts:5:9 - 3 | const SCALE = 2; + value$3. + +todo.error.object-pattern-computed-key.ts:6:9 4 | function Component(props) { -> 5 | const {[props.name]: value} = props; - | ^^^^^^^^^^^^^^^^^^^ (BuildHIR::lowerAssignment) Handle computed properties in ObjectPattern - 6 | return value; + 5 | const {[props.name]: value} = props; +> 6 | return value; + | ^^^^^ this is uninitialized 7 | } 8 | + 9 | export const FIXTURE_ENTRYPOINT = { ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.expect.md deleted file mode 100644 index 7ac6486b4703..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.expect.md +++ /dev/null @@ -1,33 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const [foo, bar] = useContext(MyContext); - return ; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const [foo, bar] = useContext(MyContext); - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.js deleted file mode 100644 index 387c30cdc1e7..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-array-destructuring.js +++ /dev/null @@ -1,5 +0,0 @@ -// @lowerContextAccess -function App() { - const [foo, bar] = useContext(MyContext); - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.expect.md deleted file mode 100644 index 3eac66304be6..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const {foo} = context; - const {bar} = context; - return ; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const context = useContext(MyContext); - const { foo } = context; - const { bar } = context; - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.js deleted file mode 100644 index e7b106e9db86..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-destructure-multiple.js +++ /dev/null @@ -1,7 +0,0 @@ -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const {foo} = context; - const {bar} = context; - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.expect.md deleted file mode 100644 index 4cca8b19d95c..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const [foo] = context; - const {bar} = context; - return ; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const context = useContext(MyContext); - const [foo] = context; - const { bar } = context; - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.js deleted file mode 100644 index fa511732e3fb..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-mixed-array-obj.js +++ /dev/null @@ -1,7 +0,0 @@ -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const [foo] = context; - const {bar} = context; - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.expect.md deleted file mode 100644 index f5a391662691..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const { - joe: {foo}, - bar, - } = useContext(MyContext); - return ; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const { joe: t0, bar } = useContext(MyContext); - const { foo } = t0; - let t1; - if ($[0] !== bar || $[1] !== foo) { - t1 = ; - $[0] = bar; - $[1] = foo; - $[2] = t1; - } else { - t1 = $[2]; - } - return t1; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.js deleted file mode 100644 index 89a7ccd7db47..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-nested-destructuring.js +++ /dev/null @@ -1,8 +0,0 @@ -// @lowerContextAccess -function App() { - const { - joe: {foo}, - bar, - } = useContext(MyContext); - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.expect.md deleted file mode 100644 index 0888d67b2aa9..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.expect.md +++ /dev/null @@ -1,37 +0,0 @@ - -## Input - -```javascript -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const foo = context.foo; - const bar = context.bar; - return ; -} - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess -function App() { - const $ = _c(3); - const context = useContext(MyContext); - const foo = context.foo; - const bar = context.bar; - let t0; - if ($[0] !== bar || $[1] !== foo) { - t0 = ; - $[0] = bar; - $[1] = foo; - $[2] = t0; - } else { - t0 = $[2]; - } - return t0; -} - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.js deleted file mode 100644 index a055114c1ec7..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo.lower-context-access-property-load.js +++ /dev/null @@ -1,7 +0,0 @@ -// @lowerContextAccess -function App() { - const context = useContext(MyContext); - const foo = context.foo; - const bar = context.bar; - return ; -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.expect.md deleted file mode 100644 index d1fd19c00573..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.expect.md +++ /dev/null @@ -1,51 +0,0 @@ - -## Input - -```javascript -// @validateNoCapitalizedCalls @enableFire @panicThreshold:"none" -import {fire} from 'react'; -const CapitalizedCall = require('shared-runtime').sum; - -function Component({prop1, bar}) { - const foo = () => { - console.log(prop1); - }; - useEffect(() => { - fire(foo(prop1)); - fire(foo()); - fire(bar()); - }); - - return CapitalizedCall(); -} - -``` - -## Code - -```javascript -import { useFire } from "react/compiler-runtime"; // @validateNoCapitalizedCalls @enableFire @panicThreshold:"none" -import { fire } from "react"; -const CapitalizedCall = require("shared-runtime").sum; - -function Component(t0) { - const { prop1, bar } = t0; - const foo = () => { - console.log(prop1); - }; - const t1 = useFire(foo); - const t2 = useFire(bar); - - useEffect(() => { - t1(prop1); - t1(); - t2(); - }); - - return CapitalizedCall(); -} - -``` - -### Eval output -(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.js deleted file mode 100644 index bd9715e91a2d..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-capitalized-fn-call.js +++ /dev/null @@ -1,16 +0,0 @@ -// @validateNoCapitalizedCalls @enableFire @panicThreshold:"none" -import {fire} from 'react'; -const CapitalizedCall = require('shared-runtime').sum; - -function Component({prop1, bar}) { - const foo = () => { - console.log(prop1); - }; - useEffect(() => { - fire(foo(prop1)); - fire(foo()); - fire(bar()); - }); - - return CapitalizedCall(); -} diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-eslint-suppressions.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-eslint-suppressions.expect.md deleted file mode 100644 index 99774bdd3e60..000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/bailout-retry/bailout-eslint-suppressions.expect.md +++ /dev/null @@ -1,55 +0,0 @@ - -## Input - -```javascript -// @enableFire @panicThreshold:"none" -import {useRef} from 'react'; - -function Component({props, bar}) { - const foo = () => { - console.log(props); - }; - useEffect(() => { - fire(foo(props)); - fire(foo()); - fire(bar()); - }); - - const ref = useRef(null); - // eslint-disable-next-line react-hooks/rules-of-hooks - ref.current = 'bad'; - return
- {enableSuspenseTab && ( - - )} +
{editorPortalContainer ? ( // window.api is defined in preload.js const {electron, readEnv, ip, getDevTools} = window.api; - const {options, useHttps, host, protocol, port} = readEnv(); + const {options, useHttps, host, protocol, port, path, clientHost, clientPort, clientUseHttps} = readEnv(); const localIp = ip.address(); - const defaultPort = (port === 443 && useHttps) || (port === 80 && !useHttps); - const server = defaultPort ? `${protocol}://${host}` : `${protocol}://${host}:${port}`; - const serverIp = defaultPort ? `${protocol}://${localIp}` : `${protocol}://${localIp}:${port}`; + + // Effective values for display URLs: client overrides take precedence over server values. + const effectiveHost = clientHost != null ? clientHost : host; + const effectivePort = clientPort != null ? clientPort : port; + const effectiveUseHttps = clientUseHttps != null ? clientUseHttps : useHttps; + const effectiveProtocol = effectiveUseHttps ? 'https' : 'http'; + const defaultPort = (effectivePort === 443 && effectiveUseHttps) || (effectivePort === 80 && !effectiveUseHttps); + const pathStr = path != null ? path : ''; + const server = defaultPort ? `${effectiveProtocol}://${effectiveHost}${pathStr}` : `${effectiveProtocol}://${effectiveHost}:${effectivePort}${pathStr}`; + const serverIp = defaultPort ? `${effectiveProtocol}://${localIp}${pathStr}` : `${effectiveProtocol}://${localIp}:${effectivePort}${pathStr}`; const $ = document.querySelector.bind(document); let timeoutID; @@ -234,7 +241,7 @@ element.innerText = status; } }) - .startServer(port, host, options); + .startServer(port, host, options, undefined, path, {host: clientHost, port: clientPort, useHttps: clientUseHttps}); diff --git a/packages/react-devtools/preload.js b/packages/react-devtools/preload.js index 33a9e3d6dd46..3286d4442097 100644 --- a/packages/react-devtools/preload.js +++ b/packages/react-devtools/preload.js @@ -36,6 +36,23 @@ contextBridge.exposeInMainWorld('api', { const host = process.env.HOST || 'localhost'; const protocol = useHttps ? 'https' : 'http'; const port = +process.env.REACT_DEVTOOLS_PORT || +process.env.PORT || 8097; - return {options, useHttps, host, protocol, port}; + const path = process.env.REACT_DEVTOOLS_PATH || undefined; + const clientHost = process.env.REACT_DEVTOOLS_CLIENT_HOST || undefined; + const clientPort = process.env.REACT_DEVTOOLS_CLIENT_PORT + ? +process.env.REACT_DEVTOOLS_CLIENT_PORT + : undefined; + const clientUseHttps = + process.env.REACT_DEVTOOLS_CLIENT_USE_HTTPS === 'true' ? true : undefined; + return { + options, + useHttps, + host, + protocol, + port, + path, + clientHost, + clientPort, + clientUseHttps, + }; }, }); diff --git a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js index a03ccc161ad1..94d37cfc902d 100644 --- a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js +++ b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js @@ -3056,13 +3056,16 @@ function indexOfEventListener( listener: EventListener, optionsOrUseCapture: void | EventListenerOptionsOrUseCapture, ): number { + if (eventListeners.length === 0) { + return -1; + } + const normalizedOptions = normalizeListenerOptions(optionsOrUseCapture); for (let i = 0; i < eventListeners.length; i++) { const item = eventListeners[i]; if ( item.type === type && item.listener === listener && - normalizeListenerOptions(item.optionsOrUseCapture) === - normalizeListenerOptions(optionsOrUseCapture) + normalizeListenerOptions(item.optionsOrUseCapture) === normalizedOptions ) { return i; } @@ -3154,18 +3157,34 @@ function collectChildren(child: Fiber, collection: Array): boolean { } // $FlowFixMe[prop-missing] FragmentInstance.prototype.blur = function (this: FragmentInstanceType): void { - // TODO: When we have a parent element reference, we can skip traversal if the fragment's parent - // does not contain document.activeElement + // Early exit if activeElement is not within the fragment's parent + const parentHostFiber = getFragmentParentHostFiber(this._fragmentFiber); + if (parentHostFiber === null) { + return; + } + const parentHostInstance = + getInstanceFromHostFiber(parentHostFiber); + const activeElement = parentHostInstance.ownerDocument.activeElement; + if (activeElement === null || !parentHostInstance.contains(activeElement)) { + return; + } + traverseFragmentInstance( this._fragmentFiber, blurActiveElementWithinFragment, + activeElement, ); }; -function blurActiveElementWithinFragment(child: Fiber): boolean { - // TODO: We can get the activeElement from the parent outside of the loop when we have a reference. +function blurActiveElementWithinFragment( + child: Fiber, + activeElement: Element, +): boolean { + // Skip text nodes - they can't be focused + if (enableFragmentRefsTextNodes && child.tag === HostText) { + return false; + } const instance = getInstanceFromHostFiber(child); - const ownerDocument = instance.ownerDocument; - if (instance === ownerDocument.activeElement) { + if (instance === activeElement) { // $FlowFixMe[prop-missing] instance.blur(); return true; @@ -3312,46 +3331,45 @@ FragmentInstance.prototype.compareDocumentPosition = function ( ); } - const firstElement = getInstanceFromHostFiber(children[0]); - const lastElement = getInstanceFromHostFiber( + const firstNode = getInstanceFromHostFiber(children[0]); + const lastNode = getInstanceFromHostFiber( children[children.length - 1], ); // If the fragment has been portaled into another host instance, we need to // our best guess is to use the parent of the child instance, rather than // the fiber tree host parent. - const firstInstance = getInstanceFromHostFiber(children[0]); const parentHostInstanceFromDOM = fiberIsPortaledIntoHost(this._fragmentFiber) - ? (firstInstance.parentElement: ?Instance) + ? (firstNode.parentElement: ?Instance) : parentHostInstance; if (parentHostInstanceFromDOM == null) { return Node.DOCUMENT_POSITION_DISCONNECTED; } - // Check if first and last element are actually in the expected document position - // before relying on them as source of truth for other contained elements - const firstElementIsContained = - parentHostInstanceFromDOM.compareDocumentPosition(firstElement) & + // Check if first and last node are actually in the expected document position + // before relying on them as source of truth for other contained nodes + const firstNodeIsContained = + parentHostInstanceFromDOM.compareDocumentPosition(firstNode) & Node.DOCUMENT_POSITION_CONTAINED_BY; - const lastElementIsContained = - parentHostInstanceFromDOM.compareDocumentPosition(lastElement) & + const lastNodeIsContained = + parentHostInstanceFromDOM.compareDocumentPosition(lastNode) & Node.DOCUMENT_POSITION_CONTAINED_BY; - const firstResult = firstElement.compareDocumentPosition(otherNode); - const lastResult = lastElement.compareDocumentPosition(otherNode); + const firstResult = firstNode.compareDocumentPosition(otherNode); + const lastResult = lastNode.compareDocumentPosition(otherNode); const otherNodeIsFirstOrLastChild = - (firstElementIsContained && firstElement === otherNode) || - (lastElementIsContained && lastElement === otherNode); + (firstNodeIsContained && firstNode === otherNode) || + (lastNodeIsContained && lastNode === otherNode); const otherNodeIsFirstOrLastChildDisconnected = - (!firstElementIsContained && firstElement === otherNode) || - (!lastElementIsContained && lastElement === otherNode); + (!firstNodeIsContained && firstNode === otherNode) || + (!lastNodeIsContained && lastNode === otherNode); const otherNodeIsWithinFirstOrLastChild = firstResult & Node.DOCUMENT_POSITION_CONTAINED_BY || lastResult & Node.DOCUMENT_POSITION_CONTAINED_BY; const otherNodeIsBetweenFirstAndLastChildren = - firstElementIsContained && - lastElementIsContained && + firstNodeIsContained && + lastNodeIsContained && firstResult & Node.DOCUMENT_POSITION_FOLLOWING && lastResult & Node.DOCUMENT_POSITION_PRECEDING; @@ -3544,40 +3562,48 @@ export function updateFragmentInstanceFiber( } export function commitNewChildToFragmentInstance( - childInstance: InstanceWithFragmentHandles, + childInstance: InstanceWithFragmentHandles | Text, fragmentInstance: FragmentInstanceType, ): void { + if (childInstance.nodeType === TEXT_NODE) { + return; + } + const instance: InstanceWithFragmentHandles = (childInstance: any); const eventListeners = fragmentInstance._eventListeners; if (eventListeners !== null) { for (let i = 0; i < eventListeners.length; i++) { const {type, listener, optionsOrUseCapture} = eventListeners[i]; - childInstance.addEventListener(type, listener, optionsOrUseCapture); + instance.addEventListener(type, listener, optionsOrUseCapture); } } if (fragmentInstance._observers !== null) { fragmentInstance._observers.forEach(observer => { - observer.observe(childInstance); + observer.observe(instance); }); } if (enableFragmentRefsInstanceHandles) { - addFragmentHandleToInstance(childInstance, fragmentInstance); + addFragmentHandleToInstance(instance, fragmentInstance); } } export function deleteChildFromFragmentInstance( - childInstance: InstanceWithFragmentHandles, + childInstance: InstanceWithFragmentHandles | Text, fragmentInstance: FragmentInstanceType, ): void { + if (childInstance.nodeType === TEXT_NODE) { + return; + } + const instance: InstanceWithFragmentHandles = (childInstance: any); const eventListeners = fragmentInstance._eventListeners; if (eventListeners !== null) { for (let i = 0; i < eventListeners.length; i++) { const {type, listener, optionsOrUseCapture} = eventListeners[i]; - childInstance.removeEventListener(type, listener, optionsOrUseCapture); + instance.removeEventListener(type, listener, optionsOrUseCapture); } } if (enableFragmentRefsInstanceHandles) { - if (childInstance.unstable_reactFragments != null) { - childInstance.unstable_reactFragments.delete(fragmentInstance); + if (instance.unstable_reactFragments != null) { + instance.unstable_reactFragments.delete(fragmentInstance); } } } diff --git a/packages/react-dom-bindings/src/events/DOMEventNames.js b/packages/react-dom-bindings/src/events/DOMEventNames.js index 73f0e2f632c5..579762b6962d 100644 --- a/packages/react-dom-bindings/src/events/DOMEventNames.js +++ b/packages/react-dom-bindings/src/events/DOMEventNames.js @@ -51,6 +51,7 @@ export type DOMEventName = | 'focusin' | 'focusout' | 'fullscreenchange' + | 'fullscreenerror' | 'gotpointercapture' | 'hashchange' | 'input' diff --git a/packages/react-dom-bindings/src/events/DOMEventProperties.js b/packages/react-dom-bindings/src/events/DOMEventProperties.js index 534cae3045e3..6b2e80ecd1eb 100644 --- a/packages/react-dom-bindings/src/events/DOMEventProperties.js +++ b/packages/react-dom-bindings/src/events/DOMEventProperties.js @@ -62,6 +62,8 @@ const simpleEventPluginEvents = [ 'encrypted', 'ended', 'error', + 'fullscreenChange', + 'fullscreenError', 'gotPointerCapture', 'input', 'invalid', diff --git a/packages/react-dom-bindings/src/events/ReactDOMEventListener.js b/packages/react-dom-bindings/src/events/ReactDOMEventListener.js index ee187bc7f480..0b2fffbcf902 100644 --- a/packages/react-dom-bindings/src/events/ReactDOMEventListener.js +++ b/packages/react-dom-bindings/src/events/ReactDOMEventListener.js @@ -362,6 +362,7 @@ export function getEventPriority(domEventName: DOMEventName): EventPriority { case 'beforeinput': case 'blur': case 'fullscreenchange': + case 'fullscreenerror': case 'focus': case 'hashchange': case 'popstate': diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js index a93c32a947f1..e654ea88007d 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js @@ -7041,7 +7041,17 @@ export function hoistHoistables( } } -export function hasSuspenseyContent(hoistableState: HoistableState): boolean { +export function hasSuspenseyContent( + hoistableState: HoistableState, + flushingInShell: boolean, +): boolean { + if (flushingInShell) { + // When flushing the shell, stylesheets with precedence are already emitted + // in the which blocks paint. There's no benefit to outlining for CSS + // alone during the shell flush. However, suspensey images (for ViewTransition + // animation reveals) should still trigger outlining even during the shell. + return hoistableState.suspenseyImages; + } return hoistableState.stylesheets.size > 0 || hoistableState.suspenseyImages; } diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js index d48e9a8dd932..46fad3c39bf4 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js @@ -326,7 +326,10 @@ export function writePreambleStart( ); } -export function hasSuspenseyContent(hoistableState: HoistableState): boolean { +export function hasSuspenseyContent( + hoistableState: HoistableState, + flushingInShell: boolean, +): boolean { // Never outline. return false; } diff --git a/packages/react-dom-bindings/src/shared/getAttributeAlias.js b/packages/react-dom-bindings/src/shared/getAttributeAlias.js index 2ed432f20275..31ce02f5d560 100644 --- a/packages/react-dom-bindings/src/shared/getAttributeAlias.js +++ b/packages/react-dom-bindings/src/shared/getAttributeAlias.js @@ -50,6 +50,7 @@ const aliases = new Map([ ['markerEnd', 'marker-end'], ['markerMid', 'marker-mid'], ['markerStart', 'marker-start'], + ['maskType', 'mask-type'], ['overlinePosition', 'overline-position'], ['overlineThickness', 'overline-thickness'], ['paintOrder', 'paint-order'], diff --git a/packages/react-dom-bindings/src/shared/possibleStandardNames.js b/packages/react-dom-bindings/src/shared/possibleStandardNames.js index 369b87110b23..b237d116e43e 100644 --- a/packages/react-dom-bindings/src/shared/possibleStandardNames.js +++ b/packages/react-dom-bindings/src/shared/possibleStandardNames.js @@ -314,6 +314,7 @@ const possibleStandardNames = { markerwidth: 'markerWidth', mask: 'mask', maskcontentunits: 'maskContentUnits', + masktype: 'maskType', maskunits: 'maskUnits', mathematical: 'mathematical', mode: 'mode', diff --git a/packages/react-dom/src/__tests__/ReactDOMEventPropagation-test.js b/packages/react-dom/src/__tests__/ReactDOMEventPropagation-test.js index 7e578fbba14e..752c8bba961b 100644 --- a/packages/react-dom/src/__tests__/ReactDOMEventPropagation-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMEventPropagation-test.js @@ -834,6 +834,40 @@ describe('ReactDOMEventListener', () => { }, }); }); + + it('onFullscreenChange', async () => { + await testNativeBubblingEvent({ + type: 'div', + reactEvent: 'onFullscreenChange', + reactEventType: 'fullscreenchange', + nativeEvent: 'fullscreenchange', + dispatch(node) { + node.dispatchEvent( + new Event('fullscreenchange', { + bubbles: true, + cancelable: false, + }), + ); + }, + }); + }); + + it('onFullscreenError', async () => { + await testNativeBubblingEvent({ + type: 'div', + reactEvent: 'onFullscreenError', + reactEventType: 'fullscreenerror', + nativeEvent: 'fullscreenerror', + dispatch(node) { + node.dispatchEvent( + new Event('fullscreenerror', { + bubbles: true, + cancelable: false, + }), + ); + }, + }); + }); }); describe('non-bubbling events that bubble in React', () => { diff --git a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js index 1caa5ed8d6e7..21bf9684b285 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js @@ -9449,4 +9449,192 @@ background-color: green; ); }); }); + + it('does not outline a boundary with suspensey CSS when flushing the shell', async () => { + // When flushing the shell, stylesheets with precedence are emitted in the + // which blocks paint anyway. So there's no benefit to outlining the + // boundary — it would just show a higher-level fallback unnecessarily. + // Instead, the boundary should be inlined so the innermost fallback is shown. + let streamedContent = ''; + writable.on('data', chunk => (streamedContent += chunk)); + + await act(() => { + renderToPipeableStream( + + + + + + + Async Content + + + + + , + ).pipe(writable); + }); + + // The middle boundary should have been inlined (not outlined) so the + // middle fallback text should never appear in the streamed HTML. + expect(streamedContent).not.toContain('Middle Fallback'); + + // The stylesheet is in the head (blocks paint), and the innermost + // fallback is visible. + expect(getMeaningfulChildren(document)).toEqual( + + + + + Inner Fallback + , + ); + + // Resolve the async content — streams in without needing to load CSS + // since the stylesheet was already in the head. + await act(() => { + resolveText('content'); + }); + + expect(getMeaningfulChildren(document)).toEqual( + + + + + Async Content + , + ); + }); + + it('outlines a boundary with suspensey CSS when flushing a streamed completion', async () => { + // When a boundary completes via streaming (not as part of the shell), + // suspensey CSS should cause the boundary to be outlined. The parent + // content can show sooner while the CSS loads separately. + let streamedContent = ''; + writable.on('data', chunk => (streamedContent += chunk)); + + await act(() => { + renderToPipeableStream( + + + + + + + + + Async Content + + + + + + + , + ).pipe(writable); + }); + + // Shell is showing root fallback + expect(getMeaningfulChildren(document)).toEqual( + + + Root Fallback + , + ); + + // Unblock the shell — content streams in. The middle boundary should + // be outlined because the CSS arrived via streaming, not in the shell head. + streamedContent = ''; + await act(() => { + resolveText('shell'); + }); + + // The middle fallback should appear in the streamed HTML because the + // boundary was outlined. + expect(streamedContent).toContain('Middle Fallback'); + + // The CSS needs to load before the boundary reveals. Until then + // the middle fallback is visible. + expect(getMeaningfulChildren(document)).toEqual( + + + + + + {'Middle Fallback'} + + + , + ); + + // Load the stylesheet — now the middle boundary can reveal + await act(() => { + loadStylesheets(); + }); + assertLog(['load stylesheet: style.css']); + + expect(getMeaningfulChildren(document)).toEqual( + + + + + + {'Inner Fallback'} + + + , + ); + + // Resolve the async content + await act(() => { + resolveText('content'); + }); + + expect(getMeaningfulChildren(document)).toEqual( + + + + + + {'Async Content'} + + + , + ); + }); + + // @gate enableViewTransition + it('still outlines a boundary with a suspensey image inside a ViewTransition when flushing the shell', async () => { + // Unlike stylesheets (which block paint from the anyway), images + // inside ViewTransitions are outlined to enable animation reveals. This + // should happen even during the shell flush. + const ViewTransition = React.ViewTransition; + + let streamedContent = ''; + writable.on('data', chunk => (streamedContent += chunk)); + + await act(() => { + renderToPipeableStream( + + + + + + +
Content
+
+
+ + , + ).pipe(writable); + }); + + // The boundary should be outlined because the suspensey image motivates + // outlining for animation reveals, even during the shell flush. + expect(streamedContent).toContain('Image Fallback'); + }); }); diff --git a/packages/react-markup/src/ReactFizzConfigMarkup.js b/packages/react-markup/src/ReactFizzConfigMarkup.js index 7dbe5592f337..d12d72e69e02 100644 --- a/packages/react-markup/src/ReactFizzConfigMarkup.js +++ b/packages/react-markup/src/ReactFizzConfigMarkup.js @@ -242,7 +242,10 @@ export function writeCompletedRoot( return true; } -export function hasSuspenseyContent(hoistableState: HoistableState): boolean { +export function hasSuspenseyContent( + hoistableState: HoistableState, + flushingInShell: boolean, +): boolean { // Never outline. return false; } diff --git a/packages/react-native-renderer/src/ReactFiberConfigFabric.js b/packages/react-native-renderer/src/ReactFiberConfigFabric.js index f5a4361fd41d..4eeac5433777 100644 --- a/packages/react-native-renderer/src/ReactFiberConfigFabric.js +++ b/packages/react-native-renderer/src/ReactFiberConfigFabric.js @@ -40,7 +40,10 @@ import { type PublicTextInstance, type PublicRootInstance, } from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'; -import {enableFragmentRefsInstanceHandles} from 'shared/ReactFeatureFlags'; +import { + enableFragmentRefsInstanceHandles, + enableFragmentRefsTextNodes, +} from 'shared/ReactFeatureFlags'; const { createNode, @@ -275,6 +278,7 @@ export function getChildHostContext( const isInAParentText = type === 'AndroidTextInput' || // Android type === 'RCTMultilineTextInputView' || // iOS + type === 'RCTSelectableText' || type === 'RCTSinglelineTextInputView' || // iOS type === 'RCTText' || type === 'RCTVirtualText'; @@ -847,10 +851,15 @@ export function updateFragmentInstanceFiber( } export function commitNewChildToFragmentInstance( - childInstance: Instance, + childInstance: Instance | TextInstance, fragmentInstance: FragmentInstanceType, ): void { - const publicInstance = getPublicInstance(childInstance); + // Text nodes are not observable + if (enableFragmentRefsTextNodes && childInstance.canonical == null) { + return; + } + const instance: Instance = (childInstance: any); + const publicInstance = getPublicInstance(instance); if (fragmentInstance._observers !== null) { if (publicInstance == null) { throw new Error('Expected to find a host node. This is a bug in React.'); @@ -869,11 +878,16 @@ export function commitNewChildToFragmentInstance( } export function deleteChildFromFragmentInstance( - childInstance: Instance, + childInstance: Instance | TextInstance, fragmentInstance: FragmentInstanceType, ): void { + // Text nodes are not observable + if (enableFragmentRefsTextNodes && childInstance.canonical == null) { + return; + } + const instance: Instance = (childInstance: any); const publicInstance = ((getPublicInstance( - childInstance, + instance, ): any): PublicInstanceWithFragmentHandles); if (enableFragmentRefsInstanceHandles) { if (publicInstance.unstable_reactFragments != null) { diff --git a/packages/react-native-renderer/src/ReactFiberConfigNative.js b/packages/react-native-renderer/src/ReactFiberConfigNative.js index fcf356776c2e..404ae7a54a85 100644 --- a/packages/react-native-renderer/src/ReactFiberConfigNative.js +++ b/packages/react-native-renderer/src/ReactFiberConfigNative.js @@ -275,6 +275,7 @@ export function getChildHostContext( const isInAParentText = type === 'AndroidTextInput' || // Android type === 'RCTMultilineTextInputView' || // iOS + type === 'RCTSelectableText' || type === 'RCTSinglelineTextInputView' || // iOS type === 'RCTText' || type === 'RCTVirtualText'; diff --git a/packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js b/packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js index 42cc136a7202..925103f2d75a 100644 --- a/packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js +++ b/packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js @@ -1099,6 +1099,8 @@ describe('ReactFabric', () => { // Check for referential equality expect(ref1.current).toBe(event.target); expect(ref1.current).toBe(event.currentTarget); + + expect(global.event).toBe(event); }} onStartShouldSetResponder={() => true} /> @@ -1110,6 +1112,8 @@ describe('ReactFabric', () => { // Check for referential equality expect(ref2.current).toBe(event.target); expect(ref2.current).toBe(event.currentTarget); + + expect(global.event).toBe(event); }} onStartShouldSetResponder={() => true} /> @@ -1123,6 +1127,9 @@ describe('ReactFabric', () => { const [dispatchEvent] = nativeFabricUIManager.registerEventHandler.mock.calls[0]; + const preexistingEvent = {}; + global.event = preexistingEvent; + dispatchEvent(getViewById('one').instanceHandle, 'topTouchStart', { target: getViewById('one').reactTag, identifier: 17, @@ -1150,7 +1157,140 @@ describe('ReactFabric', () => { changedTouches: [], }); - expect.assertions(6); + expect(global.event).toBe(preexistingEvent); + + expect.assertions(9); + }); + + it('propagates timeStamps from native events and sets defaults', async () => { + const View = createReactNativeComponentClass('RCTView', () => ({ + validAttributes: { + id: true, + }, + uiViewClassName: 'RCTView', + directEventTypes: { + topTouchStart: { + registrationName: 'onTouchStart', + }, + topTouchEnd: { + registrationName: 'onTouchEnd', + }, + }, + })); + + function getViewById(id) { + const [reactTag, , , , instanceHandle] = + nativeFabricUIManager.createNode.mock.calls.find( + args => args[3] && args[3].id === id, + ); + + return {reactTag, instanceHandle}; + } + + const ref1 = React.createRef(); + const ref2 = React.createRef(); + + const explicitTimeStampCamelCase = 'explicit-timestamp-camelcase'; + const explicitTimeStampLowerCase = 'explicit-timestamp-lowercase'; + const performanceNowValue = 'performance-now-timestamp'; + + jest.spyOn(performance, 'now').mockReturnValue(performanceNowValue); + + await act(() => { + ReactFabric.render( + <> + { + expect(event.timeStamp).toBe(performanceNowValue); + }} + /> + { + expect(event.timeStamp).toBe(explicitTimeStampCamelCase); + }} + /> + { + expect(event.timeStamp).toBe(explicitTimeStampLowerCase); + }} + /> + , + 1, + null, + true, + ); + }); + + const [dispatchEvent] = + nativeFabricUIManager.registerEventHandler.mock.calls[0]; + + dispatchEvent(getViewById('default').instanceHandle, 'topTouchStart', { + target: getViewById('default').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + }); + dispatchEvent(getViewById('default').instanceHandle, 'topTouchEnd', { + target: getViewById('default').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + // No timeStamp property + }); + + dispatchEvent( + getViewById('explicitTimeStampCamelCase').instanceHandle, + 'topTouchStart', + { + target: getViewById('explicitTimeStampCamelCase').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + }, + ); + + dispatchEvent( + getViewById('explicitTimeStampCamelCase').instanceHandle, + 'topTouchEnd', + { + target: getViewById('explicitTimeStampCamelCase').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + timeStamp: explicitTimeStampCamelCase, + }, + ); + + dispatchEvent( + getViewById('explicitTimeStampLowerCase').instanceHandle, + 'topTouchStart', + { + target: getViewById('explicitTimeStampLowerCase').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + }, + ); + + dispatchEvent( + getViewById('explicitTimeStampLowerCase').instanceHandle, + 'topTouchEnd', + { + target: getViewById('explicitTimeStampLowerCase').reactTag, + identifier: 17, + touches: [], + changedTouches: [], + timestamp: explicitTimeStampLowerCase, + }, + ); + + expect.assertions(3); }); it('findHostInstance_DEPRECATED should warn if used to find a host component inside StrictMode', async () => { diff --git a/packages/react-native-renderer/src/legacy-events/EventPluginUtils.js b/packages/react-native-renderer/src/legacy-events/EventPluginUtils.js index 64a05cef33fa..2027ac2e20fc 100644 --- a/packages/react-native-renderer/src/legacy-events/EventPluginUtils.js +++ b/packages/react-native-renderer/src/legacy-events/EventPluginUtils.js @@ -67,6 +67,9 @@ function validateEventDispatches(event) { */ export function executeDispatch(event, listener, inst) { event.currentTarget = getNodeFromInstance(inst); + const currentEvent = global.event; + global.event = event; + try { listener(event); } catch (error) { @@ -77,6 +80,8 @@ export function executeDispatch(event, listener, inst) { // TODO: Make sure this error gets logged somehow. } } + + global.event = currentEvent; event.currentTarget = null; } diff --git a/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js b/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js index 723daa0dc9e5..b6a4bf4e7cbb 100644 --- a/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js +++ b/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js @@ -11,6 +11,21 @@ import assign from 'shared/assign'; const EVENT_POOL_SIZE = 10; +let currentTimeStamp = () => { + // Lazily define the function based on the existence of performance.now() + if ( + typeof performance === 'object' && + performance !== null && + typeof performance.now === 'function' + ) { + currentTimeStamp = () => performance.now(); + } else { + currentTimeStamp = () => Date.now(); + } + + return currentTimeStamp(); +}; + /** * @interface Event * @see http://www.w3.org/TR/DOM-Level-3-Events/ @@ -26,7 +41,7 @@ const EventInterface = { bubbles: null, cancelable: null, timeStamp: function (event) { - return event.timeStamp || Date.now(); + return event.timeStamp || event.timestamp || currentTimeStamp(); }, defaultPrevented: null, isTrusted: null, diff --git a/packages/react-noop-renderer/src/ReactNoopFlightClient.js b/packages/react-noop-renderer/src/ReactNoopFlightClient.js index a5c43bd65259..4699b149e85f 100644 --- a/packages/react-noop-renderer/src/ReactNoopFlightClient.js +++ b/packages/react-noop-renderer/src/ReactNoopFlightClient.js @@ -43,9 +43,6 @@ const {createResponse, createStreamState, processBinaryChunk, getRoot, close} = requireModule(idx: string) { return readModule(idx); }, - parseModel(response: Response, json) { - return JSON.parse(json, response._fromJSON); - }, bindToConsole(methodName, args, badgeName) { return Function.prototype.bind.apply( // eslint-disable-next-line react-internal/no-production-logging diff --git a/packages/react-noop-renderer/src/ReactNoopServer.js b/packages/react-noop-renderer/src/ReactNoopServer.js index 1793180cc765..913e72d7fc4f 100644 --- a/packages/react-noop-renderer/src/ReactNoopServer.js +++ b/packages/react-noop-renderer/src/ReactNoopServer.js @@ -324,7 +324,10 @@ const ReactNoopServer = ReactFizzServer({ writeHoistablesForBoundary() {}, writePostamble() {}, hoistHoistables(parent: HoistableState, child: HoistableState) {}, - hasSuspenseyContent(hoistableState: HoistableState): boolean { + hasSuspenseyContent( + hoistableState: HoistableState, + flushingInShell: boolean, + ): boolean { return false; }, createHoistableState(): HoistableState { diff --git a/packages/react-reconciler/src/ReactFiberBeginWork.js b/packages/react-reconciler/src/ReactFiberBeginWork.js index 48ced588730b..49a4c53c8941 100644 --- a/packages/react-reconciler/src/ReactFiberBeginWork.js +++ b/packages/react-reconciler/src/ReactFiberBeginWork.js @@ -3991,9 +3991,23 @@ function attemptEarlyBailoutIfNoScheduledUpdate( // whether to retry the primary children, or to skip over it and // go straight to the fallback. Check the priority of the primary // child fragment. + // + // Propagate context changes first. If a parent context changed + // and the primary children's consumer fibers were discarded + // during initial mount suspension, normal propagation can't find + // them. In that case we conservatively retry the boundary — the + // re-mounted children will read the updated context value. + const contextChanged = lazilyPropagateParentContextChanges( + current, + workInProgress, + renderLanes, + ); const primaryChildFragment: Fiber = (workInProgress.child: any); const primaryChildLanes = primaryChildFragment.childLanes; - if (includesSomeLane(renderLanes, primaryChildLanes)) { + if ( + contextChanged || + includesSomeLane(renderLanes, primaryChildLanes) + ) { // The primary children have pending work. Use the normal path // to attempt to render the primary children again. return updateSuspenseComponent(current, workInProgress, renderLanes); diff --git a/packages/react-reconciler/src/ReactFiberCommitHostEffects.js b/packages/react-reconciler/src/ReactFiberCommitHostEffects.js index 5c7ccf398787..3626561e1b49 100644 --- a/packages/react-reconciler/src/ReactFiberCommitHostEffects.js +++ b/packages/react-reconciler/src/ReactFiberCommitHostEffects.js @@ -64,7 +64,10 @@ import {captureCommitPhaseError} from './ReactFiberWorkLoop'; import {trackHostMutation} from './ReactFiberMutationTracking'; import {runWithFiberInDEV} from './ReactCurrentFiber'; -import {enableFragmentRefs} from 'shared/ReactFeatureFlags'; +import { + enableFragmentRefs, + enableFragmentRefsTextNodes, +} from 'shared/ReactFeatureFlags'; export function commitHostMount(finishedWork: Fiber) { const type = finishedWork.type; @@ -258,7 +261,8 @@ export function commitNewChildToFragmentInstances( parentFragmentInstances: null | Array, ): void { if ( - fiber.tag !== HostComponent || + (fiber.tag !== HostComponent && + !(enableFragmentRefsTextNodes && fiber.tag === HostText)) || // Only run fragment insertion effects for initial insertions fiber.alternate !== null || parentFragmentInstances === null diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.js b/packages/react-reconciler/src/ReactFiberCommitWork.js index 08882a04766c..322c858bb9c0 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.js @@ -48,7 +48,6 @@ import { alwaysThrottleRetries, enableCreateEventHandleAPI, enableEffectEventMutationPhase, - enableHiddenSubtreeInsertionEffectCleanup, enableProfilerTimer, enableProfilerCommitHooks, enableSuspenseCallback, @@ -62,6 +61,7 @@ import { enableFragmentRefs, enableEagerAlternateStateNodeCleanup, enableDefaultTransitionIndicator, + enableFragmentRefsTextNodes, } from 'shared/ReactFeatureFlags'; import { FunctionComponent, @@ -1533,7 +1533,11 @@ function commitDeletionEffectsOnFiber( if (!offscreenSubtreeWasHidden) { safelyDetachRef(deletedFiber, nearestMountedAncestor); } - if (enableFragmentRefs && deletedFiber.tag === HostComponent) { + if ( + enableFragmentRefs && + (deletedFiber.tag === HostComponent || + (enableFragmentRefsTextNodes && deletedFiber.tag === HostText)) + ) { commitFragmentInstanceDeletionEffects(deletedFiber); } // Intentional fallthrough to next branch @@ -1658,17 +1662,12 @@ function commitDeletionEffectsOnFiber( case ForwardRef: case MemoComponent: case SimpleMemoComponent: { - if ( - enableHiddenSubtreeInsertionEffectCleanup || - !offscreenSubtreeWasHidden - ) { - // TODO: Use a commitHookInsertionUnmountEffects wrapper to record timings. - commitHookEffectListUnmount( - HookInsertion, - deletedFiber, - nearestMountedAncestor, - ); - } + // TODO: Use a commitHookInsertionUnmountEffects wrapper to record timings. + commitHookEffectListUnmount( + HookInsertion, + deletedFiber, + nearestMountedAncestor, + ); if (!offscreenSubtreeWasHidden) { commitHookLayoutUnmountEffects( deletedFiber, @@ -3028,7 +3027,11 @@ export function disappearLayoutEffects(finishedWork: Fiber) { // TODO (Offscreen) Check: flags & RefStatic safelyDetachRef(finishedWork, finishedWork.return); - if (enableFragmentRefs && finishedWork.tag === HostComponent) { + if ( + enableFragmentRefs && + (finishedWork.tag === HostComponent || + (enableFragmentRefsTextNodes && finishedWork.tag === HostText)) + ) { commitFragmentInstanceDeletionEffects(finishedWork); } diff --git a/packages/react-reconciler/src/ReactFiberNewContext.js b/packages/react-reconciler/src/ReactFiberNewContext.js index 02792d863afb..193cf74d3bec 100644 --- a/packages/react-reconciler/src/ReactFiberNewContext.js +++ b/packages/react-reconciler/src/ReactFiberNewContext.js @@ -20,7 +20,11 @@ import type {Hook} from './ReactFiberHooks'; import {isPrimaryRenderer, HostTransitionContext} from './ReactFiberConfig'; import {createCursor, push, pop} from './ReactFiberStack'; -import {ContextProvider, DehydratedFragment} from './ReactWorkTags'; +import { + ContextProvider, + DehydratedFragment, + SuspenseComponent, +} from './ReactWorkTags'; import {NoLanes, isSubsetOfLanes, mergeLanes} from './ReactFiberLane'; import { NoFlags, @@ -295,6 +299,37 @@ function propagateContextChanges( workInProgress, ); nextFiber = null; + } else if ( + fiber.tag === SuspenseComponent && + fiber.memoizedState !== null && + fiber.memoizedState.dehydrated === null + ) { + // This is a client-rendered Suspense boundary that is currently + // showing its fallback. The primary children may include context + // consumers, but their fibers may not exist in the tree — during + // initial mount, if the primary children suspended, their fibers + // were discarded since there was no current tree to preserve them. + // We can't walk into the primary tree to find consumers, so + // conservatively mark the Suspense boundary itself for retry. + // When it re-renders, it will re-mount the primary children, + // which will read the updated context value. + fiber.lanes = mergeLanes(fiber.lanes, renderLanes); + const alternate = fiber.alternate; + if (alternate !== null) { + alternate.lanes = mergeLanes(alternate.lanes, renderLanes); + } + scheduleContextWorkOnParentPath( + fiber.return, + renderLanes, + workInProgress, + ); + if (!forcePropagateEntireTree) { + // During lazy propagation, we can defer propagating changes to + // the children, same as the consumer match above. + nextFiber = null; + } else { + nextFiber = fiber.child; + } } else { // Traverse down. nextFiber = fiber.child; @@ -331,9 +366,9 @@ export function lazilyPropagateParentContextChanges( current: Fiber, workInProgress: Fiber, renderLanes: Lanes, -) { +): boolean { const forcePropagateEntireTree = false; - propagateParentContextChanges( + return propagateParentContextChanges( current, workInProgress, renderLanes, @@ -364,7 +399,7 @@ function propagateParentContextChanges( workInProgress: Fiber, renderLanes: Lanes, forcePropagateEntireTree: boolean, -) { +): boolean { // Collect all the parent providers that changed. Since this is usually small // number, we use an Array instead of Set. let contexts = null; @@ -460,6 +495,7 @@ function propagateParentContextChanges( // then we could remove both `DidPropagateContext` and `NeedsPropagation`. // Consider this as part of the next refactor to the fiber tree structure. workInProgress.flags |= DidPropagateContext; + return contexts !== null; } export function checkIfContextChanged( diff --git a/packages/react-reconciler/src/__tests__/ReactContextPropagation-test.js b/packages/react-reconciler/src/__tests__/ReactContextPropagation-test.js index 0a338c8aa871..8fe57a2e50de 100644 --- a/packages/react-reconciler/src/__tests__/ReactContextPropagation-test.js +++ b/packages/react-reconciler/src/__tests__/ReactContextPropagation-test.js @@ -2,6 +2,7 @@ let React; let ReactNoop; let Scheduler; let act; +let use; let useState; let useContext; let Suspense; @@ -19,6 +20,7 @@ describe('ReactLazyContextPropagation', () => { ReactNoop = require('react-noop-renderer'); Scheduler = require('scheduler'); act = require('internal-test-utils').act; + use = React.use; useState = React.useState; useContext = React.useContext; Suspense = React.Suspense; @@ -937,4 +939,102 @@ describe('ReactLazyContextPropagation', () => { assertLog(['B', 'B']); expect(root).toMatchRenderedOutput('BB'); }); + + it('regression: context change triggers retry of suspended Suspense boundary on initial mount', async () => { + // Regression test for a bug where a context change above a suspended + // Suspense boundary would fail to trigger a retry. When a Suspense + // boundary suspends during initial mount, the primary children's fibers + // are discarded because there is no current tree to preserve them. If + // the suspended promise never resolves, the only way to retry is + // something external — like a context change. Context propagation must + // mark suspended Suspense boundaries for retry even though the consumer + // fibers no longer exist in the tree. + // + // The Provider component owns the state update. The children are + // passed in from above, so they are not re-created when the Provider + // re-renders — this means the Suspense boundary bails out, exercising + // the lazy context propagation path where the bug manifests. + const Context = React.createContext(null); + const neverResolvingPromise = new Promise(() => {}); + const resolvedThenable = {status: 'fulfilled', value: 'Result', then() {}}; + + function Consumer() { + return ; + } + + let setPromise; + function Provider({children}) { + const [promise, _setPromise] = useState(neverResolvingPromise); + setPromise = _setPromise; + return {children}; + } + + const root = ReactNoop.createRoot(); + await act(() => { + root.render( + + }> + + + , + ); + }); + assertLog(['Loading']); + expect(root).toMatchRenderedOutput('Loading'); + + await act(() => { + setPromise(resolvedThenable); + }); + assertLog(['Result']); + expect(root).toMatchRenderedOutput('Result'); + }); + + it('regression: context change triggers retry of suspended Suspense boundary on initial mount (nested)', async () => { + // Same as above, but with an additional indirection component between + // the provider and the Suspense boundary. This exercises the + // propagateContextChanges walker path rather than the + // propagateParentContextChanges path. + const Context = React.createContext(null); + const neverResolvingPromise = new Promise(() => {}); + const resolvedThenable = {status: 'fulfilled', value: 'Result', then() {}}; + + function Consumer() { + return ; + } + + function Indirection({children}) { + Scheduler.log('Indirection'); + return children; + } + + let setPromise; + function Provider({children}) { + const [promise, _setPromise] = useState(neverResolvingPromise); + setPromise = _setPromise; + return {children}; + } + + const root = ReactNoop.createRoot(); + await act(() => { + root.render( + + + }> + + + + , + ); + }); + assertLog(['Indirection', 'Loading']); + expect(root).toMatchRenderedOutput('Loading'); + + // Indirection should not re-render — only the Suspense boundary + // should be retried. + await act(() => { + setPromise(resolvedThenable); + }); + assertLog(['Result']); + expect(root).toMatchRenderedOutput('Result'); + }); }); diff --git a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js index d82b6e31b4c8..c45db0096517 100644 --- a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js +++ b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js @@ -3070,14 +3070,10 @@ describe('ReactHooksWithNoopRenderer', () => { await act(() => { root.render(); }); - assertConsoleErrorDev( - gate('enableHiddenSubtreeInsertionEffectCleanup') - ? [ - 'useInsertionEffect must not schedule updates.\n' + - ' in App (at **)', - ] - : [], - ); + assertConsoleErrorDev([ + 'useInsertionEffect must not schedule updates.\n' + + ' in App (at **)', + ]); // Should not warn for regular effects after throw. function NotInsertion() { diff --git a/packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.js b/packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.js index 3dde9c75bf03..f759fc60ef35 100644 --- a/packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.js +++ b/packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.js @@ -213,6 +213,67 @@ describe('ReactIncrementalErrorLogging', () => { }).toThrow('logCapturedError error'); }); + it('does not report internal Offscreen component for errors thrown during reconciliation inside Suspense', async () => { + // When a child of Suspense throws during reconciliation (not render), + // a Throw fiber is created whose .return is the internal Offscreen fiber. + // We should skip Offscreen since it's an internal + // implementation detail and walk up to Suspense instead. + const lazyChild = React.lazy(() => { + throw new Error('lazy init error'); + }); + + await fakeAct(() => { + ReactNoop.render( + }>{lazyChild}, + ); + }); + expect(uncaughtExceptionMock).toHaveBeenCalledTimes(1); + expect(uncaughtExceptionMock).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'lazy init error', + }), + ); + if (__DEV__) { + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn.mock.calls[0]).toEqual([ + '%s\n\n%s\n', + 'An error occurred in the component.', + 'Consider adding an error boundary to your tree to customize error handling behavior.\n' + + 'Visit https://react.dev/link/error-boundaries to learn more about error boundaries.', + ]); + } + }); + + it('does not report internal Offscreen component for errors thrown during reconciliation inside Activity', async () => { + // Same as the Suspense test above — Activity also wraps its children in + // an internal Offscreen fiber. The error message should show Activity, + // not Offscreen. + const lazyChild = React.lazy(() => { + throw new Error('lazy init error'); + }); + + await fakeAct(() => { + ReactNoop.render( + {lazyChild}, + ); + }); + expect(uncaughtExceptionMock).toHaveBeenCalledTimes(1); + expect(uncaughtExceptionMock).toHaveBeenCalledWith( + expect.objectContaining({ + message: 'lazy init error', + }), + ); + if (__DEV__) { + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn.mock.calls[0]).toEqual([ + '%s\n\n%s\n', + 'An error occurred in the component.', + 'Consider adding an error boundary to your tree to customize error handling behavior.\n' + + 'Visit https://react.dev/link/error-boundaries to learn more about error boundaries.', + ]); + } + }); + it('resets instance variables before unmounting failed node', async () => { class ErrorBoundary extends React.Component { state = {error: null}; diff --git a/packages/react-reconciler/src/__tests__/ReactSuspenseEffectsSemantics-test.js b/packages/react-reconciler/src/__tests__/ReactSuspenseEffectsSemantics-test.js index eeb00ac98502..e65c02d89e4d 100644 --- a/packages/react-reconciler/src/__tests__/ReactSuspenseEffectsSemantics-test.js +++ b/packages/react-reconciler/src/__tests__/ReactSuspenseEffectsSemantics-test.js @@ -1610,9 +1610,7 @@ describe('ReactSuspenseEffectsSemantics', () => { 'AsyncText:InnerAsync_1 render', 'Text:OuterFallback destroy insertion', 'Text:OuterFallback destroy layout', - ...(gate(flags => flags.enableHiddenSubtreeInsertionEffectCleanup) - ? ['Text:InnerFallback destroy insertion'] - : []), + 'Text:InnerFallback destroy insertion', 'Text:Outer create layout', 'AsyncText:OuterAsync_1 create layout', 'Text:Inner create layout', @@ -1757,9 +1755,7 @@ describe('ReactSuspenseEffectsSemantics', () => { assertLog([ 'Text:Inside render', 'AsyncText:OutsideAsync render', - ...(gate(flags => flags.enableHiddenSubtreeInsertionEffectCleanup) - ? ['Text:Fallback:Inside destroy insertion'] - : []), + 'Text:Fallback:Inside destroy insertion', 'Text:Fallback:Fallback destroy insertion', 'Text:Fallback:Fallback destroy layout', 'Text:Fallback:Outside destroy insertion', @@ -2248,9 +2244,7 @@ describe('ReactSuspenseEffectsSemantics', () => { // Destroy layout and passive effects in the errored tree. 'App destroy layout', - ...(gate(flags => flags.enableHiddenSubtreeInsertionEffectCleanup) - ? ['Text:Inside destroy insertion'] - : []), + 'Text:Inside destroy insertion', 'Text:Fallback destroy insertion', 'Text:Fallback destroy layout', 'Text:Outside destroy insertion', @@ -2511,9 +2505,7 @@ describe('ReactSuspenseEffectsSemantics', () => { // Destroy layout and passive effects in the errored tree. 'App destroy layout', - ...(gate(flags => flags.enableHiddenSubtreeInsertionEffectCleanup) - ? ['Text:Inside destroy insertion'] - : []), + 'Text:Inside destroy insertion', 'Text:Fallback destroy insertion', 'Text:Fallback destroy layout', 'Text:Outside destroy insertion', diff --git a/packages/react-reconciler/src/__tests__/useEffectEvent-test.js b/packages/react-reconciler/src/__tests__/useEffectEvent-test.js index b120adf61159..1018b4975ad1 100644 --- a/packages/react-reconciler/src/__tests__/useEffectEvent-test.js +++ b/packages/react-reconciler/src/__tests__/useEffectEvent-test.js @@ -938,14 +938,9 @@ describe('useEffectEvent', () => { assertLog([ 'Parent Insertion Create: A 3 2', 'Parent Layout Cleanup: A 3 2', - ...(gate('enableHiddenSubtreeInsertionEffectCleanup') - ? [ - gate('enableViewTransition') && - !gate('enableEffectEventMutationPhase') - ? 'Child Insertion Destroy A 3 2 B 1 1 1' - : 'Child Insertion Destroy A 3 2 B 3 2 2', - ] - : []), + gate('enableViewTransition') && !gate('enableEffectEventMutationPhase') + ? 'Child Insertion Destroy A 3 2 B 1 1 1' + : 'Child Insertion Destroy A 3 2 B 3 2 2', ]); }); diff --git a/packages/react-reconciler/src/getComponentNameFromFiber.js b/packages/react-reconciler/src/getComponentNameFromFiber.js index 97124bbf5ba5..8719539cfc08 100644 --- a/packages/react-reconciler/src/getComponentNameFromFiber.js +++ b/packages/react-reconciler/src/getComponentNameFromFiber.js @@ -122,7 +122,10 @@ export default function getComponentNameFromFiber(fiber: Fiber): string | null { } return 'Mode'; case OffscreenComponent: - return 'Offscreen'; + if (fiber.return !== null) { + return getComponentNameFromFiber(fiber.return); + } + return null; case Profiler: return 'Profiler'; case ScopeComponent: diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js index d06d967b1f87..989f9184637d 100644 --- a/packages/react-server/src/ReactFizzServer.js +++ b/packages/react-server/src/ReactFizzServer.js @@ -479,7 +479,7 @@ function isEligibleForOutlining( // outlining. return ( (boundary.byteSize > 500 || - hasSuspenseyContent(boundary.contentState) || + hasSuspenseyContent(boundary.contentState, /* flushingInShell */ false) || boundary.defer) && // For boundaries that can possibly contribute to the preamble we don't want to outline // them regardless of their size since the fallbacks should only be emitted if we've @@ -5593,7 +5593,7 @@ function flushSegment( !flushingPartialBoundaries && isEligibleForOutlining(request, boundary) && (flushedByteSize + boundary.byteSize > request.progressiveChunkSize || - hasSuspenseyContent(boundary.contentState) || + hasSuspenseyContent(boundary.contentState, flushingShell) || boundary.defer) ) { // Inlining this boundary would make the current sequence being written too large @@ -5826,6 +5826,7 @@ function flushPartiallyCompletedSegment( } let flushingPartialBoundaries = false; +let flushingShell = false; function flushCompletedQueues( request: Request, @@ -5885,7 +5886,9 @@ function flushCompletedQueues( completedPreambleSegments, skipBlockingShell, ); + flushingShell = true; flushSegment(request, destination, completedRootSegment, null); + flushingShell = false; request.completedRootSegment = null; const isComplete = request.allPendingTasks === 0 && diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index 3bafdcf40bc9..4c50f6a7d20a 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -467,14 +467,6 @@ function getCurrentStackInDEV(): string { const ObjectPrototype = Object.prototype; -type JSONValue = - | string - | boolean - | number - | null - | {+[key: string]: JSONValue} - | $ReadOnlyArray; - const stringify = JSON.stringify; type ReactJSONValue = @@ -498,6 +490,7 @@ export type ReactClientValue = | React$Element | React$Element & any> | ReactComponentInfo + | ReactErrorInfo | string | boolean | number @@ -4171,6 +4164,11 @@ function serializeErrorValue(request: Request, error: Error): string { stack = []; } const errorInfo: ReactErrorInfoDev = {name, message, stack, env}; + if ('cause' in error) { + const cause: ReactClientValue = (error.cause: any); + const causeId = outlineModel(request, cause); + errorInfo.cause = serializeByValueID(causeId); + } const id = outlineModel(request, errorInfo); return '$Z' + id.toString(16); } else { @@ -4181,7 +4179,11 @@ function serializeErrorValue(request: Request, error: Error): string { } } -function serializeDebugErrorValue(request: Request, error: Error): string { +function serializeDebugErrorValue( + request: Request, + counter: {objectLimit: number}, + error: Error, +): string { if (__DEV__) { let name: string = 'Error'; let message: string; @@ -4203,6 +4205,12 @@ function serializeDebugErrorValue(request: Request, error: Error): string { stack = []; } const errorInfo: ReactErrorInfoDev = {name, message, stack, env}; + if ('cause' in error) { + counter.objectLimit--; + const cause: ReactClientValue = (error.cause: any); + const causeId = outlineDebugModel(request, counter, cause); + errorInfo.cause = serializeByValueID(causeId); + } const id = outlineDebugModel( request, {objectLimit: stack.length * 2 + 1}, @@ -4231,6 +4239,7 @@ function emitErrorChunk( let message: string; let stack: ReactStackTrace; let env = (0, request.environmentName)(); + let causeReference: null | string = null; try { if (error instanceof Error) { name = error.name; @@ -4243,6 +4252,13 @@ function emitErrorChunk( // Keep the environment name. env = errorEnv; } + if ('cause' in error) { + const cause: ReactClientValue = (error.cause: any); + const causeId = debug + ? outlineDebugModel(request, {objectLimit: 5}, cause) + : outlineModel(request, cause); + causeReference = serializeByValueID(causeId); + } } else if (typeof error === 'object' && error !== null) { message = describeObjectForErrorMessage(error); stack = []; @@ -4258,6 +4274,9 @@ function emitErrorChunk( const ownerRef = owner == null ? null : outlineComponentInfo(request, owner); errorInfo = {digest, name, message, stack, env, owner: ownerRef}; + if (causeReference !== null) { + (errorInfo: ReactErrorInfoDev).cause = causeReference; + } } else { errorInfo = {digest}; } @@ -4969,7 +4988,7 @@ function renderDebugModel( return serializeDebugFormData(request, value); } if (value instanceof Error) { - return serializeDebugErrorValue(request, value); + return serializeDebugErrorValue(request, counter, value); } if (value instanceof ArrayBuffer) { return serializeDebugTypedArray(request, 'A', new Uint8Array(value)); diff --git a/packages/react-server/src/__tests__/ReactFlightAsyncDebugInfo-test.js b/packages/react-server/src/__tests__/ReactFlightAsyncDebugInfo-test.js index 336d797efb61..5107fc0bfaea 100644 --- a/packages/react-server/src/__tests__/ReactFlightAsyncDebugInfo-test.js +++ b/packages/react-server/src/__tests__/ReactFlightAsyncDebugInfo-test.js @@ -3633,4 +3633,40 @@ describe('ReactFlightAsyncDebugInfo', () => { `); } }); + + it('should not exponentially accumulate debug info on outlined debug chunks', async () => { + // Regression test: Each Level wraps its received `context` prop in a new + // object before passing it down. This creates props deduplication + // references to the parent's outlined chunk alongside the owner reference, + // giving 2 references per level to the direct parent's chunk. Without + // skipping transferReferencedDebugInfo during debug info resolution, this + // test would fail with an infinite loop detection error. + async function Level({depth, context}) { + await delay(0); + if (depth === 0) { + return
Hello, World!
; + } + const newContext = {prev: context, id: depth}; + return ReactServer.createElement(Level, { + depth: depth - 1, + context: newContext, + }); + } + + const stream = ReactServerDOMServer.renderToPipeableStream( + ReactServer.createElement(Level, {depth: 20, context: {root: true}}), + ); + + const readable = new Stream.PassThrough(streamOptions); + const result = ReactServerDOMClient.createFromNodeStream(readable, { + moduleMap: {}, + moduleLoading: {}, + }); + stream.pipe(readable); + + const resolved = await result; + expect(resolved.type).toBe('div'); + + await finishLoadingStream(readable); + }); }); diff --git a/packages/shared/ReactFeatureFlags.js b/packages/shared/ReactFeatureFlags.js index be13ab9e5dea..ee5f22ab9588 100644 --- a/packages/shared/ReactFeatureFlags.js +++ b/packages/shared/ReactFeatureFlags.js @@ -162,11 +162,6 @@ export const enableInternalInstanceMap: boolean = false; // const __NEXT_MAJOR__ = __EXPERIMENTAL__; -/** - * Enables a fix to run insertion effect cleanup on hidden subtrees. - */ -export const enableHiddenSubtreeInsertionEffectCleanup: boolean = true; - /** * Removes legacy style context defined using static `contextTypes` and consumed with static `childContextTypes`. */ @@ -208,7 +203,7 @@ export const disableLegacyMode: boolean = true; // in open source, but www codebase still relies on it. Need to remove. export const disableCommentsAsDOMContainers: boolean = true; -export const enableTrustedTypesIntegration: boolean = false; +export const enableTrustedTypesIntegration: boolean = true; // Prevent the value and checked attributes from syncing with their related // DOM properties diff --git a/packages/shared/ReactTypes.js b/packages/shared/ReactTypes.js index e58c36f0a0cb..c8658278a5bf 100644 --- a/packages/shared/ReactTypes.js +++ b/packages/shared/ReactTypes.js @@ -228,6 +228,14 @@ export type ReactErrorInfoProd = { +digest: string, }; +export type JSONValue = + | string + | boolean + | number + | null + | {+[key: string]: JSONValue} + | $ReadOnlyArray; + export type ReactErrorInfoDev = { +digest?: string, +name: string, @@ -235,6 +243,7 @@ export type ReactErrorInfoDev = { +stack: ReactStackTrace, +env: string, +owner?: null | string, + cause?: JSONValue, }; export type ReactErrorInfo = ReactErrorInfoProd | ReactErrorInfoDev; diff --git a/packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js b/packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js index f8da141560f5..40bb6f53c743 100644 --- a/packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js +++ b/packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js @@ -19,7 +19,6 @@ export const alwaysThrottleRetries = __VARIANT__; export const enableObjectFiber = __VARIANT__; -export const enableHiddenSubtreeInsertionEffectCleanup = __VARIANT__; export const enableEagerAlternateStateNodeCleanup = __VARIANT__; export const passChildrenWhenCloningPersistedNodes = __VARIANT__; export const enableFragmentRefs = __VARIANT__; diff --git a/packages/shared/forks/ReactFeatureFlags.native-fb.js b/packages/shared/forks/ReactFeatureFlags.native-fb.js index a587e425703b..bbb13a6eb174 100644 --- a/packages/shared/forks/ReactFeatureFlags.native-fb.js +++ b/packages/shared/forks/ReactFeatureFlags.native-fb.js @@ -21,7 +21,6 @@ const dynamicFlags: DynamicExportsType = (dynamicFlagsUntyped: any); export const { alwaysThrottleRetries, enableEffectEventMutationPhase, - enableHiddenSubtreeInsertionEffectCleanup, enableObjectFiber, enableEagerAlternateStateNodeCleanup, passChildrenWhenCloningPersistedNodes, @@ -62,7 +61,7 @@ export const enableSuspenseAvoidThisFallback: boolean = false; export const enableSuspenseCallback: boolean = true; export const enableTaint: boolean = true; export const enableTransitionTracing: boolean = false; -export const enableTrustedTypesIntegration: boolean = false; +export const enableTrustedTypesIntegration: boolean = true; export const enableUpdaterTracking: boolean = __PROFILE__; export const retryLaneExpirationMs = 5000; export const syncLaneExpirationMs = 250; diff --git a/packages/shared/forks/ReactFeatureFlags.native-oss.js b/packages/shared/forks/ReactFeatureFlags.native-oss.js index 72904e251cae..6b0d93447966 100644 --- a/packages/shared/forks/ReactFeatureFlags.native-oss.js +++ b/packages/shared/forks/ReactFeatureFlags.native-oss.js @@ -30,7 +30,6 @@ export const enableCPUSuspense: boolean = false; export const enableCreateEventHandleAPI: boolean = false; export const enableMoveBefore: boolean = true; export const enableFizzExternalRuntime: boolean = true; -export const enableHiddenSubtreeInsertionEffectCleanup: boolean = false; export const enableInfiniteRenderLoopDetection: boolean = false; export const enableLegacyCache: boolean = false; export const enableLegacyFBSupport: boolean = false; @@ -50,7 +49,7 @@ export const enableSuspenseAvoidThisFallback: boolean = false; export const enableSuspenseCallback: boolean = false; export const enableTaint: boolean = true; export const enableTransitionTracing: boolean = false; -export const enableTrustedTypesIntegration: boolean = false; +export const enableTrustedTypesIntegration: boolean = true; export const passChildrenWhenCloningPersistedNodes: boolean = false; export const retryLaneExpirationMs = 5000; export const syncLaneExpirationMs = 250; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.js index afa71d5754c6..954d9d88eaf5 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.js @@ -26,14 +26,13 @@ export const disableInputAttributeSyncing: boolean = false; export const enableScopeAPI: boolean = false; export const enableCreateEventHandleAPI: boolean = false; export const enableSuspenseCallback: boolean = false; -export const enableTrustedTypesIntegration: boolean = false; +export const enableTrustedTypesIntegration: boolean = true; export const disableTextareaChildren: boolean = false; export const enableSuspenseAvoidThisFallback: boolean = false; export const enableCPUSuspense: boolean = false; export const enableNoCloningMemoCache: boolean = false; export const enableLegacyFBSupport: boolean = false; export const enableMoveBefore: boolean = false; -export const enableHiddenSubtreeInsertionEffectCleanup: boolean = false; export const enableRetryLaneExpiration: boolean = false; export const retryLaneExpirationMs = 5000; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js index a81f67ee94fe..6bc80d2b8e30 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js @@ -26,7 +26,6 @@ export const enableCreateEventHandleAPI = false; export const enableMoveBefore = false; export const enableFizzExternalRuntime = true; export const enableInfiniteRenderLoopDetection = false; -export const enableHiddenSubtreeInsertionEffectCleanup = true; export const enableLegacyCache = false; export const enableLegacyFBSupport = false; export const enableLegacyHidden = false; @@ -47,7 +46,7 @@ export const enableSuspenseAvoidThisFallback = false; export const enableSuspenseCallback = false; export const enableTaint = true; export const enableTransitionTracing = false; -export const enableTrustedTypesIntegration = false; +export const enableTrustedTypesIntegration = true; export const enableUpdaterTracking = false; export const passChildrenWhenCloningPersistedNodes = false; export const retryLaneExpirationMs = 5000; diff --git a/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js b/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js index d819bd275eb5..91dc33b28f35 100644 --- a/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js +++ b/packages/shared/forks/ReactFeatureFlags.test-renderer.www.js @@ -28,14 +28,13 @@ export const enableCreateEventHandleAPI: boolean = false; export const enableSuspenseCallback: boolean = true; export const disableLegacyContext: boolean = false; export const disableLegacyContextForFunctionComponents: boolean = false; -export const enableTrustedTypesIntegration: boolean = false; +export const enableTrustedTypesIntegration: boolean = true; export const disableTextareaChildren: boolean = false; export const enableSuspenseAvoidThisFallback: boolean = true; export const enableCPUSuspense: boolean = false; export const enableNoCloningMemoCache: boolean = false; export const enableLegacyFBSupport: boolean = false; export const enableMoveBefore: boolean = false; -export const enableHiddenSubtreeInsertionEffectCleanup: boolean = true; export const enableRetryLaneExpiration: boolean = false; export const retryLaneExpirationMs = 5000; diff --git a/packages/shared/forks/ReactFeatureFlags.www-dynamic.js b/packages/shared/forks/ReactFeatureFlags.www-dynamic.js index ea0751e87dac..1646c834ef41 100644 --- a/packages/shared/forks/ReactFeatureFlags.www-dynamic.js +++ b/packages/shared/forks/ReactFeatureFlags.www-dynamic.js @@ -16,7 +16,6 @@ export const alwaysThrottleRetries: boolean = __VARIANT__; export const disableLegacyContextForFunctionComponents: boolean = __VARIANT__; export const disableSchedulerTimeoutInWorkLoop: boolean = __VARIANT__; -export const enableHiddenSubtreeInsertionEffectCleanup: boolean = __VARIANT__; export const enableNoCloningMemoCache: boolean = __VARIANT__; export const enableObjectFiber: boolean = __VARIANT__; export const enableRetryLaneExpiration: boolean = __VARIANT__; @@ -36,7 +35,6 @@ export const enableFragmentRefs: boolean = __VARIANT__; export const enableFragmentRefsScrollIntoView: boolean = __VARIANT__; export const enableFragmentRefsTextNodes: boolean = __VARIANT__; export const enableInternalInstanceMap: boolean = __VARIANT__; -export const enableTrustedTypesIntegration: boolean = __VARIANT__; export const enableParallelTransitions: boolean = __VARIANT__; export const enableEffectEventMutationPhase: boolean = __VARIANT__; diff --git a/packages/shared/forks/ReactFeatureFlags.www.js b/packages/shared/forks/ReactFeatureFlags.www.js index 0c8ec7f25aa4..76e3909ccafc 100644 --- a/packages/shared/forks/ReactFeatureFlags.www.js +++ b/packages/shared/forks/ReactFeatureFlags.www.js @@ -19,13 +19,11 @@ export const { disableLegacyContextForFunctionComponents, disableSchedulerTimeoutInWorkLoop, enableEffectEventMutationPhase, - enableHiddenSubtreeInsertionEffectCleanup, enableInfiniteRenderLoopDetection, enableNoCloningMemoCache, enableObjectFiber, enableRetryLaneExpiration, enableTransitionTracing, - enableTrustedTypesIntegration, retryLaneExpirationMs, syncLaneExpirationMs, transitionLaneExpirationMs, @@ -45,7 +43,7 @@ export const enableProfilerTimer = __PROFILE__; export const enableProfilerCommitHooks = __PROFILE__; export const enableProfilerNestedUpdatePhase = __PROFILE__; export const enableUpdaterTracking = __PROFILE__; - +export const enableTrustedTypesIntegration: boolean = true; export const enableSuspenseAvoidThisFallback: boolean = true; export const enableAsyncDebugInfo: boolean = true; diff --git a/scripts/flow/react-devtools.js b/scripts/flow/react-devtools.js index 09a251bbe2f5..4e0f2a915ede 100644 --- a/scripts/flow/react-devtools.js +++ b/scripts/flow/react-devtools.js @@ -16,6 +16,5 @@ declare const __IS_FIREFOX__: boolean; declare const __IS_CHROME__: boolean; declare const __IS_EDGE__: boolean; declare const __IS_NATIVE__: boolean; -declare const __IS_INTERNAL_MCP_BUILD__: boolean; declare const chrome: any; diff --git a/scripts/jest/devtools/setupEnv.js b/scripts/jest/devtools/setupEnv.js index 32bf13e686c7..a797c0951435 100644 --- a/scripts/jest/devtools/setupEnv.js +++ b/scripts/jest/devtools/setupEnv.js @@ -15,7 +15,6 @@ global.__IS_FIREFOX__ = false; global.__IS_CHROME__ = false; global.__IS_EDGE__ = false; global.__IS_NATIVE__ = false; -global.__IS_INTERNAL_MCP_BUILD__ = false; const ReactVersionTestingAgainst = process.env.REACT_VERSION || ReactVersion;