diagnosing-react-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Diagnosing React code

诊断 React 代码

react-doctor scans a React package and prints a 0-100 score plus diagnostics for security, performance, correctness, and architecture. It needs Node
^20.19.0 || >=22.13.0
.
react-doctor 会扫描 React 包,并输出 0-100 的评分以及安全性、性能、正确性和架构方面的诊断结果。它需要 Node
^20.19.0 || >=22.13.0
。

Before the first run

首次运行前

  • The first run in a session downloads and executes a third-party package from npm. Ask the user before it, naming the pinned version.
  • By default the score comes from the react-doctor score API and the supply-chain scan queries Socket.dev; when the user wants nothing sent off the machine, add
    --no-score --no-supply-chain
    (the run then reports no score).
  • 当前会话中的首次运行会从 npm 下载并执行一个第三方包。执行前应询问用户,并说明所固定的版本。
  • 默认情况下,评分来自 react-doctor score API,供应链扫描会查询 Socket.dev;如果用户不希望向本机之外发送任何数据,请添加
    --no-score --no-supply-chain
    (此时运行结果不会报告评分)。

Run

运行

Run inside the package whose
package.json
lists
react
in
dependencies
or
peerDependencies
; at a monorepo root the tool prompts for a project.
bash
npx -y react-doctor@0.9.17 --verbose --scope changed
  • The pin keeps scores comparable across runs. Never fall back to
    @latest
    ; upgrade only by changing the pin deliberately. If a flag is rejected, check
    npx -y react-doctor@0.9.17 --help
    and adjust.
  • --verbose
    lists every rule and file; the default shows only the top 3.
  • --scope changed
    reports only new findings in files changed against the auto-detected base ref (
    --base <ref>
    to set it). Use
    --scope full
    for a whole-package health check. It replaces the deprecated
    --diff
    .
在
package.json
的
dependencies
或
peerDependencies
中列有
react
的包内运行;在 monorepo 根目录运行时,该工具会提示选择项目。
bash
npx -y react-doctor@0.9.17 --verbose --scope changed
  • 固定版本可确保不同运行之间的评分具有可比性。切勿回退到
    @latest
    ;仅在有意升级时修改固定版本。如果某个参数被拒绝,请检查
    npx -y react-doctor@0.9.17 --help
    并进行调整。
  • --verbose
    会列出每条规则及其对应文件;默认仅显示前 3 条。
  • --scope changed
    仅报告相对于自动检测的基准 ref 所变更文件中的新发现(可使用
    --base <ref>
    设置基准 ref)。使用
    --scope full
    可对整个包进行健康检查。该参数取代了已弃用的
    --diff
    。

Fix loop

修复循环

  1. Record the baseline score and error and warning counts.
  2. Fix error diagnostics, security and correctness first. Fix warnings only in code the current task changed.
  3. Re-run the same command until it reports no errors and the score is at least the baseline. Stop after three fix rounds that leave errors, or when a fix would change behavior outside the task, and report what remains.
Report the version and scope, the score from baseline to final (or "not computed: --no-score"), each fixed diagnostic, and each remaining one with why it was not fixed, as
<severity> <rule> <file>:<line>
.
  1. 记录基准评分以及错误和警告数量。
  2. 修复错误诊断,优先处理安全性和正确性问题。仅修复当前任务所改动代码中的警告。
  3. 使用相同命令重复运行,直到不再报告错误,且评分不低于基准评分。如果经过三轮修复后仍有错误,或某项修复会改变任务范围之外的行为,则停止修复,并报告剩余问题。
报告版本和扫描范围、从基准到最终的评分(或“not computed: --no-score”)、每项已修复的诊断,以及每项未修复的诊断及其未修复原因,格式为
<severity> <rule> <file>:<line>
。