Skip to content

Commit

Permalink
Merge branch 'main' into reactnativetypes-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
huntie authored Dec 10, 2024
2 parents 571b688 + 372ec00 commit db83038
Show file tree
Hide file tree
Showing 85 changed files with 2,861 additions and 547 deletions.
272 changes: 272 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ export type PluginOptions = {
target: CompilerReactTarget;
};

const CompilerReactTargetSchema = z.enum(['17', '18', '19']);
const CompilerReactTargetSchema = z.union([
z.literal('17'),
z.literal('18'),
z.literal('19'),
/**
* Used exclusively for Meta apps which are guaranteed to have compatible
* react runtime and compiler versions. Note that only the FB-internal bundles
* re-export useMemoCache (see
* https://github.com/facebook/react/blob/5b0ef217ef32333a8e56f39be04327c89efa346f/packages/react/index.fb.js#L68-L70),
* so this option is invalid / creates runtime errors for open-source users.
*/
z.object({
kind: z.literal('donotuse_meta_internal'),
runtimeModule: z.string().default('react'),
}),
]);
export type CompilerReactTarget = z.infer<typeof CompilerReactTargetSchema>;

const CompilationModeSchema = z.enum([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
inferReactivePlaces,
inferReferenceEffects,
inlineImmediatelyInvokedFunctionExpressions,
inferEffectDependencies,
} from '../Inference';
import {
constantPropagation,
Expand Down Expand Up @@ -354,6 +355,10 @@ function* runWithEnvironment(
value: hir,
});

if (env.config.inferEffectDependencies) {
inferEffectDependencies(hir);
}

if (env.config.inlineJsxTransform) {
inlineJsxTransform(hir, env.config.inlineJsxTransform);
yield log({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,30 +1123,23 @@ function checkFunctionReferencedBeforeDeclarationAtTopLevel(
return errors.details.length > 0 ? errors : null;
}

type ReactCompilerRuntimeModule =
| 'react/compiler-runtime' // from react namespace
| 'react-compiler-runtime'; // npm package
function getReactCompilerRuntimeModule(
opts: PluginOptions,
): ReactCompilerRuntimeModule {
let moduleName: ReactCompilerRuntimeModule | null = null;
switch (opts.target) {
case '17':
case '18': {
moduleName = 'react-compiler-runtime';
break;
}
case '19': {
moduleName = 'react/compiler-runtime';
break;
}
default:
CompilerError.invariant(moduleName != null, {
function getReactCompilerRuntimeModule(opts: PluginOptions): string {
if (opts.target === '19') {
return 'react/compiler-runtime'; // from react namespace
} else if (opts.target === '17' || opts.target === '18') {
return 'react-compiler-runtime'; // npm package
} else {
CompilerError.invariant(
opts.target != null &&
opts.target.kind === 'donotuse_meta_internal' &&
typeof opts.target.runtimeModule === 'string',
{
reason: 'Expected target to already be validated',
description: null,
loc: null,
suggestions: null,
});
},
);
return opts.target.runtimeModule;
}
return moduleName;
}
58 changes: 35 additions & 23 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,12 @@ function lowerStatement(
const left = stmt.get('left');
const leftLoc = left.node.loc ?? GeneratedSource;
let test: Place;
const advanceIterator = lowerValueToTemporary(builder, {
kind: 'IteratorNext',
loc: leftLoc,
iterator: {...iterator},
collection: {...value},
});
if (left.isVariableDeclaration()) {
const declarations = left.get('declarations');
CompilerError.invariant(declarations.length === 1, {
Expand All @@ -1087,12 +1093,6 @@ function lowerStatement(
suggestions: null,
});
const id = declarations[0].get('id');
const advanceIterator = lowerValueToTemporary(builder, {
kind: 'IteratorNext',
loc: leftLoc,
iterator: {...iterator},
collection: {...value},
});
const assign = lowerAssignment(
builder,
leftLoc,
Expand All @@ -1103,13 +1103,19 @@ function lowerStatement(
);
test = lowerValueToTemporary(builder, assign);
} else {
builder.errors.push({
reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForOfStatement`,
severity: ErrorSeverity.Todo,
loc: left.node.loc ?? null,
suggestions: null,
CompilerError.invariant(left.isLVal(), {
loc: leftLoc,
reason: 'Expected ForOf init to be a variable declaration or lval',
});
return;
const assign = lowerAssignment(
builder,
leftLoc,
InstructionKind.Reassign,
left,
advanceIterator,
'Assignment',
);
test = lowerValueToTemporary(builder, assign);
}
builder.terminateWithContinuation(
{
Expand Down Expand Up @@ -1166,6 +1172,11 @@ function lowerStatement(
const left = stmt.get('left');
const leftLoc = left.node.loc ?? GeneratedSource;
let test: Place;
const nextPropertyTemp = lowerValueToTemporary(builder, {
kind: 'NextPropertyOf',
loc: leftLoc,
value,
});
if (left.isVariableDeclaration()) {
const declarations = left.get('declarations');
CompilerError.invariant(declarations.length === 1, {
Expand All @@ -1175,11 +1186,6 @@ function lowerStatement(
suggestions: null,
});
const id = declarations[0].get('id');
const nextPropertyTemp = lowerValueToTemporary(builder, {
kind: 'NextPropertyOf',
loc: leftLoc,
value,
});
const assign = lowerAssignment(
builder,
leftLoc,
Expand All @@ -1190,13 +1196,19 @@ function lowerStatement(
);
test = lowerValueToTemporary(builder, assign);
} else {
builder.errors.push({
reason: `(BuildHIR::lowerStatement) Handle ${left.type} inits in ForInStatement`,
severity: ErrorSeverity.Todo,
loc: left.node.loc ?? null,
suggestions: null,
CompilerError.invariant(left.isLVal(), {
loc: leftLoc,
reason: 'Expected ForIn init to be a variable declaration or lval',
});
return;
const assign = lowerAssignment(
builder,
leftLoc,
InstructionKind.Reassign,
left,
nextPropertyTemp,
'Assignment',
);
test = lowerValueToTemporary(builder, assign);
}
builder.terminateWithContinuation(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,50 @@ const EnvironmentConfigSchema = z.object({

enableFunctionDependencyRewrite: z.boolean().default(true),

/**
* Enables inference of optional dependency chains. Without this flag
* a property chain such as `props?.items?.foo` will infer as a dep on
* just `props`. With this flag enabled, we'll infer that full path as
* the dependency.
*/
enableOptionalDependencies: z.boolean().default(true),

/**
* Enables inference and auto-insertion of effect dependencies. Takes in an array of
* configurable module and import pairs to allow for user-land experimentation. For example,
* [
* {
* module: 'react',
* imported: 'useEffect',
* numRequiredArgs: 1,
* },{
* module: 'MyExperimentalEffectHooks',
* imported: 'useExperimentalEffect',
* numRequiredArgs: 2,
* },
* ]
* would insert dependencies for calls of `useEffect` imported from `react` and calls of
* useExperimentalEffect` from `MyExperimentalEffectHooks`.
*
* `numRequiredArgs` tells the compiler the amount of arguments required to append a dependency
* array to the end of the call. With the configuration above, we'd insert dependencies for
* `useEffect` if it is only given a single argument and it would be appended to the argument list.
*
* numRequiredArgs must always be greater than 0, otherwise there is no function to analyze for dependencies
*
* Still experimental.
*/
inferEffectDependencies: z
.nullable(
z.array(
z.object({
function: ExternalFunctionSchema,
numRequiredArgs: z.number(),
}),
),
)
.default(null),

/**
* Enables inlining ReactElement object literals in place of JSX
* An alternative to the standard JSX transform which replaces JSX with React's jsxProd() runtime
Expand Down Expand Up @@ -601,6 +645,29 @@ const testComplexConfigDefaults: PartialEnvironmentConfig = {
source: 'react-compiler-runtime',
importSpecifierName: 'useContext_withSelector',
},
inferEffectDependencies: [
{
function: {
source: 'react',
importSpecifierName: 'useEffect',
},
numRequiredArgs: 1,
},
{
function: {
source: 'shared-runtime',
importSpecifierName: 'useSpecialEffect',
},
numRequiredArgs: 2,
},
{
function: {
source: 'useEffectWrapper',
importSpecifierName: 'default',
},
numRequiredArgs: 1,
},
],
};

/**
Expand Down Expand Up @@ -1087,3 +1154,5 @@ export function tryParseExternalFunction(
suggestions: null,
});
}

export const DEFAULT_EXPORT = 'default';
Loading

0 comments on commit db83038

Please sign in to comment.