unsupported-syntax

验证 React 编译器不支持的语法。如果需要,你仍然可以在 React 外部使用这种语法,例如在独立的实用函数中。

🌐 Validates against syntax that React Compiler does not support. If you need to, you can still use this syntax outside of React, such as in a standalone utility function.

规则详情

🌐 Rule Details

React 编译器需要静态分析你的代码以应用优化。像 evalwith 这样的特性使得在编译时不可能静态理解代码的功能,因此编译器无法优化使用它们的组件。

🌐 React Compiler needs to statically analyze your code to apply optimizations. Features like eval and with make it impossible to statically understand what the code does at compile time, so the compiler can’t optimize components that use them.

无效

🌐 Invalid

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

// ❌ Using eval in component
function Component({ code }) {
const result = eval(code); // Can't be analyzed
return <div>{result}</div>;
}

// ❌ Using with statement
function Component() {
with (Math) { // Changes scope dynamically
return <div>{sin(PI / 2)}</div>;
}
}

// ❌ Dynamic property access with eval
function Component({propName}) {
const value = eval(`props.${propName}`);
return <div>{value}</div>;
}

有效

🌐 Valid

此规则的正确代码示例:

🌐 Examples of correct code for this rule:

// ✅ Use normal property access
function Component({propName, props}) {
const value = props[propName]; // Analyzable
return <div>{value}</div>;
}

// ✅ Use standard Math methods
function Component() {
return <div>{Math.sin(Math.PI / 2)}</div>;
}

故障排除

🌐 Troubleshooting

我需要执行动态代码

🌐 I need to evaluate dynamic code

你可能需要执行用户提供的代码:

🌐 You might need to evaluate user-provided code:

// ❌ Wrong: eval in component
function Calculator({expression}) {
const result = eval(expression); // Unsafe and unoptimizable
return <div>Result: {result}</div>;
}

改用安全的表达式解析器:

🌐 Use a safe expression parser instead:

// ✅ Better: Use a safe parser
import {evaluate} from 'mathjs'; // or similar library

function Calculator({expression}) {
const [result, setResult] = useState(null);

const calculate = () => {
try {
// Safe mathematical expression evaluation
setResult(evaluate(expression));
} catch (error) {
setResult('Invalid expression');
}
};

return (
<div>
<button onClick={calculate}>Calculate</button>
{result && <div>Result: {result}</div>}
</div>
);
}

注意

永远不要将 eval 与用户输入一起使用——这是一个安全风险。对于特定的使用场景,如数学表达式、JSON解析或模板评估,请使用专用的解析库。

🌐 Never use eval with user input - it’s a security risk. Use dedicated parsing libraries for specific use cases like mathematical expressions, JSON parsing, or template evaluation.