Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,13 @@ function getFunctionName(
if (parent.isVariableDeclarator() && parent.get('init').node === path.node) {
// const useHook = () => {};
id = parent.get('id');
} else if (
parent.isCallExpression() &&
parent.parentPath.isVariableDeclarator() &&
parent.parentPath.get('init').node === parent.node
) {
// const MyComponent = wrap(() => {});
id = parent.parentPath.get('id');
} else if (
parent.isAssignmentExpression() &&
parent.get('right').node === path.node &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,94 @@ const tests: CompilerTestCases = {
},
],
},
{
name: '[Heuristic] Compiles HOF-wrapped PascalCase component - detects prop mutation',
filename: 'component.tsx',
code: normalizeIndent`
const wrap = (value) => value;
const MyComponent = wrap(({a}) => {
a.key = 'value';
return <div />;
});
`,
errors: [
{
message: /Modifying component props/,
},
],
},
],
};

const eslintTester = new ESLintTesterV8({
parser: require.resolve('@typescript-eslint/parser-v5'),
});
eslintTester.run('react-compiler', allRules['immutability'].rule, tests);

// Tests for set-state-in-effect rule with HOF-wrapped components
// Reproduction test for https://github.com/facebook/react/issues/35910
const setStateInEffectTests: CompilerTestCases = {
valid: [
{
name: 'Direct component with no setState in effect',
filename: 'test.tsx',
code: normalizeIndent`
import { useEffect, useState } from 'react';
function DirectComponent() {
const [value, setValue] = useState(0);
useEffect(() => {
console.log(value);
}, []);
return <div>{value}</div>;
}
`,
},
],
invalid: [
{
name: 'Direct component with setState in effect',
filename: 'test.tsx',
code: normalizeIndent`
import { useEffect, useState } from 'react';
function DirectComponent() {
const [value, setValue] = useState(0);
useEffect(() => {
setValue(1);
}, []);
return <div>{value}</div>;
}
`,
errors: [
{
message: /Calling setState synchronously within an effect/,
},
],
},
{
name: 'Anonymous component callback passed to HOF has setState in effect',
filename: 'test.tsx',
code: normalizeIndent`
import { useEffect, useState } from 'react';
const wrap = (value) => value;
const WrappedComponent = wrap(() => {
const [value, setValue] = useState(0);
useEffect(() => {
setValue(1);
}, []);
return <div>{value}</div>;
});
`,
errors: [
{
message: /Calling setState synchronously within an effect/,
},
],
},
],
};

eslintTester.run(
'react-compiler-set-state-in-effect',
allRules['set-state-in-effect'].rule,
setStateInEffectTests,
);
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ function checkTopLevelNode(node: ESTree.Node): boolean {
}

// Handle: const MyComponent = () => {} or const useHook = function() {}
// Also handles: const MyComponent = wrap(() => {})
if (node.type === 'VariableDeclaration') {
for (const decl of (node as ESTree.VariableDeclaration).declarations) {
if (decl.id.type === 'Identifier') {
const init = decl.init;
if (
init != null &&
(init.type === 'ArrowFunctionExpression' ||
init.type === 'FunctionExpression')
init.type === 'FunctionExpression' ||
init.type === 'CallExpression')
) {
const name = decl.id.name;
if (COMPONENT_NAME_PATTERN.test(name) || HOOK_NAME_PATTERN.test(name)) {
Expand Down
Loading