mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Forbidden variable names validation
This commit is contained in:
@@ -86,6 +86,7 @@ import {
|
||||
validateMemoizedEffectDependencies,
|
||||
validateNoCapitalizedCalls,
|
||||
validateNoRefAccessInRender,
|
||||
validateNoForbiddenVariableNames,
|
||||
validateNoSetStateInRender,
|
||||
validatePreservedManualMemoization,
|
||||
validateUseMemo,
|
||||
@@ -247,6 +248,10 @@ function runWithEnvironment(
|
||||
}
|
||||
}
|
||||
|
||||
if (env.config.validateNoForbiddenVariableNames) {
|
||||
validateNoForbiddenVariableNames(hir);
|
||||
}
|
||||
|
||||
if (!env.config.enableNewMutationAliasingModel) {
|
||||
validateLocalsNotReassignedAfterRender(hir);
|
||||
}
|
||||
|
||||
@@ -330,6 +330,11 @@ export const EnvironmentConfigSchema = z.object({
|
||||
*/
|
||||
validateNoSetStateInEffects: z.boolean().default(false),
|
||||
|
||||
/*
|
||||
* Validates there are no forbidden variable names in the component.
|
||||
*/
|
||||
validateNoForbiddenVariableNames:z.boolean().default(false),
|
||||
|
||||
/**
|
||||
* Validates that effects are not used to calculate derived data which could instead be computed
|
||||
* during render.
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import {
|
||||
GeneratedSource,
|
||||
HIRFunction,
|
||||
} from '../HIR';
|
||||
|
||||
import {CompilerError, ErrorSeverity} from '..';
|
||||
export function validateNoForbiddenVariableNames(fn: HIRFunction) {
|
||||
const errors = new CompilerError();
|
||||
|
||||
for (const block of fn.body.blocks.values()) {
|
||||
for (const instr of block.instructions) {
|
||||
const {loc} = instr;
|
||||
|
||||
console.log(instr)
|
||||
if (loc !== GeneratedSource && loc.identifierName === 'forbidden') {
|
||||
errors.push({
|
||||
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,
|
||||
severity: ErrorSeverity.InvalidReact,
|
||||
loc,
|
||||
suggestions: null,
|
||||
});
|
||||
}
|
||||
}}
|
||||
|
||||
|
||||
if (errors.hasErrors()) {
|
||||
throw errors;
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,6 @@ export {validateMemoizedEffectDependencies} from './ValidateMemoizedEffectDepend
|
||||
export {validateNoCapitalizedCalls} from './ValidateNoCapitalizedCalls';
|
||||
export {validateNoRefAccessInRender} from './ValidateNoRefAccessInRender';
|
||||
export {validateNoSetStateInRender} from './ValidateNoSetStateInRender';
|
||||
export {validateNoForbiddenVariableNames} from './ValidateNoForbiddenVariableNames';
|
||||
export {validatePreservedManualMemoization} from './ValidatePreservedManualMemoization';
|
||||
export {validateUseMemo} from './ValidateUseMemo';
|
||||
|
||||
Reference in New Issue
Block a user