@@ -32144,7 +32144,6 @@ const EnvironmentConfigSchema = zod.z.object({
3214432144 lowerContextAccess: ExternalFunctionSchema.nullable().default(null),
3214532145 validateNoVoidUseMemo: zod.z.boolean().default(false),
3214632146 validateNoDynamicallyCreatedComponentsOrHooks: zod.z.boolean().default(false),
32147- enableAllowSetStateFromRefsInEffects: zod.z.boolean().default(true),
3214832147});
3214932148class Environment {
3215032149 constructor(scope, fnType, compilerMode, config, contextIdentifiers, parentFunction, logger, filename, code, programContext) {
@@ -49448,46 +49447,12 @@ function validateNoRefAccessInRenderImpl(fn, env) {
4944849447 case 'StartMemoize':
4944949448 case 'FinishMemoize':
4945049449 break;
49451- case 'LoadGlobal': {
49452- if (instr.value.binding.name === 'undefined') {
49453- env.set(instr.lvalue.identifier.id, { kind: 'Nullable' });
49454- }
49455- break;
49456- }
4945749450 case 'Primitive': {
4945849451 if (instr.value.value == null) {
4945949452 env.set(instr.lvalue.identifier.id, { kind: 'Nullable' });
4946049453 }
4946149454 break;
4946249455 }
49463- case 'UnaryExpression': {
49464- if (instr.value.operator === '!') {
49465- const value = env.get(instr.value.value.identifier.id);
49466- const refId = (value === null || value === void 0 ? void 0 : value.kind) === 'RefValue' && value.refId != null
49467- ? value.refId
49468- : null;
49469- if (refId !== null) {
49470- env.set(instr.lvalue.identifier.id, { kind: 'Guard', refId });
49471- errors.pushDiagnostic(CompilerDiagnostic.create({
49472- category: ErrorCategory.Refs,
49473- reason: 'Cannot access refs during render',
49474- description: ERROR_DESCRIPTION,
49475- })
49476- .withDetails({
49477- kind: 'error',
49478- loc: instr.value.value.loc,
49479- message: `Cannot access ref value during render`,
49480- })
49481- .withDetails({
49482- kind: 'hint',
49483- message: 'To initialize a ref only once, check that the ref is null with the pattern `if (ref.current == null) { ref.current = ... }`',
49484- }));
49485- break;
49486- }
49487- }
49488- validateNoRefValueAccess(errors, env, instr.value.value);
49489- break;
49490- }
4949149456 case 'BinaryExpression': {
4949249457 const left = env.get(instr.value.left.identifier.id);
4949349458 const right = env.get(instr.value.right.identifier.id);
@@ -50614,7 +50579,7 @@ function emitArrayInstr(elements, env) {
5061450579 return arrayInstr;
5061550580}
5061650581
50617- function validateNoSetStateInEffects(fn, env ) {
50582+ function validateNoSetStateInEffects(fn) {
5061850583 const setStateFunctions = new Map();
5061950584 const errors = new CompilerError();
5062050585 for (const [, block] of fn.body.blocks) {
@@ -50636,7 +50601,7 @@ function validateNoSetStateInEffects(fn, env) {
5063650601 case 'FunctionExpression': {
5063750602 if ([...eachInstructionValueOperand(instr.value)].some(operand => isSetStateType(operand.identifier) ||
5063850603 setStateFunctions.has(operand.identifier.id))) {
50639- const callee = getSetStateCall(instr.value.loweredFunc.func, setStateFunctions, env );
50604+ const callee = getSetStateCall(instr.value.loweredFunc.func, setStateFunctions);
5064050605 if (callee !== null) {
5064150606 setStateFunctions.set(instr.lvalue.identifier.id, callee);
5064250607 }
@@ -50680,29 +50645,9 @@ function validateNoSetStateInEffects(fn, env) {
5068050645 }
5068150646 return errors.asResult();
5068250647}
50683- function getSetStateCall(fn, setStateFunctions, env) {
50684- const refDerivedValues = new Set();
50685- const isDerivedFromRef = (place) => {
50686- return (refDerivedValues.has(place.identifier.id) ||
50687- isUseRefType(place.identifier) ||
50688- isRefValueType(place.identifier));
50689- };
50648+ function getSetStateCall(fn, setStateFunctions) {
5069050649 for (const [, block] of fn.body.blocks) {
5069150650 for (const instr of block.instructions) {
50692- if (env.config.enableAllowSetStateFromRefsInEffects) {
50693- const hasRefOperand = Iterable_some(eachInstructionValueOperand(instr.value), isDerivedFromRef);
50694- if (hasRefOperand) {
50695- for (const lvalue of eachInstructionLValue(instr)) {
50696- refDerivedValues.add(lvalue.identifier.id);
50697- }
50698- }
50699- if (instr.value.kind === 'PropertyLoad' &&
50700- instr.value.property === 'current' &&
50701- (isUseRefType(instr.value.object.identifier) ||
50702- isRefValueType(instr.value.object.identifier))) {
50703- refDerivedValues.add(instr.lvalue.identifier.id);
50704- }
50705- }
5070650651 switch (instr.value.kind) {
5070750652 case 'LoadLocal': {
5070850653 if (setStateFunctions.has(instr.value.place.identifier.id)) {
@@ -50721,14 +50666,6 @@ function getSetStateCall(fn, setStateFunctions, env) {
5072150666 const callee = instr.value.callee;
5072250667 if (isSetStateType(callee.identifier) ||
5072350668 setStateFunctions.has(callee.identifier.id)) {
50724- if (env.config.enableAllowSetStateFromRefsInEffects) {
50725- const arg = instr.value.args.at(0);
50726- if (arg !== undefined &&
50727- arg.kind === 'Identifier' &&
50728- refDerivedValues.has(arg.identifier.id)) {
50729- return null;
50730- }
50731- }
5073250669 return callee;
5073350670 }
5073450671 }
@@ -52192,7 +52129,7 @@ function runWithEnvironment(func, env) {
5219252129 validateNoDerivedComputationsInEffects(hir);
5219352130 }
5219452131 if (env.config.validateNoSetStateInEffects) {
52195- env.logErrors(validateNoSetStateInEffects(hir, env ));
52132+ env.logErrors(validateNoSetStateInEffects(hir));
5219652133 }
5219752134 if (env.config.validateNoJSXInTryStatements) {
5219852135 env.logErrors(validateNoJSXInTryStatement(hir));
0 commit comments