审计代码中的重复界面组件
只读检查代码库中的界面组件,比较重复实现、使用位置与可访问性,并整理合并迁移建议。
完整提示词
<context>
Codebases that grew across teams end up with four buttons, three modals and a dozen card-like boxes, each with slightly different props, styles and accessibility behaviour. A visual audit of screens (audit-design-consistency) shows the symptom; this audit finds the cause in code: which components duplicate each other, where they are used, how they differ, and what it would take to merge them. Name matching alone misleads: `Button`, `Btn`, `PrimaryAction` and `CTA` may be one role, while two components named `Card` may be different things. Grouping must look at the rendered element, the props interface, the styling and the behaviour.
</context>
<task>
Audit {{repo_path}} ({{framework}}) for duplicate and near-duplicate UI components. This is a read-only analysis: do not modify, create or delete any source file.
{{#component_dirs}}Component locations:
{{component_dirs}}
{{/component_dirs}}
1. **Discover.** If no locations were given, find component folders and files by the framework's conventions. Exclude tests, stories, generated code, node_modules and vendored code. List what you scanned.
2. **Inventory.** For each component, record: name, path, the root element or primitive it renders, its props or inputs with types, styling source, the interactive behaviour (click, keyboard, focus trap, portal), accessibility attributes, and its number of import sites (count with search tools such as ripgrep, and include re-exports).
3. **Group by role.** Cluster components that serve the same UI role (button, link-as-button, text input, select, modal or dialog, drawer, tooltip, card, badge or tag, alert or toast, tabs, table, avatar, icon, layout primitives). Use evidence: the same root element, overlapping props (similar names such as `variant`, `kind`, `type`), similar styles, similar markup structure, and similar usage contexts. Record the evidence for each grouping and a confidence (high, medium, low).
4. **Compare within each group.** Show a difference matrix: props supported, visual variants, states handled, accessibility behaviour (for example, which modal traps focus and restores it, which button handles `disabled` and loading correctly), and test coverage. Identify the strongest candidate to keep, or note that none is good enough and the group needs a new system component.
5. **One-off variants.** Find components that wrap a shared component only to change one style or prop, and local copies of a design-system component (similar name and structure to a component in the shared package).
6. **Consolidation plan.** For each group: the target component, the props or variants it must gain to cover the others, a mapping from each duplicate's props to the target's API, the number of call sites to migrate, an effort estimate in t-shirt sizes with the reason, the risk (visual change, behaviour change, test gaps), and a suggested order that starts with high-use, low-risk groups. Suggest codemod candidates where the prop mapping is mechanical.
7. **Verify.** Re-run the import counts for the top five groups to confirm them, and spot-check two groups by opening the files to confirm they really share a role.
</task>
<constraints>
- Read-only. Shell use is limited to listing, searching and reading files and running existing analysis scripts; do not run builds that write output into the source tree, and do not install anything.
- Report actual counts from the search; do not estimate usage.
- Mark low-confidence groupings clearly so the reader can check them.
- Do not recommend deleting a component that has no evidence of duplication just because it is rarely used.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Summary
Components scanned, duplicate groups found, call sites affected, and the top three consolidations by value.
## Method
Folders scanned, exclusions and the grouping evidence used.
## Duplicate groups
Per group: a table (Component, Path, Root element, Key props, Import sites, A11y notes), the difference matrix, the recommended target and confidence.
## One-off variants
| Component | Wraps or copies | Difference | Suggested handling |
## Consolidation plan
| Order | Group | Target | API changes | Call sites | Effort | Risk | Codemod? |
## Risks
Behaviour or accessibility differences that a migration could break.
## Verification
Searches re-run and spot checks, with their results.
</output_format>可替换内容
- {{repo_path}}
- {{framework}}
- {{#component_dirs}}
- {{component_dirs}}
- {{/component_dirs}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、组件库审计、前端代码维护、设计系统治理