Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问许可证需确认审计通过

moonbit-refactoringMoonbit 重构

Agent Skill

moonbit-refactoring 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

408

周安装

17

GitHub Stars

72

下载量

136
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:moonbit-refactoring(Moonbit 重构)
来源仓库:https://github.com/moonbitlang/moonbit-agent-guide
仓库路径:skills/moonbit-refactoring
安装命令:
npx skills add https://github.com/moonbitlang/moonbit-agent-guide --skill moonbit-refactoring
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/moonbitlang/moonbit-agent-guide --skill moonbit-refactoring

简介

moonbit-refactoring 用于查找、检索和筛选相关信息。

  • 适合在需要围绕重构主题快速获取资料时使用。moonbit-refactoring 属于研究检索类 Skill,可作为该场景下的辅助能力补充。
  • 建议结合 Moonbit 官方指南验证技术方案。
  • 安装前请确认是否涉及代码修改或生产环境访问。
  • 该技能主要用于信息支持,不自动执行代码变更。

SKILL.md

MoonBit Refactoring Skill

Intent

  • Preserve behavior and public contracts unless explicitly changed.
  • Minimize the public API to what callers require.
  • Prefer declarative style and pattern matching over incidental mutation.
  • Use view types (ArrayView/StringView/BytesView) to avoid copies.
  • Add tests and docs alongside refactors.

Workflow

Start broad, then refine locally:

  1. Architecture first: Review package structure, dependencies, and API boundaries.
  2. Inventory public APIs and call sites (moon ide doc, moon ide find-references).
  3. Pick one refactor theme (API minimization, package splits, pattern matching, loop style).
  4. Apply the smallest safe change.
  5. Update docs/tests in the same patch.
  6. Run moon check, then moon test.
  7. Use coverage to target missing branches.

Avoid local cleanups (renaming, pattern matching) until the high-level structure is sound.

Improve Package Architecture

  • Keep packages focused: aim for <10k lines per package.
  • Keep files manageable: aim for <2k lines per file.
  • Keep functions focused: aim for <200 lines per function.

Splitting Files

Treat files in MoonBit as organizational units; move code freely within a package as long as each file stays focused on one concept.

Splitting Packages

When spinning off package A into A and B:

  1. Create the new package and re-export temporarily: // In package B using @A {...} // re-export A's APIs Ensure moon check passes before proceeding.
  2. Find and update all call sites: moon ide find-references <symbol> Replace bare f with @B.f.
  3. Remove the use statement once all call sites are updated.
  4. Audit and remove newly-unused pub APIs from both packages.

Guidelines

  • Prefer acyclic dependencies: lower-level packages should not import higher-level ones.
  • Only expose what downstream packages actually need.
  • Consider an internal/ package for helpers that shouldn't leak.

Minimize Public API and Modularize

  • Remove pub from helpers; keep only required exports.
  • Move helpers into internal/ packages to block external imports.
  • Split large files by feature; files do not define modules in MoonBit.

Local refactoring

Convert Free Functions to Methods + Chaining

  • Move behavior onto the owning type for discoverability.
  • Use .. for fluent, mutating chains when it reads clearly.

Example:

// Before
fn reader_next(r : Reader) -> Char? { ... }
let ch = reader_next(r)

// After
#as_free_fn(reader_next, deprecated="Use Reader::next instead")
fn Reader::next(self : Reader) -> Char? { ... }
let ch = r.next()

To make the transition smooth, place #as_free_fn(old_name,...) on the method; it emits a deprecated free function old_name that forwards to the method. Then you can check call sites and update them gradually by looking at warnings. Example (chaining):

buf..write_string("#\\")..write_char(ch)

Prefer Explicit Qualification

  • Use @pkg.fn instead of using when clarity matters.
  • Keep call sites explicit during wide refactors.

Example:

let n = @parser.parse_number(token)

Simplify Enum Constructors When Type Is Known

When the expected type is known from context, you can omit the full package path for enum constructors:

  • Pattern matching: Annotate the matched value; constructors need no path.
  • Nested constructors: Only the outermost needs the full path.
  • Return values: The return type provides context for constructors in the body.
  • Collections: Type-annotate the collection; elements inherit the type.

Examples:

// Pattern matching - annotate the value being matched
let tree : @pkga.Tree = ...
match tree {
  Leaf(x) => x
  Node(left~, x, right~) => left.sum() + x + right.sum()
}

// Nested constructors - only outer needs full path
let x = @pkga.Tree::Node(left=Leaf(1), x=2, right=Leaf(3))

// Return type provides context
fn make_tree() -> @pkga.Tree {
  Node(left=Leaf(1), x=2, right=Leaf(3))
}

// Collections - type annotation on the array
let trees : Array[@pkga.Tree] = [Leaf(1), Node(left=Leaf(2), x=3, right=Leaf(4))]

