Install
openclaw skills install convert-code-snippetsUse when (1) user provides a code snippet and asks to convert it from one programming language to another (e.g., Python to JavaScript, Java to Kotlin). (2) user asks to transform code between representations (e.g., JSON schema to TypeScript types, Markdown table to CSV). (3) user provides code in one style/convention and wants it converted to another (e.g., CommonJS to ESM, callback to async/await).
openclaw skills install convert-code-snippetsThis skill converts code snippets and data structures between programming languages, formats, and conventions. It handles syntactic transformation (language-to-language), representational transformation (JSON to TypeScript types), and stylistic transformation (older patterns to newer ones). It does NOT just copy-paste — it applies semantic equivalence, meaning the output code must be functionally equivalent to the input.
Key responsibilities:
/convert-code-snippets --languageLanguage-to-language conversion. Converts code from source language to target language. Requires --from and --to flags:
--from python --to javascript
--from java --to kotlin
--from go --to rust
--from typescript --to python
Supported languages: Python, JavaScript, TypeScript, Java, Kotlin, Go, Rust, C++, C#, Swift, Ruby, PHP.
/convert-code-snippets --schemaSchema to code conversion. Converts a data schema (JSON Schema, OpenAPI, SQL schema) into typed code:
JSON Schema → TypeScript interfaces, Python dataclasses, Java classes, Go structsOpenAPI spec → client code (TypeScript, Python), server stubsSQL CREATE TABLE → ORM models (SQLAlchemy, TypeORM, Prisma)/convert-code-snippets --styleCode style transformation. Converts code between conventions:
callback-to-async — converts callback-based code to async/awaitclass-to-function — converts class-based OOP to functional stylecommonjs-to-esm — converts require() to import/exportpython2-to-python3 — converts Python 2 print statements, unicode handlingjsx-to-tsx — converts JavaScript JSX to TypeScript TSX/convert-code-snippets --formatData format conversion. Converts between structured data formats:
json-to-yaml, yaml-to-json, toml-to-jsoncsv-to-json, json-to-csv, tsv-to-csvxml-to-json, json-to-xmlFrom file extension:
.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
From content (if extension ambiguous):
def , import , print( → Pythonfunction , const , let , => → JavaScriptpublic class , System.out. → Java: string, : number, interface → TypeScriptIf source or target is ambiguous, ask the user: "Cannot determine target language — please specify --from python --to javascript explicitly."
Read the source code as UTF-8 text. Use a parser if available for the source language:
ast module to parse into ASTesprima or babel parserFor languages without parser support, use structural pattern matching:
def name( or function name(class Name or class Name extendsimport or from or require(: Type or -> TypeExtract: function signatures, class definitions, imports, type annotations, comments.
Build a transformation map for the specific language pair. Common mappings:
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: (above + type annotations)
int, float, str → number, number, string
List[int] → number[]
Dict[str, int] → Record<string, number>
Optional[int] → number | null
def fn() -> int → function fn(): number
Java → Kotlin:
System.out.println() → println()
public class → class (public is default)
String → String (kotlin.String)
final → val
void method() → fun method(): Unit
if (x == null) → if (x == null)
Callback → Async/Await:
// Before (callback style)
apiCall(arg, function(err, result) {
if (err) return handleError(err);
process(result);
});
// After (async/await)
try {
const result = await apiCall(arg);
process(result);
} catch (err) {
handleError(err);
}
CommonJS → ESM:
const fs = require('fs'); → import fs from 'fs';
const {a, b} = require('./module'); → import {a, b} from './module.js';
module.exports = {fn}; → export const fn = ...;
module.exports.fn = fn; → export function fn() {...}
Indentation:
--indent flag (default: 2 spaces for most languages)Semicolons:
; at end of statement (unless already present or line ends with {/}); if presentQuotes:
Imports/exports:
For parsed languages, attempt to parse the output:
ast.parse(output) — if it fails, report the error with line numbereslint --no-eslintrc --parser-options {ecmaVersion: 2020} filename.js if availableIf no parser available, do structural validation:
{...}, brackets [...], parentheses (...)function not Function, const not CONST)If validation fails, report: "Converted code is invalid [target language]: error at line {N}: {detail}. Reverting to source format."
Return:
{
"source_language": "python",
"target_language": "javascript",
"lines_converted": 142,
"functions_mapped": 8,
"classes_mapped": 3,
"imports_mapped": 12,
"comments_preserved": 7,
"warnings": [
{"line": 42, "issue": "Python dict comprehension has no direct JS equivalent — converted to .map() but verify correctness"},
{"line": 87, "issue": "Decorator @property cannot be represented in ES5 — needs manual conversion"}
],
"approximate_functional_equivalence": "85%"
}
is vs JavaScript ===)System.out.println() must become console.log(), not just stay as-is)--schema mode — TypeScript/Java/Kotlin output must have types# self in Python → // self in JavaScript — remove or update)approximate_functional_equivalence percentage and list specific areas needing manual reviewlen() → JS .length, Python str() → JS String())--style mode, add a comment // Auto-converted from [source style] — verify correctness at the top| Criterion | Minimum | Ideal |
|---|---|---|
| Syntax validity | Output parses without error in target language | Output passes linter with no warnings |
| Semantic equivalence | Approximate functional equivalence >= 70% | >= 90% with manual review only for complex features |
| Standard library mapping | All detected stdlib calls mapped to target equivalent | Verified by running converted code against test cases |
| Type preservation | Type annotations preserved where possible | Full type inference in target language |
| Comment preservation | Comments that document behavior preserved | Comments translated, not just copied |
| Edge case handling | Handles null, empty input gracefully | All edge cases have explicit handling with test cases |
A good output is syntactically valid in the target language, semantically equivalent to the source (with known gaps documented), and formatted according to the target language's conventions.
| Scenario | Bad | Good |
|---|---|---|
Python is to JS | Replaces is with === literally | Maps is to === for primitives but warns about object identity difference |
len(list) in Python | Writes len(list) in JS (not valid) | Converts to list.length |
def fn(a, b=None) | Converts to function fn(a, b=null) (loses default intent) | Adds if (b === undefined) b = null or uses default parameter b = null |
| Python f-string | Converts f"{x}" to "${x}" (wrong in non-template context) | Detects f-string, converts to template literal `${x}` |
Java for loop | Converts for (int i=0; i<n; i++) to for i in range(n): (Pythonic but not equivalent) | Preserves index-based loop or maps to for i in range(n) with note |
| Missing import | Silently drops import os | Maps to const os = require('os') (Node) or reports "os module has no direct JS equivalent" |
| Callback to async | Converts only one level of callbacks | Recursively converts nested callbacks to async/await, flattening the chain |
| Unknown construct | Skips the function entirely | Includes function signature as comment: // function unknownFeature() — needs manual implementation |