ast-codegen-patterns
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-14
- Stars
- ★ 13
- Forks
- 2
03 SKILL.md 正文
/ast-codegen-patterns
When to Use
- Regenerate source from a modified AST (after [[babel-ast-transform]] or [[estree-ast-spec]] walker)
- Format-preserving reprints: preserve whitespace/comments in unmodified subtrees
- Round-trip pipelines: parse → mutate → generate → diff
- Custom code generation: emit domain-specific patterns from data models
Do NOT use for
- Full Babel transform pipelines (use [[babel-ast-transform]] which includes generate)
- TypeScript codegen from scratch (use [[ts-morph-refactor]] for type-safe generation)
ESTree → source (escodegen)
import escodegen from 'escodegen'
import { parse } from 'acorn'
const source = `
const arr = [1, 2, 3]
const doubled = arr.map(x => x * 2)
`
const ast = parse(source, { ecmaVersion: 2022, sourceType: 'module' })
// Mutate AST: rename 'doubled' → 'multiplied'
function rename(node, from, to) {
if (!node || typeof node !== 'object') return
if (node.type === 'Identifier' && node.name === from) node.name = to
for (const key of Object.keys(node)) rename(node[key], from, to)
}
rename(ast, 'doubled', 'multiplied')
const output = escodegen.generate(ast, {
format: {
indent: { style: ' ' },
quotes: 'single',
semicolons: true,
},
comment: true, // preserve comments from original
})
console.log(output)
// const arr = [1, 2, 3];
// const multiplied = arr.map(x => x * 2);
Source map–aware generation
import escodegen from 'escodegen'
import { SourceMapConsumer } from 'source-map'
const { code, map } = escodegen.generate(ast, {
sourceMap: 'input.js',
sourceMapWithCode: true,
format: { compact: false },
})
// map is a SourceMapGenerator — serialize for output
const sourceMapString = map.toString()
const codeWithMap = code + '\n//# sourceMappingURL=data:application/json;base64,'
+ Buffer.from(sourceMapString).toString('base64')
Manual AST builder (no parser needed)
// Build `export const VERSION = "1.0.0"` from scratch
function buildVersionExport(version: string) {
return {
type: 'ExportNamedDeclaration',
declaration: {
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
type: 'VariableDeclarator',
id: { type: 'Identifier', name: 'VERSION' },
init: { type: 'Literal', value: version, raw: JSON.stringify(version) },
}],
},
specifiers: [],
source: null,
}
}
const program = {
type: 'Program',
body: [buildVersionExport('1.0.0')],
sourceType: 'module',
}
console.log(escodegen.generate(program))
// export const VERSION = '1.0.0';
Round-trip codemod helper
import { parse } from 'acorn'
import escodegen from 'escodegen'
export function codemod(
source: string,
mutate: (ast: any) => void,
options: escodegen.GenerateOptions = {}
): string {
const ast = parse(source, {
ecmaVersion: 2022,
sourceType: 'module',
onComment: (block, text, start, end) => {
// attach comments to nearest node for escodegen to reprint
},
})
mutate(ast)
return escodegen.generate(ast, {
comment: true,
format: { indent: { style: ' ' }, ...options },
})
}
Anti-Fake-Pass Checklist
❌ escodegen.generate on Babel AST → Babel adds non-ESTree nodes; strip them first
❌ comment: false → all comments lost in round-trip
❌ Mutating raw node.loc values → code generation uses loc for source maps; corruption
❌ compact: true in a codemod → diffs become unreadable one-liners
❌ Manual AST missing 'raw' field on Literal → some generators produce undefined
❌ sourceMap: 'filename' must match the original filename exactly or stack traces break
04 同仓库更多 Skills
openai--sentry--sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
ito-basket-compare
Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.
implementing-network-traffic-baselining
Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling
detecting-evasion-techniques-in-endpoint-logs
Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.
implementing-microsegmentation-with-guardicore
Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.
ramdisk-tmpfs-patterns
暂无简介