Skill — Code Generation Patterns
When this skill activates
Any task involving automated code generation, scaffolding, code mods, template
engines, AST transformations, or schema-to-code pipelines.
Mandatory actions when this skill is active
Before generating code
- Determine the generation approach based on the use case.
- Validate that generation is worthwhile (see "When NOT to generate" below).
- Ensure generated output is clearly marked with a header comment.
Approaches
Template-based (Handlebars/EJS):
- Best for: simple, repetitive file generation with known structure.
- Use when: the output varies only in names/values, not in structure.
- Tools: Handlebars, EJS, Mustache, Liquid.
- Pattern: define template → inject variables → write output file.
AST-based (babel/ts-morph):
- Best for: code transforms, refactors, and modifications to existing code.
- Use when: you need to understand code structure, not just text patterns.
- Tools: babel (JavaScript), ts-morph (TypeScript), jscodeshift (codemods).
- Pattern: parse source → traverse AST → apply transforms → print modified source.
Schema-based (OpenAPI/GraphQL/JSON Schema):
- Best for: generating clients, types, validators from a single source of truth.
- Use when: a schema already defines the contract and code must conform.
- Tools: openapi-generator, graphql-codegen, json-schema-to-typescript.
- Pattern: schema file → generator config → output typed client/types.
Scaffolding
Project generators:
- Yeoman, create-* CLIs, degit for template repos.
- Include: directory structure, config files, CI setup, README template.
- Version your generator — breaking changes need migration paths.
File generators:
- Plop, Hygen for generating individual files within a project.
- Pattern: define prompts → apply template → write to correct directory.
- Keep templates co-located with the generator config.
Code mods
jscodeshift (JavaScript/TypeScript bulk refactors):
- Write transform as a function:
(file, api) => api.jscodeshift(file.source)...
- Test on a single file first, then run across codebase.
- Always commit before running a codemod (easy revert).
ts-morph (TypeScript-aware transforms):
- Full TypeScript compiler API access.
- Can add/remove/rename imports, functions, classes, types.
- Pattern: create Project → get SourceFiles → manipulate → save.
Systematic process:
- Find all instances of the pattern to transform.
- Write the transform function.
- Dry-run with diff output.
- Apply and verify tests still pass.
When NOT to generate
- If it is simpler to write by hand (fewer than 3 instances).
- If generated code needs frequent manual edits (generator is wrong).
- If the template is harder to understand than the output.
- If the schema changes so frequently that regeneration becomes a bottleneck.
- If the team cannot maintain the generator long-term.
Quality standards for generated code
- Generated files MUST have a header:
// THIS FILE IS AUTO-GENERATED. DO NOT EDIT.
- Generated code must pass the same lint/format rules as hand-written code.
- Generator must be idempotent (running twice produces same output).
- Include a
regenerate script in package.json for easy re-generation.
- Test the generator itself, not just the generated output.
Self-check before task completion
1---2name: code-generation-patterns3description: Skill — Code Generation Patterns4---56# Skill — Code Generation Patterns78## When this skill activates9Any task involving automated code generation, scaffolding, code mods, template10engines, AST transformations, or schema-to-code pipelines.1112## Mandatory actions when this skill is active1314### Before generating code151. Determine the generation approach based on the use case.162. Validate that generation is worthwhile (see "When NOT to generate" below).173. Ensure generated output is clearly marked with a header comment.1819### Approaches2021**Template-based (Handlebars/EJS):**22- Best for: simple, repetitive file generation with known structure.23- Use when: the output varies only in names/values, not in structure.24- Tools: Handlebars, EJS, Mustache, Liquid.25- Pattern: define template → inject variables → write output file.2627**AST-based (babel/ts-morph):**28- Best for: code transforms, refactors, and modifications to existing code.29- Use when: you need to understand code structure, not just text patterns.30- Tools: babel (JavaScript), ts-morph (TypeScript), jscodeshift (codemods).31- Pattern: parse source → traverse AST → apply transforms → print modified source.3233**Schema-based (OpenAPI/GraphQL/JSON Schema):**34- Best for: generating clients, types, validators from a single source of truth.35- Use when: a schema already defines the contract and code must conform.36- Tools: openapi-generator, graphql-codegen, json-schema-to-typescript.37- Pattern: schema file → generator config → output typed client/types.3839### Scaffolding4041**Project generators:**42- Yeoman, create-* CLIs, degit for template repos.43- Include: directory structure, config files, CI setup, README template.44- Version your generator — breaking changes need migration paths.4546**File generators:**47- Plop, Hygen for generating individual files within a project.48- Pattern: define prompts → apply template → write to correct directory.49- Keep templates co-located with the generator config.5051### Code mods5253**jscodeshift (JavaScript/TypeScript bulk refactors):**54- Write transform as a function: `(file, api) => api.jscodeshift(file.source)...`55- Test on a single file first, then run across codebase.56- Always commit before running a codemod (easy revert).5758**ts-morph (TypeScript-aware transforms):**59- Full TypeScript compiler API access.60- Can add/remove/rename imports, functions, classes, types.61- Pattern: create Project → get SourceFiles → manipulate → save.6263**Systematic process:**641. Find all instances of the pattern to transform.652. Write the transform function.663. Dry-run with diff output.674. Apply and verify tests still pass.6869### When NOT to generate7071- If it is simpler to write by hand (fewer than 3 instances).72- If generated code needs frequent manual edits (generator is wrong).73- If the template is harder to understand than the output.74- If the schema changes so frequently that regeneration becomes a bottleneck.75- If the team cannot maintain the generator long-term.7677### Quality standards for generated code7879- Generated files MUST have a header: `// THIS FILE IS AUTO-GENERATED. DO NOT EDIT.`80- Generated code must pass the same lint/format rules as hand-written code.81- Generator must be idempotent (running twice produces same output).82- Include a `regenerate` script in package.json for easy re-generation.83- Test the generator itself, not just the generated output.8485## Self-check before task completion86- [ ] Did I follow the mandatory actions for this skill?87- [ ] Did I apply the patterns appropriate to the context?88- [ ] Did I verify the implementation meets the criteria above?89- [ ] Did I document decisions and trade-offs made?