TypeSpec PR 代码审查
code-review
依据项目自身规则审查 TypeSpec 拉取请求:Tier 0-3 破坏性变更策略、`.chronus` 变更日志要求、编译器 API 使用、测试框架约定以及 emitter 输出稳定性。在本仓库中审查 diff 或拉取请求时使用。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 5.9k
- Forks
- 423
01 前言 Front Matter
- name
- code-review
- description
- Review TypeSpec pull requests against the project's own rules: the Tier 0-3 breaking change policy, `.chronus` changelog requirements, compiler API usage, test framework conventions, and emitter output stability. Use when reviewing a diff or pull request in this repository.
02 资源文件 6
- 脚本scripts/find_by_name.py7.3 KB
- 脚本scripts/grep_search.py8.7 KB
- 脚本scripts/list_dir.py3.8 KB
- 脚本scripts/patch_text.py11.6 KB
- 脚本scripts/view_file.py5.2 KB
- 脚本scripts/write_to_file.py3.4 KB
03 SKILL.md 正文
TypeSpec code review
Review the diff against the rules this project actually enforces. Every comment must point at a concrete problem in the changed code and cite the rule or code that makes it a problem — if you cannot cite it, do not post it.
Scope
Applies to the whole repository: the pnpm workspace packages (packages/compiler, packages/http,
packages/openapi3, packages/json-schema, packages/versioning, …), the language emitter
packages (packages/http-client-{csharp,java,python}), .chronus/changes/**, and their tests.
For the language emitter packages, §1 (tier policy) does not apply to their internal APIs and their
own rules in .github/instructions/*.instructions.md take precedence — but §2 (changeset), §3
(compiler API usage) and §4 (tests) apply everywhere.
Out of scope — do not comment on:
- Generated artifacts:
pnpm-lock.yaml, generated Python/C#/Java code undergenerator/,tests/generated/, and generated reference docs underwebsite/src/content/docs/docs/libraries/*/reference/**. - Formatting and lint-covered issues — prettier (
pnpm format) and oxlint own those. - Naming/style preferences, or unchanged code surrounding the diff.
Read before judging
Consult these and cite them in findings:
website/src/content/docs/docs/handbook/breaking-change-policy.mdx— the authoritative tier table and the bug exception..chronus/config.yaml— the validchangeKindvalues and thechangedFilesexclusions..github/copilot-instructions.md— changelog message guidelines and the TDD expectation.- Sibling tests in the touched package — the established tester/assertion patterns.
- The public compiler API (
@typespec/compiler) and@typespec/compiler/testingexports — to confirm a used API exists and behaves as the change assumes.
1. Breaking changes and public surface
Decide which tier each change touches, then whether it breaks an existing spec or consumer. Adding new syntax, types, or capabilities is not breaking; changing how something that already worked behaves is.
- Tier 0 — language syntax/semantics, the type graph (
Type), default TypeKits, exported helper and accessor functions. Flag: an existing valid spec that now parses differently, fails, or produces a different type graph; a removed/renamed export or changed signature; a changed runtime result for existing input; a removed or repurposed field on aType. Adding a required field to aTypeis allowed by policy but breaks downstream construction — call it out. - Tier 1 — the AST (
Node,SyntaxKind). Appended enum members and new union variants are allowed (TypeScript exhaustiveness errors are explicitly not breaking). Flag renumbered enums (members inserted rather than appended), removed node kinds, and changed shapes of existing nodes. - Tier 2 — checker, symbols,
@internal//internals//experimentalexports, template declarations, stdout/stderr output. Not breaking; mention only if a change here silently alters observable behavior. - Tier 3 — formatter and emitter output. Semantically equivalent output changes are not breaking; see §5 for the risk that matters.
- Bug exception. A technically breaking change that fixes clearly unintended behavior is not a breaking change — but say so explicitly rather than staying silent.
A change you judge breaking that has no breaking changeset is the highest-value finding in a
review. Raise it first.
2. Changeset (.chronus)
- Valid
changeKindvalues are exactly:internal,fix,dependencies,feature,deprecation,breaking. Anything else (feat,docs,patch,minor,major) is invalid. - The kind must match reality: a new capability is
feature, a fix isfix, and anything breaking per §1 isbreaking. - One entry per package and per change type. A single entry bundling a feature in one package with a fix in another must be split.
packages:must list exactly the affected packages.- Every package with a user-visible change needs an entry. Missing one is a finding — but note the
exclusions in
.chronus/config.yaml:**/*.md, test files, andpackages/*/test/**don't require a changeset, so docs-only or test-only changes legitimately have none. - Message quality: describes the change from the user's perspective; area tag only when targeting a
secondary area of a multi-area package, in
[bracket]form (e.g.[converter],[formatter]) — no genericcore -prefixes; afeatureentry includes a short illustrative code block, a simple fix does not.
3. Compiler API usage
Judge whether the change uses the TypeSpec compiler API the way the compiler expects, not merely whether it type-checks.
- Diagnostic targets decide whether a diagnostic can be suppressed. A diagnostic reported with
NoTargetcan never be suppressed (packages/compiler/src/core/program.ts—target === NoTargetreturns early before the#suppresslookup), and a diagnostic withseverity: "error"can never be suppressed either (suppressing one producessuppress-error). So a diagnostic the user is meant to be able to silence must be awarningand carry a real node/type target. Flag any target that resolves toNoTargeton a realistic input as "not actually suppressible". - Pick the most precise target available, falling back deliberately: the offending type → the
service namespace →
program.getGlobalNamespaceType()→NoTargetonly for genuinely program-wide failures with no node to point at. - Don't assume the program has a service.
listServices(program)returns a possibly empty array;[0]isundefinedfor model-only or service-less programs. Same for any lookup that can returnundefined— ask what happens in that case, and whether the diagnostic is still reachable and suppressible there. - Severity changes have control-flow consequences.
errorblocks emission viaprogram.hasError(); downgrading towarningmeans everything downstream now runs in a state it previously never saw. Verify the continue path is safe and intended. - Diagnostic hygiene. Report through the library's
reportDiagnostic/createDiagnosticwith a catalog entry and stable code; neverthrowfor a user error. The message text must still match the behavior after the change — a message saying the emitter "requires" something it now merely warns about is a finding. - Prefer stable public APIs. Use exported helpers and TypeKits over the checker, symbols, or AST
(Tier 2, see §1); use
resolvePath/joinPathsrather thannode:path; exclude template declarations withisTemplateDeclarationwhen navigating types.
4. Tests
- Every new code path, branch, diagnostic, and public API needs a test co-located in the package's
test/**. New behavior with no test is a finding. - Reuse the package's shared tester. Packages export one from
test/test-host.tsortest/tester.ts(e.g.packages/http/test/test-host.ts,packages/compiler/test/tester.ts). CallingcreateTester(...)inline in a new test file duplicates library/import setup and drifts — flag it and point at the existing module. - Assert diagnostics with the helpers. Use
expectDiagnostics/expectDiagnosticEmptyfrom@typespec/compiler/testing, not hand-rolled checks likeprogram.diagnostics.some((x) => x.code.endsWith(...)), which silently pass when the code, severity, or target is wrong. - Exercise the real entrypoint. A test must drive the shipped path (
$onEmit, the decorator, the emitter run) rather than re-implement the logic or call a helper in isolation — otherwise it passes while the actual behavior is broken. - Verify imports resolve. If a test imports a helper from
src, confirm that symbol is actually exported there; an import of a non-existent export makes the whole file fail to load. - Tests must be meaningful: they should fail if the change were reverted. Flag tests that only smoke run or assert nothing specific to the new behavior.
- Beyond the happy path: error and diagnostic paths, empty/anonymous forms, templates, and negative cases — including the fallback branches identified in §3.
- Flag any weakening of existing coverage:
.skip/.only, deleted assertions, or matchers broadened to make a change pass.
5. Emitter output stability
For packages/{openapi3,openapi,json-schema,http,http-server-*}:
- Unexplained snapshot changes are the headline risk. Every changed snapshot must be an intentional, explained consequence of the change. A snapshot diff for specs unrelated to the feature means existing users' output changed — raise it.
- Inline-vs-hoist, component naming, and declaration-creation changes must preserve behavior for existing named types and only affect genuinely new inputs. Anonymous/empty-named types inline; named types hoist without key collisions.
$refand component-key correctness: reordering name validation relative to ref generation must not silently drop validation that previously errored — if that is an intended fix, it needs to be called out and changeset'd.- Emitters must handle new type-graph shapes defensively (new node kinds, new union variants) rather than crash on them.
How to comment
- One issue per comment, anchored on the offending line.
- State the problem, cite the rule (policy section,
.chronus/config.yaml, guideline) or the relevantfile:line, and suggest the concrete fix. - One or two sentences. No preamble, no summarizing the diff back to the author.
- Don't repeat a point another comment already makes.
- Lead with correctness, compatibility, and coverage. Minor suggestions are welcome only when they are actionable and specific.
04 同仓库更多 Skills
http-client-python Emitter 构建与测试
test-unbranded-emitter
构建并测试 @typespec/http-client-python emitter。当用户想要测试其对 emitter 的本地变更、运行生成器测试套件、检查其 http-client-python 变更是否通过,或验证某项修复时使用。触发短语包括“test the emitter”(测试 emitter)、“run tests”(运行测试)、“check if my changes pass”(检查我的变更是否通过)、“run test:generator”(运行 test:generator)、“run CI”(运行 CI),或在 emitter 包中提及任何测试/验证变更的表述。
TypeSpec 生成 Python SDK 代码
generate-from-typespec
使用本地 emitter 从 TypeSpec 规范生成 Python SDK 代码。当用户想要从 TypeSpec 规范生成或重新生成 Python 客户端,提供了 TypeSpec 项目的 GitHub URL 或本地路径,或说出类似“generate from this spec”(从此规范生成)、“emit Python from this tsp”(从此 tsp 生成 Python)、“regenerate the SDK”(重新生成 SDK)、“compile this TypeSpec for Python”(将此 TypeSpec 编译为 Python)等表述时使用。
Emitter 生成代码与上游基线对比
diff-upstream
重新生成本地 emitter,并将生成的代码与已检入 autorest.python 的上游基线进行对比。当用户想查看其 emitter 变更对生成代码的影响(相较于当前生产环境的差异),或说出类似“diff upstream”(与上游对比)、“compare to baseline”(与基线比较)、“what changed vs production”(相较生产环境有哪些变化)、“show me the diff”(给我看差异)、“how does this affect generated code”(这如何影响生成代码)等表述,或希望验证其变更能产生预期的输出差异时使用。
Python SDK Spector 模拟 API 测试编写
writing-python-sdk-spector-mock-api-tests
根据 Spector 用例编写 TypeSpec http-client-python generator 的模拟 API 测试(azure/unbranded/shared)。当给定 Spector 用例链接,或修改 http-specs/azure-http-specs 下 Spector 用例的 PR 链接时使用。
pnpm 代码格式化
pnpm-format
使用 pnpm 格式化代码库,并从常见的格式化失败中恢复。
TypeSpec Bug 最小复现构建
minimal-repro
帮助创建 TypeSpec 编译器或 emitter bug 的最小复现。当用户提供 TypeSpec 源码(粘贴的代码或 GitHub URL)并描述问题时使用。在本地复现并缩减为单文件复现。