Convert Code Snippets — 转换代码片段
v1.0.0在以下情况使用:(1)用户提供代码片段,并要求将其从一种编程语言转换为另一种语言(例如,从 Python 到 JavaScript,从 Java 到 Kotlin)。(2)用户要求在不同表示形式之间转换代码(例如,从 JSON 模式到 TypeScript 类型,从 Markdown 表格到 CSV)。(3)用户提供一种风格/约定的代码,并希望将其转换为另一种风格(例如,从 CommonJS 到 ESM,从回调到 async/await)。
运行时依赖
安装命令
点击复制技能文档
核心职位
此技能可以在编程语言、格式和约定之间转换代码片段和数据结构。它处理语法转换(语言到语言)、表示转换(JSON 到 TypeScript 类型)和风格转换(旧模式到新模式)。它不仅仅是复制和粘贴 —— 它应用语义等价性,这意味着输出代码必须在功能上等同于输入代码。
主要职责:
从文件扩展名或显式注释中检测源语言和目标语言
应用语言特定的语法规则(缩进、分号、关键字差异、标准库等价物)
处理语言习惯用语 —— 不仅仅是语法替换,而是习惯用语转换
在可能的情况下保留注释和文档字符串,或者标记为需要手动审查
验证输出在目标语言中是语法正确的
模式
/convert-code-snippets --language 语言到语言转换。将代码从源语言转换为目标语言。需要 --from 和 --to 标志:
--from python --to javascript
--from java --to kotlin
--from go --to rust
--from typescript --to python
支持的语言:Python、JavaScript、TypeScript、Java、Kotlin、Go、Rust、C++、C#、Swift、Ruby、PHP。
/convert-code-snippets --schema 模式到代码转换。将数据模式(JSON 模式、OpenAPI、SQL 模式)转换为类型化代码:
JSON 模式 → TypeScript 接口、Python 数据类、Java 类、Go 结构
OpenAPI 规范 → 客户端代码(TypeScript、Python)、服务器存根
SQL CREATE TABLE → ORM 模型(SQLAlchemy、TypeORM、Prisma)
/convert-code-snippets --style 代码风格转换。将代码在约定之间转换:
callback-to-async —— 将基于回调的代码转换为 async/await
class-to-function —— 将基于类的面向对象编程转换为函数式风格
commonjs-to-esm —— 将 require() 转换为 import/export
python2-to-python3 —— 将 Python 2 打印语句、Unicode 处理转换为 Python 3
jsx-to-tsx —— 将 JavaScript JSX 转换为 TypeScript TSX
/convert-code-snippets --format 数据格式转换。将结构化数据格式之间转换:
json-to-yaml、yaml-to-json、toml-to-json
csv-to-json、json-to-csv、tsv-to-csv
xml-to-json、json-to-xml
执行步骤
检测源语言和目标语言/格式
从文件扩展名:.py → Python、.js → JavaScript、.java → Java、.ts → TypeScript、.kt → Kotlin、.go → Go、.rs → Rust、.cpp → C++、.cs → C#、.swift → Swift、.rb → Ruby、.php → PHP、.tsx → TypeScript、.jsx → JavaScript、.json → JSON、.yaml → YAML、.yml → YAML、.toml → TOML、.csv → CSV、.xml → XML、.sql → SQL
从内容(如果扩展名模糊):Python:def、import、print() → Python、JavaScript:function、const、let、=> → JavaScript、Java:public class、System.out. → Java、TypeScript:: string、: number、interface → TypeScript
如果源语言或目标语言不明确,询问用户:”无法确定目标语言 —— 请指定 --from python --to javascript。“
解析和分析源代码
以 UTF-8 文本读取源代码。对于源语言,如果有可用的解析器,则使用它:
Python:使用 ast 模块解析为 AST
JavaScript/TypeScript:使用 esprima 或 babel 解析器
Java:如果没有完整的解析器,则使用简单的基于正则表达式的方法
对于没有解析器支持的语言,使用结构模式匹配:
识别函数/方法:模式 def name() 或 function name()
识别类:模式 class Name 或 class Name extends
识别导入:模式 import 或 from 或 require()
识别类型注释:模式 : Type 或 -> Type
提取:函数签名、类定义、导入、类型注释、注释
映射语言构造
为特定的语言对建立转换映射。常见的映射:
Python → JavaScript:def fn(x, y) → function fn(x, y) {、print(x) → console.log(x)、None → null、True / False → true / false、x if cond else y → cond ? x : y、with open(f) as f: → (async () => { const fs = require('fs'); ... })()、def __init__(self): → constructor() {、self.x → this.x、f"string {x}" → string ${x}、[x for x in lst] → lst.map(x => x)
Python → TypeScript:(上述 + 类型注释)
int、float、str → number、number、string
List[int] → number[]
Dict[str, int] → Record
Optional[int] → number | null
def fn() -> int → function fn(): number
Java → Kotlin:System.out.println() → println()、public class → class(public 是默认的)、String → String(kotlin.String)、final → val、void method() → fun method(): Unit、if (x == null) → if (x == null)
回调到异步:// 之前(回调风格)apiCall(arg, function(err, result) { if (err) return handleError(err); process(result); }); // 之后(异步/等待)try { const result = await apiCall(arg); process(result); }