跳到正文
cccskills
免费在 GitHub 公开中文介绍

split-large-file

本技能指导你先为现有行为编写特征测试,再把超过 1200 行的源文件拆成职责清晰的模块,保留原有行为、公开导入路径和导出名称,并通过测试验证。

原文查看原文说明

Refactor a source file over 1200 lines into a directory of cohesive modules without changing behavior, test-first. Use when asked to split, decompose, or shrink a large file (拆分大文件, 拆文件, 重构大文件); with no file given, pick one.

lobehub/lobehub星标 83,500(GitHub 83,500 + cccskills 0)

实测评分 未实测

查看安装方法

适用场景

  • 拆分超长源文件
  • 保持行为不变并模块化
  • 规划重构前后的测试

中文介绍

功能

本技能介绍如何以测试优先的方式拆分大型源文件,在不改变行为的前提下整理成职责清晰的模块。首先阅读目标文件及其测试,查找拆分边界和所有引用,并规划目录结构;随后添加用于记录现有行为的测试,再移动文件和测试,逐个提取模块。整个过程要保留公开导入路径和导出名称,使拆分后的每个文件不超过 1200 行,并按场景整理测试,最后验证改动。

适用场景

已确定要拆分哪个文件时可以使用;如果没有指定文件,也可以让技能从超长文件中选择候选。它适用于依据可观察行为、状态或处理阶段进行重构,而不是单纯按行数切开文件。

使用示例

  • “请在不改变行为的前提下拆分这个文件。”
  • “请从超长文件中选一个候选并规划拆分方案。”
  • “请整理拆分后需要运行的测试。”

注意事项

先参考 source-layout 了解文件放置和命名规则。拆分时发现的缺陷应作为单独改动处理。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

文档检查

AI 根据 SKILL.md 的内容检查了需要注意的操作和使用前提。评分只依据实际运行的结果(实测对比)。

安全性发送数据、删除文件、执行命令等是否有说明
B部分不足资料会移动和编辑文件、运行测试,并说明如何撤销测试变异。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足资料使用 Git、命令行工具、bun 和 Vitest,但环境安装说明有限。

不计入评分。 由 AI(OpenAI Codex)于 2026年10月26日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(1)

  • SKILL.md7.6 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Split a Large File

Split one file into a directory of modules without changing behavior. Where the pieces go, what they are named, and where tests live are owned by source-layout; read it first. This skill owns the procedure: lock behavior with tests, move, extract, then split the tests.

The split changes no behavior. If you find a bug on the way, note it and fix it in a separate commit or PR after the split.

1. Pick the file

Given a file, use it. Otherwise build a shortlist:

git ls-files -z -- 'src/*.ts' 'src/*.tsx' 'apps/*.ts' 'apps/*.tsx' 'packages/*.ts' 'packages/*.tsx' \
  | rg --null-data -v '(^|/)(__tests__|__mocks__|fixtures|locales)/|\.(test|spec)\.|\.d\.ts$' \
  | xargs -0 wc -l | awk '$1 > 1200 && $NF != "total"' | sort -rn | head -30

The shortlist still contains data tables and generated code; filter it by reading. Prefer a file with:

  • A real extraction boundary: hand-written logic with separable state, sub-domains, or stages, not a lookup table.
  • A repeatable verification surface: behavior that unit or integration tests can observe (including the real-database tests testing requires for models), not one that needs a browser session or live third-party calls.
  • A reviewable scope: one PR can hold the split. When this repository is mounted as a submodule, split a file of the submodule only through a PR to the submodule.

Use recent churn (git log --since=3.months --oneline -- <file> | wc -l) only to break ties. State the choice and the reason before starting.

2. Read and plan

Read the whole file and its tests. Find every reference with rg on the module path and basename across source roots: imports, re-exports, dynamic import(), vi.mock('…/Foo'), explicit extensions, and package entry mappings.

Write a short plan before editing:

  • the real boundaries (state, reuse, render update, sub-domain, pipeline stage — see react and source-layout §3) and the module each becomes;
  • the directory layout from source-layout §4, with expected line counts;
  • duplicated branches that will collapse into one module, if any;
  • each behavior the split moves or merges, mapped to the test that will guard it.

When a boundary or layout decision is unclear, ask an independent reviewer (a different-vendor harness when available) to critique the plan.

3. Lock the behavior with characterization tests

Existing tests usually cover the happy path and miss the branches a split is most likely to break. Before editing, record the baseline commit and the original path, then:

  1. Run the existing tests by explicit path; record the pass count.
  2. Pin every branch whose code or branch selection the split changes or merges (each CTA state, early return, error path). For a service, router, or store, test through the public entry: return values and calls. For a component, do not add render tests (source-layout §2): first extract the branch selection into a pure function or hook as a mechanical step that keeps every rendered output, then pin that function in .test.ts; existing component tests must pass before and after the extraction. Name the state or mock that reaches each branch. Prefer one table test (it.each) with expected values written out; inline snapshots do not work inside it.each.
  3. For a service, router, or store, run the new tests against the original code and record the result. If extraction already started, restore the original in its original directory so its relative imports resolve (git show <baseline>:<path> > <dir>/<Name>Original.ts), point the test import at it, run, then point it back and delete only that copy.
  4. Mutation smoke check: break one guarded branch (for example make an if condition false), confirm a named test fails, undo exactly that edit, and confirm the test passes again. One mutation shows the tests are sensitive, not that coverage is complete.

Behavior the split moves unchanged and that had no test before stays "uncovered before and after"; say so in the PR instead of claiming it is guarded.

4. Move first, in its own commit

Skip this step when the file is already a directory entry (foo/index.ts); extract siblings beside it instead. Otherwise move the file and its tests together: mkdir -p Foo && git mv Foo.tsx Foo/index.tsx (git mv does not create the directory, and Foo/ may already exist), then move each matching test keeping its extension (Foo.test.tsx → Foo/index.test.tsx, Foo.<scenario>.test.ts → Foo/index.<scenario>.test.ts). Rewrite only relative module specifiers in imports, vi.mock, and dynamic imports; run the tests by explicit path, and commit. Large extraction in the same commit lowers content similarity until Git stops detecting the rename. A separate move commit keeps the extraction diff reviewable on its own; a squash merge folds both commits together.

5. Extract along the plan

Extract one boundary at a time and rerun the guard tests by explicit path after each:

  • Pure functions to utils.ts (or a named module) with explicit parameters instead of closures over component state.
  • For React components, a cohesive useXxx.ts when transitions, side effects, or handlers obscure rendering or form a reusable unit; otherwise keep state in its smallest owner (react). For services, routers, and stores, use the matching row of source-layout §4.
  • Child components to PascalCase siblings with named exports.
  • A constant stays in the module that uses it; one shared by several extracted modules goes to the nearest common directory's constants.ts.

Keep the public import path and exported names unchanged so callers do not change. Every resulting file must be at most 1200 lines, and the entry should read as composition, not logic.

6. Split and trim the tests

When the test file is large or covers distinct scenarios, split it by scenario (index.render.test.ts, index.actions.test.ts, …; keep .tsx when the tests contain JSX) with shared mocks in testUtils.ts, as source-layout §2 describes.

Preserve every observable behavior the old suite asserted. Remove a test or assertion only when it duplicates a named retained test, is coupled to implementation details (testing lists these), or checks an expectation that is demonstrably obsolete; record each removal and its replacement or evidence in the PR. Merge near-identical tests into it.each, and drop mock branches and translation keys nothing renders.

7. Verify and deliver

find <dir> -type f -exec wc -l {} + | sort -rn   # every file ≤ 800, nested folders included
bun run check <every changed source file> <every guard test file> --lint --test --type

Pass the test files explicitly: related-test discovery finds only <name>.test.* and <name>.<scenario>.test.* of each changed file, so tests that guard an extracted sibling through the entry (index.*.test.ts for usePlanActions.ts) run only when passed. Confirm the reported test count matches the suites you expect.

Then follow the normal task lifecycle: a light deep-review (suggest Deep when the file is on a high-risk surface such as billing or auth), commit, and pr. A pure split needs no product acceptance; state that reason in the PR body together with:

  • the commits (move, extraction) so reviewers can read the extraction alone;
  • the baseline run of the characterization tests and the mutation result;
  • removed tests with their replacement, and behavior left uncovered before and after.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

acceptance

免费有简介

通过实际操作产品验证交付内容,记录用户可查看的证据,并发布验收报告。可检查已有验证计划或创建检查项,选择合适的操作界面,并使用 `lh` CLI 发布结果。

  • 实际检查更改后的产品行为
  • 收集截图或其他验证证据
  • 编写验证计划和报告
lobehub/lobehub8.4万2026年10月26日 更新

add-model-provider

免费有简介

本技能指导在 LobeHub 中接入 AI 模型提供商,涵盖运行时、模型信息、设置、品牌展示、文档和验收,并帮助判断新增模型是否需要新的提供商集成。

  • 为 LobeHub 接入新的 AI 模型提供商
  • 更新提供商设置说明或介绍
  • 判断添加模型是否需要新的提供商集成
lobehub/lobehub8.4万2026年10月26日 更新

add-setting-env

免费有简介

本技能指导如何用服务器端环境变量设置用户偏好默认值,涵盖变量校验、服务器配置、写入用户状态,以及更新 `.env.example` 和自托管文档。

  • 通过环境变量设置用户偏好默认值
  • 将设置接入服务器配置和用户状态
  • 更新环境变量示例及文档
lobehub/lobehub8.4万2026年10月26日 更新

agent-runtime-hooks

免费有简介

说明如何在智能体执行步骤、工具调用、人工审批、上下文压缩和子智能体调用时注册钩子。钩子可观察事件、模拟本地工具结果,也可通过 Webhook 按配置策略允许或拒绝工具调用。

  • 记录智能体步骤和错误
  • 测试时模拟工具结果
  • 通过 Webhook 允许或拒绝工具调用
lobehub/lobehub8.4万2026年10月26日 更新

agent-signal

免费有简介

此技能用于为智能体设计事件驱动的后台工作流,划分来源、信号、动作和策略的职责,并处理异步交接、去重、执行范围、完成记录与运行观测。

  • 添加事件触发的后台任务
  • 将工作移出对话请求
  • 防止动作重复执行
lobehub/lobehub8.4万2026年10月26日 更新

agent-testing-bot

免费有简介

本技能在 macOS 桌面聊天应用中测试机器人频道:打开应用、进入频道或联系人、发送消息、等待回复并截图。支持 Discord、Slack、Telegram、微信、飞书和 QQ;iMessage 使用 BlueBubbles 桥接。

  • 检查真实 Discord 频道中的机器人回复
  • 测试 Slack 或 Telegram 的对话流程
  • 记录 macOS 应用中的操作结果
lobehub/lobehub8.4万2026年10月26日 更新

查看 lobehub 的全部技能

报告此技能的问题