@fantasticit/sumi 0.11.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -23
- package/dist/adapters.d.ts +2 -0
- package/dist/adapters.js +33 -7
- package/dist/adapters.js.map +1 -1
- package/dist/agent-hooks.d.ts +6 -1
- package/dist/agent-hooks.js +69 -39
- package/dist/agent-hooks.js.map +1 -1
- package/dist/agents.d.ts +3 -1
- package/dist/agents.js +36 -12
- package/dist/agents.js.map +1 -1
- package/dist/bin.js +0 -0
- package/dist/cli.js +36 -21
- package/dist/cli.js.map +1 -1
- package/dist/context.js +14 -3
- package/dist/context.js.map +1 -1
- package/dist/doctor-wiring.d.ts +6 -3
- package/dist/doctor-wiring.js +25 -15
- package/dist/doctor-wiring.js.map +1 -1
- package/dist/hook-routing.d.ts +17 -4
- package/dist/hook-routing.js +119 -25
- package/dist/hook-routing.js.map +1 -1
- package/dist/instructions.d.ts +1 -1
- package/dist/instructions.js +4 -4
- package/dist/instructions.js.map +1 -1
- package/dist/launcher.cjs +161 -151
- package/dist/launcher.js +6 -2
- package/dist/launcher.js.map +1 -1
- package/dist/scaffold-commands.js +69 -6
- package/dist/scaffold-commands.js.map +1 -1
- package/dist/task.d.ts +16 -0
- package/dist/task.js +51 -13
- package/dist/task.js.map +1 -1
- package/dist/trace.d.ts +16 -0
- package/dist/trace.js +30 -0
- package/dist/trace.js.map +1 -0
- package/dist/vendor.d.ts +1 -1
- package/dist/vendor.js +1 -21
- package/dist/vendor.js.map +1 -1
- package/dist/visualize-page.d.ts +1 -1
- package/dist/visualize-page.js +63 -39
- package/dist/visualize-page.js.map +1 -1
- package/dist/visualize-sessions.d.ts +34 -0
- package/dist/visualize-sessions.js +181 -0
- package/dist/visualize-sessions.js.map +1 -0
- package/dist/visualize.js +22 -3
- package/dist/visualize.js.map +1 -1
- package/node_modules/@sumi/create/README.md +12 -10
- package/node_modules/@sumi/create/dist/bin.d.ts +2 -0
- package/node_modules/@sumi/create/dist/bin.js +52 -0
- package/node_modules/@sumi/create/dist/bin.js.map +1 -0
- package/node_modules/@sumi/create/dist/scaffold.js +26 -4
- package/node_modules/@sumi/create/dist/scaffold.js.map +1 -1
- package/node_modules/@sumi/create/package.json +2 -2
- package/node_modules/@sumi/create/template/.editorconfig +4 -0
- package/node_modules/@sumi/create/template/.prettierignore +1 -0
- package/node_modules/@sumi/create/template/AGENTS.md +3 -2
- package/node_modules/@sumi/create/template/control/config.yaml +1 -0
- package/node_modules/@sumi/create/template/control/flows/check.flow.yaml +5 -0
- package/node_modules/@sumi/create/template/control/plugins/security-audit-scan/instructions/security-audit-scan.md +157 -0
- package/node_modules/@sumi/create/template/control/plugins/security-audit-scan/plugin.yaml +6 -0
- package/node_modules/@sumi/create/template/control/rubrics/architecture.yaml +1 -0
- package/node_modules/@sumi/create/template/control/rubrics/clean-code.yaml +5 -0
- package/node_modules/@sumi/create/template/control/rubrics/security.yaml +20 -0
- package/node_modules/@sumi/create/template/control/rules/codecheck-catalog.json +1171 -0
- package/node_modules/@sumi/create/template/control/rules/codecheck.md +62 -0
- package/node_modules/@sumi/create/template/control/rules/development.md +1 -0
- package/node_modules/@sumi/create/template/control/rules/git-workflow.md +98 -0
- package/node_modules/@sumi/create/template/control/rules/security/README.md +24 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/api-auth-scan.md +201 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/crypto-weakness-scan.md +83 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/csv-injection-scan.md +96 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/debug-menu-crack-tools-scan.md +40 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/dos-attack-scan.md +153 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/file-upload-security-scan.md +55 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/insecure-protocol-scan.md +29 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/log-injection-scan.md +44 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/logging-security-policy.md +208 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/nodejs-dangerous-apis-scan.md +78 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/postmessage-scan.md +34 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/public-address-scan.md +53 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/redis-sensitive-scan.md +77 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/redos-scan.md +30 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/scenarios-catalog.md +268 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/sensitive-data-taxonomy.md +50 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/sha-hash-scan.md +105 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/storage-secrets-scan.md +45 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/tls-cipher-protocol-scan.md +103 -0
- package/node_modules/@sumi/create/template/control/rules/security/references/xss-scan-methodology.md +252 -0
- package/node_modules/@sumi/create/template/eslint.config.mjs +157 -2
- package/node_modules/@sumi/create/template/gitignore +1 -0
- package/node_modules/@sumi/create/template/init/AGENTS.md +3 -2
- package/node_modules/@sumi/create/template/init/control/flows/change.flow.yaml +18 -0
- package/node_modules/@sumi/create/template/init/control/plugins/security-audit-scan/instructions/security-audit-scan.md +157 -0
- package/node_modules/@sumi/create/template/init/control/plugins/security-audit-scan/plugin.yaml +6 -0
- package/node_modules/@sumi/create/template/init/control/rubrics/architecture.yaml +2 -1
- package/node_modules/@sumi/create/template/init/control/rubrics/business-impact.yaml +6 -0
- package/node_modules/@sumi/kernel/dist/brief.js +7 -8
- package/node_modules/@sumi/kernel/dist/brief.js.map +1 -1
- package/node_modules/@sumi/kernel/dist/engine.d.ts +2 -0
- package/node_modules/@sumi/kernel/dist/engine.js +19 -9
- package/node_modules/@sumi/kernel/dist/engine.js.map +1 -1
- package/node_modules/@sumi/kernel/dist/flow.d.ts +9 -0
- package/node_modules/@sumi/kernel/dist/flow.js +16 -1
- package/node_modules/@sumi/kernel/dist/flow.js.map +1 -1
- package/node_modules/@sumi/kernel/dist/record.d.ts +5 -3
- package/node_modules/@sumi/kernel/dist/record.js +16 -5
- package/node_modules/@sumi/kernel/dist/record.js.map +1 -1
- package/node_modules/@sumi/kernel/dist/runner.js +24 -0
- package/node_modules/@sumi/kernel/dist/runner.js.map +1 -1
- package/node_modules/@sumi/kernel/dist/scope.d.ts +2 -0
- package/node_modules/@sumi/kernel/dist/scope.js +12 -2
- package/node_modules/@sumi/kernel/dist/scope.js.map +1 -1
- package/node_modules/@sumi/kernel/package.json +2 -2
- package/node_modules/@sumi/plugin-sdk/README.md +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/files.d.ts +7 -0
- package/node_modules/@sumi/plugin-sdk/dist/files.js +56 -1
- package/node_modules/@sumi/plugin-sdk/dist/files.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/git.d.ts +4 -2
- package/node_modules/@sumi/plugin-sdk/dist/git.js +14 -3
- package/node_modules/@sumi/plugin-sdk/dist/git.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/hook-input.d.ts +5 -0
- package/node_modules/@sumi/plugin-sdk/dist/hook-input.js +41 -0
- package/node_modules/@sumi/plugin-sdk/dist/hook-input.js.map +1 -0
- package/node_modules/@sumi/plugin-sdk/dist/host-event.d.ts +13 -0
- package/node_modules/@sumi/plugin-sdk/dist/host-event.js +31 -0
- package/node_modules/@sumi/plugin-sdk/dist/host-event.js.map +1 -0
- package/node_modules/@sumi/plugin-sdk/dist/index.d.ts +3 -0
- package/node_modules/@sumi/plugin-sdk/dist/index.js +3 -0
- package/node_modules/@sumi/plugin-sdk/dist/index.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/project.d.ts +7 -0
- package/node_modules/@sumi/plugin-sdk/dist/project.js +27 -1
- package/node_modules/@sumi/plugin-sdk/dist/project.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/protocol.d.ts +20 -4
- package/node_modules/@sumi/plugin-sdk/dist/protocol.js +30 -8
- package/node_modules/@sumi/plugin-sdk/dist/protocol.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/session-context.d.ts +2 -0
- package/node_modules/@sumi/plugin-sdk/dist/session-context.js +76 -50
- package/node_modules/@sumi/plugin-sdk/dist/session-context.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/shell.d.ts +28 -4
- package/node_modules/@sumi/plugin-sdk/dist/shell.js +200 -35
- package/node_modules/@sumi/plugin-sdk/dist/shell.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/snapshot.d.ts +2 -0
- package/node_modules/@sumi/plugin-sdk/dist/snapshot.js +18 -6
- package/node_modules/@sumi/plugin-sdk/dist/snapshot.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/trace.d.ts +34 -1
- package/node_modules/@sumi/plugin-sdk/dist/trace.js +94 -24
- package/node_modules/@sumi/plugin-sdk/dist/trace.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/validate.js +19 -2
- package/node_modules/@sumi/plugin-sdk/dist/validate.js.map +1 -1
- package/node_modules/@sumi/plugin-sdk/dist/worktrees.d.ts +6 -0
- package/node_modules/@sumi/plugin-sdk/dist/worktrees.js +25 -0
- package/node_modules/@sumi/plugin-sdk/dist/worktrees.js.map +1 -0
- package/node_modules/@sumi/plugin-sdk/package.json +1 -1
- package/node_modules/@sumi/plugins-core/README.md +1 -1
- package/node_modules/@sumi/plugins-core/dist/agent-rules.d.ts +2 -4
- package/node_modules/@sumi/plugins-core/dist/agent-rules.js +11 -3
- package/node_modules/@sumi/plugins-core/dist/agent-rules.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/baseline.d.ts +2 -1
- package/node_modules/@sumi/plugins-core/dist/baseline.js +12 -2
- package/node_modules/@sumi/plugins-core/dist/baseline.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/change-impact.d.ts +11 -0
- package/node_modules/@sumi/plugins-core/dist/change-impact.js +116 -0
- package/node_modules/@sumi/plugins-core/dist/change-impact.js.map +1 -0
- package/node_modules/@sumi/plugins-core/dist/commands/agent-rules-before-edit.js +2 -3
- package/node_modules/@sumi/plugins-core/dist/commands/agent-rules-before-edit.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/commands/agent-rules-prompt.js +2 -3
- package/node_modules/@sumi/plugins-core/dist/commands/agent-rules-prompt.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/commands/context-impact.d.ts +1 -0
- package/node_modules/@sumi/plugins-core/dist/commands/context-impact.js +23 -0
- package/node_modules/@sumi/plugins-core/dist/commands/context-impact.js.map +1 -0
- package/node_modules/@sumi/plugins-core/dist/plugins/control-docs.js +18 -0
- package/node_modules/@sumi/plugins-core/dist/plugins/control-docs.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/plugins/coverage.js +3 -1
- package/node_modules/@sumi/plugins-core/dist/plugins/coverage.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/plugins/test-affected.js +116 -9
- package/node_modules/@sumi/plugins-core/dist/plugins/test-affected.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/test-checkpoints.d.ts +5 -0
- package/node_modules/@sumi/plugins-core/dist/test-checkpoints.js +12 -8
- package/node_modules/@sumi/plugins-core/dist/test-checkpoints.js.map +1 -1
- package/node_modules/@sumi/plugins-core/dist/test-incremental.d.ts +38 -0
- package/node_modules/@sumi/plugins-core/dist/test-incremental.js +92 -0
- package/node_modules/@sumi/plugins-core/dist/test-incremental.js.map +1 -0
- package/node_modules/@sumi/plugins-core/dist/tool-batches.d.ts +11 -0
- package/node_modules/@sumi/plugins-core/dist/tool-batches.js +41 -0
- package/node_modules/@sumi/plugins-core/dist/tool-batches.js.map +1 -0
- package/node_modules/@sumi/plugins-core/instructions/adopt.md +1 -1
- package/node_modules/@sumi/plugins-core/package.json +2 -2
- package/node_modules/@sumi/plugins-core/sumi-plugins.yaml +8 -0
- package/node_modules/@sumi/plugins-governance/README.md +16 -30
- package/node_modules/@sumi/plugins-governance/dist/checkout.js +14 -6
- package/node_modules/@sumi/plugins-governance/dist/checkout.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/commands/gate.js +13 -52
- package/node_modules/@sumi/plugins-governance/dist/commands/gate.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/commands/guard.js +2 -2
- package/node_modules/@sumi/plugins-governance/dist/commands/guard.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/commands/pr-report.js +7 -2
- package/node_modules/@sumi/plugins-governance/dist/commands/pr-report.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/commands/protected-after-shell.d.ts +1 -0
- package/node_modules/@sumi/plugins-governance/dist/commands/protected-after-shell.js +20 -0
- package/node_modules/@sumi/plugins-governance/dist/commands/protected-after-shell.js.map +1 -0
- package/node_modules/@sumi/plugins-governance/dist/gate-cache.js +1 -1
- package/node_modules/@sumi/plugins-governance/dist/gate-cache.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/guard.js +115 -107
- package/node_modules/@sumi/plugins-governance/dist/guard.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/hooks.d.ts +2 -0
- package/node_modules/@sumi/plugins-governance/dist/hooks.js +4 -0
- package/node_modules/@sumi/plugins-governance/dist/hooks.js.map +1 -0
- package/node_modules/@sumi/plugins-governance/dist/plugins/protected-paths.js +10 -4
- package/node_modules/@sumi/plugins-governance/dist/plugins/protected-paths.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/pr-report-compose.d.ts +1 -1
- package/node_modules/@sumi/plugins-governance/dist/pr-report-compose.js +5 -2
- package/node_modules/@sumi/plugins-governance/dist/pr-report-compose.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/pr-report-markers.d.ts +1 -1
- package/node_modules/@sumi/plugins-governance/dist/pr-report-markers.js +1 -1
- package/node_modules/@sumi/plugins-governance/dist/protected-shell.d.ts +12 -0
- package/node_modules/@sumi/plugins-governance/dist/protected-shell.js +152 -0
- package/node_modules/@sumi/plugins-governance/dist/protected-shell.js.map +1 -0
- package/node_modules/@sumi/plugins-governance/dist/protected.d.ts +2 -2
- package/node_modules/@sumi/plugins-governance/dist/protected.js +22 -6
- package/node_modules/@sumi/plugins-governance/dist/protected.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/push-base.d.ts +3 -0
- package/node_modules/@sumi/plugins-governance/dist/push-base.js +13 -1
- package/node_modules/@sumi/plugins-governance/dist/push-base.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/semantic.d.ts +1 -1
- package/node_modules/@sumi/plugins-governance/dist/semantic.js +31 -10
- package/node_modules/@sumi/plugins-governance/dist/semantic.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/dist/state.d.ts +0 -46
- package/node_modules/@sumi/plugins-governance/dist/state.js +1 -146
- package/node_modules/@sumi/plugins-governance/dist/state.js.map +1 -1
- package/node_modules/@sumi/plugins-governance/instructions/push.md +6 -5
- package/node_modules/@sumi/plugins-governance/package.json +2 -2
- package/node_modules/@sumi/plugins-governance/sumi-plugins.yaml +2 -4
- package/node_modules/@sumi/plugins-openspec/dist/plugins/impact-consistency.js +2 -35
- package/node_modules/@sumi/plugins-openspec/dist/plugins/impact-consistency.js.map +1 -1
- package/node_modules/@sumi/plugins-openspec/package.json +4 -4
- package/node_modules/@sumi/plugins-precedent/README.md +8 -8
- package/node_modules/@sumi/plugins-precedent/dist/after-edit-context.d.ts +7 -4
- package/node_modules/@sumi/plugins-precedent/dist/after-edit-context.js +96 -46
- package/node_modules/@sumi/plugins-precedent/dist/after-edit-context.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/commands/after-edit-context.js +2 -3
- package/node_modules/@sumi/plugins-precedent/dist/commands/after-edit-context.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/commands/before-edit-context.js +15 -7
- package/node_modules/@sumi/plugins-precedent/dist/commands/before-edit-context.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/commands/context.js +12 -6
- package/node_modules/@sumi/plugins-precedent/dist/commands/context.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/commands/on-prompt-context.js +10 -9
- package/node_modules/@sumi/plugins-precedent/dist/commands/on-prompt-context.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/impact.js +4 -3
- package/node_modules/@sumi/plugins-precedent/dist/impact.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/plugins/harvest.js +17 -32
- package/node_modules/@sumi/plugins-precedent/dist/plugins/harvest.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/plugins/planner.js +4 -7
- package/node_modules/@sumi/plugins-precedent/dist/plugins/planner.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/dist/plugins/selftest.js +4 -3
- package/node_modules/@sumi/plugins-precedent/dist/plugins/selftest.js.map +1 -1
- package/node_modules/@sumi/plugins-precedent/instructions/distill.md +0 -1
- package/node_modules/@sumi/plugins-precedent/package.json +3 -3
- package/node_modules/@sumi/plugins-precedent/sumi-plugins.yaml +2 -4
- package/node_modules/@sumi/plugins-review/README.md +25 -19
- package/node_modules/@sumi/plugins-review/dist/command-hint.d.ts +4 -0
- package/node_modules/@sumi/plugins-review/dist/command-hint.js +6 -0
- package/node_modules/@sumi/plugins-review/dist/command-hint.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/commands/context.js +4 -2
- package/node_modules/@sumi/plugins-review/dist/commands/context.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/commands/host-model-hook.js +3 -4
- package/node_modules/@sumi/plugins-review/dist/commands/host-model-hook.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/commands/model-recheck.js +17 -8
- package/node_modules/@sumi/plugins-review/dist/commands/model-recheck.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/commands/submit.js +4 -3
- package/node_modules/@sumi/plugins-review/dist/commands/submit.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/dispute.d.ts +4 -3
- package/node_modules/@sumi/plugins-review/dist/dispute.js +10 -26
- package/node_modules/@sumi/plugins-review/dist/dispute.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/model-execution.d.ts +9 -2
- package/node_modules/@sumi/plugins-review/dist/model-execution.js +174 -40
- package/node_modules/@sumi/plugins-review/dist/model-execution.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/plugins/independent-recheck.js +32 -31
- package/node_modules/@sumi/plugins-review/dist/plugins/independent-recheck.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/plugins/rubric.js +25 -38
- package/node_modules/@sumi/plugins-review/dist/plugins/rubric.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/recheck.d.ts +8 -2
- package/node_modules/@sumi/plugins-review/dist/recheck.js +24 -8
- package/node_modules/@sumi/plugins-review/dist/recheck.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/report.js +35 -1
- package/node_modules/@sumi/plugins-review/dist/report.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/review-evidence.js +4 -4
- package/node_modules/@sumi/plugins-review/dist/review-evidence.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/review.d.ts +21 -3
- package/node_modules/@sumi/plugins-review/dist/review.js +87 -8
- package/node_modules/@sumi/plugins-review/dist/review.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/submission.js +23 -12
- package/node_modules/@sumi/plugins-review/dist/submission.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/upgrade-flow-plan.js +12 -4
- package/node_modules/@sumi/plugins-review/dist/upgrade-flow-plan.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/upgrade-flow-render.js +4 -2
- package/node_modules/@sumi/plugins-review/dist/upgrade-flow-render.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/upgrade.js +7 -2
- package/node_modules/@sumi/plugins-review/dist/upgrade.js.map +1 -1
- package/node_modules/@sumi/plugins-review/dist/validation-receipt.js +30 -3
- package/node_modules/@sumi/plugins-review/dist/validation-receipt.js.map +1 -1
- package/node_modules/@sumi/plugins-review/package.json +5 -5
- package/node_modules/@sumi/plugins-review/sumi-plugins.yaml +2 -2
- package/package.json +10 -10
|
@@ -0,0 +1,1171 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "TypeScript / JavaScript 编程规则",
|
|
3
|
+
"scope": ["zone:business"],
|
|
4
|
+
"policy": "本目录由项目自行编写,编号是本项目的规则序号;执行方式以各条目的 execution、control/rules/codecheck.md、ESLint、Prettier 与 clean-code 评审为准。",
|
|
5
|
+
"rules": [
|
|
6
|
+
{
|
|
7
|
+
"id": "CC-001",
|
|
8
|
+
"title": "不使用 eval",
|
|
9
|
+
"severity": "必须",
|
|
10
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
11
|
+
"summary": "`eval` 会把字符串当作代码运行,既难以阅读和静态检查,又可能让外部输入变成可执行代码。解析数据用专门的解析函数,按名称取值用映射表。",
|
|
12
|
+
"correct": "const settings = JSON.parse(rawSettings);",
|
|
13
|
+
"incorrect": "const settings = eval(`(${rawSettings})`);",
|
|
14
|
+
"execution": {
|
|
15
|
+
"method": "ESLint `no-eval`"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "CC-002",
|
|
20
|
+
"title": "不把字符串当作代码传给定时器",
|
|
21
|
+
"severity": "必须",
|
|
22
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
23
|
+
"summary": "向 `setTimeout`、`setInterval` 传字符串时,运行环境会像 `eval` 那样解析并运行它,风险相同。定时器的第一个参数一律传函数。",
|
|
24
|
+
"correct": "setTimeout(() => refreshBadge(), 300);",
|
|
25
|
+
"incorrect": "setTimeout('refreshBadge()', 300);",
|
|
26
|
+
"execution": {
|
|
27
|
+
"method": "ESLint `no-implied-eval`"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"id": "CC-003",
|
|
32
|
+
"title": "发布的代码不保留调试输出",
|
|
33
|
+
"severity": "建议",
|
|
34
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
35
|
+
"summary": "`console` 输出多是开发时的临时排查手段,留在产品里会干扰运行环境,还可能暴露内部细节。需要保留的运行信息交给项目统一的日志模块。",
|
|
36
|
+
"correct": "logger.info('同步完成', { syncedRows: synced.length });",
|
|
37
|
+
"incorrect": "console.log('同步完成', synced);",
|
|
38
|
+
"execution": {
|
|
39
|
+
"method": "ESLint `no-console`"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"id": "CC-004",
|
|
44
|
+
"title": "不使用浏览器原生弹窗",
|
|
45
|
+
"severity": "必须",
|
|
46
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
47
|
+
"summary": "`alert`、`confirm`、`prompt` 会打断用户操作,样式也不可控,还常被当作临时调试手段。需要提示或确认时,使用应用自己的界面组件。",
|
|
48
|
+
"correct": "await showNotice('保存成功');",
|
|
49
|
+
"incorrect": "alert('保存成功');",
|
|
50
|
+
"execution": {
|
|
51
|
+
"method": "ESLint `no-restricted-globals`,含 prompt"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "CC-005",
|
|
56
|
+
"title": "不提交 debugger 语句",
|
|
57
|
+
"severity": "必须",
|
|
58
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
59
|
+
"summary": "`debugger` 会让程序在打开开发者工具时暂停,只适合本地排查。断点应设在调试工具里,提交前确保代码中没有这条语句。",
|
|
60
|
+
"correct": "function priceWithTax(net) {\n const gross = net * TAX_RATE;\n return gross;\n}",
|
|
61
|
+
"incorrect": "function priceWithTax(net) {\n debugger;\n const gross = net * TAX_RATE;\n return gross;\n}",
|
|
62
|
+
"execution": {
|
|
63
|
+
"method": "ESLint `no-debugger`(推荐集已含)"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"id": "CC-006",
|
|
68
|
+
"title": "尽量不写非空断言",
|
|
69
|
+
"severity": "建议",
|
|
70
|
+
"languages": ["TypeScript"],
|
|
71
|
+
"summary": "后缀 `!` 只是让编译器不再检查空值,运行时的值仍可能缺失。应先做判断,或用可选链和默认值处理缺失的情况。",
|
|
72
|
+
"correct": "const city = profile.address?.city ?? '未填写';",
|
|
73
|
+
"incorrect": "const city = profile.address!.city;",
|
|
74
|
+
"execution": {
|
|
75
|
+
"method": "ESLint `@typescript-eslint/no-non-null-assertion`"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"id": "CC-007",
|
|
80
|
+
"title": "设默认值时优先用空值合并运算符",
|
|
81
|
+
"severity": "建议",
|
|
82
|
+
"languages": ["TypeScript"],
|
|
83
|
+
"summary": "`||` 遇到 0、`false`、空字符串也会取默认值,这些值有效时就会被覆盖;`??` 只在左侧是 `undefined`、`null` 这类空值时才取默认值。需要保留有效假值的地方应使用 `??`。",
|
|
84
|
+
"correct": "const retries = config.retries ?? 3;",
|
|
85
|
+
"incorrect": "const retries = config.retries || 3; // 配置为 0 时仍得到 3",
|
|
86
|
+
"execution": {
|
|
87
|
+
"method": "建议,不进入评审"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"id": "CC-008",
|
|
92
|
+
"title": "用可选链代替逐层判空",
|
|
93
|
+
"severity": "建议",
|
|
94
|
+
"languages": ["TypeScript"],
|
|
95
|
+
"summary": "逐层写 `&&` 判空既冗长又容易漏掉某一层。`?.` 在前面的值为空时直接得到 `undefined`,写法更短也更安全。",
|
|
96
|
+
"correct": "const zip = order?.shipping?.zip;",
|
|
97
|
+
"incorrect": "const zip = order && order.shipping && order.shipping.zip;",
|
|
98
|
+
"execution": {
|
|
99
|
+
"method": "ESLint `@typescript-eslint/prefer-optional-chain`(类型感知车道)"
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"id": "CC-009",
|
|
104
|
+
"title": "返回 this 的方法以 this 作返回类型",
|
|
105
|
+
"severity": "必须",
|
|
106
|
+
"languages": ["TypeScript"],
|
|
107
|
+
"summary": "链式方法的返回类型若写成类名,子类实例调用后类型会退回父类,子类新增的方法就无法接着调用。声明为 `this` 才能保留调用方的实际类型。",
|
|
108
|
+
"correct": "class Query {\n take(size: number): this {\n this.size = size;\n return this;\n }\n}",
|
|
109
|
+
"incorrect": "class Query {\n take(size: number): Query {\n this.size = size;\n return this;\n }\n}",
|
|
110
|
+
"execution": {
|
|
111
|
+
"method": "ESLint `@typescript-eslint/prefer-return-this-type`(类型感知车道)"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"id": "CC-010",
|
|
116
|
+
"title": "注释写在代码上方或行尾",
|
|
117
|
+
"severity": "建议",
|
|
118
|
+
"languages": ["TypeScript"],
|
|
119
|
+
"summary": "注释写在被说明代码的上一行时,中间不留空行,缩进保持一致;写在行尾时,与代码至少隔一个空格,内容较长就移到上方。",
|
|
120
|
+
"correct": "// 汇总本月账单\nconst overall = sumBills(bills); // 单位:分",
|
|
121
|
+
"incorrect": "// 汇总本月账单\n\nconst overall = sumBills(bills);// 单位:分",
|
|
122
|
+
"execution": {
|
|
123
|
+
"method": "建议,不进入评审"
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
"id": "CC-011",
|
|
128
|
+
"title": "抑制类型检查的指令必须写明原因",
|
|
129
|
+
"severity": "必须",
|
|
130
|
+
"languages": ["TypeScript"],
|
|
131
|
+
"summary": "`@ts-expect-error`、`@ts-ignore`、`@ts-nocheck` 这类指令会关闭编译器检查,维护者需要知道这样做的理由,因此指令后要跟一段说明。",
|
|
132
|
+
"correct": "// @ts-expect-error: 第三方声明缺少 retries 字段\ndial({ retries: 2 });",
|
|
133
|
+
"incorrect": "// @ts-expect-error\ndial({ retries: 2 });",
|
|
134
|
+
"execution": {
|
|
135
|
+
"method": "ESLint `@typescript-eslint/ban-ts-comment`,ts-expect-error 须带说明"
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"id": "CC-012",
|
|
140
|
+
"title": "注释符后留一个空格",
|
|
141
|
+
"severity": "建议",
|
|
142
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
143
|
+
"summary": "`//`、`/*` 与注释文字之间留一个空格,读起来更清楚。",
|
|
144
|
+
"correct": "// 重新计算运费",
|
|
145
|
+
"incorrect": "//重新计算运费",
|
|
146
|
+
"execution": {
|
|
147
|
+
"method": "建议,不进入评审"
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"id": "CC-013",
|
|
152
|
+
"title": "忽略类型错误时优先用预期错误指令",
|
|
153
|
+
"severity": "建议",
|
|
154
|
+
"languages": ["TypeScript"],
|
|
155
|
+
"summary": "用 `@ts-expect-error` 代替 `@ts-ignore`。前者在下一行其实没有错误时会报告指令未被使用,能及时发现过期的抑制;后者无论有无错误都保持沉默。",
|
|
156
|
+
"correct": "// @ts-expect-error: 旧接口的参数声明有误\nlegacyPrint(42);",
|
|
157
|
+
"incorrect": "// @ts-ignore: 旧接口的参数声明有误\nlegacyPrint(42);",
|
|
158
|
+
"execution": {
|
|
159
|
+
"method": "ESLint `@typescript-eslint/ban-ts-comment`:禁 ts-ignore"
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"id": "CC-014",
|
|
164
|
+
"title": "交付的代码不留 TODO 和 FIXME",
|
|
165
|
+
"severity": "建议",
|
|
166
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
167
|
+
"summary": "这类标记在开发阶段可以用来提醒,交付前必须处理完或转入任务跟踪,不能随代码一起发布。",
|
|
168
|
+
"correct": "// 运费规则见配送模块",
|
|
169
|
+
"incorrect": "// TODO: 运费规则还没写完",
|
|
170
|
+
"execution": {
|
|
171
|
+
"method": "ESLint `no-warning-comments`"
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"id": "CC-015",
|
|
176
|
+
"title": "跨窗口通信必须严格核对来源",
|
|
177
|
+
"severity": "必须",
|
|
178
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
179
|
+
"summary": "`postMessage` 能在不同来源的窗口之间传递数据,目标或来源放得过宽时,恶意页面就能窃听或伪造消息。发送时写明目标来源,不用 `*`;接收时按白名单做完全匹配,不用 `indexOf` 这类部分匹配。",
|
|
180
|
+
"correct": "frame.postMessage(payload, ALLOWED_ORIGIN);\nwindow.addEventListener('message', (event) => {\n if (event.origin === ALLOWED_ORIGIN) {\n applyUpdate(event.data);\n }\n});",
|
|
181
|
+
"incorrect": "frame.postMessage(payload, '*');\nwindow.addEventListener('message', (event) => {\n applyUpdate(event.data);\n});",
|
|
182
|
+
"execution": {
|
|
183
|
+
"method": "clean-code 评审发送明确targetOrigin,接收核对origin与source"
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"id": "CC-016",
|
|
188
|
+
"title": "switch 要有默认分支",
|
|
189
|
+
"severity": "必须",
|
|
190
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
191
|
+
"summary": "没有默认分支时,未预料到的取值会被悄悄忽略。每个 `switch` 都写 `default`;确实无需默认处理时,在最后一个 `case` 下写注释 `// no default` 表明这是有意的。",
|
|
192
|
+
"correct": "switch (phase) {\n case 'paid':\n ship();\n break;\n default:\n hold();\n}",
|
|
193
|
+
"incorrect": "switch (phase) {\n case 'paid':\n ship();\n break;\n}",
|
|
194
|
+
"execution": {
|
|
195
|
+
"method": "ESLint `default-case`,commentPattern 为 `no default`"
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
"id": "CC-017",
|
|
200
|
+
"title": "有内容的 case 必须显式结束",
|
|
201
|
+
"severity": "必须",
|
|
202
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
203
|
+
"summary": "漏写 `break` 时,程序会落入下一个分支,运行本不该运行的代码。每个含语句的 `case` 都要显式结束,也不要故意设计贯穿逻辑。",
|
|
204
|
+
"correct": "switch (kind) {\n case 'refund':\n reverseCharge();\n break;\n default:\n applyCharge();\n}",
|
|
205
|
+
"incorrect": "switch (kind) {\n case 'refund':\n reverseCharge(); // 漏写 break,会继续运行下面的分支\n default:\n applyCharge();\n}",
|
|
206
|
+
"execution": {
|
|
207
|
+
"method": "ESLint `no-fallthrough`(推荐集已含)"
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"id": "CC-018",
|
|
212
|
+
"title": "在 case 中声明变量时用大括号包住",
|
|
213
|
+
"severity": "必须",
|
|
214
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
215
|
+
"summary": "`let`、`const`、`class` 的作用域是整个 `switch`,却要运行到所在分支才初始化,其他分支访问时就会出错。给该 `case` 加一对大括号,把声明限制在分支内部。",
|
|
216
|
+
"correct": "switch (unit) {\n case 'kg': {\n const grams = weight * 1000;\n return grams;\n }\n default:\n return weight;\n}",
|
|
217
|
+
"incorrect": "switch (unit) {\n case 'kg':\n const grams = weight * 1000;\n return grams;\n default:\n return weight;\n}",
|
|
218
|
+
"execution": {
|
|
219
|
+
"method": "ESLint `no-case-declarations`(推荐集已含)"
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"id": "CC-019",
|
|
224
|
+
"title": "else if 链以 else 收尾",
|
|
225
|
+
"severity": "建议",
|
|
226
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
227
|
+
"summary": "连续的 `else if` 容易漏掉某些情况。末尾补一个 `else`,像 `switch` 的默认分支那样兜住其余情形。",
|
|
228
|
+
"correct": "if (score >= 90) {\n grade = 'A';\n} else if (score >= 60) {\n grade = 'B';\n} else {\n grade = 'C';\n}",
|
|
229
|
+
"incorrect": "if (score >= 90) {\n grade = 'A';\n} else if (score >= 60) {\n grade = 'B';\n}",
|
|
230
|
+
"execution": {
|
|
231
|
+
"method": "建议,不进入评审"
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"id": "CC-020",
|
|
236
|
+
"title": "不用逻辑运算符代替 if",
|
|
237
|
+
"severity": "建议",
|
|
238
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
239
|
+
"summary": "靠短路特性决定是否运行某段代码,读者要多想一步才能看懂。需要条件执行时直接写 `if`。",
|
|
240
|
+
"correct": "if (hasDraft) {\n saveDraft();\n}",
|
|
241
|
+
"incorrect": "hasDraft && saveDraft();",
|
|
242
|
+
"execution": {
|
|
243
|
+
"method": "ESLint `@typescript-eslint/no-unused-expressions`(推荐集默认不允许短路语句),JSX 不受影响"
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
"id": "CC-021",
|
|
248
|
+
"title": "条件表达式里不做赋值",
|
|
249
|
+
"severity": "必须",
|
|
250
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
251
|
+
"summary": "在 `if`、`while` 等条件中赋值容易与比较混淆,常常是把 `===` 误写成了 `=`。赋值应放在条件之前。",
|
|
252
|
+
"correct": "if (quota === ceiling) {\n enforce();\n}",
|
|
253
|
+
"incorrect": "if (quota = ceiling) {\n enforce();\n}",
|
|
254
|
+
"execution": {
|
|
255
|
+
"method": "ESLint `no-cond-assign`(推荐集,except-parens 兼容例外 E1)"
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
"id": "CC-022",
|
|
260
|
+
"title": "控制语句的条件不宜过多",
|
|
261
|
+
"severity": "建议",
|
|
262
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
263
|
+
"summary": "`if` 或循环的条件堆得太多,既难读又容易改错,建议不超过 3 个。判断细节可以提取为命名清楚的布尔变量或函数。",
|
|
264
|
+
"correct": "const canShip = stock > 0 && !paused;\nconst isAllowed = buyer.verified && region === 'cn';\nif (canShip && isAllowed) {\n ship();\n}",
|
|
265
|
+
"incorrect": "if (stock > 0 && !paused && buyer.verified && region === 'cn') {\n ship();\n}",
|
|
266
|
+
"execution": {
|
|
267
|
+
"method": "clean-code 评审"
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
"id": "CC-023",
|
|
272
|
+
"title": "用 const 或 let 声明变量,不用 var",
|
|
273
|
+
"severity": "必须",
|
|
274
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
275
|
+
"summary": "`var` 的作用域是函数而不是代码块,还存在变量提升,容易产生意外。默认用 `const`,只有需要重新赋值时才用 `let`。",
|
|
276
|
+
"correct": "const batchSize = 20;\nlet offset = 0;\noffset += batchSize;",
|
|
277
|
+
"incorrect": "var batchSize = 20;\nvar offset = 0;",
|
|
278
|
+
"execution": {
|
|
279
|
+
"method": "ESLint `no-var` + ESLint `prefer-const`"
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
"id": "CC-024",
|
|
284
|
+
"title": "一条语句只声明一个变量",
|
|
285
|
+
"severity": "必须",
|
|
286
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
287
|
+
"summary": "把多个变量挤在一条声明里,不便单独加注释或调整,查看改动时也更费力。解构声明和 `for` 初始化部分不受此限。",
|
|
288
|
+
"correct": "let width = 0;\nlet height = 0;",
|
|
289
|
+
"incorrect": "let width = 0, height = 0;",
|
|
290
|
+
"execution": {
|
|
291
|
+
"method": "ESLint `one-var: never`,例外 E2"
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
"id": "CC-025",
|
|
296
|
+
"title": "不写连续赋值",
|
|
297
|
+
"severity": "必须",
|
|
298
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
299
|
+
"summary": "`a = b = c` 这样的写法不易看清赋值顺序,还可能意外创建全局变量。每个变量单独赋值。",
|
|
300
|
+
"correct": "const low = 0;\nconst high = 0;",
|
|
301
|
+
"incorrect": "const low = high = 0;",
|
|
302
|
+
"execution": {
|
|
303
|
+
"method": "ESLint `no-multi-assign`"
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
"id": "CC-026",
|
|
308
|
+
"title": "声明变量时不显式赋 undefined",
|
|
309
|
+
"severity": "必须",
|
|
310
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
311
|
+
"summary": "未赋值的 `let` 变量本来就是 `undefined`,显式写出来纯属多余。",
|
|
312
|
+
"correct": "let cached;",
|
|
313
|
+
"incorrect": "let cached = undefined;",
|
|
314
|
+
"execution": {
|
|
315
|
+
"method": "ESLint `no-undef-init`"
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
"id": "CC-027",
|
|
320
|
+
"title": "用字面量创建数组和对象",
|
|
321
|
+
"severity": "必须",
|
|
322
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
323
|
+
"summary": "字面量写法简短、意图清楚,还能避开 `Array` 构造函数在单个数字参数时只创建空位的陷阱。",
|
|
324
|
+
"correct": "const tags = [];\nconst lookup = {};",
|
|
325
|
+
"incorrect": "const tags = new Array();\nconst lookup = new Object();",
|
|
326
|
+
"execution": {
|
|
327
|
+
"method": "ESLint `no-array-constructor` + ESLint `no-object-constructor`"
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
"id": "CC-028",
|
|
332
|
+
"title": "内层变量不与外层同名",
|
|
333
|
+
"severity": "建议",
|
|
334
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
335
|
+
"summary": "内层声明与外层同名的变量会遮蔽外层变量,读者容易弄错当前用的是哪一个,修改时也容易出错。内层变量应另取名字。",
|
|
336
|
+
"correct": "const rate = 0.1;\nconst applyCustom = (customRate) => price * customRate;",
|
|
337
|
+
"incorrect": "const rate = 0.1;\nconst applyCustom = (rate) => price * rate;",
|
|
338
|
+
"execution": {
|
|
339
|
+
"method": "ESLint `@typescript-eslint/no-shadow`"
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
"id": "CC-029",
|
|
344
|
+
"title": "枚举成员显式赋值",
|
|
345
|
+
"severity": "必须",
|
|
346
|
+
"languages": ["TypeScript"],
|
|
347
|
+
"summary": "不赋值的成员按定义顺序从 0 递增,后续插入成员或调整次序就会改变已有成员的值。给每个成员写明取值,含义才不会随顺序变化。",
|
|
348
|
+
"correct": "enum Channel {\n Mail = 1,\n Sms = 2,\n}",
|
|
349
|
+
"incorrect": "enum Channel {\n Mail,\n Sms,\n}",
|
|
350
|
+
"execution": {
|
|
351
|
+
"method": "ESLint `@typescript-eslint/prefer-enum-initializers`"
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
"id": "CC-030",
|
|
356
|
+
"title": "枚举成员的值用字面量",
|
|
357
|
+
"severity": "必须",
|
|
358
|
+
"languages": ["TypeScript"],
|
|
359
|
+
"summary": "枚举表示一组确定的常量。用变量或函数结果赋值时,取值要到运行时才知道,失去了这种确定性。",
|
|
360
|
+
"correct": "enum Tier {\n Basic = 'basic',\n Pro = 'pro',\n}",
|
|
361
|
+
"incorrect": "enum Tier {\n Basic = plans.basic,\n Pro = plans.pro,\n}",
|
|
362
|
+
"execution": {
|
|
363
|
+
"method": "ESLint `@typescript-eslint/prefer-literal-enum-member`"
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
"id": "CC-031",
|
|
368
|
+
"title": "规范地抛出和捕获异常",
|
|
369
|
+
"severity": "建议",
|
|
370
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
371
|
+
"summary": "只抛出 `Error` 或其子类的实例,并用 `new` 构造,不抛字符串等其他值;内置类型不够用时再自定义错误类型。捕获后确实无需处理的,要写注释说明原因,不留空的 `catch`。",
|
|
372
|
+
"correct": "throw new RangeError('库存不足');\n\ntry {\n await flushCache();\n} catch (reason) {\n // 缓存写入失败不影响主流程,下次启动时会重建\n}",
|
|
373
|
+
"incorrect": "throw '库存不足';\n\ntry {\n await flushCache();\n} catch (reason) {}",
|
|
374
|
+
"execution": {
|
|
375
|
+
"method": "ESLint `@typescript-eslint/only-throw-error`(类型感知)+ ESLint `no-empty`"
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
"id": "CC-032",
|
|
380
|
+
"title": "Promise 拒绝时传 Error 对象",
|
|
381
|
+
"severity": "建议",
|
|
382
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
383
|
+
"summary": "`Error` 对象自带调用栈,排查时能定位拒绝发生的位置;字符串等其他值没有这些信息。",
|
|
384
|
+
"correct": "return Promise.reject(new Error('网络超时'));",
|
|
385
|
+
"incorrect": "return Promise.reject('网络超时');",
|
|
386
|
+
"execution": {
|
|
387
|
+
"method": "ESLint `@typescript-eslint/prefer-promise-reject-errors`(类型感知)"
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
"id": "CC-033",
|
|
392
|
+
"title": "finally 块必须正常结束",
|
|
393
|
+
"severity": "必须",
|
|
394
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
395
|
+
"summary": "在 `finally` 中使用 `return`、`throw`、`break` 或 `continue`,会覆盖 `try` 和 `catch` 里的返回值或异常,掩盖真实结果。`finally` 只做清理,清理中可能出现的异常也要在内部处理。",
|
|
396
|
+
"correct": "function readPage(file) {\n try {\n return file.read();\n } finally {\n file.close();\n }\n}",
|
|
397
|
+
"incorrect": "function readPage(file) {\n try {\n return file.read();\n } finally {\n throw new Error('关闭失败');\n }\n}",
|
|
398
|
+
"execution": {
|
|
399
|
+
"method": "ESLint `no-unsafe-finally`(推荐集已含)"
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
"id": "CC-034",
|
|
404
|
+
"title": "比较时变量在左、常量在右",
|
|
405
|
+
"severity": "建议",
|
|
406
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
407
|
+
"summary": "把常量写在比较符左边不符合日常语序,读起来别扭。应让会变化的值在左,固定的值在右。",
|
|
408
|
+
"correct": "if (phase === 'draft') {\n publish();\n}",
|
|
409
|
+
"incorrect": "if ('draft' === phase) {\n publish();\n}",
|
|
410
|
+
"execution": {
|
|
411
|
+
"method": "ESLint `yoda`"
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
"id": "CC-035",
|
|
416
|
+
"title": "用严格相等判断",
|
|
417
|
+
"severity": "必须",
|
|
418
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
419
|
+
"summary": "`==` 和 `!=` 会先做隐式类型转换,出现空字符串等于 0 这类结果。使用 `===` 和 `!==`,类型和值都一致才成立。`== null` 可用来同时判断 `null` 与 `undefined`。",
|
|
420
|
+
"correct": "const reached = sold === quota;",
|
|
421
|
+
"incorrect": "const reached = sold == quota;",
|
|
422
|
+
"execution": {
|
|
423
|
+
"method": "ESLint `eqeqeq`,null 例外(E3)"
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
"id": "CC-036",
|
|
428
|
+
"title": "不嵌套三元表达式",
|
|
429
|
+
"severity": "必须",
|
|
430
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
431
|
+
"summary": "三元表达式层层嵌套后很难看清分支走向,应改用 `if` 语句或提取函数。",
|
|
432
|
+
"correct": "function bandOf(risk) {\n if (risk > 9) {\n return 'high';\n }\n return risk > 4 ? 'mid' : 'low';\n}",
|
|
433
|
+
"incorrect": "const band = risk > 9 ? 'high' : risk > 4 ? 'mid' : 'low';",
|
|
434
|
+
"execution": {
|
|
435
|
+
"method": "ESLint `no-nested-ternary`"
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
"id": "CC-037",
|
|
440
|
+
"title": "混用运算符时用括号表明优先级",
|
|
441
|
+
"severity": "建议",
|
|
442
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
443
|
+
"summary": "即使优先级本身没错,不同运算符混在一起时也应加括号,让后来的读者不必回忆优先级表。自增、自减有副作用,也不要和其他运算混在一个表达式里。",
|
|
444
|
+
"correct": "const canEnter = (isAdmin && hasPass) || isPublic;",
|
|
445
|
+
"incorrect": "const canEnter = isAdmin && hasPass || isPublic;",
|
|
446
|
+
"execution": {
|
|
447
|
+
"method": "clean-code 评审;不启用 no-mixed-operators,避免与 Prettier 删括号冲突"
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
{
|
|
451
|
+
"id": "CC-038",
|
|
452
|
+
"title": "正则中的连续空格写明个数",
|
|
453
|
+
"severity": "建议",
|
|
454
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
455
|
+
"summary": "在正则里连续敲多个空格,很难看出到底是几个,也容易打错。应使用量词写明数量。",
|
|
456
|
+
"correct": "const gap = /tax {4}rate/;",
|
|
457
|
+
"incorrect": "const gap = /tax rate/;",
|
|
458
|
+
"execution": {
|
|
459
|
+
"method": "ESLint `no-regex-spaces`(推荐集已含)"
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
"id": "CC-039",
|
|
464
|
+
"title": "正则捕获组尽量命名",
|
|
465
|
+
"severity": "建议",
|
|
466
|
+
"languages": ["TypeScript"],
|
|
467
|
+
"summary": "命名捕获组让匹配结果可以按含义取值,不依赖括号序号,正则调整后也不容易取错。",
|
|
468
|
+
"correct": "const stamp = /(?<year>\\d{4})-(?<month>\\d{2})/;",
|
|
469
|
+
"incorrect": "const stamp = /(\\d{4})-(\\d{2})/;",
|
|
470
|
+
"execution": {
|
|
471
|
+
"method": "建议,不进入评审"
|
|
472
|
+
}
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
"id": "CC-040",
|
|
476
|
+
"title": "单行不宜过长",
|
|
477
|
+
"severity": "建议",
|
|
478
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
479
|
+
"summary": "过长的语句、表达式、参数和注释都要换行,避免阅读时横向滚动。具体行宽以格式化工具的配置为准。",
|
|
480
|
+
"correct": "",
|
|
481
|
+
"incorrect": "",
|
|
482
|
+
"execution": {
|
|
483
|
+
"method": "Prettier printWidth 140;不设 max-len"
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
{
|
|
487
|
+
"id": "CC-041",
|
|
488
|
+
"title": "折行时运算符放在行尾",
|
|
489
|
+
"severity": "建议",
|
|
490
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
491
|
+
"summary": "语句过长需要折行时,把运算符留在上一行末尾,让读者一眼看出下一行是延续。",
|
|
492
|
+
"correct": "const isReady = hasPayment &&\n hasAddress;",
|
|
493
|
+
"incorrect": "const isReady = hasPayment\n && hasAddress;",
|
|
494
|
+
"execution": {
|
|
495
|
+
"method": "以 Prettier 输出为准(二元运算符换行留在行末)"
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
"id": "CC-042",
|
|
500
|
+
"title": "属性较多的对象字面量逐行书写",
|
|
501
|
+
"severity": "建议",
|
|
502
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
503
|
+
"summary": "对象字面量要么全写在一行,要么每个属性各占一行;属性超过 4 个时建议逐行书写。",
|
|
504
|
+
"correct": "const parcel = {\n width: 10,\n height: 20,\n depth: 5,\n weight: 2,\n fragile: true,\n};",
|
|
505
|
+
"incorrect": "const parcel = { width: 10, height: 20, depth: 5, weight: 2, fragile: true };",
|
|
506
|
+
"execution": {
|
|
507
|
+
"method": "与 Prettier 按行宽折行冲突,只报告无意义"
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
"id": "CC-043",
|
|
512
|
+
"title": "过长的链式调用分行书写",
|
|
513
|
+
"severity": "建议",
|
|
514
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
515
|
+
"summary": "一行内连续调用方法不超过 4 次,超过时换行,续行缩进并以 `.` 开头,表明这是同一条调用链。",
|
|
516
|
+
"correct": "rows\n .filter(isOpen)\n .map(toCard)\n .sort(byDate)\n .slice(0, 5)\n .forEach(render);",
|
|
517
|
+
"incorrect": "rows.filter(isOpen).map(toCard).sort(byDate).slice(0, 5).forEach(render);",
|
|
518
|
+
"execution": {
|
|
519
|
+
"method": "同上"
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
"id": "CC-044",
|
|
524
|
+
"title": "用空格分隔关键字",
|
|
525
|
+
"severity": "建议",
|
|
526
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
527
|
+
"summary": "关键字与括号之间加空格,使代码结构更醒目;函数名与调用括号之间、数组中括号内侧不加空格。",
|
|
528
|
+
"correct": "if (isLate) {\n notify([primary, secondary]);\n}",
|
|
529
|
+
"incorrect": "if(isLate) {\n notify ([ primary, secondary ]);\n}",
|
|
530
|
+
"execution": {
|
|
531
|
+
"method": "core/format:Prettier"
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
"id": "CC-045",
|
|
536
|
+
"title": "控制语句的执行体都加大括号",
|
|
537
|
+
"severity": "建议",
|
|
538
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
539
|
+
"summary": "省略大括号时,后续增加语句很容易落在分支之外,代码也不够清楚。`if`、`for`、`do`、`while` 的执行体一律用大括号包起来。",
|
|
540
|
+
"correct": "if (isEmpty) {\n return;\n}",
|
|
541
|
+
"incorrect": "if (isEmpty) return;",
|
|
542
|
+
"execution": {
|
|
543
|
+
"method": "ESLint `curly: all`"
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
"id": "CC-046",
|
|
548
|
+
"title": "语句末尾写分号",
|
|
549
|
+
"severity": "建议",
|
|
550
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
551
|
+
"summary": "依赖自动补分号时,下一行若以括号或方括号开头,就可能和上一行连在一起解析。每条语句都写分号。",
|
|
552
|
+
"correct": "const overall = base + fee;\n[primary, secondary].forEach(audit);",
|
|
553
|
+
"incorrect": "const overall = base + fee\n[primary, secondary].forEach(audit); // 会被解析成 fee[...]",
|
|
554
|
+
"execution": {
|
|
555
|
+
"method": "Prettier semi: true"
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
{
|
|
559
|
+
"id": "CC-047",
|
|
560
|
+
"title": "类型中的方法写成函数属性",
|
|
561
|
+
"severity": "建议",
|
|
562
|
+
"languages": ["TypeScript"],
|
|
563
|
+
"summary": "在 `interface` 和 `type` 中统一用属性加函数类型的形式声明方法,风格一致,参数类型的检查也更严格。",
|
|
564
|
+
"correct": "interface Printer {\n print: (page: number) => boolean;\n}",
|
|
565
|
+
"incorrect": "interface Printer {\n print(page: number): void;\n}",
|
|
566
|
+
"execution": {
|
|
567
|
+
"method": "ESLint `@typescript-eslint/method-signature-style: property`"
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
"id": "CC-048",
|
|
572
|
+
"title": "用 ES 模块组织代码,不用命名空间",
|
|
573
|
+
"severity": "建议",
|
|
574
|
+
"languages": ["TypeScript"],
|
|
575
|
+
"summary": "TypeScript 的 `module` 和 `namespace` 写法已经过时,也不利于构建工具移除无用代码。代码应通过 `import` 和 `export` 组织。",
|
|
576
|
+
"correct": "// tax.ts\nexport const vat = (net: number) => net * 0.2;",
|
|
577
|
+
"incorrect": "namespace Tax {\n export const vat = (net: number) => net * 0.2;\n}",
|
|
578
|
+
"execution": {
|
|
579
|
+
"method": "ESLint `@typescript-eslint/no-namespace`(推荐集已含)"
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
"id": "CC-049",
|
|
584
|
+
"title": "函数不宜过长",
|
|
585
|
+
"severity": "建议",
|
|
586
|
+
"languages": ["TypeScript"],
|
|
587
|
+
"summary": "短函数更容易理解、修改和测试。函数总长建议不超过 100 行,其中业务逻辑最好控制在 50 行以内(不计空行和注释),争取在一屏内看完。",
|
|
588
|
+
"correct": "",
|
|
589
|
+
"incorrect": "",
|
|
590
|
+
"execution": {
|
|
591
|
+
"method": "ESLint `max-lines-per-function: 100`(.ts;.tsx 关闭)"
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
"id": "CC-050",
|
|
596
|
+
"title": "代码块嵌套不超过 4 层",
|
|
597
|
+
"severity": "建议",
|
|
598
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
599
|
+
"summary": "嵌套过深的代码难以阅读和修改。代码块最多嵌套 4 层,超过时用提前返回或提取函数减少层级。",
|
|
600
|
+
"correct": "for (const batch of batches) {\n if (batch.done) {\n continue;\n }\n settleBatch(batch);\n}",
|
|
601
|
+
"incorrect": "for (const batch of batches) {\n if (!batch.done) {\n for (const entry of batch.entries) {\n if (entry.overdue) {\n if (entry.valid) {\n settle(entry); // 第 5 层\n }\n }\n }\n }\n}",
|
|
602
|
+
"execution": {
|
|
603
|
+
"method": "ESLint `max-depth: 4`"
|
|
604
|
+
}
|
|
605
|
+
},
|
|
606
|
+
{
|
|
607
|
+
"id": "CC-051",
|
|
608
|
+
"title": "回调嵌套不超过 4 层",
|
|
609
|
+
"severity": "建议",
|
|
610
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
611
|
+
"summary": "回调一层套一层,代码会迅速变得难读。嵌套不超过 4 层,可改用 `async`/`await` 或把回调提取为命名函数。",
|
|
612
|
+
"correct": "const session = await login();\nconst cart = await loadCart(session);\nconst bill = await priceCart(cart);\nawait pay(bill);",
|
|
613
|
+
"incorrect": "login((session) => {\n loadCart(session, (cart) => {\n priceCart(cart, (bill) => {\n pay(bill, (receipt) => {\n mailReceipt(receipt, () => {\n finish(); // 第 5 层\n });\n });\n });\n });\n});",
|
|
614
|
+
"execution": {
|
|
615
|
+
"method": "建议,不进入评审"
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
"id": "CC-052",
|
|
620
|
+
"title": "带默认值的参数放在最后",
|
|
621
|
+
"severity": "建议",
|
|
622
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
623
|
+
"summary": "默认参数排在末尾,调用时才能直接省略;排在前面时仍要为它占位。第三方接口规定的固定签名除外。",
|
|
624
|
+
"correct": "function paginate(rows, size = 20) {\n return rows.slice(0, size);\n}",
|
|
625
|
+
"incorrect": "function paginate(size = 20, rows) {\n return rows.slice(0, size);\n}",
|
|
626
|
+
"execution": {
|
|
627
|
+
"method": "ESLint `@typescript-eslint/default-param-last`,例外 E7"
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
{
|
|
631
|
+
"id": "CC-053",
|
|
632
|
+
"title": "函数各路径的返回方式保持一致",
|
|
633
|
+
"severity": "必须",
|
|
634
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
635
|
+
"summary": "函数要么所有路径都返回值,要么都不返回。只有部分分支写 `return` 时,其余路径会隐式得到 `undefined`,调用方难以预料。",
|
|
636
|
+
"correct": "function discountOf(member) {\n if (member.vip) {\n return 0.8;\n }\n return 1;\n}",
|
|
637
|
+
"incorrect": "function discountOf(member) {\n if (member.vip) {\n return 0.8;\n }\n}",
|
|
638
|
+
"execution": {
|
|
639
|
+
"method": "建议,不进入评审"
|
|
640
|
+
}
|
|
641
|
+
},
|
|
642
|
+
{
|
|
643
|
+
"id": "CC-054",
|
|
644
|
+
"title": "不用 Function 构造函数创建函数",
|
|
645
|
+
"severity": "必须",
|
|
646
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
647
|
+
"summary": "`new Function` 接受字符串作函数体,与 `eval` 一样有代码注入风险,也绕开了静态检查。应使用函数声明或箭头函数。",
|
|
648
|
+
"correct": "const double = (figure) => figure * 2;",
|
|
649
|
+
"incorrect": "const double = new Function('figure', 'return figure * 2');",
|
|
650
|
+
"execution": {
|
|
651
|
+
"method": "ESLint `no-new-func`"
|
|
652
|
+
}
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"id": "CC-055",
|
|
656
|
+
"title": "不改写函数参数",
|
|
657
|
+
"severity": "建议",
|
|
658
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
659
|
+
"summary": "给参数重新赋值后,读者分不清它是否还是传入的值,还会影响 `arguments`。需要变化时另声明一个局部变量。",
|
|
660
|
+
"correct": "function clamp(risk) {\n const bounded = Math.min(risk, 10);\n return bounded;\n}",
|
|
661
|
+
"incorrect": "function clamp(risk) {\n risk = Math.min(risk, 10);\n return risk;\n}",
|
|
662
|
+
"execution": {
|
|
663
|
+
"method": "ESLint `no-param-reassign`(props: false)"
|
|
664
|
+
}
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
"id": "CC-056",
|
|
668
|
+
"title": "用 rest 参数代替 arguments",
|
|
669
|
+
"severity": "必须",
|
|
670
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
671
|
+
"summary": "rest 参数是真正的数组,可以直接调用数组方法,而 `arguments` 只是类数组,含义也不够明确。rest 参数只能放在参数列表最后。",
|
|
672
|
+
"correct": "function sumAll(...amounts) {\n return amounts.reduce(plus, 0);\n}",
|
|
673
|
+
"incorrect": "function sumAll() {\n return Array.from(arguments).reduce(plus, 0);\n}",
|
|
674
|
+
"execution": {
|
|
675
|
+
"method": "ESLint `prefer-rest-params`"
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
"id": "CC-057",
|
|
680
|
+
"title": "匿名函数优先写成箭头函数",
|
|
681
|
+
"severity": "必须",
|
|
682
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
683
|
+
"summary": "箭头函数语法简洁,`this` 沿用定义处的值,不必再用变量保存 `this`,后一种写法容易让人混淆。",
|
|
684
|
+
"correct": "rows.forEach((row) => {\n this.render(row);\n});",
|
|
685
|
+
"incorrect": "const owner = this;\nrows.forEach(function (row) {\n owner.render(row);\n});",
|
|
686
|
+
"execution": {
|
|
687
|
+
"method": "ESLint `prefer-arrow-callback`"
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
{
|
|
691
|
+
"id": "CC-058",
|
|
692
|
+
"title": "只导出不可变的绑定",
|
|
693
|
+
"severity": "必须",
|
|
694
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
695
|
+
"summary": "导出 `var` 或 `let` 变量后,它的每次变化都会影响所有引用方,很难追踪。应导出 `const`;确需变化的状态,导出读写它的函数。",
|
|
696
|
+
"correct": "let revision = 0;\nexport const currentRevision = () => revision;\nexport const bumpRevision = () => {\n revision += 1;\n};",
|
|
697
|
+
"incorrect": "export let revision = 0;",
|
|
698
|
+
"execution": {
|
|
699
|
+
"method": "不引入 import 插件,用 ESLint `no-restricted-syntax` 禁 `export let/var`"
|
|
700
|
+
}
|
|
701
|
+
},
|
|
702
|
+
{
|
|
703
|
+
"id": "CC-059",
|
|
704
|
+
"title": "类和构造函数用大驼峰命名",
|
|
705
|
+
"severity": "建议",
|
|
706
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
707
|
+
"summary": "类和构造函数的名字首字母大写,通常是名词或名词短语,如 `Invoice`、`ShoppingCart`,不用动词,也不用含义模糊的泛称。",
|
|
708
|
+
"correct": "class ShoppingCart {}\nconst cart = new ShoppingCart();",
|
|
709
|
+
"incorrect": "class shoppingCart {}\nconst cart = new shoppingCart();",
|
|
710
|
+
"execution": {
|
|
711
|
+
"method": "ESLint `@typescript-eslint/naming-convention`:class/type 为 PascalCase"
|
|
712
|
+
}
|
|
713
|
+
},
|
|
714
|
+
{
|
|
715
|
+
"id": "CC-060",
|
|
716
|
+
"title": "函数用小驼峰命名",
|
|
717
|
+
"severity": "建议",
|
|
718
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
719
|
+
"summary": "函数名首字母小写,后续单词首字母大写,通常用动词或动词短语说明它做什么。",
|
|
720
|
+
"correct": "function calculateShipping() {}",
|
|
721
|
+
"incorrect": "function CALCULATE_SHIPPING() {}",
|
|
722
|
+
"execution": {
|
|
723
|
+
"method": "ESLint `@typescript-eslint/naming-convention`:函数 camelCase 或 PascalCase,PascalCase 用于 React 组件"
|
|
724
|
+
}
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
"id": "CC-061",
|
|
728
|
+
"title": "布尔变量不用否定式命名",
|
|
729
|
+
"severity": "建议",
|
|
730
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
731
|
+
"summary": "布尔变量建议以 `is`、`has`、`can`、`should` 等开头,但不要用否定含义的名字,否则 `!isNotVisible` 这样的双重否定很难理解。",
|
|
732
|
+
"correct": "const isVisible = true;",
|
|
733
|
+
"incorrect": "const isNotVisible = false;",
|
|
734
|
+
"execution": {
|
|
735
|
+
"method": "ESLint `@typescript-eslint/naming-convention`:布尔变量与参数不用否定式(类型感知)"
|
|
736
|
+
}
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
"id": "CC-062",
|
|
740
|
+
"title": "不用保留字和受限名称命名变量",
|
|
741
|
+
"severity": "建议",
|
|
742
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
743
|
+
"summary": "用保留字或 `undefined`、`NaN`、`Infinity` 这类受限名称命名变量,轻则造成混淆,重则导致语法错误。应换一个含义相近的词。",
|
|
744
|
+
"correct": "function scale(ratio) {\n return ratio * 2;\n}",
|
|
745
|
+
"incorrect": "function scale(NaN) {\n return NaN * 2;\n}",
|
|
746
|
+
"execution": {
|
|
747
|
+
"method": "ESLint `no-shadow-restricted-names`(推荐集已含)"
|
|
748
|
+
}
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
"id": "CC-063",
|
|
752
|
+
"title": "文件命名统一,扩展名小写",
|
|
753
|
+
"severity": "建议",
|
|
754
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
755
|
+
"summary": "项目内的文件名沿用同一种命名风格,扩展名一律小写,避免在区分大小写的系统上找不到文件。",
|
|
756
|
+
"correct": "// 文件名:invoice-summary.ts",
|
|
757
|
+
"incorrect": "// 文件名:Invoice_summary.TS",
|
|
758
|
+
"execution": {
|
|
759
|
+
"method": "建议,不进入评审"
|
|
760
|
+
}
|
|
761
|
+
},
|
|
762
|
+
{
|
|
763
|
+
"id": "CC-064",
|
|
764
|
+
"title": "new 调用始终带括号",
|
|
765
|
+
"severity": "建议",
|
|
766
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
767
|
+
"summary": "构造函数没有参数时虽然可以省略括号,但这会让代码风格不统一,因此始终写上括号。",
|
|
768
|
+
"correct": "const queue = new TaskQueue();",
|
|
769
|
+
"incorrect": "const queue = new TaskQueue;",
|
|
770
|
+
"execution": {
|
|
771
|
+
"method": "Prettier 统一输出 `new Foo()`"
|
|
772
|
+
}
|
|
773
|
+
},
|
|
774
|
+
{
|
|
775
|
+
"id": "CC-065",
|
|
776
|
+
"title": "对象属性名优先用标识符",
|
|
777
|
+
"severity": "建议",
|
|
778
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
779
|
+
"summary": "属性名写成标识符更易读,也更利于语法高亮和引擎优化。只有名称中含连字符、空格等字符时才加引号。",
|
|
780
|
+
"correct": "const sizes = { small: 1, large: 3, 'x-large': 5 };",
|
|
781
|
+
"incorrect": "const sizes = { 'small': 1, 'large': 3 };",
|
|
782
|
+
"execution": {
|
|
783
|
+
"method": "Prettier quoteProps: as-needed(默认)"
|
|
784
|
+
}
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
"id": "CC-066",
|
|
788
|
+
"title": "属性访问优先用点号",
|
|
789
|
+
"severity": "必须",
|
|
790
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
791
|
+
"summary": "点号简洁易读,也更利于代码压缩。只有属性名是变量或需要计算时才用方括号。",
|
|
792
|
+
"correct": "const title = book.title;\nconst figure = book[fieldName];",
|
|
793
|
+
"incorrect": "const title = book['title'];",
|
|
794
|
+
"execution": {
|
|
795
|
+
"method": "ESLint `dot-notation`"
|
|
796
|
+
}
|
|
797
|
+
},
|
|
798
|
+
{
|
|
799
|
+
"id": "CC-067",
|
|
800
|
+
"title": "不在实例上直接调用原型的内置方法",
|
|
801
|
+
"severity": "必须",
|
|
802
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
803
|
+
"summary": "实例可能有同名属性遮蔽原型方法,也可能根本没有原型。例如服务端解析请求体后直接调用 `hasOwnProperty`,对方传入同名字段就能让程序崩溃。应使用 `Object.hasOwn`,或借助 `call` 调用原型上的方法。",
|
|
804
|
+
"correct": "const known = Object.hasOwn(payload, 'token');",
|
|
805
|
+
"incorrect": "const known = payload.hasOwnProperty('token');",
|
|
806
|
+
"execution": {
|
|
807
|
+
"method": "ESLint `no-prototype-builtins`(推荐集已含)"
|
|
808
|
+
}
|
|
809
|
+
},
|
|
810
|
+
{
|
|
811
|
+
"id": "CC-068",
|
|
812
|
+
"title": "限定 for-in 的遍历范围",
|
|
813
|
+
"severity": "必须",
|
|
814
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
815
|
+
"summary": "`for-in` 会连同原型链上继承的可枚举属性一起遍历,可能出现意料之外的键。循环内要过滤掉继承属性,或改用 `Object.keys` 这类只取自身属性的方法。",
|
|
816
|
+
"correct": "for (const sku of Object.keys(stock)) {\n restock(sku);\n}",
|
|
817
|
+
"incorrect": "for (const sku in stock) {\n restock(sku);\n}",
|
|
818
|
+
"execution": {
|
|
819
|
+
"method": "ESLint `guard-for-in`"
|
|
820
|
+
}
|
|
821
|
+
},
|
|
822
|
+
{
|
|
823
|
+
"id": "CC-069",
|
|
824
|
+
"title": "不修改内置对象的原型",
|
|
825
|
+
"severity": "必须",
|
|
826
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
827
|
+
"summary": "内置对象是所有代码共用的接口,改动或扩展其原型会破坏约定的行为,还可能与其他库或未来的语言标准冲突。需要新功能时写独立的工具函数。",
|
|
828
|
+
"correct": "function quarterOf(date) {\n return Math.ceil((date.getMonth() + 1) / 3);\n}",
|
|
829
|
+
"incorrect": "Date.prototype.quarter = function () {\n return Math.ceil((this.getMonth() + 1) / 3);\n};",
|
|
830
|
+
"execution": {
|
|
831
|
+
"method": "ESLint `no-extend-native`"
|
|
832
|
+
}
|
|
833
|
+
},
|
|
834
|
+
{
|
|
835
|
+
"id": "CC-070",
|
|
836
|
+
"title": "安全用途的随机数必须密码学安全",
|
|
837
|
+
"severity": "必须",
|
|
838
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
839
|
+
"summary": "`Math.random` 的结果可能被预测,不能用于令牌、密钥、盐值、验证码等安全场景。这些场景要用 `crypto` 提供的安全随机数。",
|
|
840
|
+
"correct": "const bytes = crypto.getRandomValues(new Uint8Array(16));",
|
|
841
|
+
"incorrect": "const resetCode = Math.floor(Math.random() * 1000000);",
|
|
842
|
+
"execution": {
|
|
843
|
+
"method": "clean-code 评审(安全语义)"
|
|
844
|
+
}
|
|
845
|
+
},
|
|
846
|
+
{
|
|
847
|
+
"id": "CC-071",
|
|
848
|
+
"title": "废弃代码直接删除,不留成注释",
|
|
849
|
+
"severity": "必须",
|
|
850
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
851
|
+
"summary": "注释掉的代码无人维护,日后恢复时容易带入隐患;不会运行到的代码也多半是错误。不用的代码直接删除,需要时从版本历史找回。",
|
|
852
|
+
"correct": "refreshList();",
|
|
853
|
+
"incorrect": "refreshList();\n// if (isStale()) {\n// purgeCache();\n// }",
|
|
854
|
+
"execution": {
|
|
855
|
+
"method": "clean-code 评审(只看新增注释)"
|
|
856
|
+
}
|
|
857
|
+
},
|
|
858
|
+
{
|
|
859
|
+
"id": "CC-072",
|
|
860
|
+
"title": "发布内容不含开发者个人信息",
|
|
861
|
+
"severity": "必须",
|
|
862
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
863
|
+
"summary": "发布的代码和注释一旦带有姓名、工号、部门、邮箱、问题单号等,就可能暴露具体人员,带来社会工程攻击的风险。这些信息应在发布前移除。",
|
|
864
|
+
"correct": "// 按季度汇总退款记录",
|
|
865
|
+
"incorrect": "// 作者:张三,工号 00123",
|
|
866
|
+
"execution": {
|
|
867
|
+
"method": "clean-code 评审"
|
|
868
|
+
}
|
|
869
|
+
},
|
|
870
|
+
{
|
|
871
|
+
"id": "CC-073",
|
|
872
|
+
"title": "代码中不写公网地址",
|
|
873
|
+
"severity": "必须",
|
|
874
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
875
|
+
"summary": "代码或脚本里藏着用户不知情的公网 IP 或邮箱,容易引起用户的疑虑。这类地址应放到配置中管理。标准协议必须的地址和广播地址这类特殊地址除外。",
|
|
876
|
+
"correct": "const endpoint = readConfig('syncHost');",
|
|
877
|
+
"incorrect": "const endpoint = '198.51.100.24';",
|
|
878
|
+
"execution": {
|
|
879
|
+
"method": "security 评审场景 11(发布包公网地址),例外 E8"
|
|
880
|
+
}
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
"id": "CC-074",
|
|
884
|
+
"title": "源文件统一用 UTF-8 编码",
|
|
885
|
+
"severity": "建议",
|
|
886
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
887
|
+
"summary": "所有源文件连同其中的注释都用 UTF-8 保存,避免在不同环境下出现乱码。",
|
|
888
|
+
"correct": "",
|
|
889
|
+
"incorrect": "",
|
|
890
|
+
"execution": {
|
|
891
|
+
"method": ".editorconfig charset=utf-8"
|
|
892
|
+
}
|
|
893
|
+
},
|
|
894
|
+
{
|
|
895
|
+
"id": "CC-075",
|
|
896
|
+
"title": "不使用 with 语句",
|
|
897
|
+
"severity": "必须",
|
|
898
|
+
"languages": ["TypeScript"],
|
|
899
|
+
"summary": "`with` 内的名称可能指向对象属性,也可能指向局部变量,语义不清,容易改变程序本意,严格模式下也不允许使用。应直接通过对象访问属性。",
|
|
900
|
+
"correct": "draft.subject = '周报';",
|
|
901
|
+
"incorrect": "with (draft) {\n subject = '周报';\n}",
|
|
902
|
+
"execution": {
|
|
903
|
+
"method": "TS 解析与严格模式已拒绝 with"
|
|
904
|
+
}
|
|
905
|
+
},
|
|
906
|
+
{
|
|
907
|
+
"id": "CC-076",
|
|
908
|
+
"title": "小数点前后的 0 不省略",
|
|
909
|
+
"severity": "必须",
|
|
910
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
911
|
+
"summary": "`.5` 和 `5.` 这样的写法里,小数点很容易被看漏,也可能与成员访问混淆。小数点两边都要有数字。",
|
|
912
|
+
"correct": "const ratio = 0.5;\nconst scale = 2.0;",
|
|
913
|
+
"incorrect": "const ratio = .5;\nconst scale = 2.;",
|
|
914
|
+
"execution": {
|
|
915
|
+
"method": "Prettier 自动补前导 0、去尾随点,不需要 no-floating-decimal"
|
|
916
|
+
}
|
|
917
|
+
},
|
|
918
|
+
{
|
|
919
|
+
"id": "CC-077",
|
|
920
|
+
"title": "类型注解用统一的写法",
|
|
921
|
+
"severity": "建议",
|
|
922
|
+
"languages": ["TypeScript"],
|
|
923
|
+
"summary": "基础类型用小写的 `string`、`number`、`boolean`,不用包装类型。也不用宽泛的 `Function` 和 `{}`:前者失去了参数和返回值检查,连类都能通过;后者实际表示任意非空值,而不是对象。",
|
|
924
|
+
"correct": "let title: string;\nlet onDone: () => void;\nlet extra: Record<string, unknown>;",
|
|
925
|
+
"incorrect": "let title: String;\nlet onDone: Function;\nlet extra: {};",
|
|
926
|
+
"execution": {
|
|
927
|
+
"method": "ESLint `@typescript-eslint/no-wrapper-object-types`、no-unsafe-function-type、no-empty-object-type(推荐集已含)"
|
|
928
|
+
}
|
|
929
|
+
},
|
|
930
|
+
{
|
|
931
|
+
"id": "CC-078",
|
|
932
|
+
"title": "用 isNaN 判断 NaN",
|
|
933
|
+
"severity": "必须",
|
|
934
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
935
|
+
"summary": "`NaN` 与任何值都不相等,包括它自身,所以 `value === NaN` 永远为假。判断时要用 `Number.isNaN` 或全局的 `isNaN`。",
|
|
936
|
+
"correct": "const invalid = Number.isNaN(parsed);",
|
|
937
|
+
"incorrect": "const invalid = parsed === NaN;",
|
|
938
|
+
"execution": {
|
|
939
|
+
"method": "ESLint `use-isnan`(推荐集已含)"
|
|
940
|
+
}
|
|
941
|
+
},
|
|
942
|
+
{
|
|
943
|
+
"id": "CC-079",
|
|
944
|
+
"title": "浮点数不直接判等",
|
|
945
|
+
"severity": "必须",
|
|
946
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
947
|
+
"summary": "浮点运算存在精度误差,算出的结果可能与数学上相等的值不同,不能用 `==` 或 `===` 判断。应比较两者之差是否在允许误差内,项目有统一的容差函数时优先使用。",
|
|
948
|
+
"correct": "const balanced = Math.abs(net + tax - gross) < TOLERANCE;",
|
|
949
|
+
"incorrect": "const balanced = net + tax === gross;",
|
|
950
|
+
"execution": {
|
|
951
|
+
"method": "clean-code 评审;时间、坐标、金额等比较经项目统一的容差函数"
|
|
952
|
+
}
|
|
953
|
+
},
|
|
954
|
+
{
|
|
955
|
+
"id": "CC-080",
|
|
956
|
+
"title": "类型成员用分号分隔",
|
|
957
|
+
"severity": "建议",
|
|
958
|
+
"languages": ["TypeScript"],
|
|
959
|
+
"summary": "接口、类和类型别名的多行成员都以 `;` 结尾,不用逗号,也不省略。单行声明的最后一个成员可以不写。",
|
|
960
|
+
"correct": "interface Size {\n width: number;\n height: number;\n}",
|
|
961
|
+
"incorrect": "interface Size {\n width: number,\n height: number\n}",
|
|
962
|
+
"execution": {
|
|
963
|
+
"method": "Prettier 输出分号分隔"
|
|
964
|
+
}
|
|
965
|
+
},
|
|
966
|
+
{
|
|
967
|
+
"id": "CC-081",
|
|
968
|
+
"title": "字符串优先用单引号",
|
|
969
|
+
"severity": "建议",
|
|
970
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
971
|
+
"summary": "全项目统一用单引号,风格一致,字符串里含双引号(如 HTML 属性)时也不必转义。",
|
|
972
|
+
"correct": "const badge = '<b class=\"mark\">新</b>';",
|
|
973
|
+
"incorrect": "const badge = \"<b class=\\\"mark\\\">新</b>\";",
|
|
974
|
+
"execution": {
|
|
975
|
+
"method": "Prettier singleQuote"
|
|
976
|
+
}
|
|
977
|
+
},
|
|
978
|
+
{
|
|
979
|
+
"id": "CC-082",
|
|
980
|
+
"title": "对象类型优先用 interface",
|
|
981
|
+
"severity": "建议",
|
|
982
|
+
"languages": ["TypeScript"],
|
|
983
|
+
"summary": "描述对象结构时,接口和类型别名作用相近,为统一起见优先用 `interface`。基础类型别名、联合类型、元组这些接口表达不了的,才用 `type`。",
|
|
984
|
+
"correct": "type Currency = 'cny' | 'usd';\ninterface Price {\n amount: number;\n currency: Currency;\n}",
|
|
985
|
+
"incorrect": "type Price = {\n amount: number;\n currency: Currency;\n};",
|
|
986
|
+
"execution": {
|
|
987
|
+
"method": "建议,不进入评审"
|
|
988
|
+
}
|
|
989
|
+
},
|
|
990
|
+
{
|
|
991
|
+
"id": "CC-083",
|
|
992
|
+
"title": "拼接字符串用模板字符串",
|
|
993
|
+
"severity": "建议",
|
|
994
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
995
|
+
"summary": "含变量的字符串用模板字符串,比 `+` 拼接更直观,也不容易漏掉空格。",
|
|
996
|
+
"correct": "const tip = `还剩 ${left} 件`;",
|
|
997
|
+
"incorrect": "const tip = '还剩 ' + left + ' 件';",
|
|
998
|
+
"execution": {
|
|
999
|
+
"method": "ESLint `prefer-template`"
|
|
1000
|
+
}
|
|
1001
|
+
},
|
|
1002
|
+
{
|
|
1003
|
+
"id": "CC-084",
|
|
1004
|
+
"title": "不用反斜杠续行写字符串",
|
|
1005
|
+
"severity": "建议",
|
|
1006
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1007
|
+
"summary": "行尾用反斜杠接续字符串时,反斜杠后若多一个空格就会出错,而且很难发现。长字符串应分段拼接或使用模板字符串。",
|
|
1008
|
+
"correct": "const notice = '维护结束后页面将恢复,' +\n '请稍后再试。';",
|
|
1009
|
+
"incorrect": "const notice = '维护结束后页面将恢复,\\\n 请稍后再试。';",
|
|
1010
|
+
"execution": {
|
|
1011
|
+
"method": "ESLint `no-multi-str`"
|
|
1012
|
+
}
|
|
1013
|
+
},
|
|
1014
|
+
{
|
|
1015
|
+
"id": "CC-085",
|
|
1016
|
+
"title": "可选成员用问号标注,不写 undefined 联合",
|
|
1017
|
+
"severity": "建议",
|
|
1018
|
+
"languages": ["TypeScript"],
|
|
1019
|
+
"summary": "可选成员和可选参数用 `?` 标注即可,不必再联合 `undefined`。类型别名本身也不要包含 `undefined`,确有需要时在使用处联合。",
|
|
1020
|
+
"correct": "interface Ticket {\n seat?: string;\n}\nfunction book(seat?: string) {}",
|
|
1021
|
+
"incorrect": "interface Ticket {\n seat: string | undefined;\n}\nfunction book(seat: string | undefined) {}",
|
|
1022
|
+
"execution": {
|
|
1023
|
+
"method": "clean-code 评审"
|
|
1024
|
+
}
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
"id": "CC-086",
|
|
1028
|
+
"title": "数组只存放按序排列的元素",
|
|
1029
|
+
"severity": "必须",
|
|
1030
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1031
|
+
"summary": "数组虽然也是对象,但给它加非数字属性(`length` 除外)容易出错,也让遍历结果难以预料。需要键值结构时用 `Map` 或普通对象。",
|
|
1032
|
+
"correct": "const prices = new Map();\nprices.set('apple', 5);",
|
|
1033
|
+
"incorrect": "const prices = [];\nprices['apple'] = 5;",
|
|
1034
|
+
"execution": {
|
|
1035
|
+
"method": "clean-code 评审"
|
|
1036
|
+
}
|
|
1037
|
+
},
|
|
1038
|
+
{
|
|
1039
|
+
"id": "CC-087",
|
|
1040
|
+
"title": "遍历数组优先用数组方法",
|
|
1041
|
+
"severity": "建议",
|
|
1042
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1043
|
+
"summary": "遍历数组优先用 `map`、`filter`、`reduce`、`forEach` 等方法或 `for-of`,不用 `for-in`,后者取到的是字符串下标,还可能带上非数字属性。对性能敏感的热路径可以保留普通 `for` 循环。",
|
|
1044
|
+
"correct": "const subtotal = cart.reduce((acc, entry) => acc + entry.cost, 0);",
|
|
1045
|
+
"incorrect": "let subtotal = 0;\nfor (const idx in cart) {\n subtotal += cart[idx].cost;\n}",
|
|
1046
|
+
"execution": {
|
|
1047
|
+
"method": "clean-code 评审(热路径可保留 for)"
|
|
1048
|
+
}
|
|
1049
|
+
},
|
|
1050
|
+
{
|
|
1051
|
+
"id": "CC-088",
|
|
1052
|
+
"title": "遍历数组时不增删其元素",
|
|
1053
|
+
"severity": "必须",
|
|
1054
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1055
|
+
"summary": "在 `forEach` 等循环中增删元素会打乱下标,导致元素被跳过或重复处理。应先用 `filter` 等生成新数组,或者遍历副本。",
|
|
1056
|
+
"correct": "const active = jobs.filter((job) => !job.canceled);",
|
|
1057
|
+
"incorrect": "jobs.forEach((job, idx) => {\n if (job.canceled) {\n jobs.splice(idx, 1);\n }\n});",
|
|
1058
|
+
"execution": {
|
|
1059
|
+
"method": "clean-code 评审"
|
|
1060
|
+
}
|
|
1061
|
+
},
|
|
1062
|
+
{
|
|
1063
|
+
"id": "CC-089",
|
|
1064
|
+
"title": "用展开语法或 concat 复制数组",
|
|
1065
|
+
"severity": "建议",
|
|
1066
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1067
|
+
"summary": "手写循环逐项复制既冗长又容易出错。用 `[...source]`、`concat` 或 `slice()` 更简洁,得到的都是浅拷贝。",
|
|
1068
|
+
"correct": "const backup = [...queue];",
|
|
1069
|
+
"incorrect": "const backup = [];\nfor (let idx = 0; idx < queue.length; idx += 1) {\n backup[idx] = queue[idx];\n}",
|
|
1070
|
+
"execution": {
|
|
1071
|
+
"method": "clean-code 评审,例外 E9"
|
|
1072
|
+
}
|
|
1073
|
+
},
|
|
1074
|
+
{
|
|
1075
|
+
"id": "CC-090",
|
|
1076
|
+
"title": "类型转换要写得明确",
|
|
1077
|
+
"severity": "必须",
|
|
1078
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1079
|
+
"summary": "`+text`、`~list.indexOf(item)` 这类隐式转换技巧不易读懂,应使用 `Number()`、`String()`、`Boolean()` 或明确的比较。",
|
|
1080
|
+
"correct": "const qty = Number(text);\nconst hasComma = text.includes(',');\nconst codeLabel = String(phase);",
|
|
1081
|
+
"incorrect": "const qty = +text;\nconst hasComma = ~text.indexOf(',');\nconst codeLabel = '' + phase;",
|
|
1082
|
+
"execution": {
|
|
1083
|
+
"method": "ESLint `no-implicit-coercion`"
|
|
1084
|
+
}
|
|
1085
|
+
},
|
|
1086
|
+
{
|
|
1087
|
+
"id": "CC-091",
|
|
1088
|
+
"title": "转数字时用 Number() 并检查 NaN",
|
|
1089
|
+
"severity": "建议",
|
|
1090
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1091
|
+
"summary": "转换为数字时用 `Number()`,它校验整个字符串,失败时得到 `NaN`,结果要显式检查。减法等隐式转换和只解析前缀的 `parseInt` 都可能得到与预期不符的结果。",
|
|
1092
|
+
"correct": "const width = Number(input);\nif (!Number.isNaN(width)) {\n resize(width);\n}",
|
|
1093
|
+
"incorrect": "const width = parseInt(input, 10); // '12px' 也会得到 12",
|
|
1094
|
+
"execution": {
|
|
1095
|
+
"method": "clean-code 评审"
|
|
1096
|
+
}
|
|
1097
|
+
},
|
|
1098
|
+
{
|
|
1099
|
+
"id": "CC-092",
|
|
1100
|
+
"title": "不在浏览器数据库中存敏感数据",
|
|
1101
|
+
"severity": "必须",
|
|
1102
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1103
|
+
"summary": "Web SQL 和 IndexedDB 的数据以明文落在客户端,本机用户可以读取,XSS 脚本也能操作它们;加密只是把风险转移到密钥上。因此敏感数据不得写入这两类数据库。",
|
|
1104
|
+
"correct": "store.put({ theme: 'dark', rowsPerPage: 20 }, 'prefs');",
|
|
1105
|
+
"incorrect": "store.put({ bankCard: cardNumber, cvv }, 'prefs');",
|
|
1106
|
+
"execution": {
|
|
1107
|
+
"method": "clean-code 评审"
|
|
1108
|
+
}
|
|
1109
|
+
},
|
|
1110
|
+
{
|
|
1111
|
+
"id": "CC-093",
|
|
1112
|
+
"title": "不在 Web Storage 中存敏感数据",
|
|
1113
|
+
"severity": "必须",
|
|
1114
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1115
|
+
"summary": "`localStorage` 和 `sessionStorage` 没有防 XSS 的机制,页面上的脚本都能读取;即使加密,密钥外泄后数据同样失守。口令、令牌等敏感数据不得写入,状态持久化时只保存明确列出的字段。",
|
|
1116
|
+
"correct": "localStorage.setItem('theme', theme);",
|
|
1117
|
+
"incorrect": "localStorage.setItem('sessionToken', sessionToken);",
|
|
1118
|
+
"execution": {
|
|
1119
|
+
"method": "clean-code 评审;状态库持久化只写白名单字段"
|
|
1120
|
+
}
|
|
1121
|
+
},
|
|
1122
|
+
{
|
|
1123
|
+
"id": "CC-094",
|
|
1124
|
+
"title": "谨慎绕过 Angular 的内容净化",
|
|
1125
|
+
"severity": "建议",
|
|
1126
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1127
|
+
"summary": "Angular 中 `DomSanitizer` 以 `bypassSecurityTrust` 开头的函数(覆盖 HTML、脚本、样式、链接和资源链接)会跳过内置净化,传入不可信内容就有 XSS 风险。优先不用;富文本等确需使用时,必须保证参数来源可信,例如内容已在服务端净化。",
|
|
1128
|
+
"correct": "this.preview = this.sanitizer.sanitize(SecurityContext.HTML, draftHtml);",
|
|
1129
|
+
"incorrect": "this.preview = this.sanitizer.bypassSecurityTrustHtml(draftHtml);",
|
|
1130
|
+
"execution": {
|
|
1131
|
+
"method": "项目使用 Angular 时核查 DomSanitizer 信任边界;不使用时不适用"
|
|
1132
|
+
}
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
"id": "CC-095",
|
|
1136
|
+
"title": "React 中不直接写入 HTML 字符串",
|
|
1137
|
+
"severity": "必须",
|
|
1138
|
+
"languages": ["TypeScript", "JavaScript"],
|
|
1139
|
+
"summary": "`dangerouslySetInnerHTML` 把字符串直接写入 DOM,相当于使用 `innerHTML`,内容不可信时会造成 XSS。应让 React 渲染文本或组件。",
|
|
1140
|
+
"correct": "const Note = ({ text }) => <p>{text}</p>;",
|
|
1141
|
+
"incorrect": "const Note = ({ text }) => <p dangerouslySetInnerHTML={{ __html: text }} />;",
|
|
1142
|
+
"execution": {
|
|
1143
|
+
"method": "ESLint `no-restricted-syntax` 禁 dangerouslySetInnerHTML(不引入 eslint-plugin-react)"
|
|
1144
|
+
}
|
|
1145
|
+
},
|
|
1146
|
+
{
|
|
1147
|
+
"id": "CC-096",
|
|
1148
|
+
"title": "不用外部数据拼接系统命令",
|
|
1149
|
+
"severity": "必须",
|
|
1150
|
+
"languages": ["JavaScript"],
|
|
1151
|
+
"summary": "把不可信数据拼进命令字符串会造成命令注入。能枚举命令和参数时用白名单限定,不能枚举时必须过滤或转义管道、分号、重定向等特殊符号,并优先用 `execFile` 或 `spawn` 以参数数组传入。",
|
|
1152
|
+
"correct": "import { execFile } from 'node:child_process';\n\nexecFile('tar', ['-xf', archivePath]);",
|
|
1153
|
+
"incorrect": "import { exec } from 'node:child_process';\n\nexec(`tar -xf ${archivePath}`);",
|
|
1154
|
+
"execution": {
|
|
1155
|
+
"method": "ESLint `no-restricted-imports` 禁 child_process 的 exec/execSync(只允许 execFile/spawn)+ security 评审场景 4"
|
|
1156
|
+
}
|
|
1157
|
+
},
|
|
1158
|
+
{
|
|
1159
|
+
"id": "CC-097",
|
|
1160
|
+
"title": "Vue 中不使用 v-html",
|
|
1161
|
+
"severity": "必须",
|
|
1162
|
+
"languages": ["JavaScript"],
|
|
1163
|
+
"summary": "`v-html` 把字符串作为 HTML 直接写入页面,内容不可信时会造成 XSS。优先用文本插值;确需渲染富文本时,必须先净化内容。",
|
|
1164
|
+
"correct": "<p>{{ reviewText }}</p>",
|
|
1165
|
+
"incorrect": "<p v-html=\"reviewText\"></p>",
|
|
1166
|
+
"execution": {
|
|
1167
|
+
"method": "项目使用 Vue 时核查 v-html;不使用时不适用"
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
]
|
|
1171
|
+
}
|