Pattern Matching and Views

  • Pattern match arrays directly; the compiler inserts ArrayView implicitly.
  • Use .. in the middle to match prefix and suffix at once.
  • Pattern match strings directly; avoid converting to Array[Char].
  • String/StringView indexing yields UInt16 code units. Use for ch in s for Unicode-aware iteration.

We Prefer Pattern Matching Over Small Functions

For example,

 match gen_results.get(0) {
   Some(value) => Iter::singleton(value)
   None => Iter::empty()
 }

We can pattern match directly; it is often clearer and equally readable:

 match gen_results {
   [value, ..] => Iter::singleton(value)
   [] => Iter::empty()
 }

MoonBit pattern matching is pretty expressive, here are some more examples:

match items {
  [] => ()
  [head, ..tail] => handle(head, tail)
  [..prefix, mid, ..suffix] => handle_mid(prefix, mid, suffix)
}
match s {
  "" => ()
  [.."let", ..rest] => handle_let(rest)
  _ => ()
}

Char literal matching

Use char literal overloading for Char, UInt16, and Int; the examples below rely on it. This is handy when matching String indexing results (UInt16) against a char range.

test {
  let a_int : Int = 'b'
  if (a_int is 'a'..<'z') { () } else { () }
  let a_u16 : UInt16 = 'b'
  if (a_u16 is 'a'..<'z') { () } else { () }
  let a_char : Char = 'b'
  if (a_char is 'a'..<'z') { () } else { () }
}

Use Nested Patterns and is

  • Use is patterns inside if/guard to keep branches concise.

Example:

match token {
  Some(Ident([.."@", ..rest])) if process(rest) is Some(x) => handle_at(rest)
  Some(Ident(name)) => handle_ident(name)
  None => ()
}

Prefer Functional Loops to Mutation When Possible

  • Use functional state update

Example:

// Before
let mut a = 1
let mut b = 2
for i = 0 {
  if i >= n {
    break
  }
  a = a + b
  b = b + a
  continue i + 1
}
for i = 0, a = 1, b = 2 {
  if i >= n {
    break a
  }
  continue i + 1, b, b + a
}
  • Functional loops also accept range loops, so they can be simplified:
for _ in 0..<n; a = 1, b = 2 {
  continue b, a + b
} nobreak {
  a
}

Prefer Range Loops to Simple Indexing

  • Use for i in start..<end {...}, for i in start..<=end {...}, for i in large>..small, or for i in large>=..small for simple index loops.
  • Use for x in xs {...} if xs is an iterator(e.g, Array) or for i,x in xs {...} if you also want to use the index
  • Keep functional-state for loops for algorithms that update state.

Example:

// Before
for i = 0; i < len; i = i + 1{
  items.push(fill)
}

// After
for i in 0..<len {
  items.push(fill)
}

Loop Specs (Dafny-Style Comments)

  • Add specs for functional-state loops.
  • Skip invariants for simple for x in xs loops.
  • Add TODO when a decreases clause is unclear (possible bug).

Example:

for i = 0, acc = 0; i < xs.length(); {
  acc = acc + xs[i]
  i = i + 1
} nobreak { acc }
where {
  invariant: 0 <= i <= xs.length(),
  reasoning: (
    #| ... rigorous explanation ...
    #| ...
  )
}

Tests and Docs

  • Prefer black-box tests in *_test.mbt or *.mbt.md.
  • Add docstring tests using mbt check fenced blocks for public APIs, and verify with moon check && moon test.

Example:

///|
/// Return the last element of a non-empty array.
///
/// # Example
/// ```mbt check
/// test {
///   inspect(last([1, 2, 3]), content="3")
/// }
/// ```
pub fn last(xs : Array[Int]) -> Int { ... }

Coverage-Driven Refactors

  • Use coverage to target missing branches through public APIs.
  • Prefer small, focused tests over white-box checks.

Commands:

moon coverage analyze -- -f summary
moon coverage analyze -- -f caret -F path/to/file.mbt

Moon IDE Commands

moon ide doc "<query>"
moon ide outline <dir|file>
moon ide find-references <symbol>
moon ide peek-def <symbol>
moon ide rename <symbol> <new_name>
moon ide analyze [path]
moon check
moon test
moon info

Use these commands for reliable refactoring.

Example: spinning off package_b from package_a.

Temporary import in package_b:

using @package_a { a, type B }

Steps:

  1. Use moon ide find-references <symbol> to find all call sites of a and B.
  2. Replace them with @package_a.a and @package_a.B.
  3. Remove the using statement and run moon check.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.4%
按下载量换算47

Claude

29.34%
按下载量换算40

Cursor

18.16%
按下载量换算25

Gemini CLI

10.12%
按下载量换算14

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills